Advertisement
redsees

Untitled

Mar 26th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.97 KB | None | 0 0
  1. from OpenGL.GLUT import *
  2. from OpenGL.GLU import *
  3. from OpenGL.GL import *
  4. import sys
  5.  
  6. xRotated, yRotated, zRotated = 0,0,0;
  7. radius=0.5;
  8.  
  9.  
  10. def redisplayFunc():
  11.     global xRotated
  12.     global yRotated
  13.     global zRotated
  14.     glMatrixMode(GL_MODELVIEW);
  15.     glClear(GL_COLOR_BUFFER_BIT);
  16.     glLoadIdentity();
  17.  
  18.     glTranslatef(0.0,0.0,-4.5);
  19.     glColor3f(1.0, 1.0, 0.0);
  20.     glRotatef(yRotated,0.0,1.0,0.0);
  21.  
  22.     glutSolidSphere(radius,20,30);
  23.  
  24.  
  25.     glTranslate(0.0,0.0,-1.0);
  26.     glColor3f(0, 0, 1);
  27.     glutSolidSphere(radius/2,10,30);
  28.     glRotatef(yRotated*2,0.0,1.0,0.0);
  29.    
  30.     glTranslatef(0.0,0.125,0.5);
  31.     glColor3f(1, 1, 1);
  32.     glutSolidSphere(radius/4,10,30);
  33.    
  34.     glutSwapBuffers();
  35.  
  36.    
  37.  
  38. def reshapeFunc(x, y):
  39.     if (y == 0 or x == 0):
  40.         return;  #Nothing is visible then, so return
  41.     #Set a new projection matrix
  42.     glMatrixMode(GL_PROJECTION);  
  43.     glLoadIdentity();
  44.     #Angle of view:40 degrees
  45.     #Near clipping plane distance: 0.5
  46.     #Far clipping plane distance: 20.0
  47.     gluPerspective(40.0,x/y,0.5,20.0);
  48.     glMatrixMode(GL_MODELVIEW);
  49.     glViewport(0,0,x,y);  #Use the whole window for rendering
  50.  
  51. def idleFunc():
  52.     global xRotated
  53.     global yRotated
  54.     global zRotated
  55. ##    xRotated += 0.01;
  56. ##    zRotated += 0.01;
  57.     yRotated += 0.5;
  58.     # :: testing
  59.     if yRotated >= 360:
  60.         yRotated = 0
  61.     redisplayFunc();
  62.  
  63. def myinit():
  64.     mat_ambient=[0.3,0.3,0.3,1.0];
  65.     mat_diffuse=[0.6,0.6,0.6,1.0];
  66.     mat_specular=[0.9,0.9,0.9,1.0];
  67.     mat_shininess=[100.0];
  68.    
  69.     glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient);
  70.     glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse);
  71.     glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);
  72.     glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess);
  73.    
  74.     light0_ambient=[0.5,0.5,0.5,1.0];
  75.     light0_diffuse=[1.0,1.0,1.0,1.0];
  76.     light0_specular=[1.0,1.0,1.0,1.0];
  77.     light0_position=[5.0,5.0,5.0,0.0];
  78.    
  79.     glLightfv(GL_LIGHT0,GL_AMBIENT,light0_ambient);
  80.     glLightfv(GL_LIGHT0,GL_DIFFUSE,light0_diffuse);
  81.     glLightfv(GL_LIGHT0,GL_SPECULAR,light0_specular);
  82.     glLightfv(GL_LIGHT0,GL_POSITION,light0_position);  
  83.     glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE,light0_ambient);
  84.     glEnable(GL_LIGHTING);
  85.     glEnable(GL_LIGHT0);
  86.     #glEnable(GL_DEPTH_TEST);
  87.     glShadeModel(GL_SMOOTH);
  88.     #glEnable(GL_NORMALIZE);
  89.  
  90. def main():
  91.     global xRotated
  92.     global yRotated
  93.     global zRotated
  94.     # Initialize GLUT
  95.     glutInit(sys.argv);
  96.     # double buffering used to avoid flickering problem in animation
  97.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  98.     # window size
  99.     glutInitWindowSize(400,400);
  100.     # create the window
  101.     glutCreateWindow(b"Sphere Rotating Animation");
  102.     #glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
  103.     glClearColor(0.0,0.0,0.0,0.0);
  104.     #Assign  the function used in events
  105.     glutDisplayFunc(redisplayFunc);
  106.     glutReshapeFunc(reshapeFunc);
  107.     glutIdleFunc(idleFunc);
  108.     #Let start glut loop
  109.     #myinit()
  110.     glutMainLoop();
  111.  
  112. if __name__ == '__main__': main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement