-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRenderer.cpp
More file actions
54 lines (45 loc) · 1.62 KB
/
Copy pathRenderer.cpp
File metadata and controls
54 lines (45 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "Renderer.h"
Renderer::Renderer(int width , int height){
this->width = width;
this->height = height;
this->wireframeMode = false; // default to filled mode
}
void Renderer::Init() {
gladLoadGL();
gladLoadGL();
glViewport(0, 0, 800, 600);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
void Renderer::BeginFrame(const glm::vec3& bg) {
glClearColor(bg.r, bg.g, bg.b, 1.0f); // alpha = 1.0
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (wireframeMode) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
else {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
}
void Renderer::Render(VAO& vao, Shader& shader, const glm::mat4& model, Camera& camera) {
shader.Activate();
shader.SetVec3("lightColor", glm::vec3(1.0f, 1.0f, 1.0f));
shader.SetVec3("lightPos", glm::vec3(0.0f, 2.0f, 0.0f));
shader.SetVec3("viewPos", camera.Position);
// Get vertex shader uniform locations in the GPU
GLuint modelLoc = glGetUniformLocation(shader.ID, "model");
GLuint viewLoc = glGetUniformLocation(shader.ID, "view");
GLuint projLoc = glGetUniformLocation(shader.ID, "projection");
//upload the model data to the modelLoc 1 4x4 matrix and don't transpose the matrix
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glm::mat4 view = camera.GetViewMatrix();
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glm::mat4 projection = camera.GetProjectionMatrix();
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
vao.Bind();
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
vao.Unbind();
}
void Renderer::EndFrame(GLFWwindow* window) {
glfwSwapBuffers(window);
glfwPollEvents();
}