From cfa2850292dfffec3f80419e8786fa7857dfb44e Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 10 Sep 2012 23:14:02 -0400 Subject: [PATCH] positioning with shaders working, normals not --- src/client/Client.cc | 31 +++++++++++++++++++------------ src/client/Client.h | 3 +++ src/client/GL/GLMatrix.cc | 14 +++++++------- 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/src/client/Client.cc b/src/client/Client.cc index 1612d12..1213f17 100755 --- a/src/client/Client.cc +++ b/src/client/Client.cc @@ -110,18 +110,22 @@ void Client::run() update(elapsed_time); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glPushMatrix(); double dir_x = cos(m_player->direction); double dir_y = sin(m_player->direction); + glLoadIdentity(); gluLookAt(m_player->x - dir_x * 100, m_player->y - dir_y * 100, 150, m_player->x, m_player->y, 100, 0, 0, 1); + m_modelview.load_identity(); + m_modelview.look_at( + m_player->x - dir_x * 100, m_player->y - dir_y * 100, 150, + m_player->x, m_player->y, 100, + 0, 0, 1); + draw_players(); draw_map(); - glPopMatrix(); - m_window->display(); last_time = current_time; } @@ -144,9 +148,10 @@ void Client::resize_window(int width, int height) float aspect = (float)width / (float)height; glMatrixMode(GL_PROJECTION); glLoadIdentity(); - gluPerspective(60.0f, aspect, 0.01, 5000.0); + gluPerspective(60.0f, aspect, 1.0f, 5000.0f); + m_projection.load_identity(); + m_projection.perspective(60.0f, aspect, 1.0f, 5000.0f); glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); } void Client::update(double elapsed_time) @@ -183,12 +188,12 @@ void Client::update(double elapsed_time) void Client::draw_players() { - GLint uniform_locations[5]; - const char *uniforms[] = { "ambient", "diffuse", "specular", "shininess", "scale" }; - m_obj_program.get_uniform_locations(uniforms, 5, uniform_locations); - glPushMatrix(); - glTranslatef(m_player->x, m_player->y, 40); - glRotatef(m_player->direction * 180.0 / M_PI, 0, 0, 1); + GLint uniform_locations[7]; + const char *uniforms[] = { "ambient", "diffuse", "specular", "shininess", "scale", "projection", "modelview" }; + m_obj_program.get_uniform_locations(uniforms, 7, uniform_locations); + m_modelview.push(); + m_modelview.translate(m_player->x, m_player->y, 40); + m_modelview.rotate(m_player->direction * 180.0 / M_PI, 0, 0, 1); glUseProgram(m_obj_program.get_id()); m_tank_obj.bindBuffers(); glEnableVertexAttribArray(0); @@ -199,6 +204,8 @@ void Client::draw_players() glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, (GLvoid *) m_tank_obj.getNormalOffset()); glUniform1f(uniform_locations[4], 20.0f); + m_projection.to_uniform(uniform_locations[5]); + m_modelview.to_uniform(uniform_locations[6]); for (map::iterator it = m_tank_obj.getMaterials().begin(); it != m_tank_obj.getMaterials().end(); @@ -228,7 +235,7 @@ void Client::draw_players() glDisableVertexAttribArray(0); glDisableVertexAttribArray(1); glUseProgram(0); - glPopMatrix(); + m_modelview.pop(); } void Client::draw_map() diff --git a/src/client/Client.h b/src/client/Client.h index fa1a327..cd44cd4 100755 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -8,6 +8,7 @@ #include "Player.h" #include "GLProgram.h" #include "WFObj.h" +#include "GLMatrix.h" class Client { @@ -29,6 +30,8 @@ class Client int m_height; GLProgram m_obj_program; WFObj m_tank_obj; + GLMatrix m_projection; + GLMatrix m_modelview; }; #endif diff --git a/src/client/GL/GLMatrix.cc b/src/client/GL/GLMatrix.cc index f70bd12..7887fa0 100644 --- a/src/client/GL/GLMatrix.cc +++ b/src/client/GL/GLMatrix.cc @@ -148,13 +148,13 @@ void GLMatrix::look_at(GLfloat eye_x, GLfloat eye_y, GLfloat eye_z, cross(up, side, forward); GLMatrix mult; mult.m_mat[0][0] = side[0]; - mult.m_mat[0][1] = side[1]; - mult.m_mat[0][2] = side[2]; - mult.m_mat[1][0] = up[0]; + mult.m_mat[1][0] = side[1]; + mult.m_mat[2][0] = side[2]; + mult.m_mat[0][1] = up[0]; mult.m_mat[1][1] = up[1]; - mult.m_mat[1][2] = up[2]; - mult.m_mat[2][0] = -forward[0]; - mult.m_mat[2][1] = -forward[1]; + mult.m_mat[2][1] = up[2]; + mult.m_mat[0][2] = -forward[0]; + mult.m_mat[1][2] = -forward[1]; mult.m_mat[2][2] = -forward[2]; multiply(mult); translate(-eye_x, -eye_y, -eye_z); @@ -194,7 +194,7 @@ void GLMatrix::ortho(GLfloat left, GLfloat right, void GLMatrix::to_uniform(GLint uniform) { - glUniformMatrix4fv(uniform, 1, GL_TRUE, &m_mat[0][0]); + glUniformMatrix4fv(uniform, 1, GL_FALSE, &m_mat[0][0]); } void GLMatrix::push()