2024-09-27 16:41:03 +00:00
|
|
|
#include <GL/glew.h>
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#include <GL/gl.h>
|
2024-09-27 20:29:51 +00:00
|
|
|
#include <glm/ext.hpp>
|
|
|
|
#include <glm/glm.hpp>
|
|
|
|
|
2024-09-27 17:19:35 +00:00
|
|
|
#include "shader.h"
|
2024-09-27 18:12:45 +00:00
|
|
|
#include "mesh.h"
|
2024-09-27 18:26:14 +00:00
|
|
|
#include "defaultmeshes.h"
|
2024-09-27 20:29:51 +00:00
|
|
|
#include "camera.h"
|
2024-09-27 16:41:03 +00:00
|
|
|
|
2024-09-27 16:31:14 +00:00
|
|
|
#include "renderer.h"
|
2024-09-27 16:41:03 +00:00
|
|
|
|
2024-09-27 17:19:35 +00:00
|
|
|
Shader *shader = NULL;
|
2024-09-27 20:29:51 +00:00
|
|
|
Camera camera(glm::vec3(0.0, 0.0, 3.0));
|
2024-09-27 16:31:14 +00:00
|
|
|
|
|
|
|
void renderInit(GLFWwindow* window) {
|
2024-09-27 16:41:03 +00:00
|
|
|
glViewport(0, 0, 500, 500);
|
|
|
|
|
2024-09-27 18:26:14 +00:00
|
|
|
initMeshes();
|
|
|
|
|
2024-09-27 17:19:35 +00:00
|
|
|
// Compile shader
|
2024-09-27 20:29:51 +00:00
|
|
|
shader = new Shader("assets/shaders/phong.vs", "assets/shaders/phong.fs");
|
2024-09-27 16:31:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void render(GLFWwindow* window) {
|
|
|
|
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
2024-09-27 16:41:03 +00:00
|
|
|
|
2024-09-27 17:19:35 +00:00
|
|
|
// Use shader
|
|
|
|
shader->use();
|
2024-09-27 20:29:51 +00:00
|
|
|
shader->set("objectColor", glm::vec3(1.0f, 0.5f, 0.31f));
|
|
|
|
shader->set("lightColor", glm::vec3(1.0f, 1.0f, 1.0f));
|
|
|
|
|
|
|
|
// view/projection transformations
|
|
|
|
glm::mat4 projection = glm::perspective(glm::radians(45.f), (float)1200 / (float)900, 0.1f, 100.0f);
|
|
|
|
glm::mat4 view = camera.getLookAt();
|
|
|
|
shader->set("projection", projection);
|
|
|
|
shader->set("view", view);
|
|
|
|
shader->set("material", Material {
|
|
|
|
// .ambient = glm::vec3(1.0f, 0.5f, 0.31f),
|
|
|
|
.diffuse = glm::vec3(1.0f, 0.5f, 0.31f),
|
|
|
|
.specular = glm::vec3(0.5f, 0.5f, 0.5f),
|
|
|
|
.shininess = 32.0f,
|
|
|
|
});
|
|
|
|
shader->set("sunLight", DirLight {
|
|
|
|
.direction = glm::vec3(-0.2f, -1.0f, -0.3f),
|
|
|
|
.ambient = glm::vec3(0.2f, 0.2f, 0.2f),
|
|
|
|
.diffuse = glm::vec3(0.5f, 0.5f, 0.5f),
|
|
|
|
.specular = glm::vec3(1.0f, 1.0f, 1.0f),
|
|
|
|
});
|
|
|
|
shader->set("numPointLights", 0);
|
|
|
|
// shader->set("pointLights[0]", PointLight {
|
|
|
|
// .position = lightPos,
|
|
|
|
// .ambient = glm::vec3(0.4f, 0.4f, 0.4f),
|
|
|
|
// .diffuse = glm::vec3(1.0f, 1.0f, 1.0f),
|
|
|
|
// .specular = glm::vec3(1.0f, 1.0f, 1.0f),
|
|
|
|
// .constant = 1.0,
|
|
|
|
// .linear = 0.9,
|
|
|
|
// .quadratic = 0.32,
|
|
|
|
// });
|
|
|
|
|
|
|
|
// Pre-calculate the normal matrix for the shader
|
|
|
|
|
|
|
|
// Pass in the camera position
|
|
|
|
shader->set("viewPos", camera.cameraPos);
|
|
|
|
|
|
|
|
glm::mat4 model = glm::mat4(1.0f);
|
|
|
|
model = glm::translate(model, glm::vec3( 0.0f, 0.0f, 0.0f));
|
|
|
|
// float angle = 20.0f * i;
|
|
|
|
// model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f));
|
|
|
|
shader->set("model", model);
|
|
|
|
glm::mat3 normalMatrix = glm::mat3(glm::transpose(glm::inverse(model)));
|
|
|
|
shader->set("normalMatrix", normalMatrix);
|
2024-09-27 17:19:35 +00:00
|
|
|
|
2024-09-27 18:26:14 +00:00
|
|
|
CUBE_MESH->bind();
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 36);
|
2024-09-27 16:31:14 +00:00
|
|
|
}
|