Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. #include "ModelRenderer.h"
  2.  
  3. ModelRenderer::ModelRenderer(float aspectRatio) {
  4. shader = new ModelShader();
  5. createProjectionMatrix(aspectRatio);
  6. shader->start();
  7. shader->loadProjectionMatrix(projectionMatrix);
  8. shader->stop();
  9. }
  10.  
  11. void ModelRenderer::render(TexturedModel* model, Camera cam) {
  12. shader->start();
  13. glEnable(GL_DEPTH_TEST);
  14. RawModel* rawmodel = model->getModel();
  15. glBindVertexArray(rawmodel->getVaoID());
  16. glEnableVertexAttribArray(0);
  17. glEnableVertexAttribArray(1);
  18. glActiveTexture(GL_TEXTURE0);
  19. glEnable(GL_TEXTURE_2D); // the currently-active sampler should actually use the bound 2D texture.
  20.  
  21. glBindTexture(GL_TEXTURE_2D, model->getTexture());
  22. glm::vec3 pos(0.0f);
  23. glm::mat4 transformationmatrix = Maths::createTransformationMatrix(pos, 0.0f, 0.0f, 0.0f, 1.0f);
  24. shader->loadTransformationMatrix(transformationmatrix);
  25. // shader->loadViewMatrix(Maths::createViewMatrix(cam));
  26. glDrawElements(GL_TRIANGLES, model->getModel()->getVertexCount(), GL_UNSIGNED_INT, 0);
  27. glDisable(GL_TEXTURE_2D); // disable currently-active sampler's 2D sampling again, so it doesn't affect any future draw calls.
  28. glDisableVertexAttribArray(1);
  29. glDisableVertexAttribArray(0);
  30. glBindVertexArray(0);
  31. shader->stop();
  32.  
  33. std::cout << model->getTexture() << endl;
  34. }
  35. void ModelRenderer::cleanUp() {
  36. this->shader->cleanUp();
  37. }
  38.  
  39. void ModelRenderer ::createProjectionMatrix(float aspectRatio)
  40. {
  41. GLfloat angle = FOV / 2.0f;
  42. GLfloat radAngle = glm::radians(angle);
  43. GLfloat tanAngle = tanf(radAngle);
  44. GLfloat y_scale = (GLfloat)(1.0f / tanAngle) * aspectRatio;
  45. GLfloat x_scale = y_scale / aspectRatio;
  46. GLfloat frustum_length = FAR_PLANE - NEAR_PLANE;
  47.  
  48. glm::mat4 m = glm::mat4(1.0f);
  49.  
  50. m[0][0] = x_scale;
  51. m[1][1] = y_scale;
  52. m[2][2] = -((FAR_PLANE + NEAR_PLANE) / frustum_length);
  53. m[2][3] = -1;
  54. m[3][2] = -(2 * NEAR_PLANE * FAR_PLANE) / frustum_length;
  55. m[3][3] = 0;
  56.  
  57. //Maths::printMatrix(m, "proj1");
  58.  
  59. //glm::mat4 projectionMatrix2 = glm::perspective(FOV, aspectRatio, NEAR_PLANE, FAR_PLANE);
  60. //Maths::printMatrix(projectionMatrix2, "proj2");
  61.  
  62. projectionMatrix = m;
  63.  
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement