From d7970ed797bef477a09de378fa32f494b09f2627 Mon Sep 17 00:00:00 2001 From: end Date: Thu, 29 Sep 2016 04:24:14 +0200 Subject: [PATCH] + added light shit --- game/glclasses.cpp | 5 ++- game/glclasses.hpp | 1 + game/renderer_polygon_3d/gameobjects.frag | 18 +++++++++- game/renderer_polygon_3d/gameobjects.vert | 16 ++++++++- game/renderer_polygon_3d/particle.frag | 15 ++++++++ game/renderer_polygon_3d/particle.vert | 17 ++++++++++ game/renderer_polygon_3d/particle_batch.cpp | 30 ++-------------- game/renderer_polygon_3d/polygon_model.hpp | 34 ++++++++----------- .../renderer_polygon_3d.cpp | 8 ++--- 9 files changed, 88 insertions(+), 56 deletions(-) create mode 100644 game/renderer_polygon_3d/particle.frag create mode 100644 game/renderer_polygon_3d/particle.vert diff --git a/game/glclasses.cpp b/game/glclasses.cpp index c9ec0b5..6486546 100644 --- a/game/glclasses.cpp +++ b/game/glclasses.cpp @@ -9,15 +9,14 @@ namespace endofthejedi { VAO::~VAO() { glDeleteVertexArrays(1, &m_name); } void VAO::bind() { glBindVertexArray(m_name); } + void VAO::unbind() { glBindVertexArray(0); } void VAO::fill(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const GLvoid *pointer) { - (void) type; - glEnableVertexAttribArray(index); - glVertexAttribPointer(index, size, GL_FLOAT, normalized, stride, pointer); + glVertexAttribPointer(index, size, type, normalized, stride, pointer); } Shader::Shader() : m_program(0) { } diff --git a/game/glclasses.hpp b/game/glclasses.hpp index 32bed2e..cc75484 100644 --- a/game/glclasses.hpp +++ b/game/glclasses.hpp @@ -36,6 +36,7 @@ class VAO { VAO(); ~VAO(); void bind(); + void unbind(); void fill(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride = 0, const GLvoid *pointer = NULL); }; diff --git a/game/renderer_polygon_3d/gameobjects.frag b/game/renderer_polygon_3d/gameobjects.frag index f67de40..dba089f 100644 --- a/game/renderer_polygon_3d/gameobjects.frag +++ b/game/renderer_polygon_3d/gameobjects.frag @@ -1,11 +1,27 @@ R"raw_string( #version 120 + varying vec3 vertex; +varying vec3 normal; + uniform vec3 color; +varying vec3 lightvec; + void main() { //gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0); //gl_FragColor = vec4(0.5+0.5*vertex.xyz, 1.0); - gl_FragColor = vec4(color, 1.0); + + + vec3 Eye = normalize(-vertex); + vec3 Reflected = normalize(reflect( -lightvec, normal)); + +//todo materials (& light color) + vec4 IAmbient = vec4(0.0f, 0.0f, 0.0f, 0.0f); + vec4 IDiffuse = vec4(1.0f, 1.0f, 1.0f, 0.0f) * max(dot(normal, lightvec), 0.0); + //todo shininess + vec4 ISpecular = vec4(1.0f, 0.0f, 0.0f, 0.0f) * pow(max(dot(Reflected, Eye), 0.0), 3); + + gl_FragColor = vec4((IAmbient + IDiffuse) + ISpecular); } )raw_string" diff --git a/game/renderer_polygon_3d/gameobjects.vert b/game/renderer_polygon_3d/gameobjects.vert index 94b23a6..c468759 100644 --- a/game/renderer_polygon_3d/gameobjects.vert +++ b/game/renderer_polygon_3d/gameobjects.vert @@ -1,12 +1,26 @@ R"raw_string( #version 120 + +attribute vec3 in_vertex; +attribute vec3 in_normal; + varying vec3 vertex; +varying vec3 normal; +varying vec3 lightvec; + uniform mat4 model; +uniform vec3 lightpos; + void main() { //vec3 p = position + scale*gl_Vertex.xyz; + vec3 p = (model*gl_Vertex).xyz; - gl_Position = vec4(p, 1.0); + lightvec = normalize(lightpos - p); + vertex = p.xyz; + normal = in_normal.xyz; + + gl_Position = vec4(p, 1.0); } )raw_string" diff --git a/game/renderer_polygon_3d/particle.frag b/game/renderer_polygon_3d/particle.frag new file mode 100644 index 0000000..5a50944 --- /dev/null +++ b/game/renderer_polygon_3d/particle.frag @@ -0,0 +1,15 @@ +R"raw_string( +#version 120 +varying vec2 vertex; +uniform float maxAge; +uniform float time; +void main() +{ + //gl_FragColor = vec4(0.5+0.5*vertex.x, 0.5+0.5*vertex.y, 0.0, 1.0); + if (length(vertex) > 1.0) { + discard; + } + float decay = time / maxAge; + gl_FragColor = vec4(1.0/max(1.0, decay), 1.0/max(1.0, 6.0*decay), 0.0, 1.0); +} +)raw_string" \ No newline at end of file diff --git a/game/renderer_polygon_3d/particle.vert b/game/renderer_polygon_3d/particle.vert new file mode 100644 index 0000000..e994997 --- /dev/null +++ b/game/renderer_polygon_3d/particle.vert @@ -0,0 +1,17 @@ +R"raw_string( +#version 120 +attribute vec2 in_vertex; +attribute vec2 in_position; +attribute vec2 in_velocity; +varying vec2 vertex; +uniform float time; +uniform float size; +void main() +{ + vec2 p = size*in_vertex; + p += time * in_velocity; + p += in_position; + gl_Position = vec4(p, 0.0, 1.0); + vertex = in_vertex; +} +)raw_string" \ No newline at end of file diff --git a/game/renderer_polygon_3d/particle_batch.cpp b/game/renderer_polygon_3d/particle_batch.cpp index 3582aea..4530bec 100644 --- a/game/renderer_polygon_3d/particle_batch.cpp +++ b/game/renderer_polygon_3d/particle_batch.cpp @@ -30,37 +30,11 @@ namespace endofthejedi { m_data_max_age.resize(m_numParticles); std::string vss_particles = - "#version 120\n" - "attribute vec2 in_vertex;\n" - "attribute vec2 in_position;\n" - "attribute vec2 in_velocity;\n" - "varying vec2 vertex;\n" - "uniform float time;\n" - "uniform float size;\n" - "void main()\n" - "{\n" - " vec2 p = size*in_vertex;\n" - " p += time * in_velocity;\n" - " p += in_position;\n" - " gl_Position = vec4(p, 0.0, 1.0);\n" - " vertex = in_vertex;\n" - "}\n" + #include "particle.vert" ; std::string fss_particles = - "#version 120\n" - "varying vec2 vertex;\n" - "uniform float maxAge;\n" - "uniform float time;\n" - "void main()\n" - "{\n" - //" gl_FragColor = vec4(0.5+0.5*vertex.x, 0.5+0.5*vertex.y, 0.0, 1.0);\n" - " if (length(vertex) > 1.0) {\n" - " discard;\n" - "}\n" - " float decay = time / maxAge;\n" - " gl_FragColor = vec4(1.0/max(1.0, decay), 1.0/max(1.0, 6.0*decay), 0.0, 1.0);\n" - "}\n" + #include "particle.frag" ; m_shader.init(); diff --git a/game/renderer_polygon_3d/polygon_model.hpp b/game/renderer_polygon_3d/polygon_model.hpp index 7138026..5a56cb9 100644 --- a/game/renderer_polygon_3d/polygon_model.hpp +++ b/game/renderer_polygon_3d/polygon_model.hpp @@ -95,14 +95,12 @@ namespace endofthejedi { m_data_position.data(), // Buffer data pointer GL_STATIC_DRAW); // Usage - Data never changes; - // TODO -#if 0 - // 2 holds normal data - glEnableVertexAttribArray(2); - glGenBuffers(1, &(model->vbo_normal)); // Generate buffer - glBindBuffer(GL_ARRAY_BUFFER, model->vbo_normal); // Bind buffer + //normal data + glEnableVertexAttribArray(1); + glGenBuffers(1, &m_vbo_id_normal); // Generate buffer + glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id_normal); // Bind buffer - glVertexAttribPointer(2, + glVertexAttribPointer(1, 3, // three floats per normal GL_FLOAT, // Data is floating point type GL_FALSE, // No fixed point scaling @@ -112,10 +110,10 @@ namespace endofthejedi { // Fill bound buffer glBufferData( GL_ARRAY_BUFFER, // Buffer target - 3*model->numVertices*sizeof(float), // Buffer data size - model->normals, // Buffer data pointer + 3*m_numVertices*sizeof(float), // Buffer data size + m_data_normal.data(), // Buffer data pointer GL_STATIC_DRAW); // Usage - Data never changes; -#endif + m_loaded_to_opengl = true; return true; @@ -123,7 +121,6 @@ namespace endofthejedi { bool bind() { - // TODO: check whether this vbo is bound! if (!m_loaded_to_opengl) { std::cout<<"[polygonmodel] warning: try to bind model vbo " << "which was not uploaded to OpenGL!" << std::endl; @@ -137,21 +134,17 @@ namespace endofthejedi { glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id_position); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); - // TODO -#if 0 // bind normal vbo - GLuint normalLoc = glGetAttribLocation(program, "attr_normal"); - glEnableVertexAttribArray(normalLoc); - glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo_normal); - glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL); -#endif + glEnableVertexAttribArray(1); + glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id_normal); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL); m_binding_active = true; return true; } - bool render() + bool render(Shader& shader) { if (!m_binding_active || !m_loaded_to_opengl) { std::cout<<"[polygonmodel] warning: try to render model without bind()" << std::endl; @@ -159,6 +152,9 @@ namespace endofthejedi { return false; } + glBindAttribLocation(shader.program(), 0, "in_vertex"); + glBindAttribLocation(shader.program(), 1, "in_normal"); + glDrawArrays(GL_TRIANGLES, 0, m_numVertices); return true; } diff --git a/game/renderer_polygon_3d/renderer_polygon_3d.cpp b/game/renderer_polygon_3d/renderer_polygon_3d.cpp index ac4073b..b9f9102 100644 --- a/game/renderer_polygon_3d/renderer_polygon_3d.cpp +++ b/game/renderer_polygon_3d/renderer_polygon_3d.cpp @@ -110,7 +110,7 @@ namespace endofthejedi { glm::vec3 c = planet->getColor(); glUniform3f(m_shader.location("color"), c.x, c.y, c.z); - m_planetModel->render(); + m_planetModel->render(m_shader); } } @@ -126,7 +126,7 @@ namespace endofthejedi { glm::mat4 model = computeModelMatrix(missile); glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model)); - m_missileModel->render(); + m_missileModel->render(m_shader); } } } @@ -140,9 +140,9 @@ namespace endofthejedi { glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model)); glm::vec3 c = glm::vec3(0.1, 1.0, 0.2); - glUniform3f(m_shader.location("color"), c.x, c.y, c.z); + glUniform3f(m_shader.location("color"), c.x, c.y, c.z); - m_shipModel->render(); + m_shipModel->render(m_shader); } }