more lighting

This commit is contained in:
Claire Schwarzer 2026-02-08 04:48:42 +01:00
parent 16b674efc1
commit 79fa59c3aa
5 changed files with 89 additions and 27 deletions

View file

@ -74,7 +74,6 @@ int main(int argc, char* argv[])
*/
// Load Shader
Shader shaderTest("assets/shaders/basicVertex.vert", "assets/shaders/basicFragment.frag");
std::vector<Vertex> vertices = {
// Front face (-z) normal: (0, 0, -1)
{ -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f },
@ -125,23 +124,12 @@ int main(int argc, char* argv[])
{ -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f }
};
std::vector<unsigned int> indices = {
// Front (-z) ── outer view CCW
0, 1, 2, 0, 2, 5,
// Back (+z) ── outer view CCW
6, 7, 8, 6, 8, 11,
// Left (-x) ── outer view CCW
12, 13, 14, 12, 14, 17,
// Right (+x) ── outer view CCW
18, 19, 20, 18, 20, 23,
// Bottom (-y) ── outer view CCW
24, 25, 26, 24, 26, 29,
// Top (+y) ── outer view CCW
30, 31, 32, 30, 32, 35
0, 1, 2, 2, 4, 0,
6, 7, 8, 8, 10, 6,
12, 13, 14, 14, 16, 12,
18, 19, 20, 20, 22, 18,
24, 25, 26, 26, 28, 24,
30, 31, 32, 32, 34, 30
};
Mesh testMesh(vertices, indices);
@ -149,12 +137,18 @@ int main(int argc, char* argv[])
Texture texture1("assets/wall.jpg");
Texture texture2("assets/awesomeface.png", true);
Shader shaderTest("assets/shaders/basicVertex.vert", "assets/shaders/basicFragment.frag");
shaderTest.Use(); // don't forget to activate/use the shader before setting uniforms!
shaderTest.setInt("ourTexture", 0);
shaderTest.setInt("decal", 1);
Shader lightingShader("assets/shaders/basicVertex.vert", "assets/shaders/lightFragment.frag");
Mesh lightMesh(vertices, indices);
// Render Loop
float lastUpdate = 0;
glm::vec3 lightPos = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 orbitCenter = glm::vec3(0.0f, 0.0f, 0.0f);
while (!glfwWindowShouldClose(window))
{
// per-frame time logic
@ -183,6 +177,9 @@ int main(int argc, char* argv[])
glBindTexture(GL_TEXTURE_2D, texture2.ID);
shaderTest.Use();
shaderTest.setVec3("lightColor", 1.0f, 1.0f, 1.0f);
shaderTest.setVec3("lightPos", orbitCenter);
shaderTest.setVec3("viewPos", camera.position);
// pass projection matrix to shader (note that in this case it could change every frame)
glm::mat4 projection = glm::perspective(glm::radians(camera.zoom), (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT, 0.1f, 100.0f);
@ -197,6 +194,25 @@ int main(int argc, char* argv[])
testMesh.Draw();
lightingShader.Use();
lightingShader.setMat4("projection", projection);
lightingShader.setMat4("view", view);
model = glm::mat4(1.0f);
glm::vec3 offset = glm::vec3(
2.0f * cos(glfwGetTime() * 1.0f),
0.0f,
sin(glfwGetTime() * 1.0f) * 2.0f
);
orbitCenter = lightPos + offset;
model = glm::translate(model, orbitCenter);
model = glm::scale(model, glm::vec3(0.2f)); // a smaller cube
lightingShader.setMat4("model", model);
lightMesh.Draw();
// Call events & swap buffers
glfwSwapBuffers(window);
glfwPollEvents();