mirror of
https://github.com/JoeyDeVries/LearnOpenGL.git
synced 2026-01-02 04:37:54 +08:00
Fixed normal calculation in PBR shaders
This commit is contained in:
@@ -10,12 +10,13 @@ out vec3 Normal;
|
|||||||
uniform mat4 projection;
|
uniform mat4 projection;
|
||||||
uniform mat4 view;
|
uniform mat4 view;
|
||||||
uniform mat4 model;
|
uniform mat4 model;
|
||||||
|
uniform mat3 normalMatrix;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
TexCoords = aTexCoords;
|
TexCoords = aTexCoords;
|
||||||
WorldPos = vec3(model * vec4(aPos, 1.0));
|
WorldPos = vec3(model * vec4(aPos, 1.0));
|
||||||
Normal = mat3(model) * aNormal;
|
Normal = normalMatrix * aNormal;
|
||||||
|
|
||||||
gl_Position = projection * view * vec4(WorldPos, 1.0);
|
gl_Position = projection * view * vec4(WorldPos, 1.0);
|
||||||
}
|
}
|
||||||
@@ -151,6 +151,7 @@ int main()
|
|||||||
0.0f
|
0.0f
|
||||||
));
|
));
|
||||||
shader.setMat4("model", model);
|
shader.setMat4("model", model);
|
||||||
|
shader.setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
|
||||||
renderSphere();
|
renderSphere();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -169,6 +170,7 @@ int main()
|
|||||||
model = glm::translate(model, newPos);
|
model = glm::translate(model, newPos);
|
||||||
model = glm::scale(model, glm::vec3(0.5f));
|
model = glm::scale(model, glm::vec3(0.5f));
|
||||||
shader.setMat4("model", model);
|
shader.setMat4("model", model);
|
||||||
|
shader.setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
|
||||||
renderSphere();
|
renderSphere();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,12 +10,13 @@ out vec3 Normal;
|
|||||||
uniform mat4 projection;
|
uniform mat4 projection;
|
||||||
uniform mat4 view;
|
uniform mat4 view;
|
||||||
uniform mat4 model;
|
uniform mat4 model;
|
||||||
|
uniform mat3 normalMatrix;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
TexCoords = aTexCoords;
|
TexCoords = aTexCoords;
|
||||||
WorldPos = vec3(model * vec4(aPos, 1.0));
|
WorldPos = vec3(model * vec4(aPos, 1.0));
|
||||||
Normal = mat3(model) * aNormal;
|
Normal = normalMatrix * aNormal;
|
||||||
|
|
||||||
gl_Position = projection * view * vec4(WorldPos, 1.0);
|
gl_Position = projection * view * vec4(WorldPos, 1.0);
|
||||||
}
|
}
|
||||||
@@ -162,6 +162,7 @@ int main()
|
|||||||
0.0f
|
0.0f
|
||||||
));
|
));
|
||||||
shader.setMat4("model", model);
|
shader.setMat4("model", model);
|
||||||
|
shader.setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
|
||||||
renderSphere();
|
renderSphere();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -180,6 +181,7 @@ int main()
|
|||||||
model = glm::translate(model, newPos);
|
model = glm::translate(model, newPos);
|
||||||
model = glm::scale(model, glm::vec3(0.5f));
|
model = glm::scale(model, glm::vec3(0.5f));
|
||||||
shader.setMat4("model", model);
|
shader.setMat4("model", model);
|
||||||
|
shader.setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
|
||||||
renderSphere();
|
renderSphere();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,12 +10,13 @@ out vec3 Normal;
|
|||||||
uniform mat4 projection;
|
uniform mat4 projection;
|
||||||
uniform mat4 view;
|
uniform mat4 view;
|
||||||
uniform mat4 model;
|
uniform mat4 model;
|
||||||
|
uniform mat3 normalMatrix;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
TexCoords = aTexCoords;
|
TexCoords = aTexCoords;
|
||||||
WorldPos = vec3(model * vec4(aPos, 1.0));
|
WorldPos = vec3(model * vec4(aPos, 1.0));
|
||||||
Normal = mat3(model) * aNormal;
|
Normal = normalMatrix * aNormal;
|
||||||
|
|
||||||
gl_Position = projection * view * vec4(WorldPos, 1.0);
|
gl_Position = projection * view * vec4(WorldPos, 1.0);
|
||||||
}
|
}
|
||||||
@@ -252,6 +252,7 @@ int main()
|
|||||||
-2.0f
|
-2.0f
|
||||||
));
|
));
|
||||||
pbrShader.setMat4("model", model);
|
pbrShader.setMat4("model", model);
|
||||||
|
pbrShader.setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
|
||||||
renderSphere();
|
renderSphere();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -271,6 +272,7 @@ int main()
|
|||||||
model = glm::translate(model, newPos);
|
model = glm::translate(model, newPos);
|
||||||
model = glm::scale(model, glm::vec3(0.5f));
|
model = glm::scale(model, glm::vec3(0.5f));
|
||||||
pbrShader.setMat4("model", model);
|
pbrShader.setMat4("model", model);
|
||||||
|
pbrShader.setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
|
||||||
renderSphere();
|
renderSphere();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,12 +10,13 @@ out vec3 Normal;
|
|||||||
uniform mat4 projection;
|
uniform mat4 projection;
|
||||||
uniform mat4 view;
|
uniform mat4 view;
|
||||||
uniform mat4 model;
|
uniform mat4 model;
|
||||||
|
uniform mat3 normalMatrix;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
TexCoords = aTexCoords;
|
TexCoords = aTexCoords;
|
||||||
WorldPos = vec3(model * vec4(aPos, 1.0));
|
WorldPos = vec3(model * vec4(aPos, 1.0));
|
||||||
Normal = mat3(model) * aNormal;
|
Normal = normalMatrix * aNormal;
|
||||||
|
|
||||||
gl_Position = projection * view * vec4(WorldPos, 1.0);
|
gl_Position = projection * view * vec4(WorldPos, 1.0);
|
||||||
}
|
}
|
||||||
@@ -297,6 +297,7 @@ int main()
|
|||||||
-2.0f
|
-2.0f
|
||||||
));
|
));
|
||||||
pbrShader.setMat4("model", model);
|
pbrShader.setMat4("model", model);
|
||||||
|
pbrShader.setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
|
||||||
renderSphere();
|
renderSphere();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -316,6 +317,7 @@ int main()
|
|||||||
model = glm::translate(model, newPos);
|
model = glm::translate(model, newPos);
|
||||||
model = glm::scale(model, glm::vec3(0.5f));
|
model = glm::scale(model, glm::vec3(0.5f));
|
||||||
pbrShader.setMat4("model", model);
|
pbrShader.setMat4("model", model);
|
||||||
|
pbrShader.setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
|
||||||
renderSphere();
|
renderSphere();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,12 +10,13 @@ out vec3 Normal;
|
|||||||
uniform mat4 projection;
|
uniform mat4 projection;
|
||||||
uniform mat4 view;
|
uniform mat4 view;
|
||||||
uniform mat4 model;
|
uniform mat4 model;
|
||||||
|
uniform mat3 normalMatrix;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
TexCoords = aTexCoords;
|
TexCoords = aTexCoords;
|
||||||
WorldPos = vec3(model * vec4(aPos, 1.0));
|
WorldPos = vec3(model * vec4(aPos, 1.0));
|
||||||
Normal = mat3(model) * aNormal;
|
Normal = normalMatrix * aNormal;
|
||||||
|
|
||||||
gl_Position = projection * view * vec4(WorldPos, 1.0);
|
gl_Position = projection * view * vec4(WorldPos, 1.0);
|
||||||
}
|
}
|
||||||
@@ -389,6 +389,7 @@ int main()
|
|||||||
-2.0f
|
-2.0f
|
||||||
));
|
));
|
||||||
pbrShader.setMat4("model", model);
|
pbrShader.setMat4("model", model);
|
||||||
|
pbrShader.setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
|
||||||
renderSphere();
|
renderSphere();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -408,6 +409,7 @@ int main()
|
|||||||
model = glm::translate(model, newPos);
|
model = glm::translate(model, newPos);
|
||||||
model = glm::scale(model, glm::vec3(0.5f));
|
model = glm::scale(model, glm::vec3(0.5f));
|
||||||
pbrShader.setMat4("model", model);
|
pbrShader.setMat4("model", model);
|
||||||
|
pbrShader.setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
|
||||||
renderSphere();
|
renderSphere();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,12 +10,13 @@ out vec3 Normal;
|
|||||||
uniform mat4 projection;
|
uniform mat4 projection;
|
||||||
uniform mat4 view;
|
uniform mat4 view;
|
||||||
uniform mat4 model;
|
uniform mat4 model;
|
||||||
|
uniform mat3 normalMatrix;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
TexCoords = aTexCoords;
|
TexCoords = aTexCoords;
|
||||||
WorldPos = vec3(model * vec4(aPos, 1.0));
|
WorldPos = vec3(model * vec4(aPos, 1.0));
|
||||||
Normal = mat3(model) * aNormal;
|
Normal = normalMatrix * aNormal;
|
||||||
|
|
||||||
gl_Position = projection * view * vec4(WorldPos, 1.0);
|
gl_Position = projection * view * vec4(WorldPos, 1.0);
|
||||||
}
|
}
|
||||||
@@ -424,6 +424,7 @@ int main()
|
|||||||
model = glm::mat4(1.0f);
|
model = glm::mat4(1.0f);
|
||||||
model = glm::translate(model, glm::vec3(-5.0, 0.0, 2.0));
|
model = glm::translate(model, glm::vec3(-5.0, 0.0, 2.0));
|
||||||
pbrShader.setMat4("model", model);
|
pbrShader.setMat4("model", model);
|
||||||
|
pbrShader.setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
|
||||||
renderSphere();
|
renderSphere();
|
||||||
|
|
||||||
// gold
|
// gold
|
||||||
@@ -441,6 +442,7 @@ int main()
|
|||||||
model = glm::mat4(1.0f);
|
model = glm::mat4(1.0f);
|
||||||
model = glm::translate(model, glm::vec3(-3.0, 0.0, 2.0));
|
model = glm::translate(model, glm::vec3(-3.0, 0.0, 2.0));
|
||||||
pbrShader.setMat4("model", model);
|
pbrShader.setMat4("model", model);
|
||||||
|
pbrShader.setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
|
||||||
renderSphere();
|
renderSphere();
|
||||||
|
|
||||||
// grass
|
// grass
|
||||||
@@ -458,6 +460,7 @@ int main()
|
|||||||
model = glm::mat4(1.0f);
|
model = glm::mat4(1.0f);
|
||||||
model = glm::translate(model, glm::vec3(-1.0, 0.0, 2.0));
|
model = glm::translate(model, glm::vec3(-1.0, 0.0, 2.0));
|
||||||
pbrShader.setMat4("model", model);
|
pbrShader.setMat4("model", model);
|
||||||
|
pbrShader.setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
|
||||||
renderSphere();
|
renderSphere();
|
||||||
|
|
||||||
// plastic
|
// plastic
|
||||||
@@ -475,6 +478,7 @@ int main()
|
|||||||
model = glm::mat4(1.0f);
|
model = glm::mat4(1.0f);
|
||||||
model = glm::translate(model, glm::vec3(1.0, 0.0, 2.0));
|
model = glm::translate(model, glm::vec3(1.0, 0.0, 2.0));
|
||||||
pbrShader.setMat4("model", model);
|
pbrShader.setMat4("model", model);
|
||||||
|
pbrShader.setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
|
||||||
renderSphere();
|
renderSphere();
|
||||||
|
|
||||||
// wall
|
// wall
|
||||||
@@ -492,6 +496,7 @@ int main()
|
|||||||
model = glm::mat4(1.0f);
|
model = glm::mat4(1.0f);
|
||||||
model = glm::translate(model, glm::vec3(3.0, 0.0, 2.0));
|
model = glm::translate(model, glm::vec3(3.0, 0.0, 2.0));
|
||||||
pbrShader.setMat4("model", model);
|
pbrShader.setMat4("model", model);
|
||||||
|
pbrShader.setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
|
||||||
renderSphere();
|
renderSphere();
|
||||||
|
|
||||||
// render light source (simply re-render sphere at light positions)
|
// render light source (simply re-render sphere at light positions)
|
||||||
@@ -508,6 +513,7 @@ int main()
|
|||||||
model = glm::translate(model, newPos);
|
model = glm::translate(model, newPos);
|
||||||
model = glm::scale(model, glm::vec3(0.5f));
|
model = glm::scale(model, glm::vec3(0.5f));
|
||||||
pbrShader.setMat4("model", model);
|
pbrShader.setMat4("model", model);
|
||||||
|
pbrShader.setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
|
||||||
renderSphere();
|
renderSphere();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user