Rabbid76

Perspective projection inverting x axis (glm::perspective)

Nov 28th, 2020
645
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // glm
  2. #include <glm/glm.hpp>
  3. #include <glm/gtc/matrix_transform.hpp>
  4. #include <glm/gtc/type_ptr.hpp>
  5.  
  6. // glfw
  7. #include <GLFW/glfw3.h>
  8.  
  9. // stl
  10. #include <vector>
  11. #include <stdexcept>
  12. #include <array>
  13.  
  14. #define _USE_MATH_DEFINES
  15. #include <cmath>
  16. #include <math.h>
  17.  
  18. // project includes
  19. #include <gl/gl_debug.h>
  20. #include <gl/gl_shader.h>
  21.  
  22. // preprocessor definitions
  23.  
  24. #ifndef M_PI
  25. #define M_PI 3.14159265358979323846
  26. #endif
  27.  
  28. std::string sh_vert = R"(
  29. #version 460 core
  30.  
  31. layout (location = 0) in vec3 inPos;
  32. layout (location = 1) in vec4 inColor;
  33.  
  34. out vec3 vertPos;
  35. out vec4 vertCol;
  36.  
  37. uniform mat4 project;
  38. uniform mat4 view;
  39. uniform mat4 model;
  40.  
  41. void main()
  42. {
  43.    vec4 view_pos = view * model * vec4(inPos, 1.0);
  44.  
  45.    vertCol     = inColor;
  46.         vertPos     = view_pos.xyz;
  47.         gl_Position = project * vec4(view_pos.xyz, 1.0);
  48. }
  49. )";
  50.  
  51. std::string sh_frag = R"(
  52. #version 460 core
  53.  
  54. in vec3 vertPos;
  55. in vec4 vertCol;
  56.  
  57. out vec4 fragColor;
  58.  
  59. void main()
  60. {
  61.    fragColor = vertCol;
  62. }
  63. )";
  64.  
  65.  
  66. static float ang_x = 0.0f;
  67. static float ang_y = 0.1f;
  68.  
  69. int main(void)
  70. {
  71.     if ( glfwInit() != GLFW_TRUE )
  72.         throw std::runtime_error( "error initializing glfw" );
  73.  
  74.     //glfwWindowHint(GLFW_REFRESH_RATE, 10);
  75.  
  76.     GLFWwindow *wnd = glfwCreateWindow( 800, 600, "OGL window", nullptr, nullptr );
  77.     if ( wnd == nullptr )
  78.     {
  79.         glfwTerminate();
  80.         throw std::runtime_error( "error initializing window" );
  81.     }
  82.  
  83.     glfwMakeContextCurrent(wnd);
  84.  
  85.     //glfwSwapInterval( 2 );
  86.  
  87.     if ( glewInit() != GLEW_OK )
  88.         throw std::runtime_error( "error initializing glew" );
  89.  
  90.     OpenGL::CContext::TDebugLevel debug_level = OpenGL::CContext::TDebugLevel::all;
  91.     OpenGL::CContext context;
  92.     context.Init( debug_level );
  93.  
  94.    
  95.     OpenGL::ShaderProgramSimple prog(
  96.     {
  97.       { sh_vert, GL_VERTEX_SHADER },
  98.       { sh_frag, GL_FRAGMENT_SHADER }
  99.     } );
  100.    
  101.  
  102.     const float sin120 = 0.8660254f;
  103.     static const std::vector<float> varray
  104.     {
  105.        0.0f,             0.0f,           1.0f,    1.0f, 0.0f, 0.0f, 1.0f,
  106.        0.0f,            -sin120,        -0.5f,    1.0f, 1.0f, 0.0f, 1.0f,
  107.        sin120 * sin120,  0.5f * sin120, -0.5f,    0.0f, 0.0f, 1.0f, 1.0f,
  108.       -sin120 * sin120,  0.5f * sin120, -0.5f,    0.0f, 1.0f, 0.0f, 1.0f
  109.     };
  110.     const std::vector<unsigned int> iarray
  111.     {
  112.       0, 1, 2, 0, 2, 3, 0, 3, 1, 1, 3, 2
  113.     };
  114.  
  115.     GLuint vertex_binding_index = 0;
  116.     GLuint color_binding_index = 1;
  117.  
  118.     GLuint vbo;
  119.     glGenBuffers( 1, &vbo );
  120.     glBindBuffer( GL_ARRAY_BUFFER, vbo );
  121.  
  122.     GLuint ibo;
  123.     glGenBuffers( 1, &ibo );
  124.  
  125.     //glBufferData( GL_ARRAY_BUFFER, varray.size()*sizeof(*varray.data()), varray.data(), GL_STATIC_DRAW );
  126.     glBufferData( GL_ARRAY_BUFFER, varray.size()*sizeof(*varray.data()), nullptr, GL_STATIC_DRAW );
  127.     glBufferSubData( GL_ARRAY_BUFFER, GL_ZERO, varray.size()*sizeof(*varray.data()), varray.data() );
  128.  
  129.     GLuint vao;
  130.     glGenVertexArrays( 1, &vao );
  131.     glBindVertexArray( vao );
  132.  
  133.     GLuint vertex_attrib_inx = 0;
  134.     GLuint color_attrib_inx  = 1;
  135.    
  136.     glBindVertexBuffer( vertex_binding_index, vbo, 0, 7*sizeof(*varray.data()) );
  137.     glVertexAttribFormat( vertex_attrib_inx, 3, GL_FLOAT, GL_FALSE, 0 );
  138.  
  139.     glBindVertexBuffer( color_binding_index, vbo, 3*sizeof(*varray.data()), 7*sizeof(*varray.data()) );
  140.     glVertexAttribFormat( color_attrib_inx, 4, GL_FLOAT, GL_FALSE, 0 );
  141.    
  142.     glVertexAttribBinding( vertex_attrib_inx, vertex_binding_index );
  143.     glVertexAttribBinding( color_attrib_inx,  color_binding_index );
  144.  
  145.     glEnableVertexAttribArray( vertex_attrib_inx );
  146.     glEnableVertexAttribArray( color_attrib_inx );
  147.    
  148.     glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ibo );
  149.     //glBufferData( GL_ELEMENT_ARRAY_BUFFER, varray.size()*sizeof(*varray.data()), varray.data(), GL_STATIC_DRAW );
  150.     glBufferData( GL_ELEMENT_ARRAY_BUFFER, iarray.size()*sizeof(*iarray.data()), nullptr, GL_STATIC_DRAW );
  151.     glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, GL_ZERO, iarray.size()*sizeof(*iarray.data()), iarray.data() );
  152.  
  153.     glBindVertexArray( 0 );
  154.  
  155.     // ...
  156.  
  157.     glBindVertexArray( vao );
  158.  
  159.     prog.Use();      
  160.  
  161.     while (!glfwWindowShouldClose(wnd))
  162.     {
  163.         static float angle = 1.0f;
  164.  
  165.         int vpSize[2];
  166.         glfwGetFramebufferSize( wnd, &vpSize[0], &vpSize[1] );
  167.        
  168.         glm::vec3 position = glm::vec3(0, 0, 0);
  169.         glm::vec3 look_at = glm::vec3(0, 0, -1);
  170.  
  171.         glm::mat4 projectionMatrix = glm::perspective(glm::radians(45.0f), (float)800 / (float)600, 0.1f, 100.f);
  172.         glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -5.0f));
  173.         glm::mat4 viewMatrix = glm::lookAt(
  174.           position,
  175.           look_at,
  176.           glm::vec3(0, 1, 0)  // Tilt/rotation (unclear) set y to -1 for upside down view
  177.         );
  178.  
  179.         modelMatrix = glm::rotate( modelMatrix, ang_x, glm::vec3( 1.0f, 0.0f, 0.0f ) );
  180.         modelMatrix = glm::rotate( modelMatrix, ang_y, glm::vec3( 0.0f, 0.1f, 0.0f ) );
  181.         ang_x += 0.013f;
  182.         ang_y += 0.017f;
  183.        
  184.         prog.SetUniformM44( "project", glm::value_ptr(projectionMatrix) );
  185.         prog.SetUniformM44( "view", glm::value_ptr(viewMatrix) );
  186.         prog.SetUniformM44( "model", glm::value_ptr(modelMatrix) );
  187.  
  188.         glViewport( 0, 0, vpSize[0], vpSize[1] );
  189.         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  190.         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  191.      
  192.         glEnable( GL_DEPTH_TEST );
  193.  
  194.         glDrawElements( GL_TRIANGLES, (GLsizei)iarray.size(), GL_UNSIGNED_INT, nullptr );
  195.  
  196.         glfwSwapBuffers(wnd);
  197.         glfwPollEvents();
  198.     }
  199.  
  200.     glfwDestroyWindow( wnd );
  201.     wnd = nullptr;
  202.     glfwTerminate();
  203.  
  204.     return 0;
  205. }
RAW Paste Data