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 <bits/stdc++.h>
- using namespace std;
- double windowWidth=600.0 ,windowHeight=600.0;
- float Txval,Tyval,rotateVal = 0.0,rx,ry,rz,Crotate,Fflip,rtt=0.0,rtr=0.0;
- bool isrotate,istranslate,mirror,flip;
- int sel_port;
- void lineX() {
- glBegin(GL_LINES);
- glColor3f(0.0,1.0,1.0);
- glVertex2f(-1.0,0.0);
- glColor3f(0.0,1.0,0.0);
- glVertex2f(2.0,0.0);
- glEnd();
- }
- void lineMin(float x, float y) {
- glBegin(GL_LINES);
- glColor3f(0.0, 1.0, 0.0);
- glVertex2f(-x,0.0);
- glColor3f(0.0,1.0,0.0);
- glVertex2f(x,0.0);
- glEnd();
- }
- void lineSec(float x,float y) {
- glBegin(GL_LINES);
- glColor3f(0.0, 1.0, 0.0);
- glVertex2f(0.0, -y);
- glColor3f(0.0, 0.0, 1.0);
- glVertex2f(0.0, y);
- glEnd();
- }
- void Torus2d( float inner, float outer, unsigned int pts ,float x,float y) {
- glBegin( GL_QUAD_STRIP);
- for( unsigned int i = 0; i <= pts; ++i ) {
- float angle = ( i / (float)pts ) * 3.14159f * 2.0f;
- glVertex2f( x + inner * cos( angle ), y + inner * sin( angle ) );
- glVertex2f( x + outer * cos( angle ), y + outer * sin( angle ) );
- }
- glEnd();
- }
- void horse() {
- glColor3f(0.0, 1.0, 0.0);
- glBegin(GL_QUADS);
- glVertex3f(0.0, 0.0, 0.0);
- glVertex3f(0.2, 0.0, 0);
- glVertex3f(0.2, 1.0, 0);
- glVertex3f(0, 1.0, 0);
- glEnd();
- glBegin(GL_QUADS);
- glVertex3f(-0.3, 0.0, 0);
- glVertex3f(-0.1, 0.0, 0);
- glVertex3f(0.2, 1.0, 0);
- glVertex3f(0.0, 1.0, 0);
- glEnd();
- glBegin(GL_QUADS);
- glVertex3f(0.8, 0.0, 0);
- glVertex3f(1.0, 0.0, 0);
- glVertex3f(1.0, 1.0, 0);
- glVertex3f(0.8, 1.0, 0);
- glEnd();
- glBegin(GL_QUADS);
- glVertex3f(0.5, 0.0, 0);
- glVertex3f(0.7, 0.0, 0);
- glVertex3f(1.0, 1.0, 0);
- glVertex3f(0.8, 1.0, 0);
- glEnd();
- glBegin(GL_QUADS);
- glVertex2f(0.0, 0.5);
- glVertex2f(1.0, 0.5);
- glVertex2f(1.0, 1.1);
- glVertex2f(0.0, 1.1);
- glEnd();
- // Ass
- glBegin(GL_QUADS);
- glVertex3f(1.0, 0.5, 0);
- glVertex3f(1.4, 0.8, 0);
- glVertex3f(1.3, 1.0, 0);
- glVertex3f(1.0, 1.1, 0);
- glEnd();
- // tail
- glBegin(GL_TRIANGLES);
- glVertex2f(1.2, 0.7);
- glVertex2f(1.4, 0.3);
- glVertex2f(1.4, 0.8);
- glEnd();
- glColor3f(0.0, 0.0, 0.0);
- Torus2d(0.0, 0.07, 50, -0.4, 1.1 );
- glColor3f(0.0, 1.0, 0.0);
- glBegin(GL_QUADS);
- glVertex2f(0.6,1.05);
- glVertex2f(0,1);
- glVertex2f(-.5,1.5);
- glVertex2f(0.0,1.6);
- glEnd();
- glBegin(GL_QUADS);
- glVertex3f(-0.6, 0.7, 0);
- glVertex3f(-0.4, 0.5, 0);
- glVertex3f(0.2, 1.2, 0);
- glVertex3f(-0.5, 1.5, 0);
- glEnd();
- }
- void myKeyboardFunc( unsigned char key, int x, int y ) {
- switch(key) {
- case 'm':
- mirror = mirror?false:true;
- break;
- case 'f':
- flip = flip ? false : true;
- break;
- case 't':
- istranslate = istranslate ? 0 : 1;
- break;
- case 'd':
- if(istranslate) Txval += 0.1;
- break;
- case 'a':
- if(istranslate) Txval -= 0.1;
- break;
- case 'w':
- if(istranslate) Tyval += 0.1;
- break;
- case 's':
- if(istranslate) Tyval -= 0.1;
- break;
- case 'r':
- isrotate = isrotate ? 0 : 1;
- if(isrotate) rx = 0.0, ry = 0.0, rz = 1.0;
- break;
- case 'x':
- if(isrotate) rx = 1.0, ry= 0.0, rz = 0.0;
- break;
- case 'y':
- if(isrotate) rx = 0.0, ry= 1.0, rz = 0.0;
- break;
- case 'z':
- if(isrotate) rx = 0.0, ry = 0.0, rz = 1.0;
- break;
- case 'c':
- Crotate = Crotate > 0.0 ? -1.0 : 1.0;
- break;
- case '1':
- sel_port = 1;
- break;
- case '2':
- sel_port = 2;
- break;
- case '3':
- sel_port = 3;
- break;
- case '4':
- sel_port = 4;
- break;
- case 27:
- exit(1);
- }
- glutPostRedisplay();
- cout << sel_port << endl;
- }
- void animate() {
- if (isrotate == true) {
- rotateVal += 0.05;
- if(rotateVal > 360)
- rotateVal = 0.05;
- }
- rtt++;
- rtr--;
- glutPostRedisplay();
- }
- const float DEG2RAD = 3.14159 / 180;
- void display(void) {
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluOrtho2D(-3, 3, -3, 3);
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- Fflip = 1.0;
- glPushMatrix();
- if(istranslate) glTranslatef(Txval,Tyval,0.0);
- if(isrotate) glRotatef(-1*rotateVal*Crotate,rx,ry,rz);
- Fflip = flip?-1.0:1.0;
- if(sel_port==1) {
- glViewport(300, 300 ,300 ,300);
- if(flip)
- glRotatef(180,1,0,0);
- lineX();
- horse();
- glPushMatrix();
- glColor3ub( 255, 0, 0 );
- glTranslatef(0,-.2,0.0);
- Torus2d( 0.15, 0.2, 20,0.0,0.0 );
- glRotatef(rtt,0,0,1);
- lineMin(0.15,0);
- lineSec(0,.15);
- glPopMatrix();
- glPushMatrix();
- glColor3ub( 255, 0, 0 );
- glTranslatef(1,-.2,0.0);
- glRotatef(rtt,0,0,1);
- Torus2d( 0.15, 0.2, 20,0.0,0.0 );
- lineMin(0.15,0);
- lineSec(0,.15);
- glPopMatrix();
- }
- else if(sel_port==2) {
- glViewport(0, 300 ,300 ,300);
- glRotatef(180,0,1,0);
- if(flip)
- glRotatef(180,1,0,0);
- lineX();
- horse();
- glPushMatrix();
- glColor3ub( 255, 0, 0 );
- glTranslatef(0,-.2,0.0);
- Torus2d( 0.15, 0.2, 20,0.0,0.0 );
- glRotatef(rtt,0,0,1);
- lineMin(0.15,0);
- lineSec(0,.15);
- glPopMatrix();
- glPushMatrix();
- glColor3ub( 255, 0, 0 );
- glTranslatef(1,-.2,0.0);
- Torus2d( 0.15, 0.2, 20,0.0,0.0 );
- glRotatef(rtt,0,0,1);
- lineMin(0.15,0);
- lineSec(0,.15);
- glPopMatrix();
- }
- else if(sel_port==3) {
- glViewport(300, 0 ,300 ,300);
- glRotatef(180,1,0,0);
- glTranslatef(-1,0,0.0);
- if(flip)
- glRotatef(180,1,0,0);
- lineX();
- horse();
- glPushMatrix();
- glColor3ub( 255, 0, 0 );
- glTranslatef(0,-.2,0.0);
- glRotatef(rtr,0,0,1);
- Torus2d( 0.15, 0.2, 20,0.0,0.0 );
- lineMin(0.15,0);
- lineSec(0,.15);
- glPopMatrix();
- glPushMatrix();
- glColor3ub( 255, 0, 0 );
- glTranslatef(1,-.2,0.0);
- Torus2d( 0.15, 0.2, 20,0.0,0.0 );
- glRotatef(rtr,0,0,1);
- lineMin(0.15,0);
- lineSec(0,.15);
- glPopMatrix();
- }
- else if(sel_port==4) {
- glViewport(0, 0 ,300 ,300);
- glRotatef(180,0,0,1);
- if(flip)
- glRotatef(180,1,0,0);
- lineX();
- horse();
- glPushMatrix();
- glColor3ub( 255, 0, 0 );
- glTranslatef(0,-.2,0.0);
- Torus2d( 0.15, 0.2, 20,0.0,0.0 );
- glRotatef(rtr,0,0,1);
- lineMin(0.15,0);
- lineSec(0,.15);
- glPopMatrix();
- glPushMatrix();
- glColor3ub( 255, 0, 0 );
- glTranslatef(1,-.2,0.0);
- Torus2d( 0.15, 0.2, 20,0.0,0.0 );
- glRotatef(rtr,0,0,1);
- lineMin(0.15,0);
- lineSec(0,.15);
- glPopMatrix();
- }
- glPopMatrix();
- if(mirror and sel_port==1) {
- glPushMatrix();
- glViewport(0, 300 ,300 ,300);
- if(istranslate) glTranslatef(Txval,Tyval,0.0);
- if(isrotate) glRotatef(-1*rotateVal*Crotate,rx,ry,rz);
- Fflip = flip?-1.0:1.0;
- glRotatef(180,0,1,0);
- lineX();
- horse();
- glPushMatrix();
- glColor3ub( 255, 0, 0 );
- glTranslatef(0,-.2,0.0);
- Torus2d( 0.15, 0.2, 20,0.0,0.0 );
- glRotatef(rtt,0,0,1);
- lineMin(0.15,0);
- lineSec(0,.15);
- glPopMatrix();
- glPushMatrix();
- glColor3ub( 255, 0, 0 );
- glTranslatef(1,-.2,0.0);
- Torus2d( 0.15, 0.2, 20,0.0,0.0 );
- glRotatef(rtt,0,0,1);
- lineMin(0.15,0);
- lineSec(0,.15);
- glPopMatrix();
- glPopMatrix();
- glPushMatrix();
- glViewport(300, 0 ,300 ,300);
- if(istranslate) glTranslatef(Txval,Tyval,0.0);
- if(isrotate) glRotatef(-1*rotateVal*Crotate,rx,ry,rz);
- Fflip = flip?-1.0:1.0;
- glRotatef(180,1,0,0);
- //glTranslatef(-1,0,0.0);
- lineX();
- horse();
- glPushMatrix();
- glColor3ub( 255, 0, 0 );
- glTranslatef(0,-.2,0.0);
- Torus2d( 0.15, 0.2, 20,0.0,0.0 );
- glRotatef(rtr,0,0,1);
- lineMin(0.15,0);
- lineSec(0,.15);
- glPopMatrix();
- glPushMatrix();
- glColor3ub( 255, 0, 0 );
- glTranslatef(1,-.2,0.0);
- Torus2d( 0.15, 0.2, 20,0.0,0.0 );
- glRotatef(rtr,0,0,1);
- lineMin(0.15,0);
- lineSec(0,.15);
- glPopMatrix();
- glPopMatrix();
- glPushMatrix();
- glViewport(0, 0 ,300 ,300);
- if(istranslate) glTranslatef(Txval,Tyval,0.0);
- if(isrotate) glRotatef(-1*rotateVal*Crotate,rx,ry,rz);
- Fflip = flip ? -1.0 : 1.0;
- glRotatef(180,0,0,1);
- lineX();
- horse();
- glPushMatrix();
- glColor3ub( 255, 0, 0 );
- glTranslatef(0,-.2,0.0);
- Torus2d( 0.15, 0.2, 20,0.0,0.0 );
- glRotatef(rtr,0,0,1);
- lineMin(0.15,0);
- lineSec(0,.15);
- glPopMatrix();
- glPushMatrix();
- glColor3ub( 255, 0, 0 );
- glTranslatef(1,-.2,0.0);
- Torus2d( 0.15, 0.2, 20,0.0,0.0 );
- glRotatef(rtr,0,0,1);
- lineMin(0.15,0);
- lineSec(0,.15);
- glPopMatrix();
- glPopMatrix();
- }
- glFlush();
- glutSwapBuffers();
- }
- int main (int argc, char **argv) {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowPosition(100, 100);
- glutInitWindowSize(windowWidth, windowHeight);
- glutCreateWindow("Toy Horse");
- glShadeModel(GL_SMOOTH);
- glEnable(GL_DEPTH_TEST);
- glutKeyboardFunc(myKeyboardFunc);
- glutIdleFunc(animate);
- Crotate = 1.0;
- Fflip = 1.0;
- glutDisplayFunc(display);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement