Skip to content

Commit

Permalink
Fixes LaG1924#29 fixes LaG1924#18
Browse files Browse the repository at this point in the history
  • Loading branch information
uis246 committed Dec 20, 2018
1 parent fcdaed0 commit 6ca1d78
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 22 deletions.
57 changes: 46 additions & 11 deletions src/RendererEntity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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());
Expand All @@ -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();
}
}
4 changes: 3 additions & 1 deletion src/RendererEntity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ class RendererEntity {

GLint modelLoc = 0;
GLint colorLoc = 0;
};

static GLuint GetVao();
};
19 changes: 9 additions & 10 deletions src/RendererWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -464,4 +463,4 @@ void RendererWorld::Update(double timeToUpdate) {

GameState* RendererWorld::GameStatePtr() {
return gs;
}
}

0 comments on commit 6ca1d78

Please sign in to comment.