Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ModelRenderer.h"
- ModelRenderer::ModelRenderer(float aspectRatio) {
- shader = new ModelShader();
- createProjectionMatrix(aspectRatio);
- shader->start();
- shader->loadProjectionMatrix(projectionMatrix);
- shader->stop();
- }
- void ModelRenderer::render(TexturedModel* model, Camera cam) {
- shader->start();
- glEnable(GL_DEPTH_TEST);
- RawModel* rawmodel = model->getModel();
- glBindVertexArray(rawmodel->getVaoID());
- glEnableVertexAttribArray(0);
- glEnableVertexAttribArray(1);
- glActiveTexture(GL_TEXTURE0);
- glEnable(GL_TEXTURE_2D); // the currently-active sampler should actually use the bound 2D texture.
- glBindTexture(GL_TEXTURE_2D, model->getTexture());
- glm::vec3 pos(0.0f);
- glm::mat4 transformationmatrix = Maths::createTransformationMatrix(pos, 0.0f, 0.0f, 0.0f, 1.0f);
- shader->loadTransformationMatrix(transformationmatrix);
- // shader->loadViewMatrix(Maths::createViewMatrix(cam));
- glDrawElements(GL_TRIANGLES, model->getModel()->getVertexCount(), GL_UNSIGNED_INT, 0);
- glDisable(GL_TEXTURE_2D); // disable currently-active sampler's 2D sampling again, so it doesn't affect any future draw calls.
- glDisableVertexAttribArray(1);
- glDisableVertexAttribArray(0);
- glBindVertexArray(0);
- shader->stop();
- std::cout << model->getTexture() << endl;
- }
- void ModelRenderer::cleanUp() {
- this->shader->cleanUp();
- }
- void ModelRenderer ::createProjectionMatrix(float aspectRatio)
- {
- GLfloat angle = FOV / 2.0f;
- GLfloat radAngle = glm::radians(angle);
- GLfloat tanAngle = tanf(radAngle);
- GLfloat y_scale = (GLfloat)(1.0f / tanAngle) * aspectRatio;
- GLfloat x_scale = y_scale / aspectRatio;
- GLfloat frustum_length = FAR_PLANE - NEAR_PLANE;
- glm::mat4 m = glm::mat4(1.0f);
- m[0][0] = x_scale;
- m[1][1] = y_scale;
- m[2][2] = -((FAR_PLANE + NEAR_PLANE) / frustum_length);
- m[2][3] = -1;
- m[3][2] = -(2 * NEAR_PLANE * FAR_PLANE) / frustum_length;
- m[3][3] = 0;
- //Maths::printMatrix(m, "proj1");
- //glm::mat4 projectionMatrix2 = glm::perspective(FOV, aspectRatio, NEAR_PLANE, FAR_PLANE);
- //Maths::printMatrix(projectionMatrix2, "proj2");
- projectionMatrix = m;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement