Advertisement
Guest User

OpenGl_something

a guest
Nov 28th, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.18 KB | None | 0 0
  1. #include<GL/gl.h>
  2. #include<GL/glu.h>
  3. #include<GL/glut.h>
  4. #include<iostream>
  5. #include<vector>
  6. #include<math.h>
  7. #include<stdio.h>
  8. using namespace std;
  9. float forward = 0.005;
  10. float current[3] ={0,0,0};
  11. float a = drand48();
  12. float b = drand48();
  13. float c = drand48();
  14. float center_x =0 , center_y =0 , center_z =0;
  15.  
  16. class Triangle
  17. {
  18. public:
  19.   //float position[9],color[3];
  20.   Triangle()
  21.   {}
  22.   Triangle(float position[], float color[])
  23.   {
  24.     glPushMatrix();
  25.     // float a = 0.03;
  26.     //float b = 0.04;
  27.     //float c = 0.05;
  28.     float increment = 0.001;
  29.     glTranslatef(a+increment,b+increment,c+increment);
  30.     //    glRotatef()
  31.     glBegin(GL_TRIANGLES);
  32.     glColor3f(color[0],color[1],color[2]);glVertex3f(position[0],position[1],position[2]);
  33.     glColor3f(color[0],color[1],color[2]);glVertex3f(position[3],position[4],position[5]);
  34.     glColor3f(color[0],color[1],color[2]);glVertex3f(position[6],position[7],position[8]);
  35.     glEnd();
  36.     glPopMatrix();
  37.    
  38.   }
  39.  
  40. };
  41.  
  42. class Mesh
  43. {
  44. public:
  45.   /*float center[3],position[9],color[3];
  46.     float size;*/
  47.   vector<Triangle> elements;
  48.   float center[3],position[9],color[3];
  49.   float size;
  50.  
  51.   Mesh(){}
  52.   Mesh(float center_in[3], float color_in[3])
  53.   {
  54.     for (int i=0;i<3;i++)
  55.       {
  56.     color[i] = color_in[i];
  57.     center[i] = center_in[i];
  58.       }
  59.   }
  60.  
  61.   vector<Triangle> makeCube()
  62.   {
  63.     vector<Triangle> elements_cube;
  64.     float d =0.03;
  65.     // First face
  66.     position[0] = d;position[1] = d; position[2] = d;
  67.     position[3] = -d;position[4] = -d; position[5] = d;
  68.     position[6] = d;position[7] = -d; position[8] = d;
  69.     color[0]=1; color[1] = 0; color[2]=0;
  70.     Triangle T1(position, color);
  71.     elements_cube[0] = T1;
  72.     position[0] = -d;position[1] = -d; position[2] = d;
  73.     position[3] = d;position[4] = d; position[5] = d;
  74.     position[6] = -d;position[7] = d; position[8] = d;
  75.     color[0]=1; color[1] = 0; color[2]=0;
  76.     Triangle T1_1(position, color);
  77.     elements_cube[1] = T1_1;
  78.  
  79.     //Second Face
  80.     position[0] = d;position[1] = d; position[2] = d;
  81.     position[3] = d;position[4] = -d; position[5] = d;
  82.     position[6] = d;position[7] = -d; position[8] = -d;
  83.     color[0]=1; color[1] = 1; color[2]=0;
  84.     Triangle T2(position, color);
  85.     elements_cube[2] = T2;
  86.     position[0] = d;position[1] = d; position[2] = d;
  87.     position[3] = d;position[4] = -d; position[5] = -d;
  88.     position[6] = d;position[7] = d; position[8] = -d;
  89.     color[0]=1; color[1] = 1; color[2]=0;
  90.     Triangle T2_2(position, color);
  91.     elements_cube[3] = T2_2;
  92.  
  93.     //Third face
  94.     position[0] = d;position[1] = d; position[2] = -d;
  95.     position[3] = -d;position[4] = -d; position[5] = d;
  96.     position[6] = -d;position[7] = d; position[8] = d;
  97.     color[0]=0; color[1] = 1; color[2]=1;
  98.     Triangle T3(position, color);
  99.     elements_cube[2] = T3;
  100.     position[0] = -d;position[1] = -d; position[2] = d;
  101.     position[3] = d;position[4] = -d; position[5] = -d;
  102.     position[6] = d;position[7] = d; position[8] = -d;
  103.     color[0]=0; color[1] = 1; color[2]=1;
  104.     Triangle T3_2(position, color);
  105.     elements_cube[3] = T3_2;
  106.  
  107.     return(elements_cube);
  108.    
  109.   }
  110.  
  111.   void getColor()
  112.   {
  113.     color[0] = 1.; color[1]=0.; color[2]=0.;
  114.   }
  115.  
  116.   static Mesh makeMesh()
  117.   {
  118.     Mesh a;
  119.     a.elements.resize(4);
  120.     a.elements=a.makeCube();
  121.     return a;
  122.   }
  123.  
  124. };
  125.  
  126. void render()
  127. {
  128.   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  129.   glPushMatrix();
  130.   glBegin(GL_TRIANGLES);
  131.   glColor3f(1,1,1);glVertex3f(-0.3,-0.3,-1);
  132.   glColor3f(1,1,1);glVertex3f(-1,-0.3,0);
  133.   glColor3f(1,1,1);glVertex3f(1,-0.3,0);
  134.   glEnd();
  135.   glBegin(GL_TRIANGLES);
  136.   glColor3f(1,0,1);glVertex3f(-0.3,-0.3,1);
  137.   glColor3f(1,1,1);glVertex3f(-1,-0.3,0);
  138.   glColor3f(1,0,0);glVertex3f(1,-0.3,0);
  139.   glEnd();
  140.   glPopMatrix();
  141.   Mesh a;
  142.   //a.elements.resize(2);
  143.   //a.getPositions();
  144.   //a.getColor();
  145.   //Triangle T(a.position,a.color);  
  146.   vector<Mesh> m;
  147.   for (int i=1;i<10;i++)
  148.     {glPushMatrix();
  149.       glTranslatef(0.1,.1,(float)i/10);
  150.       m.push_back(Mesh::makeMesh());
  151.       glPopMatrix();
  152.     }
  153.   glPushMatrix();
  154.   //glLoadIdentity();
  155.   //glTranslatef(current[0], current[1], current[2]);
  156.   m;
  157.   glPopMatrix();
  158.   glFlush();
  159.   glutSwapBuffers();
  160.   glutPostRedisplay();
  161. }
  162.  
  163. void keyboard(unsigned char c, int x,int y)
  164. {
  165.   if(c==27)
  166.     {exit(0);}
  167.   if (c=='w')
  168.     {
  169.       float forward_x, forward_y,forward_z;
  170.       gluLookAt(current[0], current[1], current[2],
  171.         current[0]+center_x, current[1]+center_y, current[2]+center_z,
  172.         0.0, 1.0, 0.0);  
  173.       // glScalef(1.2,1.2,1.2);
  174.       current[0]= (current[0]- 0.1*center_x);
  175.       current[1]= (current[1]-0.1*center_y);
  176.       current[2]= (current[2]- 0.1*center_z);
  177.      
  178.     }
  179.    if (c=='s')
  180.     {
  181.       glMatrixMode(GL_MODELVIEW);
  182.       glLoadIdentity();
  183.      
  184.       float forward_x, forward_y,forward_z;
  185.       gluLookAt(current[0], current[1], current[2],
  186.         current[0]+center_x, current[1]+center_y, current[2]+center_z,
  187.         0.0, 1.0, 0.0);  
  188.       //glScalef(1.2,1.2,1.2);
  189.       current[0]= (current[0]+ 0.1*center_x);
  190.       current[1]= (current[1]+ 0.1*center_y);
  191.       current[2]= (current[2]+ 0.1*center_z);
  192.      
  193.     }
  194.  
  195. }
  196.  
  197. void mouse(int button, int state,int x,int y)
  198. {
  199.   if(button == GLUT_RIGHT_BUTTON){exit(0);}
  200. }
  201.  
  202. void Camera_rotation(int x, int y)
  203. {
  204.   glMatrixMode(GL_MODELVIEW);
  205.   glLoadIdentity();
  206.   center_x = cos(-0.05*x)*sin(-.05*y);
  207.   center_y = cos(-.05*y);
  208.   center_z = sin(0.05*x)*sin(.05*y);
  209.   //  eye_x = x; eye_y = 1.0,
  210.   // float new_x = x;
  211.   gluLookAt(current[0], current[1], current[2],
  212.             current[0]+center_x,
  213.         current[1]+center_y,
  214.             current[2]+center_z,
  215.             0.0, 1.0, 0.0);
  216.   /*current[0] = center_x;
  217.   current[1] = center_y;
  218.   current[2] = center_z;*/
  219.   render();
  220. }
  221.  
  222. int main(int argc, char**argv)
  223. {
  224.   glutInit(&argc,argv);
  225.   glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  226.   glutInitWindowPosition(100,100);
  227.   glutInitWindowSize(1000,1000);
  228.   glutCreateWindow("Window screen");
  229.   glEnable(GL_DEPTH_TEST);
  230.   glutDisplayFunc(render);
  231.   glutKeyboardFunc(keyboard);
  232.   glutMouseFunc(mouse);
  233.   glutPassiveMotionFunc(Camera_rotation);
  234.   glutMainLoop();
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement