Advertisement
Guest User

Untitled

a guest
Apr 15th, 2024
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.14 KB | None | 0 0
  1. /*
  2. * SimpleLight.c
  3. *
  4. * Example program illustrating a simple use
  5. * of lighting. Creates six spheres of different
  6. * fully saturated colors. Adds a white light that
  7. * slow rotates around the spheres.
  8. *
  9. * USAGE:
  10. * Press "r" key to toggle (off and on) running the animation
  11. * Press "s" key to single-step animation
  12. * The up arrow key and down array key control the
  13. * time step used in the animation rate. This will
  14. * speed up or slow down the rate at which the light
  15. * revolves around the spheres.
  16. * Press ESCAPE to exit.
  17. *
  18. */
  19.  
  20. #include <math.h> // For math routines (such as sqrt & trig).
  21. #include <stdio.h>
  22. #include <stdlib.h> // For the "exit" function
  23. #include "glut.h" // OpenGL Graphics Utility Library
  24. #include "SimpleLight.h"
  25.  
  26. // Values that control the material properties.
  27. float Noemit[4] = {0.0, 0.0, 0.0, 1.0};
  28. float SphShininess = 20; // Specular exponent for the spheres.
  29. float SphAmbDiff[6][4] = { // The ambient/diffuse colors of the six spheres
  30. {0.5, 0.0, 0.0, 1.0}, // Red
  31. {0.5, 0.8, 0.0, 1.0}, // Yellow
  32. {0.0, 0.5, 0.0, 1.0}, // Green
  33. {0.1, 0.5, 0.7, 1.0}, // Cyan
  34. {0.0, 0.0, 0.5, 0.0}, // Blue
  35. {0.5, 0.5, 1.0, 0.5} // Purple
  36. };
  37. float SphSpecular[4] = { 1, 1, 1, 1.0 };
  38.  
  39. // Lighting values
  40. float ambientLight[4] = {0.2, 0.2, 0.2, 1.0};
  41. float Lt0amb[4] = {0.3, 0.3, 0.3, 1.0};
  42. float Lt0diff[4] = {1.0, 1.0, 1.0, 1.0};
  43. float Lt0spec[4] = {1.0, 1.0, 1.0, 1.0};
  44.  
  45. float zeroPos[4] = {0, 0, 0, 1}; // Origin (homogeneous representation)
  46. float dirI[4] = {1, 0, 0, 0}; // Direction of unit vector I (point at infinity)
  47.  
  48. int LightIsPositional = 0;
  49.  
  50. int RunMode = 1; // Used as a boolean (1 or 0) for "on" and "off"
  51.  
  52. // The next global variable controls the animation's state and speed.
  53. float CurrentAngle = 0.0f; // Angle in degrees
  54. float AnimateStep = 0.5f; // Rotation step per update
  55.  
  56. // glutKeyboardFunc is called below to set this function to handle
  57. // all "normal" key presses.
  58. void myKeyboardFunc( unsigned char key, int x, int y )
  59. {
  60. switch ( key ) {
  61. case 'r':
  62. RunMode = 1-RunMode; // Toggle to opposite value
  63. if ( RunMode==1 ) {
  64. glutPostRedisplay();
  65. }
  66. break;
  67. case 's':
  68. RunMode = 1;
  69. drawScene();
  70. RunMode = 0;
  71. break;
  72. case 'l': // Toggle local light mode
  73. LightIsPositional = 1 - LightIsPositional;
  74. if ( RunMode==0 ) {
  75. drawScene();
  76. }
  77. break;
  78. case 27: // Escape key
  79. exit(1);
  80. }
  81. }
  82.  
  83. // glutSpecialFunc is called below to set this function to handle
  84. // all "special" key presses. See glut.h for the names of
  85. // special keys.
  86. void mySpecialKeyFunc( int key, int x, int y )
  87. {
  88. switch ( key ) {
  89. case GLUT_KEY_UP:
  90. if ( AnimateStep < 5.0) { // Don't let speed get very big.
  91. AnimateStep *= sqrt(2.0); // Increase the angle increment
  92. }
  93. break;
  94. case GLUT_KEY_DOWN:
  95. if (AnimateStep>1.0e-3) { // Avoid underflow problems.
  96. AnimateStep /= sqrt(2.0); // Decrease the angle increment
  97. }
  98. break;
  99. }
  100. }
  101.  
  102. /*
  103. * drawScene() handles the animation and the redrawing of the
  104. * graphics window contents.
  105. */
  106. void drawScene(void)
  107. {
  108. int i;
  109.  
  110. // Clear the rendering window
  111. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  112.  
  113. if (RunMode==1) {
  114. // Calculate animation parameters
  115. CurrentAngle += AnimateStep;
  116. if ( CurrentAngle > 360.0 ) {
  117. CurrentAngle -= 360.0*floor(CurrentAngle/360.0); // Don't allow overflow
  118. }
  119. }
  120.  
  121. // Rotate the image
  122. glMatrixMode( GL_MODELVIEW ); // Current matrix affects objects positions
  123. glLoadIdentity(); // Initialize to the identity
  124. //glRotatef(90, 0.0, 0.0, 0.0);
  125. // Position the light (before drawing the illuminated objects)
  126.  
  127. glPushMatrix();
  128. glRotatef( CurrentAngle, 0.0, 1.0, 0.0 ); // Rotate through animation angle
  129. glTranslatef( 2.5, 0.0, 0.0 ); // Translate rotation center to origin
  130. glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, Lt0spec); // Make sphere glow (emissive)
  131. glutSolidSphere(0.3, 10, 10);
  132. glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, Noemit); // Turn off emission
  133.  
  134. if ( LightIsPositional==1 ) {
  135. glLightfv(GL_LIGHT0, GL_POSITION, zeroPos ); // Position is transformed by ModelView matrix
  136. }
  137. else {
  138. glLightfv(GL_LIGHT0, GL_POSITION, dirI ); // Direction is transformed by ModelView matrix
  139. }
  140. glPopMatrix();
  141.  
  142. // Draw six spheres of different colors
  143. for ( i=0; i<1; i++ ) {
  144. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, SphAmbDiff[i] );
  145. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, SphSpecular );
  146. glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, SphShininess);
  147. glPushMatrix();
  148. //glRotatef( 60.0*(float)i, 0.0, 0.0, 1.0 ); // Rotate each one another 60 degrees
  149. glTranslatef(0.0, 0.0, -2.0);
  150. glutSolidSphere( 1.5, 30, 30 );
  151. glPopMatrix();
  152.  
  153. }
  154.  
  155.  
  156.  
  157. // Flush the pipeline, swap the buffers
  158. glFlush();
  159. glutSwapBuffers();
  160.  
  161. if ( RunMode==1 ) {
  162. glutPostRedisplay(); // Trigger an automatic redraw for animation
  163. }
  164.  
  165. }
  166.  
  167. // Initialize OpenGL's rendering modes
  168. void initRendering()
  169. {
  170. glEnable( GL_DEPTH_TEST ); // Depth testing must be turned on
  171.  
  172. glEnable(GL_LIGHTING); // Enable lighting calculations
  173. glEnable(GL_LIGHT0); // Turn on light #0.
  174.  
  175. // Set global ambient light
  176. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
  177.  
  178. // Light 0 light values. Its position is set in drawScene().
  179. glLightfv(GL_LIGHT0, GL_AMBIENT, Lt0amb);
  180. glLightfv(GL_LIGHT0, GL_DIFFUSE, Lt0diff);
  181. glLightfv(GL_LIGHT0, GL_SPECULAR, Lt0spec);
  182. }
  183.  
  184. // Called when the window is resized
  185. // w, h - width and height of the window in pixels.
  186. void resizeWindow(int w, int h)
  187. {
  188. float viewWidth = 7.0; // Actually this is half of the width
  189. float viewHeight = 7.0; // Again, this is half of the height
  190. glViewport(0, 0, w, h);
  191. h = (h==0) ? 1 : h;
  192. w = (w==0) ? 1 : w;
  193. glMatrixMode(GL_PROJECTION);
  194. glLoadIdentity();
  195. if ( h < w ) {
  196. viewWidth *= (float)w/(float)h;
  197. }
  198. else {
  199. viewHeight *= (float)h/(float)w;
  200. }
  201. glOrtho( -viewWidth, viewWidth, -viewHeight, viewHeight, -1.0, 1.0 );
  202.  
  203. }
  204.  
  205.  
  206. // Main routine
  207. // Set up OpenGL, define the callbacks and start the main loop
  208. int main( int argc, char** argv )
  209. {
  210. glutInit(&argc,argv);
  211.  
  212. // We're going to animate it, so double buffer
  213. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
  214.  
  215. // Window position (from top corner), and size (width% and hieght)
  216. glutInitWindowPosition( 10, 60 );
  217. glutInitWindowSize( 360, 360 );
  218. glutCreateWindow( "SimpleLight" );
  219.  
  220. // Initialize OpenGL parameters.
  221. initRendering();
  222.  
  223. // Set up callback functions for key presses
  224. glutKeyboardFunc( myKeyboardFunc ); // Handles "normal" ascii symbols
  225. glutSpecialFunc( mySpecialKeyFunc ); // Handles "special" keyboard keys
  226.  
  227. // Set up the callback function for resizing windows
  228. glutReshapeFunc( resizeWindow );
  229.  
  230. // Call this for background processing
  231. // glutIdleFunc( myIdleFunction );
  232.  
  233. // Call this whenever window needs redrawing
  234. glutDisplayFunc( drawScene );
  235.  
  236. fprintf(stdout, "Arrow keys control speed. Press \"r\" to run, \"s\" to single step.\n");
  237.  
  238. // Start the main loop. glutMainLoop never returns.
  239. glutMainLoop( );
  240.  
  241. return(0); // This line is never reached.
  242. }
  243.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement