Advertisement
Kaidul

horse

Mar 29th, 2014
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.28 KB | None | 0 0
  1. #include <GL/gl.h>
  2. #include <GL/glu.h>
  3. #include <GL/glut.h>
  4. #include <bits/stdc++.h>
  5.  
  6. using namespace std;
  7.  
  8. double windowWidth=600.0 ,windowHeight=600.0;
  9. float Txval,Tyval,rotateVal = 0.0,rx,ry,rz,Crotate,Fflip,rtt=0.0,rtr=0.0;
  10. bool isrotate,istranslate,mirror,flip;
  11. int sel_port;
  12. void lineX() {
  13.     glBegin(GL_LINES);
  14.     glColor3f(0.0,1.0,1.0);
  15.     glVertex2f(-1.0,0.0);
  16.     glColor3f(0.0,1.0,0.0);
  17.     glVertex2f(2.0,0.0);
  18.     glEnd();
  19. }
  20.  
  21. void lineMin(float x, float y) {
  22.     glBegin(GL_LINES);
  23.     glColor3f(0.0, 1.0, 0.0);
  24.     glVertex2f(-x,0.0);
  25.     glColor3f(0.0,1.0,0.0);
  26.     glVertex2f(x,0.0);
  27.     glEnd();
  28. }
  29.  
  30. void lineSec(float x,float y) {
  31.     glBegin(GL_LINES);
  32.     glColor3f(0.0, 1.0, 0.0);
  33.     glVertex2f(0.0, -y);
  34.     glColor3f(0.0, 0.0, 1.0);
  35.     glVertex2f(0.0, y);
  36.     glEnd();
  37. }
  38.  
  39. void Torus2d( float inner, float outer, unsigned int pts ,float x,float y) {
  40.     glBegin( GL_QUAD_STRIP);
  41.     for( unsigned int i = 0; i <= pts; ++i ) {
  42.         float angle = ( i / (float)pts ) * 3.14159f * 2.0f;
  43.         glVertex2f( x + inner * cos( angle ), y + inner * sin( angle ) );
  44.         glVertex2f( x + outer * cos( angle ), y + outer * sin( angle ) );
  45.     }
  46.     glEnd();
  47. }
  48.  
  49. void horse() {
  50.     glColor3f(0.0, 1.0, 0.0);
  51.     glBegin(GL_QUADS);
  52.     glVertex3f(0.0, 0.0, 0.0);
  53.     glVertex3f(0.2, 0.0, 0);
  54.     glVertex3f(0.2, 1.0, 0);
  55.     glVertex3f(0, 1.0, 0);
  56.     glEnd();
  57.  
  58.     glBegin(GL_QUADS);
  59.     glVertex3f(-0.3, 0.0, 0);
  60.     glVertex3f(-0.1, 0.0, 0);
  61.     glVertex3f(0.2, 1.0, 0);
  62.     glVertex3f(0.0, 1.0, 0);
  63.     glEnd();
  64.  
  65.     glBegin(GL_QUADS);
  66.     glVertex3f(0.8, 0.0, 0);
  67.     glVertex3f(1.0, 0.0, 0);
  68.     glVertex3f(1.0, 1.0, 0);
  69.     glVertex3f(0.8, 1.0, 0);
  70.     glEnd();
  71.  
  72.     glBegin(GL_QUADS);
  73.     glVertex3f(0.5, 0.0, 0);
  74.     glVertex3f(0.7, 0.0, 0);
  75.     glVertex3f(1.0, 1.0, 0);
  76.     glVertex3f(0.8, 1.0, 0);
  77.     glEnd();
  78.  
  79.     glBegin(GL_QUADS);
  80.     glVertex2f(0.0, 0.5);
  81.     glVertex2f(1.0, 0.5);
  82.     glVertex2f(1.0, 1.1);
  83.     glVertex2f(0.0, 1.1);
  84.     glEnd();
  85.  
  86.     // Ass
  87.     glBegin(GL_QUADS);
  88.     glVertex3f(1.0, 0.5, 0);
  89.     glVertex3f(1.4, 0.8, 0);
  90.     glVertex3f(1.3, 1.0, 0);
  91.     glVertex3f(1.0, 1.1, 0);
  92.     glEnd();
  93.  
  94.     // tail
  95.     glBegin(GL_TRIANGLES);
  96.     glVertex2f(1.2, 0.7);
  97.     glVertex2f(1.4, 0.3);
  98.     glVertex2f(1.4, 0.8);
  99.     glEnd();
  100.  
  101.  
  102.     glColor3f(0.0, 0.0, 0.0);
  103.     Torus2d(0.0, 0.07, 50, -0.4, 1.1 );
  104.  
  105.     glColor3f(0.0, 1.0, 0.0);
  106.     glBegin(GL_QUADS);
  107.     glVertex2f(0.6,1.05);
  108.     glVertex2f(0,1);
  109.     glVertex2f(-.5,1.5);
  110.     glVertex2f(0.0,1.6);
  111.     glEnd();
  112.  
  113.     glBegin(GL_QUADS);
  114.     glVertex3f(-0.6, 0.7, 0);
  115.     glVertex3f(-0.4, 0.5, 0);
  116.     glVertex3f(0.2, 1.2, 0);
  117.     glVertex3f(-0.5, 1.5, 0);
  118.     glEnd();
  119.  
  120. }
  121.  
  122. void myKeyboardFunc( unsigned char key, int x, int y ) {
  123.     switch(key) {
  124.     case 'm':
  125.         mirror = mirror?false:true;
  126.         break;
  127.     case 'f':
  128.         flip = flip ? false : true;
  129.         break;
  130.     case 't':
  131.         istranslate = istranslate ? 0 : 1;
  132.         break;
  133.     case 'd':
  134.         if(istranslate) Txval += 0.1;
  135.         break;
  136.     case 'a':
  137.         if(istranslate) Txval -= 0.1;
  138.         break;
  139.     case 'w':
  140.         if(istranslate) Tyval += 0.1;
  141.         break;
  142.     case 's':
  143.         if(istranslate) Tyval -= 0.1;
  144.         break;
  145.     case 'r':
  146.         isrotate = isrotate ? 0 : 1;
  147.         if(isrotate) rx = 0.0, ry = 0.0, rz = 1.0;
  148.         break;
  149.     case 'x':
  150.         if(isrotate) rx = 1.0, ry= 0.0, rz = 0.0;
  151.         break;
  152.     case 'y':
  153.         if(isrotate) rx = 0.0, ry= 1.0, rz = 0.0;
  154.         break;
  155.     case 'z':
  156.         if(isrotate) rx = 0.0, ry = 0.0, rz = 1.0;
  157.         break;
  158.     case 'c':
  159.         Crotate = Crotate > 0.0 ? -1.0 : 1.0;
  160.         break;
  161.     case '1':
  162.         sel_port = 1;
  163.         break;
  164.     case '2':
  165.         sel_port = 2;
  166.         break;
  167.     case '3':
  168.         sel_port = 3;
  169.         break;
  170.     case '4':
  171.         sel_port = 4;
  172.         break;
  173.     case 27:
  174.         exit(1);
  175.     }
  176.     glutPostRedisplay();
  177.     cout << sel_port << endl;
  178. }
  179.  
  180. void animate() {
  181.     if (isrotate == true) {
  182.         rotateVal += 0.05;
  183.         if(rotateVal > 360)
  184.             rotateVal = 0.05;
  185.     }
  186.     rtt++;
  187.     rtr--;
  188.     glutPostRedisplay();
  189. }
  190. const float DEG2RAD = 3.14159 / 180;
  191.  
  192. void display(void) {
  193.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  194.     glMatrixMode( GL_PROJECTION );
  195.     glLoadIdentity();
  196.     gluOrtho2D(-3, 3, -3, 3);
  197.     glMatrixMode( GL_MODELVIEW );
  198.     glLoadIdentity();
  199.     Fflip = 1.0;
  200.     glPushMatrix();
  201.     if(istranslate) glTranslatef(Txval,Tyval,0.0);
  202.     if(isrotate) glRotatef(-1*rotateVal*Crotate,rx,ry,rz);
  203.     Fflip = flip?-1.0:1.0;
  204.     if(sel_port==1) {
  205.         glViewport(300, 300 ,300 ,300);
  206.         if(flip)
  207.             glRotatef(180,1,0,0);
  208.         lineX();
  209.         horse();
  210.         glPushMatrix();
  211.         glColor3ub( 255, 0, 0 );
  212.         glTranslatef(0,-.2,0.0);
  213.         Torus2d( 0.15, 0.2, 20,0.0,0.0 );
  214.         glRotatef(rtt,0,0,1);
  215.         lineMin(0.15,0);
  216.         lineSec(0,.15);
  217.         glPopMatrix();
  218.  
  219.         glPushMatrix();
  220.         glColor3ub( 255, 0, 0 );
  221.         glTranslatef(1,-.2,0.0);
  222.         glRotatef(rtt,0,0,1);
  223.         Torus2d( 0.15, 0.2, 20,0.0,0.0 );
  224.         lineMin(0.15,0);
  225.         lineSec(0,.15);
  226.         glPopMatrix();
  227.     }
  228.  
  229.     else if(sel_port==2) {
  230.         glViewport(0, 300 ,300 ,300);
  231.         glRotatef(180,0,1,0);
  232.         if(flip)
  233.             glRotatef(180,1,0,0);
  234.         lineX();
  235.         horse();
  236.         glPushMatrix();
  237.         glColor3ub( 255, 0, 0 );
  238.         glTranslatef(0,-.2,0.0);
  239.         Torus2d( 0.15, 0.2, 20,0.0,0.0 );
  240.         glRotatef(rtt,0,0,1);
  241.         lineMin(0.15,0);
  242.         lineSec(0,.15);
  243.         glPopMatrix();
  244.  
  245.         glPushMatrix();
  246.         glColor3ub( 255, 0, 0 );
  247.         glTranslatef(1,-.2,0.0);
  248.         Torus2d( 0.15, 0.2, 20,0.0,0.0 );
  249.         glRotatef(rtt,0,0,1);
  250.         lineMin(0.15,0);
  251.         lineSec(0,.15);
  252.         glPopMatrix();
  253.     }
  254.  
  255.     else if(sel_port==3) {
  256.         glViewport(300, 0 ,300 ,300);
  257.         glRotatef(180,1,0,0);
  258.         glTranslatef(-1,0,0.0);
  259.         if(flip)
  260.             glRotatef(180,1,0,0);
  261.         lineX();
  262.         horse();
  263.         glPushMatrix();
  264.         glColor3ub( 255, 0, 0 );
  265.         glTranslatef(0,-.2,0.0);
  266.         glRotatef(rtr,0,0,1);
  267.         Torus2d( 0.15, 0.2, 20,0.0,0.0 );
  268.         lineMin(0.15,0);
  269.         lineSec(0,.15);
  270.         glPopMatrix();
  271.  
  272.         glPushMatrix();
  273.         glColor3ub( 255, 0, 0 );
  274.         glTranslatef(1,-.2,0.0);
  275.         Torus2d( 0.15, 0.2, 20,0.0,0.0 );
  276.         glRotatef(rtr,0,0,1);
  277.         lineMin(0.15,0);
  278.         lineSec(0,.15);
  279.         glPopMatrix();
  280.     }
  281.  
  282.     else if(sel_port==4) {
  283.         glViewport(0, 0 ,300 ,300);
  284.         glRotatef(180,0,0,1);
  285.         if(flip)
  286.             glRotatef(180,1,0,0);
  287.         lineX();
  288.         horse();
  289.         glPushMatrix();
  290.         glColor3ub( 255, 0, 0 );
  291.         glTranslatef(0,-.2,0.0);
  292.         Torus2d( 0.15, 0.2, 20,0.0,0.0 );
  293.         glRotatef(rtr,0,0,1);
  294.         lineMin(0.15,0);
  295.         lineSec(0,.15);
  296.         glPopMatrix();
  297.  
  298.         glPushMatrix();
  299.         glColor3ub( 255, 0, 0 );
  300.         glTranslatef(1,-.2,0.0);
  301.         Torus2d( 0.15, 0.2, 20,0.0,0.0 );
  302.         glRotatef(rtr,0,0,1);
  303.         lineMin(0.15,0);
  304.         lineSec(0,.15);
  305.         glPopMatrix();
  306.     }
  307.     glPopMatrix();
  308.  
  309.     if(mirror and sel_port==1) {
  310.  
  311.         glPushMatrix();
  312.         glViewport(0, 300 ,300 ,300);
  313.         if(istranslate) glTranslatef(Txval,Tyval,0.0);
  314.         if(isrotate) glRotatef(-1*rotateVal*Crotate,rx,ry,rz);
  315.         Fflip = flip?-1.0:1.0;
  316.  
  317.         glRotatef(180,0,1,0);
  318.         lineX();
  319.         horse();
  320.  
  321.         glPushMatrix();
  322.         glColor3ub( 255, 0, 0 );
  323.         glTranslatef(0,-.2,0.0);
  324.         Torus2d( 0.15, 0.2, 20,0.0,0.0 );
  325.         glRotatef(rtt,0,0,1);
  326.         lineMin(0.15,0);
  327.         lineSec(0,.15);
  328.         glPopMatrix();
  329.  
  330.         glPushMatrix();
  331.         glColor3ub( 255, 0, 0 );
  332.         glTranslatef(1,-.2,0.0);
  333.         Torus2d( 0.15, 0.2, 20,0.0,0.0 );
  334.         glRotatef(rtt,0,0,1);
  335.         lineMin(0.15,0);
  336.         lineSec(0,.15);
  337.         glPopMatrix();
  338.  
  339.         glPopMatrix();
  340.         glPushMatrix();
  341.         glViewport(300, 0 ,300 ,300);
  342.         if(istranslate) glTranslatef(Txval,Tyval,0.0);
  343.         if(isrotate) glRotatef(-1*rotateVal*Crotate,rx,ry,rz);
  344.         Fflip = flip?-1.0:1.0;
  345.  
  346.         glRotatef(180,1,0,0);
  347.         //glTranslatef(-1,0,0.0);
  348.         lineX();
  349.         horse();
  350.  
  351.         glPushMatrix();
  352.         glColor3ub( 255, 0, 0 );
  353.         glTranslatef(0,-.2,0.0);
  354.         Torus2d( 0.15, 0.2, 20,0.0,0.0 );
  355.         glRotatef(rtr,0,0,1);
  356.         lineMin(0.15,0);
  357.         lineSec(0,.15);
  358.         glPopMatrix();
  359.  
  360.         glPushMatrix();
  361.         glColor3ub( 255, 0, 0 );
  362.         glTranslatef(1,-.2,0.0);
  363.         Torus2d( 0.15, 0.2, 20,0.0,0.0 );
  364.         glRotatef(rtr,0,0,1);
  365.         lineMin(0.15,0);
  366.         lineSec(0,.15);
  367.         glPopMatrix();
  368.  
  369.         glPopMatrix();
  370.  
  371.         glPushMatrix();
  372.         glViewport(0, 0 ,300 ,300);
  373.         if(istranslate) glTranslatef(Txval,Tyval,0.0);
  374.         if(isrotate) glRotatef(-1*rotateVal*Crotate,rx,ry,rz);
  375.         Fflip = flip ? -1.0 : 1.0;
  376.  
  377.         glRotatef(180,0,0,1);
  378.  
  379.         lineX();
  380.         horse();
  381.  
  382.         glPushMatrix();
  383.         glColor3ub( 255, 0, 0 );
  384.         glTranslatef(0,-.2,0.0);
  385.         Torus2d( 0.15, 0.2, 20,0.0,0.0 );
  386.         glRotatef(rtr,0,0,1);
  387.         lineMin(0.15,0);
  388.         lineSec(0,.15);
  389.         glPopMatrix();
  390.  
  391.         glPushMatrix();
  392.         glColor3ub( 255, 0, 0 );
  393.         glTranslatef(1,-.2,0.0);
  394.         Torus2d( 0.15, 0.2, 20,0.0,0.0 );
  395.         glRotatef(rtr,0,0,1);
  396.         lineMin(0.15,0);
  397.         lineSec(0,.15);
  398.         glPopMatrix();
  399.  
  400.         glPopMatrix();
  401.  
  402.     }
  403.     glFlush();
  404.     glutSwapBuffers();
  405. }
  406.  
  407. int main (int argc, char **argv) {
  408.     glutInit(&argc, argv);
  409.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  410.     glutInitWindowPosition(100, 100);
  411.     glutInitWindowSize(windowWidth, windowHeight);
  412.     glutCreateWindow("Toy Horse");
  413.     glShadeModel(GL_SMOOTH);
  414.     glEnable(GL_DEPTH_TEST);
  415.     glutKeyboardFunc(myKeyboardFunc);
  416.     glutIdleFunc(animate);
  417.     Crotate = 1.0;
  418.     Fflip = 1.0;
  419.     glutDisplayFunc(display);
  420.     glutMainLoop();
  421.     return 0;
  422. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement