lighting setup

This commit is contained in:
Claire Schwarzer 2026-02-07 00:01:56 +01:00
parent ccc1c76762
commit 16b674efc1
4 changed files with 85 additions and 90 deletions

View file

@ -8,5 +8,9 @@ uniform sampler2D decal;
void main() void main()
{ {
FragColor = mix(texture(ourTexture, TexCoord), texture(decal, TexCoord), 0.2f); vec4 tex1 = texture(ourTexture, TexCoord);
vec4 tex2 = texture(decal, TexCoord);
vec4 textures = mix(tex1, tex2, 0.2f);
FragColor = textures;
} }

View file

@ -1,8 +1,9 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor; layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoord; layout (location = 2) in vec2 aTexCoord;
out vec3 Normal;
out vec2 TexCoord; out vec2 TexCoord;
uniform mat4 model; uniform mat4 model;
@ -11,7 +12,8 @@ uniform mat4 projection;
void main() void main()
{ {
//gl_Position = vec4(aPos, 1.0);
gl_Position = projection * view * model * vec4(aPos, 1.0f); gl_Position = projection * view * model * vec4(aPos, 1.0f);
TexCoord = vec2(aTexCoord.x, aTexCoord.y);
Normal = aNormal;
TexCoord = aTexCoord;
} }

View file

@ -5,9 +5,8 @@
struct Vertex { struct Vertex {
float x, y, z; // position float x, y, z; // position
float r, g, b; // color float nX, nY, zY; // normal
float u, v; // texcoord float u, v; // texcoord
// = 8 floats = 32 bytes
}; };
class Mesh class Mesh
@ -28,10 +27,6 @@ public:
glGenBuffers(1, &VBO); glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO); glGenBuffers(1, &EBO);
std::cout << VAO << std::endl;
std::cout << VBO << std::endl;
std::cout << EBO << std::endl;
glBindVertexArray(VAO); glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO);
@ -40,13 +35,15 @@ public:
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW);
// position attribute // Position
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, x)); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, x));
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
// color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, r)); // Normals
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, nX));
glEnableVertexAttribArray(1); glEnableVertexAttribArray(1);
// texture coord attribute
// Texcoords
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, u)); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, u));
glEnableVertexAttribArray(2); glEnableVertexAttribArray(2);
} }
@ -58,9 +55,9 @@ public:
void Draw() const void Draw() const
{ {
glDrawArrays(GL_TRIANGLES, 0, 36); //glDrawArrays(GL_TRIANGLES, 0, 36);
//glBindVertexArray(VAO); glBindVertexArray(VAO);
//glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(indexCount), glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(indexCount),
// GL_UNSIGNED_INT, nullptr); GL_UNSIGNED_INT, 0);
} }
}; };

128
main.cpp
View file

