1
0
Fork 0

Fix light to consider object transformation

This commit is contained in:
Meoweg 2015-11-07 17:30:34 +00:00
parent 6ba5794ea2
commit 5dff8f3587
5 changed files with 26 additions and 14 deletions

View File

@ -6,9 +6,11 @@ varying vec2 f_tex_coord;
varying float light_weight;
uniform mat4 mvp;
uniform mat3 local_modelview;
void main(void) {
gl_Position = mvp * position;
f_tex_coord = tex_coord;
light_weight = max(dot(normal, vec3(0.0, 0.0, 1.0)), 0.0);
vec3 transformed_normal = local_modelview * normal;
light_weight = max(dot(transformed_normal, vec3(0.0, 0.0, 1.0)), 0.0);
}

View File

@ -5,5 +5,6 @@
#include <GL/glfw.h>
extern GLuint mvp_uniform;
extern GLuint local_modelview_uniform;
#endif // _GL_HPP_

View File

@ -18,6 +18,8 @@ static GLFWCALL void on_key(int key, int action);
static EM_BOOL on_em_mousemove(int event_type, const EmscriptenMouseEvent *mouse_event, void *user_data);
GLuint mvp_uniform;
GLuint local_modelview_uniform;
static GLuint texture_uniform;
static bool keys[GLFW_KEY_LAST];
@ -58,6 +60,7 @@ int main()
program.use();
mvp_uniform = program.get_uniform_location("mvp");
local_modelview_uniform = program.get_uniform_location("local_modelview");
texture_uniform = program.get_uniform_location("texture");
glUniform1i(texture_uniform, 0);
@ -73,7 +76,7 @@ int main()
teapot1 = new Object(*teapot);
teapot1->position.x = -2.0;
teapot1->position.z = 1.0;
teapot1->rotation.z = 45;
teapot1->angles.z = 45;
bunny1 = new Object(*bunny);
bunny1->position.x = 2.0;
@ -133,21 +136,21 @@ void iterate()
pos_y -= 0.1 * sin(glm::radians(delta_z));
}
suzanne1->rotation.y = glfwGetTime() * 360 / 4;
teapot1->rotation.x = glfwGetTime() * 360 / 6;
bunny1->rotation.z = glfwGetTime() * 360 / 2;
suzanne1->angles.y = glfwGetTime() * 360 / 4;
teapot1->angles.x = glfwGetTime() * 360 / 6;
bunny1->angles.z = glfwGetTime() * 360 / 2;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(-pos_x, -pos_y, -2.0f));
const glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(-pos_x, -pos_y, -2.0f));
glm::mat4 view = glm::mat4(1.0f)
const glm::mat4 view = glm::mat4(1.0f)
* glm::rotate(glm::mat4(1.0f), glm::radians(delta_x - 90), glm::vec3(1.0f, 0.0f, 0.0f))
* glm::rotate(glm::mat4(1.0f), glm::radians(delta_z), glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 projection = glm::perspective(45.0f, (float)640 / (float)480, 0.1f, 10.0f);
const glm::mat4 projection = glm::perspective(45.0f, (float)640 / (float)480, 0.1f, 10.0f);
glm::mat4 mvp = projection * view * model;
const glm::mat4 mvp = projection * view * model;
suzanne1->draw(mvp);
teapot1->draw(mvp);

View File

@ -5,12 +5,18 @@
void Object::draw(const glm::mat4 &mvp) const
{
const glm::mat4 transform = glm::translate(mvp, position)
* glm::rotate(glm::mat4(1.0f), glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f))
* glm::rotate(glm::mat4(1.0f), glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f))
* glm::rotate(glm::mat4(1.0f), glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
const glm::mat4 translation = glm::translate(glm::mat4(1.0f), position);
const glm::mat4 rotation =
glm::rotate(glm::mat4(1.0f), glm::radians(angles.z), glm::vec3(0.0f, 0.0f, 1.0f))
* glm::rotate(glm::mat4(1.0f), glm::radians(angles.y), glm::vec3(0.0f, 1.0f, 0.0f))
* glm::rotate(glm::mat4(1.0f), glm::radians(angles.x), glm::vec3(1.0f, 0.0f, 0.0f));
const glm::mat4 transform = mvp * translation * rotation;
glUniformMatrix4fv(mvp_uniform, 1, GL_FALSE, glm::value_ptr(transform));
const glm::mat3 local_modelview = glm::transpose(glm::inverse(glm::mat3(translation * rotation)));
glUniformMatrix3fv(local_modelview_uniform, 1, GL_FALSE, glm::value_ptr(local_modelview));
_model.draw();
}

View File

@ -12,7 +12,7 @@ public:
void draw(const glm::mat4 &mvp) const;
glm::vec3 position;
glm::vec3 rotation;
glm::vec3 angles;
private:
const Model &_model;