Advertisement
Guest User

kolory

a guest
Dec 10th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.94 KB | None | 0 0
  1. #include<windows.h>
  2. #include <cmath>
  3. #define GLUT_DISABLE_ATEXIT_HACK
  4. #include <GL/glut.h>
  5. #include <stdlib.h>
  6. //Karolina Dabrowska 203867
  7.  
  8. float width=800, height=700;
  9. float kat=0.0,deltakat=0.0,katrozgladania=0,deltarozgladania=0, ratio;
  10. float x=0.0f, y=1.75f, z=5.0f;
  11. float lx=0.0f, ly=0.0f, lz=-1.0f;
  12. int deltaRuch=0;
  13. float wielkoscDomku=0.5;
  14. float podlozeR=0, podlozeG=1, podlozeB=0;
  15.  
  16. void vertexowyDomek();
  17. void domek(float wielkoscDomku);
  18. void zorientujMnie (float kat);
  19. void plaskiRuch(int i);
  20. void goraDol(float kat);
  21. void initGL();
  22. void display();
  23. void reshape(GLsizei width, GLsizei height);
  24. void mysz(int przycisk, int state, int x, int y);
  25. void pressKey(int key, int x, int y);
  26. void releaseKey(int key, int x, int y);
  27. void processNormalKeys (unsigned char key, int x, int y);
  28. void kolorTla(int x);
  29. void kolorPodloza(int x);
  30. void podloze();
  31. void createMenus();
  32.  
  33.  
  34. /* Main function: GLUT runs as a console application starting at main() */
  35. int main(int argc, char** argv) {
  36.    glutInit(&argc, argv);            // Initialize GLUT
  37.    glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode
  38.    glutInitWindowSize(640, 480);   // Set the window's initial width & height
  39.    glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
  40.    glutCreateWindow("Domki");          // Create window with the given title
  41.    glutDisplayFunc(display);       // Register callback handler for window re-paint event
  42.    glutReshapeFunc(reshape);       // Register callback handler for window re-size event
  43.    initGL();                       // Our own OpenGL initialization
  44.  
  45.   //  glutIgnoreKeyRepeat(1);
  46.     glutKeyboardFunc(processNormalKeys);
  47.     glutSpecialFunc(pressKey);
  48. //   glutSpecialUpFunc(releaseKey);
  49.  
  50.    glutMouseFunc(mysz);
  51.  
  52.     createMenus();
  53.    glutMainLoop();                 // Enter the infinite event-processing loop
  54.    return 0;
  55. }
  56.  
  57. void vertexowyDomek()
  58. {
  59.  
  60.     ///*********************************SZESCIAN******************************
  61.    glBegin(GL_QUADS);                // Begin drawing the color cube with 6 quads
  62.       // Top face (y = 0.5f)
  63.       // Define vertices in counter-clockwise (CCW) order with normal pointing out
  64.       glColor3f(0.0f, 1.0f, 0.0f);     // Green
  65.       glVertex3f( wielkoscDomku, wielkoscDomku, -wielkoscDomku);
  66.       glVertex3f(-wielkoscDomku, wielkoscDomku, -wielkoscDomku);
  67.       glVertex3f(-wielkoscDomku, wielkoscDomku,  wielkoscDomku);
  68.       glVertex3f( wielkoscDomku, wielkoscDomku,  wielkoscDomku);
  69.  
  70.       // Bottom face (y = -0.5f)
  71.       glColor3f(1.0f, 0.5f, 0.0f);     // Orange
  72.       glVertex3f( wielkoscDomku, -wielkoscDomku,  wielkoscDomku);
  73.       glVertex3f(-wielkoscDomku, -wielkoscDomku,  wielkoscDomku);
  74.       glVertex3f(-wielkoscDomku, -wielkoscDomku, -wielkoscDomku);
  75.       glVertex3f( wielkoscDomku, -wielkoscDomku, -wielkoscDomku);
  76.  
  77.       // Front face  (z = 1.0f)
  78.       glColor3f(1.0f, 0.0f, 0.0f);     // Red
  79.       glVertex3f( wielkoscDomku,  wielkoscDomku, wielkoscDomku);
  80.       glVertex3f(-wielkoscDomku,  wielkoscDomku, wielkoscDomku);
  81.       glVertex3f(-wielkoscDomku, -wielkoscDomku, wielkoscDomku);
  82.       glVertex3f( wielkoscDomku, -wielkoscDomku, wielkoscDomku);
  83.  
  84.       // Back face (z = -1.0f)
  85.       glColor3f(1.0f, 1.0f, 0.0f);     // Yellow
  86.       glVertex3f( wielkoscDomku, -wielkoscDomku, -wielkoscDomku);
  87.       glVertex3f(-wielkoscDomku, -wielkoscDomku, -wielkoscDomku);
  88.       glVertex3f(-wielkoscDomku,  wielkoscDomku, -wielkoscDomku);
  89.       glVertex3f( wielkoscDomku,  wielkoscDomku, -wielkoscDomku);
  90.  
  91.       // Left face (x = -1.0f)
  92.       glColor3f(0.0f, 0.0f, 1.0f);     // Blue
  93.       glVertex3f(-wielkoscDomku,  wielkoscDomku,  wielkoscDomku);
  94.       glVertex3f(-wielkoscDomku,  wielkoscDomku, -wielkoscDomku);
  95.       glVertex3f(-wielkoscDomku, -wielkoscDomku, -wielkoscDomku);
  96.       glVertex3f(-wielkoscDomku, -wielkoscDomku,  wielkoscDomku);
  97.  
  98.       // Right face (x = 1.0f)
  99.       glColor3f(1.0f, 0.0f, 1.0f);     // Magenta
  100.       glVertex3f(wielkoscDomku,  wielkoscDomku, -wielkoscDomku);
  101.       glVertex3f(wielkoscDomku,  wielkoscDomku,  wielkoscDomku);
  102.       glVertex3f(wielkoscDomku, -wielkoscDomku,  wielkoscDomku);
  103.       glVertex3f(wielkoscDomku, -wielkoscDomku, -wielkoscDomku);
  104.    glEnd();  // End of drawing color-cub
  105.  
  106.     ///****************************DACH****************************
  107.    glTranslated(0,wielkoscDomku,0);
  108.  
  109.     glBegin(GL_QUADS);
  110.      glColor3f(0.0f, 1.0f, 0.0f);
  111.         glVertex3f(-wielkoscDomku,0,-wielkoscDomku);
  112.         glVertex3f(wielkoscDomku,0,-wielkoscDomku);
  113.         glVertex3f(wielkoscDomku,0,wielkoscDomku);
  114.         glVertex3f(-wielkoscDomku,0,wielkoscDomku);
  115.     glEnd();
  116.  
  117.     glBegin(GL_TRIANGLES);
  118.         glColor3f(1.0f, 0.0f, 0.0f);
  119.         glVertex3f(-wielkoscDomku,0,-wielkoscDomku);
  120.         glColor3f(0.0f, 0.0f, 1.0f);
  121.         glVertex3f(wielkoscDomku,0,-wielkoscDomku);
  122.         glColor3f(0.0f, 1.0f, 0.0f);
  123.         glVertex3f(0,wielkoscDomku*0.8,0);
  124.     glEnd();
  125.  
  126.     glBegin(GL_TRIANGLES);
  127.         glColor3f(1.0f, 0.0f, 0.0f);
  128.         glVertex3f(wielkoscDomku,0,-wielkoscDomku);
  129.         glColor3f(0.0f, 0.0f, 1.0f);
  130.         glVertex3f(wielkoscDomku,0,wielkoscDomku);
  131.         glColor3f(0.0f, 1.0f, 0.0f);
  132.         glVertex3f(0,wielkoscDomku*0.8,0);
  133.     glEnd();
  134.  
  135.     glBegin(GL_TRIANGLES);
  136.         glColor3f(1.0f, 0.0f, 0.0f);
  137.         glVertex3f(wielkoscDomku,0,wielkoscDomku);
  138.         glColor3f(0.0f, 0.0f, 1.0f);
  139.         glVertex3f(-wielkoscDomku,0,wielkoscDomku);
  140.         glColor3f(0.0f, 1.0f, 0.0f);
  141.         glVertex3f(0,wielkoscDomku*0.8,0);
  142.     glEnd();
  143.  
  144.     glBegin(GL_TRIANGLES);
  145.         glColor3f(1.0f, 0.0f, 0.0f);
  146.         glVertex3f(-wielkoscDomku,0,-wielkoscDomku);
  147.         glColor3f(0.0f, 0.0f, 1.0f);
  148.         glVertex3f(-wielkoscDomku,0,wielkoscDomku);
  149.         glColor3f(0.0f, 1.0f, 0.0f);
  150.         glVertex3f(0,wielkoscDomku*0.8,0);
  151.     glEnd();
  152. }
  153.  
  154. void domek(float wielkoscDomku)
  155. {
  156.     glPushMatrix();
  157.     glTranslated(0,wielkoscDomku,0);
  158.     vertexowyDomek();
  159.     glPopMatrix();
  160. }
  161.  
  162. void zorientujMnie (float kat)
  163. {
  164.     lx = sin(kat);
  165.     lz = -cos(kat);
  166.     glLoadIdentity();
  167.     gluLookAt(x,    y,    z,
  168.               x+lx, y+ly, z+lz,
  169.               0.0f, 1.0f, 0.0f);
  170.     glutPostRedisplay();
  171. }
  172.  
  173. void plaskiRuch(int i)//i=deltaruch
  174. {
  175.     x = x+i*lx*0.5;
  176.     z = z+i*lz*0.5;
  177.     glLoadIdentity();
  178.     gluLookAt(x,    y,    z,
  179.               x+lx, y+ly, z+lz,
  180.               0.0f, 1.0f, 0.0f);
  181.              // glutPostRedisplay();///////////////
  182. }
  183.  
  184. void goraDol(float kat)
  185. {
  186.    /* if(czyWGore)
  187.         x+=0.1;
  188.     else
  189.         y-=0.1;
  190.  
  191.     }
  192.    */ ly=sin(kat);
  193.     glLoadIdentity();
  194.     gluLookAt(x,    y,    z,
  195.               x+lx, y+ly, z+lz,
  196.                0.0f, 1.0f, 0.0f);
  197.     glutPostRedisplay();
  198. }
  199. /* Initialize OpenGL Graphics */
  200. void initGL() {
  201.    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
  202.    glClearDepth(1.0f);                   // Set background depth to farthest
  203.    glEnable(GL_DEPTH_TEST);   // Enable depth testing for z-culling
  204.    glDepthFunc(GL_LEQUAL);    // Set the type of depth-test
  205.    glShadeModel(GL_SMOOTH);   // Enable smooth shading
  206.    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Nice perspective corrections
  207. }
  208.  
  209. /* Handler for window-repaint event. Called back when the window first appears and
  210.    whenever the window needs to be re-painted. */
  211. void display() {
  212.  
  213.    // plaskiRuch(kat);
  214.     if(deltaRuch)
  215.         plaskiRuch(deltaRuch);
  216.  
  217.     if (deltakat)
  218.     {
  219.         kat+=deltakat;
  220.         zorientujMnie(kat);
  221.     }
  222.      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
  223.    // glTranslatef(0.0f, 2.0f, 0.0f
  224.    podloze();
  225.  
  226.    /* glColor3f(0,0,1);
  227.   // glColor3f(podlozeR,podlozeG,podlozeB);
  228.  //  glutPostRedisplay();
  229.    glBegin(GL_QUADS);
  230.         glVertex3f(-100.0f, 0.0f, -100.0f);
  231.         glVertex3f(-100.0f, 0.0f, 100.0f);
  232.         glVertex3f(100.0f, 0.0f, 100.0f);
  233.         glVertex3f(100.0f, 0.0f, -100.0f);
  234.     glEnd();*/
  235.  
  236.     for (int i=-3; i<3; i++)
  237.         for (int j=-3; j<3; j++)
  238.         {
  239.             glPushMatrix();
  240.             glTranslatef(i*10.0,0,j*10.0);
  241.             domek(wielkoscDomku);
  242.             glPopMatrix();
  243.         }
  244.    glutSwapBuffers();  // Swap the front and back frame buffers (double buffering)
  245. }
  246.  
  247. /* Handler for window re-size event. Called back when the window first appears and
  248.    whenever the window is re-sized with its new width and height */
  249. void reshape(GLsizei width, GLsizei height) {  // GLsizei for non-negative integer
  250.    // Compute aspect ratio of the new window
  251.    if (height == 0) height = 1;                // To prevent divide by 0
  252.     ratio = (GLfloat)width / (GLfloat)height;
  253.    // Set the aspect ratio of the clipping volume to match the viewport
  254.    glMatrixMode(GL_PROJECTION);  // To operate on the Projection matrix
  255.     glLoadIdentity();
  256.    // Set the viewport to cover the new window
  257.    glViewport(0, 0, width, height);
  258.    gluPerspective(45.0f, ratio, 1.0f, 1000.0f);
  259.    glMatrixMode(GL_MODELVIEW);
  260.    glLoadIdentity();             // Reset
  261.    gluLookAt(x,    y,    z,
  262.              x+lx, y+ly, z+lz,
  263.              0.0f, 1.0f, 0.0f);
  264. }
  265.  
  266. void mysz(int przycisk, int state, int x, int y)
  267. {
  268. if(przycisk==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
  269. {
  270.     deltaRuch=0.0;                                          ///     \(^.^)/
  271.         if(y!=height/2)
  272.          {
  273.              if(y<height/2) deltarozgladania=0.0005;
  274.              else deltarozgladania=-0.0005;
  275.          }
  276.         if(x!=width/2)
  277.          {
  278.              if(x>width/2)
  279.              {
  280.                  deltakat=0.0005;
  281.              }
  282.              else{deltakat=-0.0005;}
  283.          }
  284. }
  285. else {deltakat=0, deltarozgladania=0;}
  286. glutPostRedisplay();
  287. }
  288.  
  289. void pressKey(int key, int x, int y)
  290. {
  291.     switch(key)
  292.     {
  293.         case GLUT_KEY_LEFT: deltakat=-0.00f; break;//-0.001
  294.         case GLUT_KEY_RIGHT: deltakat=0.00; break;
  295.         case GLUT_KEY_UP: deltaRuch=1.0; break;
  296.         case GLUT_KEY_DOWN: deltaRuch=-1.0; break;
  297.     }
  298.     glutPostRedisplay();
  299. }
  300.  
  301. void releaseKey(int key, int x, int y)
  302. {
  303.     switch(key)
  304.     {
  305.         case GLUT_KEY_LEFT:
  306.         case GLUT_KEY_RIGHT: deltakat=0.0f; break;
  307.         case GLUT_KEY_UP:
  308.         case GLUT_KEY_DOWN: deltaRuch=0; break;
  309.     }
  310.     glutPostRedisplay();
  311. }
  312.  
  313. void processNormalKeys (unsigned char key, int x, int y)
  314. {
  315.     if (key == 27)
  316.         exit(0);
  317. }
  318. void kolorTla(int x)
  319. {
  320.     if(x==1){
  321.         glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
  322.         glutPostRedisplay();}
  323.     if(x==2){
  324.         glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
  325.         glutPostRedisplay();}
  326.     if(x==3){
  327.         glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
  328.         glutPostRedisplay();}
  329. }
  330. void kolorPodloza(int x)
  331. {
  332.     if(x==1){
  333.         podlozeR=1, podlozeG=0, podlozeB=0;
  334.         glutPostRedisplay();
  335.         }
  336.  
  337.     if(x==2){
  338.         podlozeR=0, podlozeG=1, podlozeB=0;
  339.         glutPostRedisplay();
  340.         }
  341.     if(x==3){
  342.         podlozeR=0, podlozeG=0, podlozeB=1;
  343.         glutPostRedisplay();
  344.        }
  345.     else{
  346.         podlozeR=1, podlozeG=0, podlozeB=1;
  347.         glutPostRedisplay();
  348.        }
  349. }
  350.  
  351. void podloze()
  352. {
  353.     glutPostRedisplay();
  354.     glColor3f(podlozeR,podlozeG,podlozeB);
  355.    glBegin(GL_QUADS);
  356.         glVertex3f(-100.0f, 0.0f, -100.0f);
  357.         glVertex3f(-100.0f, 0.0f, 100.0f);
  358.         glVertex3f(100.0f, 0.0f, 100.0f);
  359.         glVertex3f(100.0f, 0.0f, -100.0f);
  360.     glEnd();
  361. }
  362.  
  363. void createMenus()
  364. {
  365.    int subMenu1 = glutCreateMenu(kolorPodloza);
  366.    int subMenu2 = glutCreateMenu(kolorTla);
  367.   // int subMenu3 = glutCreateMenu(zmianaFigur);
  368.  
  369.     glutSetMenu(subMenu1);
  370.    glutAddMenuEntry("Czerwony",1);
  371.    glutAddMenuEntry("Zielony",2);
  372.    glutAddMenuEntry("Niebieski",3);
  373.  
  374.    glutSetMenu(subMenu2);
  375.    glutAddMenuEntry("Czerwony",1);
  376.    glutAddMenuEntry("Zielony",2);
  377.    glutAddMenuEntry("Niebieski",3);
  378.  
  379.     glutCreateMenu(kolorTla);
  380.    glutAddSubMenu("Zmien kolor podloza", subMenu1);
  381.    glutAddSubMenu("Zmien kolor tla", subMenu2);
  382.  
  383.    glutAttachMenu(GLUT_RIGHT_BUTTON);
  384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement