diff --git a/core/Shaders/Shader.cpp b/core/Shaders/Shader.cpp index 934617e..705b5b7 100644 --- a/core/Shaders/Shader.cpp +++ b/core/Shaders/Shader.cpp @@ -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) { diff --git a/core/Shaders/Shader.h b/core/Shaders/Shader.h index 0e5e9b8..1048565 100644 --- a/core/Shaders/Shader.h +++ b/core/Shaders/Shader.h @@ -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); diff --git a/core/Skybox.cpp b/core/Skybox.cpp index a04894e..10edf16 100644 --- a/core/Skybox.cpp +++ b/core/Skybox.cpp @@ -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); diff --git a/core/couch.cpp b/core/couch.cpp index 0dea082..98ebc36 100644 --- a/core/couch.cpp +++ b/core/couch.cpp @@ -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(curr); mesh->Draw(shader); diff --git a/demo/main.lua b/demo/main.lua index 2961da5..8a441bb 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -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) diff --git a/demo/skybox/back.jpg b/demo/skybox/back.jpg deleted file mode 100644 index 470a679..0000000 Binary files a/demo/skybox/back.jpg and /dev/null differ diff --git a/demo/skybox/bottom.jpg b/demo/skybox/bottom.jpg deleted file mode 100644 index 893f394..0000000 Binary files a/demo/skybox/bottom.jpg and /dev/null differ diff --git a/demo/skybox/front.jpg b/demo/skybox/front.jpg deleted file mode 100644 index 4e17b77..0000000 Binary files a/demo/skybox/front.jpg and /dev/null differ diff --git a/demo/skybox/left.jpg b/demo/skybox/left.jpg deleted file mode 100644 index 5750b91..0000000 Binary files a/demo/skybox/left.jpg and /dev/null differ diff --git a/demo/skybox/nx.png b/demo/skybox/nx.png new file mode 100644 index 0000000..8d63d66 Binary files /dev/null and b/demo/skybox/nx.png differ diff --git a/demo/skybox/ny.png b/demo/skybox/ny.png new file mode 100644 index 0000000..7aff24f Binary files /dev/null and b/demo/skybox/ny.png differ diff --git a/demo/skybox/nz.png b/demo/skybox/nz.png new file mode 100644 index 0000000..c46ba21 Binary files /dev/null and b/demo/skybox/nz.png differ diff --git a/demo/skybox/px.png b/demo/skybox/px.png new file mode 100644 index 0000000..77e9459 Binary files /dev/null and b/demo/skybox/px.png differ diff --git a/demo/skybox/py.png b/demo/skybox/py.png new file mode 100644 index 0000000..3ab0356 Binary files /dev/null and b/demo/skybox/py.png differ diff --git a/demo/skybox/pz.png b/demo/skybox/pz.png new file mode 100644 index 0000000..971ce54 Binary files /dev/null and b/demo/skybox/pz.png differ diff --git a/demo/skybox/right.jpg b/demo/skybox/right.jpg deleted file mode 100644 index 8963037..0000000 Binary files a/demo/skybox/right.jpg and /dev/null differ diff --git a/demo/skybox/top.jpg b/demo/skybox/top.jpg deleted file mode 100644 index 4db3c2a..0000000 Binary files a/demo/skybox/top.jpg and /dev/null differ diff --git a/roadmap.md b/roadmap.md index eabd222..5fabfaa 100644 --- a/roadmap.md +++ b/roadmap.md @@ -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!) + diff --git a/shaders/flat.frag b/shaders/flat.frag index b49d84d..9d4e267 100644 --- a/shaders/flat.frag +++ b/shaders/flat.frag @@ -1,7 +1,6 @@ #version 330 core noperspective in vec2 UV; -in vec3 NORMAL; in vec3 AMBIENT; in vec3 DIFFUSE; diff --git a/shaders/flat.vert b/shaders/flat.vert index cdba140..cff38f4 100644 --- a/shaders/flat.vert +++ b/shaders/flat.vert @@ -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);