====== Getting Started in 3D ====== In our class example, we used GLUT to display some simple 3D geometric primitives. To use GLUT, we needed to include a new import statement at the top of our source code. from __future__ import division from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * # <---- New Import for GLUT import numpy as np import pygame We also had to add a couple lines of code to our OpenGL init function to setup our 3D viewing volume. class GLContext( ): def __init__( self, screen): self.screen=screen self.aspect=screen.get_width()/screen.get_height() #Calculates and stores our aspect ratio gluPerspective(45.0, self.aspect, 0.1, 200.0) #sets up our 3D perspective view return Now, we are ready to start drawing some 3D objects. We'll start by just looking a glutWireCube. In the code below we can see that we're translating our cube -1.0 unit in depth and then we rotate it 20 degrees about the X-axis, 30 degrees about the Y-axis, and 40 degrees about the Z-axis. The following code will need to be placed in the display() function that we discussed in the previous set of examples. glPushMatrix() #stores previous transformation matrix glTranslatef(0.0, 0.0, -1.0) #trasnlates scene -1.0 units in depth (away from the camera) glRotatef(20.0, 1, 0, 0) #20deg rotation around the x-axis glRotatef(30.0, 0, 1, 0) #30deg rotation around the y-axis glRotatef(40.0, 0, 0, 1) #49deg rotation around the z-axis glutWireCube(0.5) #draws a wireframe cube of width 0.5 units glPopMatrix() #restores previous transformation matrix The resulting image from this example should look like this:\\ {{ :tutorials:cubeexample.tok.8dd786_w.200.png?200 |}}