• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Feb 27th, 2011 174 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import sys
2. from math import sin, cos
3. from random import randint
4. from OpenGL.GL import *
5. from OpenGL.GLU import *
6. from OpenGL.GLUT import *
7. #angle of rotation
8. xpos= ypos= zpos= xrot= yrot= angle= lastx= lasty = 0
9.
10. #positions of the cubes
11. positionz = [0]*10
12. positionx = [0]*10;
13.
14. def cubepositions(): #set the positions of the cubes
15.     global positionz, positionx
16.     for x in xrange(10):
17.         positionz[x] = randint(0,10)
18.         positionx[x] = randint(0,10)
19.
20. #draw the cube
21. def cube():
22.     for x in xrange(10):
23.         glPushMatrix()
24.         glTranslated(-positionx[x + 1] * 10, 0, -positionz[x + 1] * 10); #translate the cube
25.         glutSolidCube(2); #draw the cube
26.         glPopMatrix();
27.
28. def init():
29.     cubepositions();
30.
31. def enable ():
32.     glEnable (GL_DEPTH_TEST); #enable the depth testing
33.     glEnable (GL_LIGHTING); #enable the lighting
34.     glEnable (GL_LIGHT0); #enable LIGHT0, our Diffuse Light
36.
37. def camera ():
38.     global xrot, yrot, xpos, ypos, zpos
39.     glRotatef(xrot,1.0,0.0,0.0);  #rotate our camera on teh x-axis (left and right)
40.     glRotatef(yrot,0.0,1.0,0.0);  #rotate our camera on the y-axis (up and down)
41.     glTranslated(-xpos,-ypos,-zpos); #translate the screen to the position of our camera
42.
43. def display ():
44.     glClearColor (0.0,0.0,0.0,1.0); #clear the screen to black
45.     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); #clear the color buffer and the depth buffer
47.     camera();
48.     enable();
49.     cube(); #call the cube drawing function
50.     glutSwapBuffers(); #swap the buffers
51.     angle += angle #increase the angle
52.
53. def reshape (w, h):
54.     glViewport (0, 0, w, h); #set the viewport to the current window specifications
55.     glMatrixMode (GL_PROJECTION); #set the matrix to projection
56.
58.     gluPerspective (60, w / h, 1.0, 1000.0)
59.     #set the perspective (angle of sight, width, height, , depth)
60.     glMatrixMode (GL_MODELVIEW); #set the matrix back to model
61.
62. def keyboard (key, x, y):
63.     global xrot, xpos, ypos, zpos, xrot, yrot, angle, lastx, lasty, positionz, positionx
64.     if (key==chr('q')):
65.         xrot += 1;
66.         if (xrot >360):
67.             xrot -= 360;
68.     if (key==chr('z')):
69.         xrot -= 1;
70.         if (xrot < -360): xrot += 360;
71.     if (key=='w'):
72.         yrotrad = (yrot / 180 * 3.141592654);
73.         xrotrad = (xrot / 180 * 3.141592654);
77.     if (key=='s'):
78.         yrotrad = (yrot / 180 * 3.141592654);
79.         xrotrad = (xrot / 180 * 3.141592654);
83.     if (key=='d'):
84.         yrotrad = (yrot / 180 * 3.141592654);
85.         xpos += float(cos(yrotrad)) * 0.2;
86.         zpos += float(sin(yrotrad)) * 0.2;
87.     if (key=='a'):
88.         yrotrad = (yrot / 180 * 3.141592654);
89.         xpos -= float(cos(yrotrad)) * 0.2;
90.         zpos -= float(sin(yrotrad)) * 0.2;
91.     if (key==27):
92.         sys.exit(0);
93.
94. def mouseMovement(x, y):
95.     diffx=x-lastx; #check the difference between the current x and the last x position
96.     diffy=y-lasty; #check the difference between the current y and the last y position
97.     lastx=x; #set lastx to the current x position
98.     lasty=y; #set lasty to the current y position
99.     xrot += float(diffy); #set the xrot to xrot with the addition of the difference in the y position
100.     yrot += float(diffx);    #set the xrot to yrot with the addition of the difference in the x position
101.
102. glutInit ()
103. glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH);
104. glutInitWindowSize (500, 500);
105. glutInitWindowPosition (100, 100);
106. glutCreateWindow ("A basic OpenGL Window");
107. init ();
108. glutDisplayFunc (display);
109. glutIdleFunc (display);
110. glutReshapeFunc (reshape);
111. glutPassiveMotionFunc(mouseMovement);
112. #check for mouse movement
113. glutKeyboardFunc (keyboard);
114. glutMainLoop ();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Not a member of Pastebin yet?