@ -76,56 +76,72 @@ int main(int argc, char* argv[])
// Load Shader // Load Shader
Shader shaderTest("assets/shaders/basicVertex.vert", "assets/shaders/basicFragment.frag"); Shader shaderTest("assets/shaders/basicVertex.vert", "assets/shaders/basicFragment.frag");
std::vector<Vertex> vertices = { std::vector<Vertex> vertices = {
{ -0.5f, -0.5f, -0.5f, 1.0f, 0.2f, 0.2f, 0.0f, 0.0f }, // Front face (-z) normal: (0, 0, -1)
{ 0.5f, -0.5f, -0.5f, 1.0f, 0.2f, 0.2f, 1.0f, 0.0f }, { -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f },
{ 0.5f, 0.5f, -0.5f, 1.0f, 0.2f, 0.2f, 1.0f, 1.0f }, { 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f },
{ 0.5f, 0.5f, -0.5f, 1.0f, 0.2f, 0.2f, 1.0f, 1.0f }, { 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f },
{ -0.5f, 0.5f, -0.5f, 1.0f, 0.2f, 0.2f, 0.0f, 1.0f }, { 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f },
{ -0.5f, -0.5f, -0.5f, 1.0f, 0.2f, 0.2f, 0.0f, 0.0f }, { -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f },
{ -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f },
// Back face (+z) green-ish // Back face (+z) normal: (0, 0, +1)
{ -0.5f, -0.5f, 0.5f, 0.2f, 1.0f, 0.2f, 0.0f, 0.0f }, { -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
{ 0.5f, -0.5f, 0.5f, 0.2f, 1.0f, 0.2f, 1.0f, 0.0f }, { 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f },
{ 0.5f, 0.5f, 0.5f, 0.2f, 1.0f, 0.2f, 1.0f, 1.0f }, { 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f },
{ 0.5f, 0.5f, 0.5f, 0.2f, 1.0f, 0.2f, 1.0f, 1.0f }, { 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f },
{ -0.5f, 0.5f, 0.5f, 0.2f, 1.0f, 0.2f, 0.0f, 1.0f }, { -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f },
{ -0.5f, -0.5f, 0.5f, 0.2f, 1.0f, 0.2f, 0.0f, 0.0f }, { -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
// Left face (-x) blue-ish // Left face (-x) normal: (-1, 0, 0)
{ -0.5f, 0.5f, 0.5f, 0.2f, 0.2f, 1.0f, 1.0f, 0.0f }, { -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f },
{ -0.5f, 0.5f, -0.5f, 0.2f, 0.2f, 1.0f, 1.0f, 1.0f }, { -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f },
{ -0.5f, -0.5f, -0.5f, 0.2f, 0.2f, 1.0f, 0.0f, 1.0f }, { -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f },
{ -0.5f, -0.5f, -0.5f, 0.2f, 0.2f, 1.0f, 0.0f, 1.0f }, { -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f },
{ -0.5f, -0.5f, 0.5f, 0.2f, 0.2f, 1.0f, 0.0f, 0.0f }, { -0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
{ -0.5f, 0.5f, 0.5f, 0.2f, 0.2f, 1.0f, 1.0f, 0.0f }, { -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f },
// Right face (+x) yellow-ish // Right face (+x) normal: (+1, 0, 0)
{ 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.2f, 1.0f, 0.0f }, { 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0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0.0f, -1.0f, 0.0f, 0.0f, 0.0f },
{ -0.5f, -0.5f, -0.5f, 1.0f, 0.2f, 1.0f, 0.0f, 1.0f }, { -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f },
// Top face (+y) cyan-ish // Top face (+y) normal: (0, +1, 0)
{ -0.5f, 0.5f, -0.5f, 0.2f, 1.0f, 1.0f, 0.0f, 1.0f }, { -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f },
{ 0.5f, 0.5f, -0.5f, 0.2f, 1.0f, 1.0f, 1.0f, 1.0f }, { 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f },
{ 0.5f, 0.5f, 0.5f, 0.2f, 1.0f, 1.0f, 1.0f, 0.0f }, { 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f },
{ 0.5f, 0.5f, 0.5f, 0.2f, 1.0f, 1.0f, 1.0f, 0.0f }, { 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f },
{ -0.5f, 0.5f, 0.5f, 0.2f, 1.0f, 1.0f, 0.0f, 0.0f }, { -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f },
{ -0.5f, 0.5f, -0.5f, 0.2f, 1.0f, 1.0f, 0.0f, 1.0f } { -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f }
}; };
std::vector<unsigned int> indices = { std::vector<unsigned int> indices = {
0, 1, 3, // Front (-z) ── outer view CCW
1, 2, 3 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
}; };
Mesh testMesh(vertices, indices); Mesh testMesh(vertices, indices);
@ -137,20 +153,6 @@ int main(int argc, char* argv[])
shaderTest.setInt("ourTexture", 0); shaderTest.setInt("ourTexture", 0);
shaderTest.setInt("decal", 1); shaderTest.setInt("decal", 1);
glm::vec3 cubePositions[] = {
glm::vec3( 0.0f, 0.0f, 0.0f),
glm::vec3( 2.0f, 5.0f, -15.0f),
glm::vec3(-1.5f, -2.2f, -2.5f),
glm::vec3(-3.8f, -2.0f, -12.3f),
glm::vec3( 2.4f, -0.4f, -3.5f),
glm::vec3(-1.7f, 3.0f, -7.5f),
glm::vec3( 1.3f, -2.0f, -2.5f),
glm::vec3( 1.5f, 2.0f, -2.5f),
glm::vec3( 1.5f, 0.2f, -1.5f),
glm::vec3(-1.3f, 1.0f, -1.5f)
};
// Render Loop // Render Loop
float lastUpdate = 0; float lastUpdate = 0;
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
@ -190,20 +192,10 @@ int main(int argc, char* argv[])
glm::mat4 view = camera.GetViewMatrix(); glm::mat4 view = camera.GetViewMatrix();
shaderTest.setMat4("view", view); shaderTest.setMat4("view", view);
//testMesh.Draw();
glBindVertexArray(testMesh.VAO);
for(unsigned int i = 0; i < 10; i++)
{
glm::mat4 model = glm::mat4(1.0f); glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, cubePositions[i]);
float angle = 20.0f * i;
model = glm::rotate(model, (float)glfwGetTime() * glm::radians(50.0f), glm::vec3(0.5f, 1.0f, 0.0f));
shaderTest.setMat4("model", model); shaderTest.setMat4("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36); testMesh.Draw();
}
// Call events & swap buffers // Call events & swap buffers
glfwSwapBuffers(window); glfwSwapBuffers(window);