Correct for non uniform scale in lighting

This commit is contained in:
Dane Johnson 2021-01-23 12:33:10 -06:00
parent 9056127f90
commit cff580b7f1
20 changed files with 21 additions and 11 deletions

View File

@ -53,6 +53,10 @@ void Shader::UpdateProjection(Matrix projection) {
glUniformMatrix4fv(glGetUniformLocation(id, "PROJECTION"), 1, GL_FALSE, glm::value_ptr(projection));
}
void Shader::UpdateNormal(glm::mat3 normal) {
glUniformMatrix3fv(glGetUniformLocation(id, "NORMAL"), 1, GL_FALSE, glm::value_ptr(normal));
}
void Shader::UpdateMaterial(Material material) {
glUniform1i(glGetUniformLocation(id, "material.usesTex"), (int) material.usesTex);
if (material.usesTex) {

View File

@ -16,6 +16,7 @@ public:
void UpdateView(Matrix view);
void UpdateModel(Matrix model);
void UpdateProjection(Matrix projection);
void UpdateNormal(glm::mat3 normal);
void UpdateMaterial(Material material);

View File

@ -74,6 +74,9 @@ Skybox *Skybox::FromFiles(const char *right, const char* left, const char* top,
const char* files[] = {right, left, top, bottom, front, back};
for (int i = 0; i < 6; i++) {
data = stbi_load(files[i], &width, &height, &nrChannels, 3);
if (!data) {
Util::Die("Could not load skybox image ", files[i]);
}
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);

View File

@ -46,6 +46,7 @@ void render(Node *curr, Shader *shader, Matrix model) {
model = glm::translate(model, spatial->transform.position);
model = glm::scale(model, spatial->transform.scale);
shader->UpdateModel(model);
shader->UpdateNormal(glm::mat3(glm::transpose(glm::inverse(model))));
}
Mesh *mesh = dynamic_cast<Mesh*>(curr);
mesh->Draw(shader);

View File

@ -37,12 +37,12 @@ function init()
couch.Node.GetRoot().children:Append(light)
skybox = couch.Skybox.FromFiles(
"skybox/right.jpg",
"skybox/left.jpg",
"skybox/top.jpg",
"skybox/bottom.jpg",
"skybox/front.jpg",
"skybox/back.jpg"
"skybox/px.png",
"skybox/nx.png",
"skybox/py.png",
"skybox/ny.png",
"skybox/pz.png",
"skybox/nz.png"
)
couch.Node.GetRoot().children:Append(skybox)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 723 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 274 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 462 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 588 KiB

BIN
demo/skybox/nx.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 106 KiB

BIN
demo/skybox/ny.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

BIN
demo/skybox/nz.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 106 KiB

BIN
demo/skybox/px.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 106 KiB

BIN
demo/skybox/py.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 107 KiB

BIN
demo/skybox/pz.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 112 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 525 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 338 KiB

View File

@ -58,3 +58,4 @@ calling it from the lua file, so stubs aren't necessary to prevent a crash
I'm a bit happier with the system now, I think it's time I started the lighting.
We're going to use a pretty basic Phong lighting system, with
Flat and Garourd shaders available (This was all that was available on the PSX!)

View File

@ -1,7 +1,6 @@
#version 330 core
noperspective in vec2 UV;
in vec3 NORMAL;
in vec3 AMBIENT;
in vec3 DIFFUSE;

View File

@ -7,9 +7,9 @@ layout (location = 2) in vec2 uv;
uniform mat4 MODEL;
uniform mat4 VIEW;
uniform mat4 PROJECTION;
uniform mat3 NORMAL;
noperspective out vec2 UV; // PSX use affine texture mapping
out vec3 NORMAL;
out vec3 AMBIENT;
out vec3 DIFFUSE;
@ -46,18 +46,19 @@ void main() {
UV = uv;
NORMAL = (VIEW * MODEL * vec4(normal, 0.0)).xyz;
vec3 my_normal = (VIEW * MODEL * vec4(normal, 0.0)).xyz;
my_normal *= NORMAL;
// Flat shading, we compute light per vertex
AMBIENT = directionalLight.ambient * directionalLight.color * material.ambient;
vec3 direction = -(VIEW * vec4(directionalLight.direction, 0.0)).xyz;
float diff = dot(normalize(direction), normalize(NORMAL));
float diff = dot(normalize(direction), normalize(my_normal));
diff = max(diff, 0.0);
DIFFUSE = directionalLight.diffuse * diff * directionalLight.color * material.diffuse;
vec3 viewDir = normalize((VIEW * MODEL * vec4(pos, 1.0)).xyz);
vec3 reflectionDir = reflect(normalize(direction), normalize(NORMAL));
vec3 reflectionDir = reflect(normalize(direction), normalize(my_normal));
float spec = dot(viewDir, reflectionDir);
spec = max(spec, 0.0);
spec = pow(spec, material.shininess);