Advertisement
Guest User

OGL ORTHO PROBLEM

a guest
Aug 3rd, 2020
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. ///////////////////////////// VERTEX SHADER///////////////////////////////////////////
  2. #version 330 core
  3. layout (location = 0) in vec2 aPos;
  4. layout (location = 1) in vec2 aTexCoord;
  5.  
  6. out vec2 TexCoord;
  7.  
  8. uniform mat4 model;
  9. uniform mat4 projection;
  10.  
  11. void main()
  12. {
  13. gl_Position = vec4((vec4(aPos, 1.0, 1.0) * model * projection));
  14. TexCoord = vec2(aTexCoord.x, aTexCoord.y);
  15. }
  16.  
  17. /////////////////// GRAPHIC MATH FUNCTIONS ///////////////////////////////////////
  18. matrix4 matrix4::ortho(const float width, const float height,
  19. const float near_plane, const float far_plane)
  20. {
  21. float zoom_x = 2/width;
  22. float zoom_y = 2/height;
  23. float n1 = -2 / (far_plane - near_plane);
  24. float n2 = -(far_plane + near_plane) / (far_plane - near_plane);
  25.  
  26. return matrix4{{ zoom_x, 0, 0, 0,
  27. 0, zoom_y, 0, 0,
  28. 0, 0, n1, n2,
  29. 0, 0, 0, 1,}};
  30. }
  31.  
  32. matrix4 matrix4::translation(const float x, const float y, const float z)
  33. {
  34.  
  35. return matrix4{{ 1, 0, 0, 0,
  36. 0, 1, 0, 0,
  37. 0, 0, 1, 0,
  38. x, y, z, 1,}};
  39. }
  40.  
  41. matrix4 matrix4::from_martrix2(const matrix2x2 a)
  42. {
  43. return matrix4{{ a.n[0], a.n[1], 0, 0,
  44. a.n[2], a.n[3], 0, 0,
  45. 0 , 1, 0, 0,
  46. 0, 0, 0, 1,}};
  47. }
  48.  
  49. ///////////////////////////////////////// RENDER FUNCTION //////////////////////////////////////////////////////
  50. void Sprite::draw_sprite(uint64_t tex, PF location, PF scale, float rotation, float layer)
  51. {
  52. auto scale_2d = matrix2x2::scale(scale.first, scale.second);
  53. auto scaled = matrix4::from_martrix2(scale_2d.multiply(matrix2x2::rotation(rotation)));
  54. auto model = scaled.multiply(matrix4::translation(location.first, location.second, layer));
  55.  
  56. shader.set_mat4("model", model);
  57. shader.set_mat4("projection", projection);
  58.  
  59. shader.set_int("texture1", 0);
  60. glActiveTexture(GL_TEXTURE0);
  61. glBindTexture(GL_TEXTURE_2D, tex);
  62.  
  63. shader.use();
  64. glBindVertexArray(VAO);
  65.  
  66. glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  67. glBindVertexArray(0);
  68. }
  69.  
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement