SHARE
TWEET

Untitled

a guest Oct 21st, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "glew.h"
  2. #include "freeglut.h"
  3. #include "glm.hpp"
  4. #include "ext.hpp"
  5. #include <iostream>
  6. #include <cmath>
  7.  
  8. #include "Shader_Loader.h"
  9. #include "Render_Utils.h"
  10.  
  11. #include "Box.cpp"
  12.  
  13. GLuint program;
  14. Core::Shader_Loader shaderLoader;
  15.  
  16. glm::mat4 createPerspectiveMatrix()
  17. {
  18.     const float zNear = 0.1f, zFar = 10.0f;
  19.     const float frustumScale = 1.0f;
  20.     glm::mat4 perspective;
  21.     perspective[0][0] = frustumScale;
  22.     perspective[1][1] = frustumScale;
  23.     perspective[2][2] = (zFar + zNear) / (zNear - zFar);
  24.     perspective[3][2] = (2 * zFar * zNear) / (zNear - zFar);
  25.     perspective[2][3] = -1;
  26.     perspective[3][3] = 0;
  27.  
  28.     return perspective;
  29. }
  30.  
  31. void renderScene()
  32. {
  33.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  34.     glClearColor(0.0f, 0.3f, 0.3f, 1.0f);
  35.  
  36.     // ZADANIE: Add a second, smaller box to the scene using the scaling transformation. Make it rotate around the first box.
  37.    
  38.  
  39.     glUseProgram(program);
  40.  
  41.     Core::VertexData VertexData;
  42.     VertexData.NumActiveAttribs = 2;
  43.     VertexData.Attribs[0].Pointer = boxPositions;
  44.     VertexData.Attribs[0].Size = 4;
  45.     VertexData.Attribs[1].Pointer = boxColors;
  46.     VertexData.Attribs[1].Size = 4;
  47.     VertexData.NumVertices = 36;
  48.  
  49.     glm::mat4 transformation = glm::translate(glm::vec3(-0.4f, 0.3f, -2.0f));
  50.     float time = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
  51.     float t = sin(time) * 0.2f;
  52.  
  53.     glm::mat4 persp = createPerspectiveMatrix();
  54.     transformation = persp * transformation;
  55.  
  56.     glm::mat4 trans;
  57.     trans = glm::rotate(
  58.         trans,
  59.         time * glm::radians(180.0f),
  60.         glm::vec3(0.0f, 0.0f, 1.0f)
  61.     );
  62.     transformation *= trans;
  63.     glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)& transformation);
  64.     Core::DrawVertexArray(VertexData);
  65.  
  66.  
  67.  
  68.     glm::mat4 Transformation = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, -2.0f));
  69.     glm::mat4 scale = glm::scale(glm::vec3(0.4f, 0.4f, 0.4f));
  70.     glm::mat4 o1 = glm::translate(glm::vec3(-0.4f, 0.3f, -2.0f));
  71.     glm::mat4 mo1 = glm::translate(glm::mat4(), glm::vec3(0.4f, -0.3f, 2.0f));
  72.     glm::mat4 t2;
  73.     t2 = glm::rotate(
  74.         time * glm::radians(180.0f),
  75.         glm::vec3(0.0f, 0.0f, 1.0f)
  76.     );
  77.     Transformation = persp * Transformation * scale;
  78.     Transformation = o1 * t2 * mo1*Transformation;
  79.     glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)& Transformation);
  80.  
  81.     Core::DrawVertexArray(VertexData);
  82.     glUseProgram(0);
  83.     glutSwapBuffers();
  84. }
  85.  
  86. void init()
  87. {
  88.    
  89.     glEnable(GL_DEPTH_TEST);
  90.     program = shaderLoader.CreateProgram("shaders/shader_2_1.vert", "shaders/shader_2_1.frag");
  91. }
  92.  
  93. void shutdown()
  94. {
  95.     shaderLoader.DeleteProgram(program);
  96. }
  97.  
  98. void idle()
  99. {
  100.     glutPostRedisplay();
  101. }
  102.  
  103. int main(int argc, char ** argv)
  104. {
  105.     glutInit(&argc, argv);
  106.     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  107.     glutInitWindowPosition(200, 200);
  108.     glutInitWindowSize(600, 600);
  109.     glutCreateWindow("OpenGL Pierwszy Program");
  110.     glewInit();
  111.  
  112.     init();
  113.     glutDisplayFunc(renderScene);
  114.     glutIdleFunc(idle);
  115.  
  116.     glutMainLoop();
  117.  
  118.     shutdown();
  119.  
  120.     return 0;
  121. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top