From 6ca1d78a20096da93744dbd2a408c72805b6dde7 Mon Sep 17 00:00:00 2001 From: uis Date: Wed, 19 Dec 2018 14:45:39 +0300 Subject: [PATCH] Fixes #29 fixes #18 --- src/RendererEntity.cpp | 57 ++++++++++++++++++++++++++++++++++-------- src/RendererEntity.hpp | 4 ++- src/RendererWorld.cpp | 19 +++++++------- 3 files changed, 58 insertions(+), 22 deletions(-) diff --git a/src/RendererEntity.cpp b/src/RendererEntity.cpp index a987139d..6a655fb0 100644 --- a/src/RendererEntity.cpp +++ b/src/RendererEntity.cpp @@ -7,6 +7,40 @@ #include "World.hpp" #include "Renderer.hpp" +const GLfloat verticesLines[] = { + //Z+ edge + -0.5f, 0.5f, 0.5f, + -0.5f, -0.5f, 0.5f, + -0.5f, -0.5f, 0.5f, + 0.5f, -0.5f, 0.5f, + 0.5f, -0.5f, 0.5f, + 0.5f, 0.5f, 0.5f, + 0.5f, 0.5f, 0.5f, + -0.5f, 0.5f, 0.5f, + + //Z- edge + -0.5f, 0.5f, -0.5f, + -0.5f, -0.5f, -0.5f, + -0.5f, -0.5f, -0.5f, + 0.5f, -0.5f, -0.5f, + 0.5f, -0.5f, -0.5f, + 0.5f, 0.5f, -0.5f, + 0.5f, 0.5f, -0.5f, + -0.5f, 0.5f, -0.5f, + + //X+ edge + -0.5f, -0.5f, -0.5f, + -0.5f, -0.5f, 0.5f, + -0.5f, 0.5f, 0.5f, + -0.5f, 0.5f, -0.5f, + + //X- edge + 0.5f, -0.5f, -0.5f, + 0.5f, -0.5f, 0.5f, + 0.5f, 0.5f, 0.5f, + 0.5f, 0.5f, -0.5f +}; + const GLfloat vertices[] = { //Z+ edge -0.5f, 0.5f, 0.5f, @@ -110,16 +144,11 @@ const GLfloat uv_coords[] = { const GLuint magic = 993214; GLuint Vbo = magic,Vao = magic,Vbo2 = magic; -RendererEntity::RendererEntity(World *ptr, unsigned int id) -{ - world = ptr; - entityId = id; - - +GLuint RendererEntity::GetVao(){ if (Vbo == magic) { glGenBuffers(1, &Vbo); glBindBuffer(GL_ARRAY_BUFFER, Vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(verticesLines), verticesLines, GL_STATIC_DRAW); glGenBuffers(1, &Vbo2); glBindBuffer(GL_ARRAY_BUFFER, Vbo2); @@ -128,7 +157,7 @@ RendererEntity::RendererEntity(World *ptr, unsigned int id) glGenVertexArrays(1, &Vao); glBindVertexArray(Vao); { - glBindBuffer(GL_ARRAY_BUFFER, Vbo); + glBindBuffer(GL_ARRAY_BUFFER, Vbo); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0); glEnableVertexAttribArray(0); @@ -138,13 +167,19 @@ RendererEntity::RendererEntity(World *ptr, unsigned int id) } glBindVertexArray(0); } + return Vao; +} + +RendererEntity::RendererEntity(World *ptr, unsigned int id) +{ + world = ptr; + entityId = id; } RendererEntity::~RendererEntity() { } void RendererEntity::Render(RenderState & renderState) { - renderState.SetActiveVao(Vao); glm::mat4 model = glm::mat4(1.0); Entity& entity = world->GetEntity(entityId); model = glm::translate(model, entity.pos.glm()); @@ -154,7 +189,7 @@ void RendererEntity::Render(RenderState & renderState) { glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); glUniform3f(colorLoc, entity.renderColor.x, entity.renderColor.y, entity.renderColor.z); glCheckError(); - glDrawArrays(GL_LINE_STRIP, 0, 36); + glDrawArrays(GL_LINES, 0, 24); glCheckError(); -} \ No newline at end of file +} diff --git a/src/RendererEntity.hpp b/src/RendererEntity.hpp index 76548c66..51d41636 100644 --- a/src/RendererEntity.hpp +++ b/src/RendererEntity.hpp @@ -16,4 +16,6 @@ class RendererEntity { GLint modelLoc = 0; GLint colorLoc = 0; -}; \ No newline at end of file + + static GLuint GetVao(); +}; diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index bb8c8f4d..e1dd52c1 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -291,13 +291,13 @@ void RendererWorld::Render(RenderState & renderState) { glCheckError(); modelLoc = glGetUniformLocation(entityShader->Program, "model"); colorLoc = glGetUniformLocation(entityShader->Program, "color"); + + renderState.SetActiveVao(RendererEntity::GetVao()); for (auto& it : entities) { it.modelLoc = modelLoc; it.colorLoc = colorLoc; it.Render(renderState); } - glLineWidth(1.0); - glCheckError(); //Render selected block Vector selectedBlock = gs->selectedBlock; @@ -311,16 +311,14 @@ void RendererWorld::Render(RenderState & renderState) { glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); glUniform3f(colorLoc, 0.0, 0.0, 0.0); glCheckError(); - glDrawArrays(GL_LINE_STRIP, 0, 36); + glDrawArrays(GL_LINES, 0, 24); } - glLineWidth(1.0f); - glCheckError(); } //Render raycast hit - bool renderHit = false; + const bool renderHit = false; if (renderHit) { - VectorF hit = gs->raycastHit; + VectorF hit = gs->raycastHit; glLineWidth(2.0f); { glm::mat4 model; @@ -334,10 +332,11 @@ void RendererWorld::Render(RenderState & renderState) { glCheckError(); glDrawArrays(GL_LINE_STRIP, 0, 36); } - glLineWidth(1.0f); - glCheckError(); } + glLineWidth(1.0); + glCheckError(); + //Render sky renderState.TimeOfDay = gs->TimeOfDay; renderState.SetActiveShader(skyShader->Program); @@ -464,4 +463,4 @@ void RendererWorld::Update(double timeToUpdate) { GameState* RendererWorld::GameStatePtr() { return gs; -} \ No newline at end of file +}