Search code examples
openglhaskellrenderingprojectionisometric

Isometric Projection using Opengl


I am trying to use opengl to make isometric drawings.

According to: http://en.wikipedia.org/wiki/Isometric_projection#Mathematics

"this is done by first looking straight towards one face. Next the cube is rotated ±45° about the vertical axis, followed by a rotation of approximately ±35.264° (precisely arcsin(tan 30°) or arctan(sin 45°) ) about the horizontal axis."

But clearly I am missing some detail. This code sort of works but it is at a strange angle. The yellow should be the bottom most line.

import           Graphics.UI.GLFW
import           Graphics.Rendering.OpenGL

main :: IO ()
main = do
  initialize 
  openWindow  (Size 800 600)  [] Window

  windowTitle $= "Test Projection"

  clearColor  $= Color4 0 0 0 1
  pointSize   $= 3
  lineWidth   $= 1
  blend       $= Enabled
  blendFunc   $= (SrcAlpha, OneMinusSrcAlpha)
  viewport    $= (Position 0 0, Size 800 600)

  matrixMode  $= Projection
  loadIdentity

  --  ortho (-2) (2) (-2) 2 (-20.0) 20.0 

  matrixMode  $= Modelview 0 
  loadIdentity

  rotate (35.264) (Vector3 1.0 0.0 0.0 :: Vector3 GLfloat)
  rotate    (-45) (Vector3 0.0 1.0 0.0 :: Vector3 GLfloat)


  clear [ColorBuffer]

  renderPrimitive Lines $ do
    color $ Color3 1 zero zero
    vertex' (0,0,0)
    vertex' (1,0,0)
    color $ Color3 zero 1 zero
    vertex' (0,0,0)
    vertex' (0,1,0)
    color $ Color3 zero zero 1 
    vertex' (0,0,0)
    vertex' (0,0,1)
    color $ Color3 1 1 zero
    vertex' (1,0,0)
    vertex' (0,1,0)


  swapBuffers

  getChar

  return ()

vertex' :: Integral a => (a, a, a) -> IO ()
vertex' (x, y, z) = vertex (Vertex3 (f x) (f y) (f z))
                    where f p = fromIntegral p :: GLint

zero :: GLfloat
zero = 0.0

Solution

  • Actually your code is correct. You just went for some misconception of OpenGLs default coordinate system. In OpenGL's standard coordinate system X goes right, Y goes up and Z points out of the screen. Your yellow line goes from (1,0,0) to (1,0,0), i.e. from a point to the right to a point up. It's perfectly correct.