Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<GL/gl.h>
- #include<GL/glu.h>
- #include<GL/glut.h>
- #include<iostream>
- #include<vector>
- #include<math.h>
- #include<stdio.h>
- using namespace std;
- float forward = 0.005;
- float current[3] ={0,0,0};
- float a = drand48();
- float b = drand48();
- float c = drand48();
- float center_x =0 , center_y =0 , center_z =0;
- class Triangle
- {
- public:
- //float position[9],color[3];
- Triangle()
- {}
- Triangle(float position[], float color[])
- {
- glPushMatrix();
- // float a = 0.03;
- //float b = 0.04;
- //float c = 0.05;
- float increment = 0.001;
- glTranslatef(a+increment,b+increment,c+increment);
- // glRotatef()
- glBegin(GL_TRIANGLES);
- glColor3f(color[0],color[1],color[2]);glVertex3f(position[0],position[1],position[2]);
- glColor3f(color[0],color[1],color[2]);glVertex3f(position[3],position[4],position[5]);
- glColor3f(color[0],color[1],color[2]);glVertex3f(position[6],position[7],position[8]);
- glEnd();
- glPopMatrix();
- }
- };
- class Mesh
- {
- public:
- /*float center[3],position[9],color[3];
- float size;*/
- vector<Triangle> elements;
- float center[3],position[9],color[3];
- float size;
- Mesh(){}
- Mesh(float center_in[3], float color_in[3])
- {
- for (int i=0;i<3;i++)
- {
- color[i] = color_in[i];
- center[i] = center_in[i];
- }
- }
- vector<Triangle> makeCube()
- {
- vector<Triangle> elements_cube;
- float d =0.03;
- // First face
- position[0] = d;position[1] = d; position[2] = d;
- position[3] = -d;position[4] = -d; position[5] = d;
- position[6] = d;position[7] = -d; position[8] = d;
- color[0]=1; color[1] = 0; color[2]=0;
- Triangle T1(position, color);
- elements_cube[0] = T1;
- position[0] = -d;position[1] = -d; position[2] = d;
- position[3] = d;position[4] = d; position[5] = d;
- position[6] = -d;position[7] = d; position[8] = d;
- color[0]=1; color[1] = 0; color[2]=0;
- Triangle T1_1(position, color);
- elements_cube[1] = T1_1;
- //Second Face
- position[0] = d;position[1] = d; position[2] = d;
- position[3] = d;position[4] = -d; position[5] = d;
- position[6] = d;position[7] = -d; position[8] = -d;
- color[0]=1; color[1] = 1; color[2]=0;
- Triangle T2(position, color);
- elements_cube[2] = T2;
- position[0] = d;position[1] = d; position[2] = d;
- position[3] = d;position[4] = -d; position[5] = -d;
- position[6] = d;position[7] = d; position[8] = -d;
- color[0]=1; color[1] = 1; color[2]=0;
- Triangle T2_2(position, color);
- elements_cube[3] = T2_2;
- //Third face
- position[0] = d;position[1] = d; position[2] = -d;
- position[3] = -d;position[4] = -d; position[5] = d;
- position[6] = -d;position[7] = d; position[8] = d;
- color[0]=0; color[1] = 1; color[2]=1;
- Triangle T3(position, color);
- elements_cube[2] = T3;
- position[0] = -d;position[1] = -d; position[2] = d;
- position[3] = d;position[4] = -d; position[5] = -d;
- position[6] = d;position[7] = d; position[8] = -d;
- color[0]=0; color[1] = 1; color[2]=1;
- Triangle T3_2(position, color);
- elements_cube[3] = T3_2;
- return(elements_cube);
- }
- void getColor()
- {
- color[0] = 1.; color[1]=0.; color[2]=0.;
- }
- static Mesh makeMesh()
- {
- Mesh a;
- a.elements.resize(4);
- a.elements=a.makeCube();
- return a;
- }
- };
- void render()
- {
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- glBegin(GL_TRIANGLES);
- glColor3f(1,1,1);glVertex3f(-0.3,-0.3,-1);
- glColor3f(1,1,1);glVertex3f(-1,-0.3,0);
- glColor3f(1,1,1);glVertex3f(1,-0.3,0);
- glEnd();
- glBegin(GL_TRIANGLES);
- glColor3f(1,0,1);glVertex3f(-0.3,-0.3,1);
- glColor3f(1,1,1);glVertex3f(-1,-0.3,0);
- glColor3f(1,0,0);glVertex3f(1,-0.3,0);
- glEnd();
- glPopMatrix();
- Mesh a;
- //a.elements.resize(2);
- //a.getPositions();
- //a.getColor();
- //Triangle T(a.position,a.color);
- vector<Mesh> m;
- for (int i=1;i<10;i++)
- {glPushMatrix();
- glTranslatef(0.1,.1,(float)i/10);
- m.push_back(Mesh::makeMesh());
- glPopMatrix();
- }
- glPushMatrix();
- //glLoadIdentity();
- //glTranslatef(current[0], current[1], current[2]);
- m;
- glPopMatrix();
- glFlush();
- glutSwapBuffers();
- glutPostRedisplay();
- }
- void keyboard(unsigned char c, int x,int y)
- {
- if(c==27)
- {exit(0);}
- if (c=='w')
- {
- float forward_x, forward_y,forward_z;
- gluLookAt(current[0], current[1], current[2],
- current[0]+center_x, current[1]+center_y, current[2]+center_z,
- 0.0, 1.0, 0.0);
- // glScalef(1.2,1.2,1.2);
- current[0]= (current[0]- 0.1*center_x);
- current[1]= (current[1]-0.1*center_y);
- current[2]= (current[2]- 0.1*center_z);
- }
- if (c=='s')
- {
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- float forward_x, forward_y,forward_z;
- gluLookAt(current[0], current[1], current[2],
- current[0]+center_x, current[1]+center_y, current[2]+center_z,
- 0.0, 1.0, 0.0);
- //glScalef(1.2,1.2,1.2);
- current[0]= (current[0]+ 0.1*center_x);
- current[1]= (current[1]+ 0.1*center_y);
- current[2]= (current[2]+ 0.1*center_z);
- }
- }
- void mouse(int button, int state,int x,int y)
- {
- if(button == GLUT_RIGHT_BUTTON){exit(0);}
- }
- void Camera_rotation(int x, int y)
- {
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- center_x = cos(-0.05*x)*sin(-.05*y);
- center_y = cos(-.05*y);
- center_z = sin(0.05*x)*sin(.05*y);
- // eye_x = x; eye_y = 1.0,
- // float new_x = x;
- gluLookAt(current[0], current[1], current[2],
- current[0]+center_x,
- current[1]+center_y,
- current[2]+center_z,
- 0.0, 1.0, 0.0);
- /*current[0] = center_x;
- current[1] = center_y;
- current[2] = center_z;*/
- render();
- }
- int main(int argc, char**argv)
- {
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
- glutInitWindowPosition(100,100);
- glutInitWindowSize(1000,1000);
- glutCreateWindow("Window screen");
- glEnable(GL_DEPTH_TEST);
- glutDisplayFunc(render);
- glutKeyboardFunc(keyboard);
- glutMouseFunc(mouse);
- glutPassiveMotionFunc(Camera_rotation);
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement