Advertisement
xerpi

opengl cube shiat biatch

Jul 13th, 2013
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.84 KB | None | 0 0
  1. #define GL_GLEXT_PROTOTYPES
  2. #include <SFML/Graphics.hpp>
  3. #include <SFML/OpenGL.hpp>
  4. #include <stdlib.h>
  5. #include <string>
  6. #include <fstream>
  7. #include <iostream>
  8. #include <stdio.h>
  9. #include <vector>
  10. #include <algorithm>
  11. #include <sys/time.h>
  12. #include "utils.h"
  13. #include "Camera.h"
  14. #include <glm/glm.hpp>
  15. #include <glm/gtc/matrix_transform.hpp>
  16. #include <glm/gtc/type_ptr.hpp>
  17.  
  18. void events();
  19. void Init(void);
  20. void input();
  21. void resize(int w, int h);
  22. float pitch = 0.0f, yaw = 0.0f;
  23. glm::vec3 position(0.0f);
  24. Camera camera(&position, &pitch, &yaw);
  25.  
  26. sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
  27.  
  28.  
  29.  
  30. #define ARRAY_COUNT( array ) (sizeof(array) / sizeof(*array))
  31.  
  32. GLuint vao1, vao2, vertexBufferObject, indexBufferObject, program;
  33. GLuint offsetLoc, perspectiveMatrixLoc, cameraMatrixLoc, modelMatrixLoc;
  34. float frustumScale = 1.0f;
  35. float fzNear = 0.01f, fzFar = 300.0f;
  36. float perspectiveMatrix[16];
  37. float modelMatrix[16];
  38. float cameraMatrix[16];
  39.  
  40. #define GREEN_COLOR 0.75f, 0.75f, 1.0f, 1.0f
  41. #define BLUE_COLOR  0.0f, 0.5f, 0.0f, 1.0f
  42. #define RED_COLOR 1.0f, 0.0f, 0.0f, 1.0f
  43. #define GREY_COLOR 0.8f, 0.8f, 0.8f, 1.0f
  44. #define BROWN_COLOR 0.5f, 0.5f, 0.0f, 1.0f
  45. #define YELLOW_COLOR 1.0f, 1.0f, 0.0f, 1.0f
  46. #define PINK_COLOR 1.0f, 0.0f, 1.0f, 1.0f
  47. #define CYAN_COLOR 0.0f, 1.0f, 1.0f, 1.0f
  48.  
  49. #define CUBE_SIZE 0.5f
  50.  
  51. const float vertexData[] = {
  52.     -CUBE_SIZE, -CUBE_SIZE, CUBE_SIZE,
  53.     -CUBE_SIZE, CUBE_SIZE, CUBE_SIZE,
  54.     CUBE_SIZE, -CUBE_SIZE, CUBE_SIZE,
  55.     CUBE_SIZE, CUBE_SIZE, CUBE_SIZE,
  56.     CUBE_SIZE, -CUBE_SIZE, -CUBE_SIZE,
  57.     CUBE_SIZE, CUBE_SIZE, -CUBE_SIZE,
  58.     -CUBE_SIZE, -CUBE_SIZE, -CUBE_SIZE,
  59.     -CUBE_SIZE, CUBE_SIZE, -CUBE_SIZE,
  60.  
  61.     BLUE_COLOR,
  62.     GREEN_COLOR,
  63.     RED_COLOR,
  64.     YELLOW_COLOR,
  65.     PINK_COLOR,
  66.     GREY_COLOR,
  67.     BROWN_COLOR,
  68.     CYAN_COLOR
  69. };
  70.  
  71.  
  72. const GLubyte indexData[] =
  73. {
  74.     0, 1, 2, 3,
  75.     4, 5, 6, 7,
  76.     0, 1,
  77.     1, 7, 3, 5,
  78.     6, 0, 4, 2
  79. };
  80.  
  81.  
  82. float r = 0.0f;
  83. float radius = 5.0f;
  84. float tmpMtx1[16], tmpMtx2[16], rotMatrix[16];
  85.  
  86.  
  87. static void display(void)
  88. {
  89.     glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  90.     glClearDepth(1.0f);
  91.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  92.  
  93.     IdentityMatrix(modelMatrix);
  94.     IdentityMatrix(cameraMatrix);
  95.  
  96.     /*RotationMatrix_Y(rotMatrix, r);
  97.     CopyMatrix(modelMatrix, tmpMatrix);
  98.     MultiplyMatrix(modelMatrix, tmpMatrix, rotMatrix);
  99.  
  100.     RotationMatrix_X(rotMatrix, r);
  101.     CopyMatrix(modelMatrix, tmpMatrix);
  102.     MultiplyMatrix(modelMatrix, tmpMatrix, rotMatrix);
  103.     */
  104.     TranslationMatrix(cameraMatrix, position.x, position.y, position.z);
  105.  
  106.  
  107.  
  108.     glUseProgram(program);
  109.         glUniformMatrix4fv(perspectiveMatrixLoc, 1, GL_FALSE, perspectiveMatrix);
  110.         glUniformMatrix4fv(cameraMatrixLoc, 1, GL_FALSE, cameraMatrix);
  111.         glUniformMatrix4fv(modelMatrixLoc, 1, GL_FALSE, modelMatrix);
  112.  
  113.         glBindVertexArray(vao1);
  114.             glDrawElements(GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_BYTE, (void*)0);
  115.             glDrawElements(GL_TRIANGLE_STRIP, 4,  GL_UNSIGNED_BYTE, (void*)10);
  116.             glDrawElements(GL_TRIANGLE_STRIP, 4,  GL_UNSIGNED_BYTE, (void*)14);
  117.         glBindVertexArray(0);
  118.     glUseProgram(0);
  119.  
  120.     r+=0.025f;
  121.  
  122.     window.display();
  123. }
  124.  
  125. int main(void)
  126. {
  127.     Init();
  128.     while (window.isOpen()) {
  129.         events();
  130.         input();
  131.         display();
  132.     }
  133.     return EXIT_SUCCESS;
  134. }
  135.  
  136.  
  137.  
  138. void input()
  139. {
  140.     #define SPEED 0.01f
  141. //    glm::vec3 look = camera.getLookVector();
  142. //    glm::vec3 right = camera.getRightVector();
  143.  
  144.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
  145.         //pitch += SPEED;
  146.     } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
  147.         //pitch -= SPEED;
  148.     }
  149.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
  150.         //yaw += SPEED;
  151.     } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
  152.         //yaw -= SPEED;
  153.     }
  154.  
  155.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
  156.         position.x += SPEED;
  157.     } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
  158.         position.x -= SPEED;
  159.     }
  160.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
  161.         position.z += SPEED;
  162.     } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
  163.         position.z -= SPEED;
  164.     }
  165.  
  166.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num1)) {
  167.         position.y += SPEED;
  168.     } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num2)) {
  169.         position.y -= SPEED;
  170.     }
  171.  
  172.  
  173.     if(pitch > PI_2) pitch -= PI_2;
  174.     if(yaw > PI_2) yaw -= PI_2;
  175.     if(pitch < 0) pitch += PI_2;
  176.     if(yaw < 0) yaw += PI_2;
  177.  
  178.     //camera.update();
  179. }
  180.  
  181. void events()
  182. {
  183.     sf::Event event;
  184.     while (window.pollEvent(event)) {
  185.         switch (event.type)
  186.         {
  187.         case sf::Event::Closed:
  188.             window.close();
  189.             break;
  190.         case sf::Event::Resized:
  191.             resize(event.size.width, event.size.height);
  192.             break;
  193.         default:
  194.             break;
  195.         }
  196.     }
  197. }
  198.  
  199.  
  200. void resize(int w, int h)
  201. {
  202.     perspectiveMatrix[0] = frustumScale / (w / (float)h);
  203.     perspectiveMatrix[5] = frustumScale;
  204.     glViewport(0, 0, (GLsizei) w, (GLsizei) h);
  205. }
  206.  
  207.  
  208. void Init(void)
  209. {
  210.     program = InitializeProgram();
  211.  
  212.     glGenBuffers(1, &vertexBufferObject);
  213.     glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
  214.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
  215.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  216.  
  217.     glGenBuffers(1, &indexBufferObject);
  218.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferObject);
  219.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexData), indexData, GL_STATIC_DRAW);
  220.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  221.  
  222.     glGenVertexArrays(1, &vao1);
  223.     glBindVertexArray(vao1);
  224.         glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
  225.         glEnableVertexAttribArray(0);
  226.         glEnableVertexAttribArray(1);
  227.         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
  228.         glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(float) * 3 * 8));
  229.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferObject);
  230.     glBindVertexArray(0);
  231.  
  232.  
  233.     offsetLoc = glGetUniformLocation(program, "offset");
  234.     modelMatrixLoc        = glGetUniformLocation(program, "modelMatrix");
  235.     perspectiveMatrixLoc  = glGetUniformLocation(program, "perspectiveMatrix");
  236.     cameraMatrixLoc       = glGetUniformLocation(program, "cameraMatrix");
  237.  
  238.     memset(perspectiveMatrix, 0, sizeof(float) * 16);
  239.     IdentityMatrix(modelMatrix);
  240.  
  241.     perspectiveMatrix[0] = frustumScale;
  242.     perspectiveMatrix[5] = frustumScale;
  243.     perspectiveMatrix[10] = (fzFar + fzNear) / (fzNear - fzFar);
  244.     perspectiveMatrix[14] = (2 * fzFar * fzNear) / (fzNear - fzFar);
  245.     perspectiveMatrix[11] = -1.0f;
  246.  
  247.     glEnable(GL_DEPTH_TEST);
  248.     glDepthMask(GL_TRUE);
  249.     glDepthFunc(GL_LEQUAL);
  250.     glDepthRange(0.0f, 1.0f);
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement