diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-09-17 17:50:36 +0200 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2018-01-13 03:39:31 +0100 |
commit | 94006e599c8d591096b768d1cd0c8b75eb763c45 (patch) | |
tree | e1464cabe071e62015a822e1e3f36af524bbc16c /src | |
parent | 2017-09-16 (diff) | |
download | AltCraft-94006e599c8d591096b768d1cd0c8b75eb763c45.tar AltCraft-94006e599c8d591096b768d1cd0c8b75eb763c45.tar.gz AltCraft-94006e599c8d591096b768d1cd0c8b75eb763c45.tar.bz2 AltCraft-94006e599c8d591096b768d1cd0c8b75eb763c45.tar.lz AltCraft-94006e599c8d591096b768d1cd0c8b75eb763c45.tar.xz AltCraft-94006e599c8d591096b768d1cd0c8b75eb763c45.tar.zst AltCraft-94006e599c8d591096b768d1cd0c8b75eb763c45.zip |
Diffstat (limited to '')
-rw-r--r-- | src/RendererSection.cpp | 178 | ||||
-rw-r--r-- | src/RendererSection.hpp | 7 | ||||
-rw-r--r-- | src/Shader.cpp | 4 | ||||
-rw-r--r-- | src/World.cpp | 2 |
4 files changed, 110 insertions, 81 deletions
diff --git a/src/RendererSection.cpp b/src/RendererSection.cpp index 8262cef..b02f044 100644 --- a/src/RendererSection.cpp +++ b/src/RendererSection.cpp @@ -2,47 +2,7 @@ #include <thread> -const GLfloat vertices[] = { - 0, 0, 0, - 1, 0, 1, - 1, 0, 0, - - 0, 0, 0, - 0, 0, 1, - 1, 0, 1, -}; - -const GLfloat uv_coords[] = { - 0.0f, 0.0f, - 1.0f, 1.0f, - 0.0f, 1.0f, - - 0.0f, 0.0f, - 1.0f, 0.0f, - 1.0f, 1.0f, -}; - -const GLuint magicUniqueConstant = 88375; -GLuint RendererSection::VboVertices = magicUniqueConstant; -GLuint RendererSection::VboUvs = magicUniqueConstant; - RendererSection::RendererSection(RendererSectionData data) { - if (VboVertices == magicUniqueConstant) { - glGenBuffers(1, &VboVertices); - glGenBuffers(1, &VboUvs); - - //Cube vertices - glBindBuffer(GL_ARRAY_BUFFER, VboVertices); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - - //Cube UVs - glBindBuffer(GL_ARRAY_BUFFER, VboUvs); - glBufferData(GL_ARRAY_BUFFER, sizeof(uv_coords), uv_coords, GL_STATIC_DRAW); - - LOG(INFO) << "Created VBOs with vertices (" << VboVertices << ") and UVs (" << VboUvs - << ") for faces"; - } - glGenVertexArrays(1, &Vao); glGenBuffers(VBOCOUNT, Vbo); @@ -51,13 +11,13 @@ RendererSection::RendererSection(RendererSectionData data) { { //Cube vertices GLuint VertAttribPos = 0; - glBindBuffer(GL_ARRAY_BUFFER, VboVertices); + glBindBuffer(GL_ARRAY_BUFFER, Vbo[VERTICES]); glVertexAttribPointer(VertAttribPos, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr); glEnableVertexAttribArray(VertAttribPos); //Cube UVs GLuint UvAttribPos = 2; - glBindBuffer(GL_ARRAY_BUFFER, VboUvs); + glBindBuffer(GL_ARRAY_BUFFER, Vbo[UV]); glVertexAttribPointer(UvAttribPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr); glEnableVertexAttribArray(UvAttribPos); @@ -66,39 +26,19 @@ RendererSection::RendererSection(RendererSectionData data) { glBindBuffer(GL_ARRAY_BUFFER, Vbo[TEXTURES]); glVertexAttribPointer(textureAttribPos, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), nullptr); glEnableVertexAttribArray(textureAttribPos); - glVertexAttribDivisor(textureAttribPos, 1); glCheckError(); - //Blocks models - GLuint matAttribPos = 8; - size_t sizeOfMat4 = 4 * 4 * sizeof(GLfloat); - glBindBuffer(GL_ARRAY_BUFFER, Vbo[MODELS]); - glVertexAttribPointer(matAttribPos + 0, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, nullptr); - glVertexAttribPointer(matAttribPos + 1, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, (void *)(1 * 4 * sizeof(GLfloat))); - glVertexAttribPointer(matAttribPos + 2, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, (void *)(2 * 4 * sizeof(GLfloat))); - glVertexAttribPointer(matAttribPos + 3, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, (void *)(3 * 4 * sizeof(GLfloat))); - glEnableVertexAttribArray(matAttribPos + 0); - glEnableVertexAttribArray(matAttribPos + 1); - glEnableVertexAttribArray(matAttribPos + 2); - glEnableVertexAttribArray(matAttribPos + 3); - glVertexAttribDivisor(matAttribPos + 0, 1); - glVertexAttribDivisor(matAttribPos + 1, 1); - glVertexAttribDivisor(matAttribPos + 2, 1); - glVertexAttribDivisor(matAttribPos + 3, 1); - //Color GLuint colorAttribPos = 12; glBindBuffer(GL_ARRAY_BUFFER, Vbo[COLORS]); glVertexAttribPointer(colorAttribPos, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr); glEnableVertexAttribArray(colorAttribPos); - glVertexAttribDivisor(colorAttribPos, 1); //Light GLuint lightAttribPos = 13; glBindBuffer(GL_ARRAY_BUFFER, Vbo[LIGHTS]); glVertexAttribPointer(lightAttribPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr); glEnableVertexAttribArray(lightAttribPos); - glVertexAttribDivisor(lightAttribPos, 1); glBindBuffer(GL_ARRAY_BUFFER, 0); } @@ -107,11 +47,14 @@ RendererSection::RendererSection(RendererSectionData data) { //Upload data to VRAM - glBindBuffer(GL_ARRAY_BUFFER, Vbo[TEXTURES]); - glBufferData(GL_ARRAY_BUFFER, data.textures.size() * sizeof(glm::vec4), data.textures.data(), GL_DYNAMIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, Vbo[VERTICES]); + glBufferData(GL_ARRAY_BUFFER, data.vertices.size() * sizeof(glm::vec3), data.vertices.data(), GL_DYNAMIC_DRAW); + + glBindBuffer(GL_ARRAY_BUFFER, Vbo[UV]); + glBufferData(GL_ARRAY_BUFFER, data.uv.size() * sizeof(glm::vec2), data.uv.data(), GL_DYNAMIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, Vbo[MODELS]); - glBufferData(GL_ARRAY_BUFFER, data.models.size() * sizeof(glm::mat4), data.models.data(), GL_DYNAMIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, Vbo[TEXTURES]); + glBufferData(GL_ARRAY_BUFFER, data.textures.size() * sizeof(glm::vec4), data.textures.data(), GL_DYNAMIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, Vbo[COLORS]); glBufferData(GL_ARRAY_BUFFER, data.colors.size() * sizeof(glm::vec3), data.colors.data(), GL_DYNAMIC_DRAW); @@ -119,11 +62,11 @@ RendererSection::RendererSection(RendererSectionData data) { glBindBuffer(GL_ARRAY_BUFFER, Vbo[LIGHTS]); glBufferData(GL_ARRAY_BUFFER, data.lights.size() * sizeof(glm::vec2), data.lights.data(), GL_DYNAMIC_DRAW); - glBindBuffer(GL_ARRAY_BUFFER, 0); +glBindBuffer(GL_ARRAY_BUFFER, 0); - numOfFaces = data.textures.size(); - sectionPos = data.sectionPos; - hash = data.hash; +numOfFaces = data.textures.size(); +sectionPos = data.sectionPos; +hash = data.hash; } RendererSection::RendererSection(RendererSection && other) { @@ -134,7 +77,7 @@ RendererSection::RendererSection(RendererSection && other) { RendererSection::~RendererSection() { if (Vao != 0) glDeleteVertexArrays(1, &Vao); - + for (int i = 0; i < VBOCOUNT; i++) if (Vbo[i] != 0) { glBindBuffer(GL_ARRAY_BUFFER, Vbo[i]); @@ -153,9 +96,9 @@ void swap(RendererSection & lhs, RendererSection & rhs) { } void RendererSection::Render(RenderState &renderState) { - renderState.SetActiveVao(Vao); - glDrawArraysInstanced(GL_TRIANGLES, 0, 6, numOfFaces); - glCheckError(); + renderState.SetActiveVao(Vao); + glDrawArrays(GL_TRIANGLES, 0, numOfFaces); + glCheckError(); } Vector RendererSection::GetPosition() @@ -168,13 +111,13 @@ size_t RendererSection::GetHash() return hash; } -RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) { +RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) { const std::map<BlockTextureId, glm::vec4> &textureAtlas = AssetManager::Instance().GetTextureAtlasIndexes(); const Section §ion = world->GetSection(sectionPosition); hash = section.GetHash(); sectionPos = sectionPosition; - glm::mat4 baseOffset = glm::translate(glm::mat4(), (section.GetPosition() * 16).glm()),transform; + glm::mat4 baseOffset = glm::translate(glm::mat4(), (section.GetPosition() * 16).glm()), transform; auto sectionsList = world->GetSectionsList(); @@ -196,12 +139,13 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) } else { transform = glm::translate(transform, glm::vec3(0, 1, 0)); - + if (block.id == 8 || block.id == 9) { textures.push_back(AssetManager::Instance().GetTextureByAssetName("minecraft/textures/blocks/water_still")); textures.back().w /= 32.0f; transform = glm::translate(transform, glm::vec3(0, -0.2, 0)); - } else + } + else textures.push_back(AssetManager::Instance().GetTextureByAssetName("minecraft/textures/blocks/tnt_side")); models.push_back(transform); @@ -212,9 +156,47 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) } } } + + CreateVertices(); + models.clear(); + + const int mul = 6; + + textures.resize(textures.size() * mul); + for (int i = textures.size()/mul - 1; i > 0; i--) { + textures[i * mul] = textures[i]; + } + for (int i = 0; i < textures.size(); i += mul) { + for (int j = 1; j < mul; j++) { + textures[i + j] = textures[i]; + } + } + + colors.resize(colors.size() * mul); + for (int i = colors.size() / mul - 1; i > 0; i--) { + colors[i * mul] = colors[i]; + } + for (int i = 0; i < colors.size(); i += mul) { + for (int j = 1; j < mul; j++) { + colors[i + j] = colors[i]; + } + } + + lights.resize(lights.size() * mul); + for (int i = lights.size() / mul - 1; i > 0; i--) { + lights[i * mul] = lights[i]; + } + for (int i = 0; i < lights.size(); i += mul) { + for (int j = 1; j < mul; j++) { + lights[i + j] = lights[i]; + } + } + textures.shrink_to_fit(); models.shrink_to_fit(); colors.shrink_to_fit(); + lights.shrink_to_fit(); + vertices.shrink_to_fit(); } void RendererSectionData::AddFacesByBlockModel(const std::vector<Vector> §ionsList, World *world, Vector blockPos, const BlockModel &model, glm::mat4 transform, unsigned char light, unsigned char skyLight) { @@ -340,4 +322,44 @@ bool RendererSectionData::TestBlockExists(const std::vector<Vector> §ionsLis auto blockModel = AssetManager::Instance().GetBlockModelByBlockId(world->GetSection(section).GetBlockId(blockPos)); return blockId.id != 0 && blockModel && blockModel->IsBlock; +} + +void RendererSectionData::CreateVertices() { + const GLfloat verts[] = { + 0, 0, 0, + 1, 0, 1, + 1, 0, 0, + + 0, 0, 0, + 0, 0, 1, + 1, 0, 1, + }; + + const GLfloat uvs[] = { + 0.0f, 0.0f, + 1.0f, 1.0f, + 0.0f, 1.0f, + + 0.0f, 0.0f, + 1.0f, 0.0f, + 1.0f, 1.0f, + }; + + for (const auto &model : models) { + vertices.push_back(model * glm::vec4(verts[0], verts[1], verts[2], 1.0f)); + vertices.push_back(model * glm::vec4(verts[3], verts[4], verts[5], 1.0f)); + vertices.push_back(model * glm::vec4(verts[6], verts[7], verts[8], 1.0f)); + + vertices.push_back(model * glm::vec4(verts[9], verts[10], verts[11], 1.0f)); + vertices.push_back(model * glm::vec4(verts[12], verts[13], verts[14], 1.0f)); + vertices.push_back(model * glm::vec4(verts[15], verts[16], verts[17], 1.0f)); + + uv.push_back(glm::vec2(uvs[0], uvs[1])); + uv.push_back(glm::vec2(uvs[2], uvs[3])); + uv.push_back(glm::vec2(uvs[4], uvs[5])); + + uv.push_back(glm::vec2(uvs[6], uvs[7])); + uv.push_back(glm::vec2(uvs[8], uvs[9])); + uv.push_back(glm::vec2(uvs[10], uvs[11])); + } }
\ No newline at end of file diff --git a/src/RendererSection.hpp b/src/RendererSection.hpp index 1c7fc3d..9ba694e 100644 --- a/src/RendererSection.hpp +++ b/src/RendererSection.hpp @@ -14,6 +14,8 @@ #include "Renderer.hpp" struct RendererSectionData { + std::vector<glm::vec3> vertices; + std::vector<glm::vec2> uv; std::vector<glm::mat4> models; std::vector<glm::vec4> textures; std::vector<glm::vec3> colors; @@ -26,10 +28,13 @@ private: bool TestBlockExists(const std::vector<Vector> §ionsList, World *world, Vector blockPos); void AddFacesByBlockModel(const std::vector<Vector> §ionsList, World *world, Vector blockPos, const BlockModel &model, glm::mat4 transform, unsigned char light, unsigned char skyLight); + + void CreateVertices(); }; class RendererSection { enum Vbos { - MODELS = 0, + VERTICES = 0, + UV, TEXTURES, COLORS, LIGHTS, diff --git a/src/Shader.cpp b/src/Shader.cpp index 164da69..2b69c02 100644 --- a/src/Shader.cpp +++ b/src/Shader.cpp @@ -1,5 +1,7 @@ #include "Shader.hpp" +#include "Event.hpp" + Shader::Shader(const GLchar *vertexPath, const GLchar *fragmentPath, const GLchar *geometryPath) { vertex = vertexPath; fragment = fragmentPath; @@ -88,7 +90,7 @@ Shader::Shader(const GLchar *vertexPath, const GLchar *fragmentPath, const GLcha glGetProgramiv(this->Program, GL_LINK_STATUS, &success); if (!success) { glGetProgramInfoLog(this->Program, 512, NULL, infoLog); - LOG(FATAL) << "Shader program not linked: " << infoLog; + EventAgregator::PushEvent(EventType::Disconnect, DisconnectData{ "Shader linking failed" }); } glDeleteShader(vertex); diff --git a/src/World.cpp b/src/World.cpp index 8cff53a..80ee4b9 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -120,7 +120,7 @@ static Section fallbackSection; const Section &World::GetSection(Vector sectionPos) { auto result = sections.find(sectionPos); if (result == sections.end()) { - LOG(ERROR) << "Accessed not loaded section " << sectionPos; + //LOG(ERROR) << "Accessed not loaded section " << sectionPos; return fallbackSection; } else { |