Check out the Pastebin Gadgets Shop. We have thousands of fun, geeky & affordable gadgets on sale :-)Want more features on Pastebin? Sign Up, it's FREE!
tweet

# hawk

By: a guest on May 11th, 2011  |  syntax: None  |  size: 17.03 KB  |  views: 14  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1.
2.
3. #include <math.h>               // For math routines (such as sqrt & trig).
4. #include <stdio.h>
5. #include <GL/glut.h>    // OpenGL Graphics Utility Library
6. #include "GluCylinders.h"
7. void ani(float , float , float );
8. void renderCylinder(float x1, float y1, float z1, float x2,float y2, float z2, float radius,int subdivisions,GLUquadricObj *quadric);
9. void renderCylinder_convenient(float x1, float y1, float z1, float x2,float y2, float z2, float radius,int subdivisions);
10. // The next global variable controls the animation's state and speed.
11. float RotateAngle = 0.0f;               // Angle in degrees of rotation around y-axis
12. float Azimuth = 0.0;                    // Rotated up or down by this amount
13.
14. float AngleStepSize = 3.0f;             // Step three degrees at a time
15. const float AngleStepMax = 10.0f;
16. const float AngleStepMin = 0.1f;
17.
18. int WireFrameOn = 1;                    // == 1 for wire frame mode
19.
20. //points
21.
22. float Geometry[9][3] = {
23.         { 4,2.0,0},
24.         { 4,2.0,0},     //      Point1
25.         {-1.5,0.2,0},
26.
27.         {-2,0.3,0},
28.         {-2.5,0.6,0},
29.
30.         {-3,0.8,0},
31.
32.         {-3.5,1.0,0},
33.         { -5.5,1.8,0},//point 2
34.         { -5.5,1.8,0}
35.
36. };
37. int x2[5],y2[5],z2[5];
38. unsigned int LOD=20;
39.
40. // glutKeyboardFunc is called below to set this function to handle
41. //              all "normal" key presses.
42. void myKeyboardFunc( unsigned char key, int x, int y )
43. {
44.         switch ( key ) {
45.         case 'w':
46.                 WireFrameOn = 1-WireFrameOn;
47.                 if ( WireFrameOn ) {
48.                         glPolygonMode ( GL_FRONT_AND_BACK, GL_LINE );           // Just show wireframes
49.                 }
50.                 else {
51.                         glPolygonMode ( GL_FRONT_AND_BACK, GL_FILL );           // Show solid polygons
52.                 }
53.                 glutPostRedisplay();
54.                 break;
55.         case 'R':
56.                 AngleStepSize *= 1.5;
57.                 if (AngleStepSize>AngleStepMax ) {
58.                         AngleStepSize = AngleStepMax;
59.                 }
60.                 break;
61.         case 'r':
62.                 AngleStepSize /= 1.5;
63.                 if (AngleStepSize<AngleStepMin ) {
64.                         AngleStepSize = AngleStepMin;
65.                 }
66.                 break;
67.         case 27:        // Escape key
68.                 exit(1);
69.         }
70. }
71.
72. // glutSpecialFunc is called below to set this function to handle
73. //              all "special" key presses.  See glut.h for the names of
74. //              special keys.
75. void mySpecialKeyFunc( int key, int x, int y )
76. {
77.         switch ( key ) {
78.         case GLUT_KEY_UP:
79.                 Azimuth += AngleStepSize;
80.                 if ( Azimuth>80.0f ) {
81.                         Azimuth = 80.0f;
82.                 }
83.                 break;
84.         case GLUT_KEY_DOWN:
85.                 Azimuth -= AngleStepSize;
86.                 if ( Azimuth < -80.0f ) {
87.                         Azimuth = -80.0f;
88.                 }
89.                 break;
90.         case GLUT_KEY_LEFT:
91.                 RotateAngle += AngleStepSize;
92.                 if ( RotateAngle > 180.0f ) {
93.                         RotateAngle -= 360.0f;
94.                 }
95.                 break;
96.         case GLUT_KEY_RIGHT:
97.
98.                 RotateAngle -= AngleStepSize;
99.                 if ( RotateAngle < -180.0f ) {
100.                         RotateAngle += 360.0f;
101.
102.                 }
103.                 break;
104.         }
105.         glutPostRedisplay();
106.
107. }
108. /*
109.  * drawScene() handles the animation and the redrawing of the
110.  *              graphics window contents.
111.  */
112. void drawScene(void)
113. {int i,j,k=0,m=0;
114. float b0,b1,b2,b3,t,x3,y3,z3,n1,n2,n3;
115.         //Clear the rendering window
116.
117. //for( i=0;i<5;i++)
118. //{
119. //x2[i]=0.5*(G[i+1][0]-G[i-1][0]);
120. //y2[i]=0.5*(G[i+1][1]-G[i-1][1]);
121. //z2[i]=0.5*(G[i+1][2]-G[i-1][2]);
122. //}
123.
124. // Clear the rendering window
125.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
126.
127.
128.         // Rotate the image
129.         glMatrixMode( GL_MODELVIEW );                           // Current matrix affects objects positions
130.         glLoadIdentity();                                                       // Initialize to the identity
131.
132.         glTranslatef( -0.5, 0.0, -35.0 );                               // Translate  from origin (in front of viewer)
133.         glRotatef( RotateAngle, 0.0, 1.0, 0.0 );                // Rotate around y-axis
134.         glRotatef( Azimuth, 1.0, 0.0, 0.0 );                    // Set Azimuth angle
135. //------------------------------------------------------------
136.         glDisable( GL_CULL_FACE );
137.         glPushMatrix();
138.         glTranslatef( -4.5, 0.5, 0.0 );
139.         glRotatef( -90.0, 1.0, 0.0, 0.0 );
140.         glColor3f( 1.0, 0.2, 0.2 );                                     // Reddish color x<=4.5 0.5=<y<=2.5
141.         // Parameters: height, radius, slices, stacks
142.         drawGluCylinder(1.5, 0.2, 8, 8 );
143.         glPopMatrix();
144.
145.         glEnable( GL_CULL_FACE );
146.         glPushMatrix();
147.         glTranslatef( -4.5, 0.0, 0.0 );
148.         glRotatef( -90.0, 1.0, 0.0, 0.0 );
149.         glColor3f( 0.2, 1.0, 0.2 );                                     // Greenish color
151.         drawGluSlantCylinderWithCaps( 0.5, 0.01, 0.2, 28, 28 );
152.         glPopMatrix();
153.
154. //------------------------------------------------------------------1st wicket
155.                 glDisable( GL_CULL_FACE );
156.         glPushMatrix();
157.         glTranslatef( -4.5, 0.5, 0.6 );
158.         glRotatef( -90.0, 1.0, 0.0, 0.0 );
159.         glColor3f( 1.0, 0.2, 0.2 );                                     // Reddish color
160.         // Parameters: height, radius, slices, stacks
161.         drawGluCylinder(1.5, 0.2, 8, 8 );
162.         glPopMatrix();
163.
164.         glEnable( GL_CULL_FACE );
165.         glPushMatrix();
166.         glTranslatef( -4.5, 0.0, 0.6 );
167.         glRotatef( -90.0, 1.0, 0.0, 0.0 );
168.         glColor3f( 0.2, 1.0, 0.2 );                                     // Greenish color
170.         drawGluSlantCylinderWithCaps( 0.5, 0.01, 0.2, 28, 28 );
171.         glPopMatrix();
172.
173.
174. //wills
175.                 glDisable( GL_CULL_FACE );
176.         glPushMatrix();
177.         glTranslatef( -4.5, 2.05, 0.6 );
178.         glRotatef( -180.0, 1.0, 0.0, 0.0 );
179.         glColor3f( 1.0, 0.2, 0.2 );                                     // Reddish color
180.         // Parameters: height, radius, slices, stacks
181.         drawGluCylinder(0.6, 0.05, 18, 18 );
182.         glPopMatrix();
183.
184. //---------------------------------------------------------------------------2nd
185.                 glDisable( GL_CULL_FACE );
186.         glPushMatrix();
187.         glTranslatef( -4.5, 0.5, 1.2 );
188.         glRotatef( -90.0, 1.0, 0.0, 0.0 );
189.         glColor3f( 1.0, 0.2, 0.2 );                                     // Reddish color
190.         // Parameters: height, radius, slices, stacks
191.         drawGluCylinder(1.5, 0.2, 8, 8 );
192.         glPopMatrix();
193.
194.         glEnable( GL_CULL_FACE );
195.         glPushMatrix();
196.         glTranslatef( -4.5, 0.0, 1.2 );
197.         glRotatef( -90.0, 1.0, 0.0, 0.0 );
198.         glColor3f( 0.2, 1.0, 0.2 );                                     // Greenish color
200.         drawGluSlantCylinderWithCaps( 0.5, 0.01, 0.2, 28, 28 );
201.         glPopMatrix();
202.         //wills
203.         glDisable( GL_CULL_FACE );
204.         glPushMatrix();
205.         glTranslatef( -4.5, 2.05, 1.3 );
206.         glRotatef( -180.0, 1.0, 0.0, 0.0 );
207.         glColor3f( 1.0, 0.2, 0.2 );                                     // Reddish color
208.         // Parameters: height, radius, slices, stacks
209.         drawGluCylinder(0.6, 0.05, 18, 18 );
210.         glPopMatrix();
211.
212.
213.         //--------------------------------------------------3rd wicket
214.  //   glDisable( GL_CULL_FACE );
215.         //glPushMatrix();
216.         //glTranslatef( -4.5, 0.5, 0.0 );
217.         //glRotatef( -90.0, 1.0, 0.0, 0.0 );
218.         //glColor3f( 1.0, 0.2, 0.2 );                                   // Reddish color
219.         //// Parameters: height, radius, slices, stacks
220.         //drawGluCylinder(1.5, 0.2, 8, 8 );
221.         //glPopMatrix();
222.  //   glEnable( GL_CULL_FACE );
223.         //glPushMatrix();
224.         //glTranslatef( -4.5, 0.0, 0.0 );
225.         //glRotatef( -90.0, 1.0, 0.0, 0.0 );
226.         //glColor3f( 0.2, 1.0, 0.2 );                                   // Greenish color
228.         //drawGluSlantCylinderWithCaps( 0.5, 0.05, 0.2, 28, 28 );
229.         //glPopMatrix();
230.  //   glDisable( GL_CULL_FACE );
231.         //glPushMatrix();
232.         //glTranslatef( -4.5, 0.5, 1.0 );
233.         //glRotatef( -90.0, 1.0, 0.0, 0.0 );
234.         //glColor3f( 1.0, 0.2, 0.2 );                                   // Reddish color
235.         //// Parameters: height, radius, slices, stacks
236.         //drawGluCylinder(1.5, 0.2, 8, 8 );
237.         //glPopMatrix();
238.
239.
240.
241.
242.
243.
244.
245.
246.         //-------------------------------
247.         glDisable( GL_CULL_FACE );
248.         glPushMatrix();
249.         glTranslatef( 4.5, 0.5, 0.0 );
250.         glRotatef( -90.0, 1.0, 0.0, 0.0 );
251.         glColor3f( 1.0, 0.2, 0.2 );                                     // Reddish color
252.         // Parameters: height, radius, slices, stacks
253.         drawGluCylinder(1.5, 0.2, 8, 8 );
254.         glPopMatrix();
255.
256.         glEnable( GL_CULL_FACE );
257.         glPushMatrix();
258.         glTranslatef( 4.5, 0.0, 0.0 );
259.         glRotatef( -90.0, 1.0, 0.0, 0.0 );
260.         glColor3f( 0.2, 1.0, 0.2 );                                     // Greenish color
262.         drawGluSlantCylinderWithCaps( 0.5, 0.01, 0.2, 28, 28 );
263.         glPopMatrix();
264.
265.
266.                 glDisable( GL_CULL_FACE );
267.         glPushMatrix();
268.         glTranslatef( 4.5, 0.5, 0.6 );
269.         glRotatef( -90.0, 1.0, 0.0, 0.0 );
270.         glColor3f( 1.0, 0.2, 0.2 );                                     // Reddish color
271.         // Parameters: height, radius, slices, stacks
272.         drawGluCylinder(1.5, 0.2, 8, 8 );
273.         glPopMatrix();
274.
275.         glEnable( GL_CULL_FACE );
276.         glPushMatrix();
277.         glTranslatef( 4.5, 0.0, 0.6 );
278.         glRotatef( -90.0, 1.0, 0.0, 0.0 );
279.         glColor3f( 0.2, 1.0, 0.2 );                                     // Greenish color
281.         drawGluSlantCylinderWithCaps( 0.5, 0.01, 0.2, 28, 28 );
282.         glPopMatrix();
283.
284.         //wills
285.         glDisable( GL_CULL_FACE );
286.         glPushMatrix();
287.         glTranslatef( 4.5, 2.05, 0.6 );
288.         glRotatef( -180.0, 1.0, 0.0, 0.0 );
289.         glColor3f( 1.0, 0.2, 0.2 );                                     // Reddish color
290.         // Parameters: height, radius, slices, stacks
291.         drawGluCylinder(0.6, 0.05, 18, 18 );
292.         glPopMatrix();
293.
294.
295.                 glDisable( GL_CULL_FACE );
296.         glPushMatrix();
297.         glTranslatef( 4.5, 0.5, 1.2 );
298.         glRotatef( -90.0, 1.0, 0.0, 0.0 );
299.         glColor3f( 1.0, 0.2, 0.2 );                                     // Reddish color
300.         // Parameters: height, radius, slices, stacks
301.         drawGluCylinder(1.5, 0.2, 8, 8 );
302.         glPopMatrix();
303.
304.         glEnable( GL_CULL_FACE );
305.         glPushMatrix();
306.         glTranslatef( 4.5, 0.0, 1.2 );
307.         glRotatef( -90.0, 1.0, 0.0, 0.0 );
308.         glColor3f( 0.2, 1.0, 0.2 );                                     // Greenish color
310.         drawGluSlantCylinderWithCaps( 0.5, 0.01, 0.2, 28, 28 );
311.         glPopMatrix();
312.     glDisable( GL_CULL_FACE );
313.         glPushMatrix();
314.         glTranslatef( 4.5, 0.5, 0.0 );
315.         glRotatef( -90.0, 1.0, 0.0, 0.0 );
316.         glColor3f( 1.0, 0.2, 0.2 );                                     // Reddish color
317.         // Parameters: height, radius, slices, stacks
318.         drawGluCylinder(1.5, 0.2, 8, 8 );
319.         glPopMatrix();
320.
321.         //wills
322.         glDisable( GL_CULL_FACE );
323.         glPushMatrix();
324.         glTranslatef( 4.5, 2.05, 1.3 );
325.         glRotatef( -180.0, 1.0, 0.0, 0.0 );
326.         glColor3f( 1.0, 0.2, 0.2 );                                     // Reddish color
327.         // Parameters: height, radius, slices, stacks
328.         drawGluCylinder(0.6, 0.05, 18, 18 );
329.         glPopMatrix();
330.
331.
332.
333.
334.         glBegin(GL_LINE_STRIP);
335.         // use the parametric time value 0 to 1
336.         for( j=1;j<=2;j++)
337.         {
338.         for(i=0;i!=LOD;++i) {
339.                 //glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
340.                  t = (float)i/(LOD-1);
341.
342.                   x3 = 0.5*((-Geometry[j-1][0] + 3*Geometry[j][0]  -3*Geometry[j+1][0] + Geometry[j+2][0])*t*t*t+
343.                         (2*Geometry[j-1][0] -5*Geometry[j][0] +4*Geometry[j+1][0] -Geometry[j+2][0])*t*t +(-Geometry[j-1][0] + Geometry[j+1][0])*t + 2*Geometry[j][0]);
344.
345.                 y3 = 0.5*((-Geometry[j-1][1] + 3*Geometry[j][1]  -3*Geometry[j+1][1] + Geometry[j+2][1])*t*t*t+
346.                         (2*Geometry[j-1][1] -5*Geometry[j][1] +4*Geometry[j+1][1] -Geometry[j+2][1])*t*t +(-Geometry[j-1][1] + Geometry[j+1][1])*t + 2*Geometry[j][1]);
347.
348.
349.
350.                 z3 = 0.5*((-Geometry[j-1][2] + 3*Geometry[j][2]  -3*Geometry[j+1][2] + Geometry[j+2][2])*t*t*t+
351.                         (2*Geometry[j-1][2] -5*Geometry[j][2] +4*Geometry[j+1][2] -Geometry[j+2][2])*t*t +(-Geometry[j-1][2] + Geometry[j+1][2])*t + 2*Geometry[j][2]);
352.
353.                 // specify the point
354.
355.                 glVertex3f( x3,y3,z3);
356.
357. glColor3f(1,0,0);
358.
359.                 // specify the point
360.                 glVertex3f( x3,y3,z3 );
361.                 ani(x3,y3,z3);
362.                 //for(i=0;i<=2;i++);3
363.                  glColor3f(1,1,0);
364.                   if(i==0)
365.                         renderCylinder_convenient(Geometry[j][0],Geometry[j][1],Geometry[j][2],x3,y3,z3,0.3,32);
366.                   else
367.                           renderCylinder_convenient(n1,n2,n3,x3,y3,z3,0.3,32);
368.                   n1=x3; n2=y3; n3=z3;
369.
370.                 glutSwapBuffers();
371.                 }
372.
373.                 }
374.
375.
376.         glEnd();
377.
378.
379.
380.   //  glFlush();
381.     //glutSwapBuffers();
382. }
383.
384.
385.
386. // Initialize OpenGL's rendering modes
387. void initRendering()
388. {
389.     glEnable( GL_DEPTH_TEST );  // Depth testing must be turned on
390.
391.         glCullFace( GL_BACK );
392.
393.         glPolygonMode ( GL_FRONT_AND_BACK, GL_LINE );           // Just show wireframes at first
394. }
395.
396. // Called when the window is resized
397. //              w, h - width and height of the window in pixels.
398. void resizeWindow(int w, int h)
399. {
400.         double aspectRatio;
401.
402.         // Define the portion of the window used for OpenGL rendering.
403.         glViewport( 0, 0, w, h );       // View port uses whole window
404.
405.         // Set up the projection view matrix: perspective projection
406.         // Determine the min and max values for x and y that should appear in the window.
407.         // The complication is that the aspect ratio of the window may not match the
408.         //              aspect ratio of the scene we want to view.
409.         w = (w==0) ? 1 : w;
410.         h = (h==0) ? 1 : h;
411.         aspectRatio = (double)w / (double)h;
412.     glMatrixMode( GL_PROJECTION );
414.     gluPerspective( 15.0, aspectRatio, 25.0, 45.0 );
415.
416. }
417.
418.
419. // Main routine
420. // Set up OpenGL, define the callbacks and start the main loop
421. int main( int argc, char** argv )
422. {
423.         glutInit(&argc, argv);
424.
425.         // We're going to animate it, so double buffer
426.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
427.
428.         // Window position (from top corner), and size (width% and hieght)
429.     glutInitWindowPosition( 10, 60 );
430.     glutInitWindowSize( 360, 360 );
431.     glutCreateWindow( "GluCylinders" );
432.
433.         // Initialize OpenGL as we like it..
434.     initRendering();
435.
436.         // Set up callback functions for key presses
437.         glutKeyboardFunc( myKeyboardFunc );                     // Handles "normal" ascii symbols
438.         glutSpecialFunc( mySpecialKeyFunc );            // Handles "special" keyboard keys
439.
440.         // Set up the callback function for resizing windows
441.     glutReshapeFunc( resizeWindow );
442.
443.         // Call this for background processing
444.         // glutIdleFunc( myIdleFunction );
445.
446.         // call this whenever window needs redrawing
447.     glutDisplayFunc( drawScene );
448.
449.         fprintf(stdout, "Arrow keys control viewpoint.\n");
450.         fprintf(stdout, "Press \"w\" to toggle wireframe mode.\n");
451.         fprintf(stdout, "Press \"R\" or \"r\" to increase or decrease rate of movement (respectively).\n");
452.
453.         // Start the main loop.  glutMainLoop never returns.
454.         glutMainLoop(  );
455.
456.     return(0);  // This line is never reached.
457. }
458.
459. // ************************************************************************
460. // These are four general purpose routines for generating
461. //      cylinders, with or without caps.
462. // See the OpenGL redbook or other glu documentation for more information.
463. // These generate normals, but not texture coordinates.
464. // To generate texture coordinates, you need to modify the code to
466. // For higher performance, you should consider putting your cylinders into
467. //       a display list.
468. // Please note these routines do not do all possible error checking, and
469. //       thus should not be used in a production or other critical environment.
470. // ************************************************************************
471.
472. //  A Reusable gluQuadric object:
474.
475. void drawGluCylinder( double height, double radius, int slices, int stacks ) {
477. }
478.
479. void drawGluSlantCylinder( double height, double radiusBase, double radiusTop, int slices, int stacks )
480. {
481.         if ( ! myReusableQuadric ) {
483.                 // Should (but don't) check if pointer is still null --- to catch memory allocation errors.
485.         }
486.         // Draw the cylinder.
488. }
489.
490.
491. void drawGluCylinderWithCaps( double height, double radius, int slices, int stacks ) {
493. }
494. void ani(float x, float y, float z)
495. {int i, j;
496.
498.
499. glTranslatef(-0.5, 0.0, -35.0);
500. glRotatef( RotateAngle, 0.0, 1.0, 0.0 );
501. glRotatef( Azimuth, 1.0, 0.0, 0.0 );
502.
503. glPushMatrix();
504. glTranslatef (x, y, z);
505.
506. glColor3f( 1, 0.0, 0.2 );
507. glutSolidSphere (0.1, 10, 10);
508.
509.
510.
511. for(i = 0; i < 1000; i++)
512. for(j = 0; j < 5000; j++);
513.
514.
515.
516. glPopMatrix();
517. //glPushMatrix();
518. glutSwapBuffers();
519. }
520. void renderCylinder(float x1, float y1, float z1, float x2,float y2, float z2, float radius,int subdivisions,GLUquadricObj *quadric)
521. {   float v,rx,ry,ax,vx = x2-x1;
522. float vy = y2-y1;
523. float vz = z2-z1;   //handle the degenerate case of z1 == z2 with an approximation
524. if(vz == 0)
525. vz =.00000001;
526.  v = sqrt( vx*vx + vy*vy + vz*vz );
527.  ax = 57.2957795*acos( vz/v );
528. if ( vz < 0.0 )
529. ax = -ax;
530.  rx = -vy*vz;
531.  ry = vx*vz;
532. glPushMatrix();   //draw the cylinder body
533. glTranslatef( x1,y1,z1 );
534. glRotatef(ax, rx, ry, 0.0);
539. glTranslatef( 0,0,v );   //draw the second cap
542. glPopMatrix();
543. }
544.
545. void renderCylinder_convenient(float x1, float y1, float z1, float x2,float y2, float z2, float radius,int subdivisions){
546.         //the same quadric can be re-used for drawing many cylinders
551.
552.
553. void drawGluSlantCylinderWithCaps( double height, double radiusBase, double radiusTop, int slices, int stacks )
554. {
555.         // First draw the cylinder
557.
558.         // Draw the top disk cap
559.         glPushMatrix();
560.         glTranslated(0.0, 0.0, height);