From 44a7048c4a49370c0107b979f322643692e5a085 Mon Sep 17 00:00:00 2001 From: Andreas Ortmann Date: Thu, 29 Sep 2016 08:28:56 +0200 Subject: [PATCH] light working. --- game/renderer_polygon_3d/gameobjects.frag | 7 +- game/renderer_polygon_3d/gameobjects.vert | 6 +- game/renderer_polygon_3d/particle_batch.cpp | 17 +++- game/renderer_polygon_3d/particle_batch.hpp | 6 +- game/renderer_polygon_3d/polygon_model.hpp | 60 +++++++++----- .../renderer_polygon_3d.cpp | 83 +++++++++++++------ .../renderer_polygon_3d.hpp | 11 ++- game/state/state.cpp | 4 + game/state/state.hpp | 6 +- 9 files changed, 139 insertions(+), 61 deletions(-) diff --git a/game/renderer_polygon_3d/gameobjects.frag b/game/renderer_polygon_3d/gameobjects.frag index dba089f..023123d 100644 --- a/game/renderer_polygon_3d/gameobjects.frag +++ b/game/renderer_polygon_3d/gameobjects.frag @@ -14,14 +14,15 @@ void main() vec3 Eye = normalize(-vertex); - vec3 Reflected = normalize(reflect( -lightvec, normal)); + vec3 Reflected = normalize(reflect( -lightvec, normal)); //todo materials (& light color) - vec4 IAmbient = vec4(0.0f, 0.0f, 0.0f, 0.0f); + vec4 IAmbient = vec4(0.2f, 0.2f, 0.2f, 1.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); + //gl_FragColor = vec4(0.5+0.5*normal, 1.0); } )raw_string" diff --git a/game/renderer_polygon_3d/gameobjects.vert b/game/renderer_polygon_3d/gameobjects.vert index c468759..6594746 100644 --- a/game/renderer_polygon_3d/gameobjects.vert +++ b/game/renderer_polygon_3d/gameobjects.vert @@ -13,13 +13,13 @@ uniform vec3 lightpos; void main() { - //vec3 p = position + scale*gl_Vertex.xyz; + //vec3 p = position + scale*in_vertex.xyz; - vec3 p = (model*gl_Vertex).xyz; + vec3 p = (model*vec4(in_vertex, 1.0)).xyz; lightvec = normalize(lightpos - p); vertex = p.xyz; - normal = in_normal.xyz; + normal = normalize((model*vec4(in_normal.xyz, 0.0)).xyz); gl_Position = vec4(p, 1.0); } diff --git a/game/renderer_polygon_3d/particle_batch.cpp b/game/renderer_polygon_3d/particle_batch.cpp index 4530bec..3b44f1b 100644 --- a/game/renderer_polygon_3d/particle_batch.cpp +++ b/game/renderer_polygon_3d/particle_batch.cpp @@ -14,6 +14,7 @@ namespace endofthejedi { : m_numParticles(numParticles) , m_particleRadius(particleSize) , m_maxAge(maxAge) + , m_age(0.0) // 2d quad drawn as a triangle fan , m_data_quad({ @@ -112,14 +113,12 @@ namespace endofthejedi { m_shader.bind(); + // TODO: does that work? glBindAttribLocation(m_shader.program(), 0, "in_vertex"); glBindAttribLocation(m_shader.program(), 1, "in_position"); glBindAttribLocation(m_shader.program(), 2, "in_velocity"); - static float time = 0.0f; - time += 1.0/50.0; - - glUniform1f(m_shader.location("time"), time); + glUniform1f(m_shader.location("time"), m_age); glUniform1f(m_shader.location("maxAge"), m_maxAge); glUniform1f(m_shader.location("size"), m_particleRadius); @@ -175,4 +174,14 @@ namespace endofthejedi { exit(-1); } } + + void ParticleBatch::tick(float dt) + { + m_age += dt; + } + + bool ParticleBatch::done() const + { + return m_age >= m_maxAge; + } } diff --git a/game/renderer_polygon_3d/particle_batch.hpp b/game/renderer_polygon_3d/particle_batch.hpp index 59d971f..273b484 100644 --- a/game/renderer_polygon_3d/particle_batch.hpp +++ b/game/renderer_polygon_3d/particle_batch.hpp @@ -22,6 +22,9 @@ namespace endofthejedi { void upload(); void render(); + void tick(float dt); + bool done() const; + Shader *shader() { return &m_shader; } private: @@ -31,7 +34,8 @@ namespace endofthejedi { private: size_t m_numParticles; float m_particleRadius; - float m_maxAge; + const float m_maxAge; + float m_age; GLuint m_data_vbos[5]; diff --git a/game/renderer_polygon_3d/polygon_model.hpp b/game/renderer_polygon_3d/polygon_model.hpp index 5a56cb9..6335bf3 100644 --- a/game/renderer_polygon_3d/polygon_model.hpp +++ b/game/renderer_polygon_3d/polygon_model.hpp @@ -76,6 +76,10 @@ namespace endofthejedi { return false; } + /************************************************************/ + /* Position data */ + /************************************************************/ + glEnableVertexAttribArray(0); glGenBuffers(1, &m_vbo_id_position); // Generate buffer @@ -95,31 +99,35 @@ namespace endofthejedi { m_data_position.data(), // Buffer data pointer GL_STATIC_DRAW); // Usage - Data never changes; - //normal data - glEnableVertexAttribArray(1); - glGenBuffers(1, &m_vbo_id_normal); // Generate buffer - glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id_normal); // Bind buffer + /************************************************************/ + /* Normal data */ + /************************************************************/ - glVertexAttribPointer(1, - 3, // three floats per normal - GL_FLOAT, // Data is floating point type - GL_FALSE, // No fixed point scaling - 0, // stride: no - NULL); // No offset + glEnableVertexAttribArray(1); + glGenBuffers(1, &m_vbo_id_normal); // Generate buffer + glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id_normal); // Bind buffer + + glVertexAttribPointer( + 1, + 3, // three floats per normal + GL_FLOAT, // Data is floating point type + GL_FALSE, // No fixed point scaling + 0, // stride: no + NULL); // No offset + + // Fill bound buffer + glBufferData( + GL_ARRAY_BUFFER, // Buffer target + 3 * m_numVertices * sizeof(float), // Buffer data size + m_data_normal.data(), // Buffer data pointer + GL_STATIC_DRAW); // Usage - Data never changes; - // Fill bound buffer - glBufferData( - GL_ARRAY_BUFFER, // Buffer target - 3*m_numVertices*sizeof(float), // Buffer data size - m_data_normal.data(), // Buffer data pointer - GL_STATIC_DRAW); // Usage - Data never changes; - m_loaded_to_opengl = true; return true; } - bool bind() + bool bind(Shader &shader) { if (!m_loaded_to_opengl) { std::cout<<"[polygonmodel] warning: try to bind model vbo " @@ -135,9 +143,10 @@ namespace endofthejedi { glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); // bind normal vbo - glEnableVertexAttribArray(1); + GLuint loc = glGetAttribLocation(shader.program(), "in_normal"); + glEnableVertexAttribArray(loc); glBindBuffer(GL_ARRAY_BUFFER, m_vbo_id_normal); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL); + glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, 0, NULL); m_binding_active = true; @@ -152,8 +161,13 @@ namespace endofthejedi { return false; } - glBindAttribLocation(shader.program(), 0, "in_vertex"); - glBindAttribLocation(shader.program(), 1, "in_normal"); + //GLuint l0 = glGetAttribLocation(shader.program(), "in_normal"); + //GLuint l1 = glGetAttribLocation(shader.program(), "in_vertex"); + //GLuint l2 = glGetAttribLocation(shader.program(), "in_fakkkke"); + //std::cout<<"locations: " << l0 << " " << l1 << " " << l2 << std::endl; + + //glBindAttribLocation(shader.program(), 0, "in_vertex"); + //glBindAttribLocation(shader.program(), 1, "in_normal"); glDrawArrays(GL_TRIANGLES, 0, m_numVertices); return true; @@ -195,6 +209,8 @@ namespace endofthejedi { m_data_normal.push_back(mesh->mNormals[index].x); m_data_normal.push_back(mesh->mNormals[index].y); m_data_normal.push_back(mesh->mNormals[index].z); + + //std::cout<<"adding normal: " << mesh->mNormals[index].x << " " << mesh->mNormals[index].y << " " << mesh->mNormals[index].z << std::endl; } } diff --git a/game/renderer_polygon_3d/renderer_polygon_3d.cpp b/game/renderer_polygon_3d/renderer_polygon_3d.cpp index b9f9102..18ae797 100644 --- a/game/renderer_polygon_3d/renderer_polygon_3d.cpp +++ b/game/renderer_polygon_3d/renderer_polygon_3d.cpp @@ -9,6 +9,8 @@ namespace endofthejedi { void RendererPolygon3d::setup() { + m_lastTime = -1.0; + std::cout<<"setup polygon 3d" << std::endl; //m_missileModel = new PolygonModel("../data/mesh/quad_screen_coords.stl"); @@ -18,25 +20,6 @@ namespace endofthejedi { addModel("../data/mesh/planet_12.stl", &m_planetModel); addModel("../data/mesh/planet_12.stl", &m_shipModel); - size_t n = 100; - m_particles = new ParticleBatch(n, 0.05, 3.0); - - // distribute in a circle - for (size_t i=0; isetParticle( - i, - //glm::vec2(i/(float) n, 0.0), - glm::vec2(0.0, 0.0), - v); - } - - m_particles->upload(); - std::string vss_simple = #include "simple.vert" ; @@ -70,10 +53,21 @@ namespace endofthejedi { void RendererPolygon3d::render(const game::State *state) { + if (m_lastTime == -1.0) { + m_lastTime = state->timestamp(); + } + + float dt = state->timestamp() - m_lastTime; + if (dt < 0.0) { + dt = 0.0; + } + glClearColor(0.0, 0.0, 0.0, 1.0); m_state = state; + advanceGraphicObjects(dt); + m_shader.bind(); // TODO :Z? @@ -91,17 +85,54 @@ namespace endofthejedi { //glVertex2f(1.0f, 1.0f); //glVertex2f(-1.0f, 1.0f); //glEnd(); + + m_lastTime = state->timestamp(); } void RendererPolygon3d::renderParticles() { - //m_particles->bind(); - m_particles->render(); + for (ParticleBatch *batch : m_particles) { + batch->bind(); + batch->render(); + } + } + + void RendererPolygon3d::addExplosionEffect(const glm::vec2 &pos, size_t n, float duration) + { + ParticleBatch *batch = new ParticleBatch(n, 0.005, duration); + + for (size_t i=0; isetParticle(i, pos, v); + } + + batch->upload(); + + m_particles.push_back(batch); + } + + void RendererPolygon3d::advanceGraphicObjects(float dt) + { + for (ParticleBatch *batch : m_particles) { + batch->tick(dt); + if (batch->done()) { + m_particles.remove(batch); + delete(batch); + + std::cout<<"particle batch done!" << std::endl; + } + } } void RendererPolygon3d::renderPlanets() { - m_planetModel->bind(); + m_planetModel->bind(m_shader); for (const game::Planet *planet : m_state->planets) { glm::mat4 model = computeModelMatrix(planet); @@ -116,12 +147,12 @@ namespace endofthejedi { void RendererPolygon3d::renderMissiles() { - m_missileModel->bind(); + m_missileModel->bind(m_shader); for (const game::Player *player : m_state->players) { for (const game::Missile *missile : player->missiles) { glm::vec3 c = glm::vec3(1.0, 1.0, 0.3); - glUniform3f(m_shader.location("color"), c.x, c.y, c.z); + glUniform3f(m_shader.location("color"), c.x, c.y, c.z); glm::mat4 model = computeModelMatrix(missile); glUniformMatrix4fv(m_shader.location("model"), 1, GL_FALSE, glm::value_ptr(model)); @@ -133,7 +164,7 @@ namespace endofthejedi { void RendererPolygon3d::renderShips() { - m_shipModel->bind(); + m_shipModel->bind(m_shader); for (const game::Ship *ship : m_state->ships) { glm::mat4 model = computeModelMatrix(ship); @@ -173,7 +204,7 @@ namespace endofthejedi { // TODO: which visual size has the rocket? in game its just a point with // no size because all others have size. - return computeModelMatrix(missile->position, 0.02f, a); + return computeModelMatrix(missile->position, 0.05f, a); } glm::mat4 RendererPolygon3d::computeModelMatrix(const game::Ship *ship) diff --git a/game/renderer_polygon_3d/renderer_polygon_3d.hpp b/game/renderer_polygon_3d/renderer_polygon_3d.hpp index 19cad60..0c30d1a 100644 --- a/game/renderer_polygon_3d/renderer_polygon_3d.hpp +++ b/game/renderer_polygon_3d/renderer_polygon_3d.hpp @@ -1,5 +1,7 @@ #include "renderer.hpp" +#include + #include "glclasses.hpp" #include "game.hpp" @@ -32,6 +34,10 @@ namespace endofthejedi { void addModel(const std::string &filename, PolygonModel **dest); + void addExplosionEffect(const glm::vec2 &pos, size_t n, float duration); + + void advanceGraphicObjects(float dt); + // shortcuts which use the lower versions glm::mat4 computeModelMatrix(const game::Planet *planet); glm::mat4 computeModelMatrix(const game::Missile *missile); @@ -55,6 +61,9 @@ namespace endofthejedi { // for accessing const game::State *m_state; - ParticleBatch *m_particles; + std::list m_particles; + + // time value for last rendering cycle (-1 after setup/startup) + float m_lastTime; }; } diff --git a/game/state/state.cpp b/game/state/state.cpp index 3527b52..1728f01 100644 --- a/game/state/state.cpp +++ b/game/state/state.cpp @@ -21,6 +21,8 @@ namespace game { void State::init() { + m_nextId = 0; + m_time = 0.0; m_shipRadius = 0.02; m_maxMissileDistance = 2.0; m_playerRespawnTime = 2.0; @@ -253,6 +255,8 @@ namespace game { void State::advance(float dt) { + m_time += dt; + advancePlayerShipSpawns(dt); advanceExplosions(dt); diff --git a/game/state/state.hpp b/game/state/state.hpp index bf2e213..d952cca 100644 --- a/game/state/state.hpp +++ b/game/state/state.hpp @@ -77,6 +77,9 @@ namespace game { // delete traces with this command void deleteTrace(Trace *trace); // using a pointer + // get the current time + float timestamp() const { return m_time; } + /*************************************************************************/ /* Rendering */ /*************************************************************************/ @@ -115,7 +118,8 @@ namespace game { float m_playerRespawnTime; float m_shipRadius; float m_defaultEnergy; - int m_nextId=0; + int m_nextId; int m_maxNumTraces; + float m_time; }; };