more lighting
This commit is contained in:
parent
16b674efc1
commit
79fa59c3aa
5 changed files with 89 additions and 27 deletions
|
|
@ -1,16 +1,56 @@
|
|||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
in vec3 FragPos;
|
||||
in vec3 Normal;
|
||||
in vec2 TexCoord;
|
||||
|
||||
uniform sampler2D ourTexture;
|
||||
uniform sampler2D decal;
|
||||
|
||||
void main()
|
||||
{
|
||||
uniform vec3 lightPos;
|
||||
uniform vec3 lightColor;
|
||||
uniform vec3 viewPos;
|
||||
|
||||
vec4 calcTextures(){
|
||||
vec4 tex1 = texture(ourTexture, TexCoord);
|
||||
vec4 tex2 = texture(decal, TexCoord);
|
||||
vec4 textures = mix(tex1, tex2, 0.2f);
|
||||
|
||||
FragColor = textures;
|
||||
return textures;
|
||||
}
|
||||
|
||||
vec4 calcDirectLight(norm, viewDir){
|
||||
// Ambient Lighting
|
||||
float ambientStrength = 0.1;
|
||||
vec3 ambient = ambientStrength * lightColor;
|
||||
|
||||
// Diffuse Lighting
|
||||
vec3 lightDir = normalize(lightPos - FragPos);
|
||||
|
||||
float diff = max(dot(norm, lightDir), 0.0);
|
||||
vec3 diffuse = diff * lightColor;
|
||||
|
||||
// Specular Lighting
|
||||
float specularStrength = 0.5;
|
||||
|
||||
vec3 reflectDir = reflect(-lightDir, norm);
|
||||
|
||||
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
|
||||
vec3 specular = specularStrength * spec * lightColor;
|
||||
|
||||
vec4 result = vec4(ambient.rgb + diffuse.rgb + specular.rgb, 1.0f);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 norm = normalize(Normal);
|
||||
vec3 viewDir = normalize(viewPos - FragPos);
|
||||
|
||||
vec4 result = calcTextures();
|
||||
result += calcDirectLight(norm, viewDir);
|
||||
|
||||
FragColor = result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ layout (location = 0) in vec3 aPos;
|
|||
layout (location = 1) in vec3 aNormal;
|
||||
layout (location = 2) in vec2 aTexCoord;
|
||||
|
||||
out vec3 FragPos;
|
||||
out vec3 Normal;
|
||||
out vec2 TexCoord;
|
||||
|
||||
|
|
@ -12,8 +13,9 @@ uniform mat4 projection;
|
|||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * view * model * vec4(aPos, 1.0f);
|
||||
|
||||
Normal = aNormal;
|
||||
FragPos = vec3(model * vec4(aPos, 1.0));
|
||||
Normal = mat3(transpose(inverse(model))) * aNormal;
|
||||
TexCoord = aTexCoord;
|
||||
|
||||
gl_Position = projection * view * model * vec4(aPos, 1.0f);
|
||||
}
|
||||
|
|
|
|||
6
assets/shaders/lightFragment.frag
Normal file
6
assets/shaders/lightFragment.frag
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
|
||||
void main() {
|
||||
FragColor = vec4(1.0);
|
||||
}
|
||||
|
|
@ -18,9 +18,7 @@ public:
|
|||
{
|
||||
std::string vertexCode, fragmentCode;
|
||||
std::ifstream vertexFile, fragmentFile;
|
||||
|
||||
std::cout << vertexPath << std::endl;
|
||||
|
||||
|
||||
vertexFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
|
||||
fragmentFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
|
||||
|
||||
|
|
|
|||
52
main.cpp
52
main.cpp
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue