Tuesday, November 13, 2007

Example 3-2 : Using Modeling Transformations

I'm tired.

I scratched my head over the Z-axis stuff; be sure to read the paragraph called "Troubleshooting Transformations" in chapter 3. It'll help prevent a lot of scratch-marks.

In this case, the following phrase made me realise the mistake I made:
Remember that with the projection commands, the near and far coordinates measure distance from the viewpoint and that (by default) you're looking down the negative z axis. Thus, if the near value is 1.0 and the far 3.0, objects must have z coordinates between -1.0 and -3.0 in order to be visible.

The vertices I drew were all on the positive side of the z-axis, while I had to give them negative z-coordinates ofcourse.

(Note to self: first read the whole chapter before trying out all the examples in the book).

The code:

# Example 3-2 : Using Modeling Transformations

from pyglet.gl import *
from pyglet import window
from OpenGL.GLUT import *

win = window.Window()

def init():
glClearColor (0.0, 0.0, 0.0, 0.0)
glShadeModel (GL_FLAT)

def draw_triangle():
glBegin(GL_LINES)
glVertex3f(50.0, 50.0, -2.0)
glVertex3f(150.0, 50.0, -2.0)
glVertex3f(150.0, 50.0, -2.0)
glVertex3f(150.0, 150.0, -2.0)
glVertex3f(150.0, 150.0, -2.0)
glVertex3f(50.0, 50.0, -2.0)
glEnd()

def reshape (w, h):
glViewport (0, 0, w, h)
print w, h
glMatrixMode (GL_PROJECTION)
glLoadIdentity ()
glFrustum (-150.0, 150.0, -150.0, 150.0, 1.0, 20.0)
glMatrixMode (GL_MODELVIEW)

def display():
glClear (GL_COLOR_BUFFER_BIT)
glLoadIdentity()

glColor3f(1.0, 1.0, 1.0)
draw_triangle() # solid lines
glEnable(GL_LINE_STIPPLE) # dashed lines
glLineStipple(1, 0xF0F0)
glLoadIdentity()
glTranslatef(-20.0, 0.0, 0.0)
draw_triangle()

glLineStipple(1, 0xF00F) #long dashed lines
glLoadIdentity()
glScalef(1.5, 0.5, 1.0)
draw_triangle()

glLineStipple(1, 0x8888) # dotted lines
glLoadIdentity()
glRotatef (90.0, 0.0, 0.0, 1.0)
draw_triangle ()
glDisable (GL_LINE_STIPPLE);
glFlush ()

init()

win.on_resize = reshape

while not win.has_exit:
win.dispatch_events()
win.clear()
display()
win.flip()


(As a side-note, I've used GL_LINES instead of GL_TRIANGLES to draw the triangles, so that the original glEnable(GL_LINE_STIPPLE) would remain unaltered).

No comments: