Advertisement
Charlesr456

gluLookAt Problem

Nov 12th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <GL/glut.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <iostream>
  5. #include <fstream>
  6. using namespace std;
  7.  
  8. //fstream output("data.txt",ios::out);
  9.  
  10. double dt = 0.05; // this is the time step
  11. float coeF = 0.5;
  12. float verysmall = 0.001;
  13. float totaltime = 0.0;
  14. float gravity = 0.1;
  15. GLfloat near = 0.1,far = 20.0;
  16. GLfloat startdispz = -1.0;
  17.  
  18. const float DEG2RAD = 3.14159/180;
  19.        
  20. class ball
  21. {
  22.     private:
  23.         // ax acceleration in x, px position in x, vx velocity in x
  24.         double ax,ay,az,px,py,pz,vx,vy,vz,size;
  25.  
  26.     public:
  27.         ball()
  28.         {
  29.             px=0.0;py=0.0;
  30.         }
  31.         ball(double dummyvx, double dummyvy,double dummyvz)
  32.         {
  33.             px=0;   py=0.1; pz=0;   size=0.1;
  34.             vx=dummyvx; vy=dummyvy; vz=dummyvz; ay=gravity;
  35.         }
  36.         double getpx(){return px;}  double getpy(){return py;}  double getpz(){return pz;}
  37.         double getvx(){return vx;}  double getvy(){return vy;}  double getvz(){return vz;}
  38.         double getballsize(){return size;}
  39.  
  40.         double getspeed(){return  sqrt(py*py + px*px + pz*pz);}
  41.  
  42.         void evolve()
  43.         {
  44.             //cout << "vy" << vy << "\n";
  45.             vy += (-gravity*dt);                            // equation for the y component of v
  46.             px += vx*dt;    py += vy*dt;    pz += vz*dt;    // equations for all 3 components of p
  47.             totaltime += dt;                                // update total time
  48.             //output << totaltime << " " << px << " "   << py << " "  << pz << "\n";
  49.             //cout << totaltime << " " << py  << " " << pz << "\n";
  50.             //terminate when we hit the ground i.e pz < 0
  51.    
  52.             if(py < size)  
  53.             {          
  54.                 py = size;
  55.                 vy = -vy*coeF;
  56.  
  57.                 //cout << "The ball has hit the ground\n";
  58.                 //vx=0; vy=0;   vz=0;
  59.                 //output.close();
  60.                 //exit(0);
  61.             }
  62.         }
  63. };
  64.  
  65. // ball start at origin with velocity vector as in the constructor
  66. ball golfball(0.0, 0.4, 0.8);
  67.  
  68. void init(void)
  69. {
  70.     glClearColor(1.0,1.0,1.0,0.0);
  71. }
  72.  
  73. void drawCircle(float Radius, int numPoints)
  74. {
  75.     glBegin( GL_LINE_LOOP );
  76.     for( int i=0; i<numPoints; i++ )
  77.     {
  78.         float Angle = i * (2.0*M_PI/numPoints);
  79.         float X = cos( Angle )*Radius;
  80.         float Y = sin( Angle )*Radius;
  81.         glVertex2f( X, Y );
  82.     }
  83.     glEnd();
  84. }
  85.  
  86. void display(void)
  87. {
  88.     glMatrixMode(GL_PROJECTION);
  89.     glLoadIdentity();
  90.     gluPerspective(40,1,near,far);
  91.    
  92.     glMatrixMode(GL_MODELVIEW);
  93.     glLoadIdentity();  
  94.     gluLookAt(0.0, 2.0, -3.0, 0.0, 0.0, 10.0, 0.0, 1.0, 0.0);
  95.        
  96.     glClear(GL_COLOR_BUFFER_BIT);  
  97.  
  98.     glPushMatrix();    
  99.         glColor3f (0.0, 0.0, 0.0);
  100.         glTranslatef(golfball.getpx(),golfball.getpy(),startdispz - golfball.getpz());
  101.         glutSolidSphere(golfball.getballsize(),20,20);
  102.     glPopMatrix(); 
  103.    
  104.     glPushMatrix();
  105.         glColor3f (0.0, 1.0, 0.0);
  106.         glTranslatef(0,0.0,-7.5);
  107.         glRotatef(90, 1.0, 0.0, 0.0);
  108.         drawCircle(0.1,24);
  109.         glRotatef(-90, 1.0, 0.0, 0.0);
  110.     glPopMatrix();
  111.    
  112.     glFlush();
  113.     glutSwapBuffers();
  114. }
  115.  
  116. void keyboard(unsigned char key, int x, int y)
  117. {
  118.     switch (key)
  119.     {
  120.         case 'q':
  121.         exit(0);
  122.     }
  123. }
  124.  
  125. void evolve(int a)
  126. {
  127.     // evolve system here
  128.     golfball.evolve();
  129.     glutPostRedisplay();  
  130.     glutTimerFunc(20,evolve,20);
  131. }
  132.  
  133. int main(int argc, char** argv)
  134. {
  135.    glutInit(&argc, argv);
  136.    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
  137.    glutInitWindowSize (800, 800);
  138.    glutCreateWindow (argv[0]);   init();
  139.    glutKeyboardFunc (keyboard);
  140.    //  glutIdleFunc(evolve);
  141.    glutTimerFunc(100,evolve,100);
  142.    glutDisplayFunc (display);
  143.    cout << "Hello All on CG6301\n";
  144.    cout << "This program models projectile motion, the golf ball moves under gravity(no friction)\n";
  145.    cout << "The file is closed and the program terminates when the ball is deemed to have hit the ground \n";
  146.    cout << "The lab questions ask you to determine how accurately we have modelled the system\n";
  147.  
  148.    glutMainLoop();
  149.    return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement