Advertisement
Guest User

kamera,zjebany dom, cos zyje

a guest
Dec 8th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.64 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.  
  14. void domek()
  15. {
  16.    /* glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
  17.     glMatrixMode(GL_MODELVIEW);     // To operate on model-view matrix
  18. */
  19.    // Render a color-cube consisting of 6 quads with different colors
  20.    glLoadIdentity();                 // Reset the model-view matrix
  21.    glTranslatef(1.5f, 0.0f, -7.0f);  // Move right and into the screen
  22.  
  23.    glBegin(GL_QUADS);                // Begin drawing the color cube with 6 quads
  24.       // Top face (y = 1.0f)
  25.       // Define vertices in counter-clockwise (CCW) order with normal pointing out
  26.       glColor3f(0.0f, 1.0f, 0.0f);     // Green
  27.       glVertex3f( 1.0f, 1.0f, -1.0f);
  28.       glVertex3f(-1.0f, 1.0f, -1.0f);
  29.       glVertex3f(-1.0f, 1.0f,  1.0f);
  30.       glVertex3f( 1.0f, 1.0f,  1.0f);
  31.  
  32.       // Bottom face (y = -1.0f)
  33.       glColor3f(1.0f, 0.5f, 0.0f);     // Orange
  34.       glVertex3f( 1.0f, -1.0f,  1.0f);
  35.       glVertex3f(-1.0f, -1.0f,  1.0f);
  36.       glVertex3f(-1.0f, -1.0f, -1.0f);
  37.       glVertex3f( 1.0f, -1.0f, -1.0f);
  38.  
  39.       // Front face  (z = 1.0f)
  40.       glColor3f(1.0f, 0.0f, 0.0f);     // Red
  41.       glVertex3f( 1.0f,  1.0f, 1.0f);
  42.       glVertex3f(-1.0f,  1.0f, 1.0f);
  43.       glVertex3f(-1.0f, -1.0f, 1.0f);
  44.       glVertex3f( 1.0f, -1.0f, 1.0f);
  45.  
  46.       // Back face (z = -1.0f)
  47.       glColor3f(1.0f, 1.0f, 0.0f);     // Yellow
  48.       glVertex3f( 1.0f, -1.0f, -1.0f);
  49.       glVertex3f(-1.0f, -1.0f, -1.0f);
  50.       glVertex3f(-1.0f,  1.0f, -1.0f);
  51.       glVertex3f( 1.0f,  1.0f, -1.0f);
  52.  
  53.       // Left face (x = -1.0f)
  54.       glColor3f(0.0f, 0.0f, 1.0f);     // Blue
  55.       glVertex3f(-1.0f,  1.0f,  1.0f);
  56.       glVertex3f(-1.0f,  1.0f, -1.0f);
  57.       glVertex3f(-1.0f, -1.0f, -1.0f);
  58.       glVertex3f(-1.0f, -1.0f,  1.0f);
  59.  
  60.       // Right face (x = 1.0f)
  61.       glColor3f(1.0f, 0.0f, 1.0f);     // Magenta
  62.       glVertex3f(1.0f,  1.0f, -1.0f);
  63.       glVertex3f(1.0f,  1.0f,  1.0f);
  64.       glVertex3f(1.0f, -1.0f,  1.0f);
  65.       glVertex3f(1.0f, -1.0f, -1.0f);
  66.    glEnd();  // End of drawing color-cube
  67.  
  68.    // Render a pyramid consists of 4 triangles
  69.    glLoadIdentity();                  // Reset the model-view matrix
  70.    glTranslatef(1.5f, 0.0f, -7.0f);  // Move left and into the screen
  71.  
  72.  
  73.    glBegin(GL_TRIANGLES);           // Begin drawing the pyramid with 4 triangles
  74.       // Front
  75.       glColor3f(1.0f, 0.0f, 0.0f);     // Red
  76.       glVertex3f(-1.0f, 1.0f, -1.0f);
  77.       glColor3f(0.0f, 1.0f, 0.0f);     // Green
  78.       glVertex3f(-1.0f, 1.0f, 1.0f);
  79.       glColor3f(0.0f, 0.0f, 1.0f);     // Blue
  80.       glVertex3f(0, 3.0f, 0);
  81.  
  82.       // Right
  83.       glColor3f(1.0f, 0.0f, 0.0f);     // Red
  84.       glVertex3f(-1.0f, 1.0f, 1.0f);
  85.       glColor3f(0.0f, 0.0f, 1.0f);     // Blue
  86.       glVertex3f( 1.0f, 1.0f, 1.0f);
  87.       glColor3f(0.0f, 1.0f, 0.0f);     // Green
  88.       glVertex3f(0, 3.0f, 0);
  89.  
  90.       // Back
  91.       glColor3f(1.0f, 0.0f, 0.0f);     // Red
  92.       glVertex3f(1.0f, 1.0f, 1.0f);
  93.       glColor3f(0.0f, 1.0f, 0.0f);     // Green
  94.       glVertex3f(1.0f, 1.0f, -1.0f);
  95.       glColor3f(0.0f, 0.0f, 1.0f);     // Blue
  96.       glVertex3f(0, 3.0f, 0);
  97.  
  98.       // Left
  99.       glColor3f(1.0f,0.0f,0.0f);       // Red
  100.       glVertex3f(1.0f, 1.0f, -1.0f);
  101.       glColor3f(0.0f,0.0f,1.0f);       // Blue
  102.       glVertex3f(-1.0f, 1.0f, -1.0f);
  103.       glColor3f(0.0f,1.0f,0.0f);       // Green
  104.       glVertex3f(0, 3.0f, 0);
  105.    glEnd();   // Done drawing the pyramid
  106.  
  107.  /*  glTranslatef(0.0f, -2.0f, 0.0f);
  108.  glBegin(GL_QUADS);
  109.  // Bottom face (y = -1.0f)
  110.       glColor3f(1.0f, 1.0f, 1.0f);     // bialy
  111.       glVertex3f(-100.0f, 0.0f, -100.0f);
  112.         glVertex3f(-100.0f, 0.0f, 100.0f);
  113.         glVertex3f(100.0f, 0.0f, 100.0f);
  114.         glVertex3f(100.0f, 0.0f, -100.0f);
  115.     glEnd();
  116. */
  117. }
  118.  
  119. void zorientujMnie (float ang)
  120. {
  121.     lx = sin(ang);
  122.     lz = -cos(ang);
  123.     glLoadIdentity();
  124.     gluLookAt(x,y,z,
  125.               x+lx, y+ly, z+lz,
  126.               0.0f, 1.0f, 0.0f);
  127. }
  128.  
  129. void plaskiRuch(int i)
  130. {
  131.  
  132.     x = x+i*lx*0.1;
  133.     z = z+i*lz*0.1;
  134.     glLoadIdentity();
  135.     gluLookAt(x,y,z,
  136.               x+lx, y+ly, z+lz,
  137.               0.0f, 1.0f, 0.0f);
  138. }
  139. void goraDol(float ang)
  140. {
  141.     ly=sin(ang);
  142.     glLoadIdentity();
  143.     gluLookAt(x, y, z, x+lx, y+ly, z+lz, 0.0f, 1.0f, 0.0f);
  144.     glutPostRedisplay();
  145. }
  146. /* Initialize OpenGL Graphics */
  147. void initGL() {
  148.    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
  149.    glClearDepth(1.0f);                   // Set background depth to farthest
  150.    glEnable(GL_DEPTH_TEST);   // Enable depth testing for z-culling
  151.    glDepthFunc(GL_LEQUAL);    // Set the type of depth-test
  152.    glShadeModel(GL_SMOOTH);   // Enable smooth shading
  153.    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Nice perspective corrections
  154. }
  155.  
  156. /* Handler for window-repaint event. Called back when the window first appears and
  157.    whenever the window needs to be re-painted. */
  158. void display() {
  159.     if(deltarozgladania)
  160.     {
  161.         katrozgladania+=deltarozgladania;
  162.         goraDol(katrozgladania);
  163.     }
  164.  
  165.     if (deltaRuch)
  166.         plaskiRuch(deltaRuch);
  167.     if (deltakat)
  168.     {
  169.         kat += deltakat;
  170.         zorientujMnie(kat);
  171.     }
  172.  
  173.    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
  174.    // glTranslatef(0.0f, 2.0f, 0.0f);
  175.    glColor3f(1.0f,1.0f,1.0f);
  176.    glBegin(GL_QUADS);
  177.         glVertex3f(-100.0f, 0.0f, -100.0f);
  178.         glVertex3f(-100.0f, 0.0f, 100.0f);
  179.         glVertex3f(100.0f, 0.0f, 100.0f);
  180.         glVertex3f(100.0f, 0.0f, -100.0f);
  181.     glEnd();
  182.  
  183.  
  184.       /*glBegin(GL_QUADS);
  185.       glVertex3f(100.0f, -100.0f, 100.0f);
  186.         glVertex3f(-100.0f, -100.0f, 100.0f);
  187.         glVertex3f(-100.0f, -100.0f, -100.0f);
  188.         glVertex3f(100.0f, -100.0f, -100.0f);
  189.     glEnd();*/
  190.     for (int i=-3; i<3; i++)
  191.         for (int j=-3; j<3; j++)
  192.         {
  193.             glPushMatrix();
  194.             glTranslatef(i*10.0,0,j*10.0);
  195.             domek();
  196.             glPopMatrix();
  197.         }
  198.  
  199.  
  200.   /*
  201.  
  202.  glTranslatef(0.0f, 2.0f, 0.0f);
  203.  glBegin(GL_QUADS);
  204.  // Bottom face (y = -1.0f)
  205.       glColor3f(1.0f, 1.0f, 1.0f);     // bialy
  206.       glVertex3f( 5.0f, -5.0f,  5.0f);
  207.       glVertex3f(-5.0f, -5.0f,  5.0f);
  208.       glVertex3f(-5.0f, -5.0f, -5.0f);
  209.       glVertex3f( 5.0f, -5.0f, -5.0f);
  210.     glEnd();
  211. */
  212.    glutSwapBuffers();  // Swap the front and back frame buffers (double buffering)
  213. }
  214.  
  215. /* Handler for window re-size event. Called back when the window first appears and
  216.    whenever the window is re-sized with its new width and height */
  217. void reshape(GLsizei width, GLsizei height) {  // GLsizei for non-negative integer
  218.    // Compute aspect ratio of the new window
  219.    if (height == 0) height = 1;                // To prevent divide by 0
  220.     ratio = (GLfloat)width / (GLfloat)height;
  221.    // Set the aspect ratio of the clipping volume to match the viewport
  222.    glMatrixMode(GL_PROJECTION);  // To operate on the Projection matrix
  223.     glLoadIdentity();
  224.    // Set the viewport to cover the new window
  225.    glViewport(0, 0, width, height);
  226.  
  227.    gluPerspective(45.0f, ratio, 1.0f, 1000.0f);
  228.    glMatrixMode(GL_MODELVIEW);
  229.    glLoadIdentity();             // Reset
  230.    gluLookAt(x,y,z,
  231.              x+lx, y+ly, z+lz,
  232.              0.0f, 1.0f, 0.0f);
  233. }
  234.  
  235.  
  236. void mysz(int przycisk, int state, int x, int y)
  237. {
  238. if(przycisk==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
  239. {
  240.         if(y!=height/2)
  241.          {
  242.              if(y<height/2) deltarozgladania=0.0005;
  243.              else deltarozgladania=-0.0005;
  244.          }
  245.         if(x!=width/2)
  246.          {
  247.              if(x>width/2)
  248.              {
  249.                  deltakat=0.0005;
  250.              }
  251.              else{deltakat=-0.0005;}
  252.          }
  253.  
  254. }
  255. else {deltakat=0, deltarozgladania=0;}
  256.  
  257.  
  258. glutPostRedisplay();
  259. }
  260.  
  261. void pressKey(int key, int x, int y)
  262. {
  263.     switch(key)
  264.     {
  265.         case GLUT_KEY_LEFT: deltakat=-0.01f; break;//-0.001
  266.         case GLUT_KEY_RIGHT: deltakat=0.01; break;
  267.         case GLUT_KEY_UP: deltaRuch=1.0; break;
  268.         case GLUT_KEY_DOWN: deltaRuch=-1.0; break;
  269.     }
  270.    // glutPostRedisplay();
  271. }
  272.  
  273.  
  274. void releaseKey(int key, int x, int y)
  275. {
  276.     switch(key)
  277.     {
  278.         case GLUT_KEY_LEFT:
  279.         case GLUT_KEY_RIGHT: deltakat=0.0f; break;
  280.         case GLUT_KEY_UP:
  281.         case GLUT_KEY_DOWN: deltaRuch=0; break;
  282.     }
  283.    // glutPostRedisplay();
  284. }
  285.  
  286. void processNormalKeys (unsigned char key, int x, int y)
  287. {
  288.     if (key == 27)
  289.         exit(0);
  290. }
  291.  
  292.  
  293.  
  294. /* Main function: GLUT runs as a console application starting at main() */
  295. int main(int argc, char** argv) {
  296.    glutInit(&argc, argv);            // Initialize GLUT
  297.    glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode
  298.    glutInitWindowSize(640, 480);   // Set the window's initial width & height
  299.    glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
  300.    glutCreateWindow("Domki");          // Create window with the given title
  301.    glutDisplayFunc(display);       // Register callback handler for window re-paint event
  302.    glutReshapeFunc(reshape);       // Register callback handler for window re-size event
  303.    initGL();                       // Our own OpenGL initialization
  304.  
  305. //    glutIgnoreKeyRepeat(1);
  306.     glutKeyboardFunc(processNormalKeys);
  307.     glutSpecialFunc(pressKey);
  308.  //   glutSpecialUpFunc(releaseKey);
  309.    glutMouseFunc(mysz);
  310.  
  311.  
  312.    glutMainLoop();                 // Enter the infinite event-processing loop
  313.    return 0;
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement