basic lighting

This commit is contained in:
2025-11-27 11:37:56 +00:00
parent bb7a3bafd2
commit 25a54bab1b
6 changed files with 88 additions and 19 deletions

View File

@@ -92,6 +92,10 @@ void Shader::setMat4(const std::string &name, const glm::mat4 &mat) const {
glUniformMatrix4fv(glGetUniformLocation(ID, name.c_str()), 1, GL_FALSE, &mat[0][0]);
}
void Shader::setVec3(const std::string &name, const glm::vec3 &vec) const {
glUniform3fv(glGetUniformLocation(ID, name.c_str()), 1, &vec[0]);
}
Shader::~Shader() {
if (ID != 0) {
glDeleteProgram(ID);

View File

@@ -1,3 +1,4 @@
#define GL_GLEXT_PROTOTYPES
#include "engine/sdl.hpp"
#include "engine/opengl.hpp"
#include "engine/obj_loader.hpp"
@@ -10,7 +11,7 @@ int main(int argc, char* argv[]) {
glEnable(GL_DEPTH_TEST);
Camera camera(
glm::vec3(3.0f, 4.0f, 6.0f), // Position
glm::vec3(5.0f, 2.0f, 4.0f), // Position
glm::vec3(0.0f, 1.0f, 0.0f), // Target
glm::vec3(0.0f, 1.0f, 0.0f) // Up vector
);
@@ -22,17 +23,24 @@ int main(int argc, char* argv[]) {
100.0f // Far Plane
);
Shader shader_program("shaders/triangle.vert", "shaders/triangle.frag");
Shader cubeShader("shaders/triangle.vert", "shaders/triangle.frag");
Shader lightShader("shaders/triangle.vert", "shaders/triangle_light_source.frag");
OBJLoader objLoader("assets/models/Sci-fi_Container_Game.obj");
if (!objLoader.isLoaded()) {
OBJLoader mainObj("assets/models/Sci-fi_Container_Game.obj");
if (!mainObj.isLoaded()) {
SDL_Log("Failed to load OBJ model.");
return -1;
}
OBJLoader lightObj("assets/models/cube.obj");
if (!lightObj.isLoaded()) {
SDL_Log("Failed to load OBJ model.");
return -1;
}
VertexArrayObject VAO;
VAO.bufferVertexData(objLoader.getVertexData(), objLoader.getVertexCount() * sizeof(float) * 8);
VAO.bufferElementData(objLoader.getIndexData(), objLoader.getIndexCount() * sizeof(unsigned int));
VAO.bufferVertexData(mainObj.getVertexData(), mainObj.getVertexCount() * sizeof(float) * 8);
VAO.bufferElementData(mainObj.getIndexData(), mainObj.getIndexCount() * sizeof(unsigned int));
VAO.setAttributePointer(0, 3, GL_FLOAT, false, 8 * sizeof(float), 0); // position
VAO.setAttributePointer(1, 3, GL_FLOAT, false, 8 * sizeof(float), 3 * sizeof(float)); // normal
VAO.setAttributePointer(2, 2, GL_FLOAT, false, 8 * sizeof(float), 6 * sizeof(float)); // texCoord
@@ -40,6 +48,15 @@ int main(int argc, char* argv[]) {
Texture texture("assets/textures/Sci-fi_Container_Base_Color.png");
texture.bind(GL_TEXTURE0);
VertexArrayObject lightCubeVAO;
lightCubeVAO.bufferVertexData(lightObj.getVertexData(), lightObj.getVertexCount() * sizeof(float) * 8);
lightCubeVAO.bufferElementData(lightObj.getIndexData(), lightObj.getIndexCount() * sizeof(unsigned int));
lightCubeVAO.setAttributePointer(0, 3, GL_FLOAT, false, 8 * sizeof(float), 0); // position
lightCubeVAO.setAttributePointer(1, 3, GL_FLOAT, false, 8 * sizeof(float), 3 * sizeof(float)); // normal
lightCubeVAO.setAttributePointer(2, 2, GL_FLOAT, false, 8 * sizeof(float), 6 * sizeof(float)); // texCoord
glm::vec3 lightPos(6.0f, 3.0f, 3.0f);
window.setPosition(SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
window.showWindow();
@@ -49,13 +66,28 @@ int main(int argc, char* argv[]) {
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shader_program.use();
shader_program.setMat4("view", camera.getViewMatrix());
shader_program.setMat4("projection", camera.getProjectionMatrix());
shader_program.setMat4("model", glm::mat4(1.0f));
cubeShader.use();
cubeShader.setMat4("view", camera.getViewMatrix());
cubeShader.setMat4("projection", camera.getProjectionMatrix());
cubeShader.setMat4("model", glm::mat4(1.0f));
cubeShader.setVec3("objectColor", glm::vec3(1.0f, 0.5f, 0.31f));
cubeShader.setVec3("lightColor", glm::vec3(1.0f, 1.0f, 1.0f));
cubeShader.setVec3("lightPos", lightPos);
cubeShader.setVec3("viewPos", camera.getPosition());
VAO.draw();
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, lightPos);
model = glm::scale(model, glm::vec3(0.2f));
lightShader.use();
lightShader.setMat4("view", camera.getViewMatrix());
lightShader.setMat4("projection", camera.getProjectionMatrix());
lightShader.setMat4("model", model);
lightCubeVAO.draw();
glContext.swapBuffers();
}