diff options
author | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2017-09-19 17:52:09 +0200 |
---|---|---|
committer | LaG1924 <12997935+LaG1924@users.noreply.github.com> | 2018-01-13 03:39:31 +0100 |
commit | d56fa5d68fcdfa2fdfc601d10e9292d6a5883dcd (patch) | |
tree | 86133ce5c166d20cc25706b09a81225105a580e2 | |
parent | 2017-09-17 (diff) | |
download | AltCraft-d56fa5d68fcdfa2fdfc601d10e9292d6a5883dcd.tar AltCraft-d56fa5d68fcdfa2fdfc601d10e9292d6a5883dcd.tar.gz AltCraft-d56fa5d68fcdfa2fdfc601d10e9292d6a5883dcd.tar.bz2 AltCraft-d56fa5d68fcdfa2fdfc601d10e9292d6a5883dcd.tar.lz AltCraft-d56fa5d68fcdfa2fdfc601d10e9292d6a5883dcd.tar.xz AltCraft-d56fa5d68fcdfa2fdfc601d10e9292d6a5883dcd.tar.zst AltCraft-d56fa5d68fcdfa2fdfc601d10e9292d6a5883dcd.zip |
-rw-r--r-- | cwd/shaders/face.fs | 4 | ||||
-rw-r--r-- | cwd/shaders/face.vs | 15 | ||||
-rw-r--r-- | src/Render.cpp | 4 | ||||
-rw-r--r-- | src/RendererSection.cpp | 89 | ||||
-rw-r--r-- | src/RendererSection.hpp | 7 | ||||
-rw-r--r-- | src/RendererWorld.cpp | 5 | ||||
-rw-r--r-- | src/Shader.cpp | 4 | ||||
-rw-r--r-- | src/World.cpp | 2 |
8 files changed, 97 insertions, 33 deletions
diff --git a/cwd/shaders/face.fs b/cwd/shaders/face.fs index ae2eb56..f4fd1ff 100644 --- a/cwd/shaders/face.fs +++ b/cwd/shaders/face.fs @@ -40,6 +40,10 @@ vec3 hsv2rgb(vec3 c) } void main() { +// gl_FragColor = vec4(fs_in.Face / 1000.0f, fs_in.Face / 1000.0f, fs_in.Face / 1000.0f, 1.0f); + gl_FragColor = vec4(fs_in.UvPosition.xy,0.0f,1.0f); + return; + gl_FragColor = texture(textureAtlas,TransformTextureCoord(fs_in.Texture,fs_in.UvPosition)); if (gl_FragColor.a < 0.3) discard; diff --git a/cwd/shaders/face.vs b/cwd/shaders/face.vs index 4cf4e75..96204f1 100644 --- a/cwd/shaders/face.vs +++ b/cwd/shaders/face.vs @@ -1,9 +1,9 @@ #version 330 core layout (location = 0) in vec3 position; -layout (location = 2) in vec2 UvCoordinates; +/*layout (location = 2) in vec2 UvCoordinates; layout (location = 7) in vec4 Texture; layout (location = 12) in vec3 color; -layout (location = 13) in vec2 light; +layout (location = 13) in vec2 light;*/ out VS_OUT { vec2 UvPosition; @@ -15,15 +15,22 @@ out VS_OUT { uniform mat4 view; uniform mat4 projection; +uniform mat4 model; void main() { vec4 sourcePosition = vec4(position,1.0f); - gl_Position = projection * view * sourcePosition; + gl_Position = projection * view * model * sourcePosition; - vs_out.UvPosition = vec2(UvCoordinates.x,UvCoordinates.y); +/* vs_out.UvPosition = vec2(UvCoordinates.x,UvCoordinates.y); vs_out.Texture = Texture; vs_out.Color = color; vs_out.Light = light; + vs_out.Face = gl_VertexID / 6;*/ + + vs_out.UvPosition = vec2(0,0); + vs_out.Texture = vec4(0,0,1,1); + vs_out.Color = vec3(0,0.2,1.0); + vs_out.Light = vec2(16,16); vs_out.Face = gl_VertexID / 6; } diff --git a/src/Render.cpp b/src/Render.cpp index 1473ae0..cdbb480 100644 --- a/src/Render.cpp +++ b/src/Render.cpp @@ -64,9 +64,9 @@ void Render::InitGlew() { glViewport(0, 0, width, height); glClearColor(0.8,0.8,0.8, 1.0f); glEnable(GL_DEPTH_TEST); - glEnable(GL_CULL_FACE); + /*glEnable(GL_CULL_FACE); glCullFace(GL_BACK); - glFrontFace(GL_CCW); + glFrontFace(GL_CCW);*/ glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glCheckError(); diff --git a/src/RendererSection.cpp b/src/RendererSection.cpp index b02f044..1f0a0a3 100644 --- a/src/RendererSection.cpp +++ b/src/RendererSection.cpp @@ -2,20 +2,58 @@ #include <thread> +std::vector<glm::vec3> vertArray; +GLuint vertVbo = -1; +std::mutex vertMutex; + +GLuint GetVertex(glm::vec3 vertex) { + vertMutex.lock(); + int i = 0; + for (; i < vertArray.size(); i++) { + if (vertArray[i] == vertex) { + vertMutex.unlock(); + return i; + } + } + vertArray.push_back(vertex); + vertMutex.unlock(); + return i + 1; +} + +void SyncVertices() { + vertMutex.lock(); + if (vertVbo == -1) { + glGenBuffers(1, &vertVbo); + } + glBindBuffer(GL_ARRAY_BUFFER, vertVbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * vertArray.size(), vertArray.data(), GL_DYNAMIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glCheckError(); + vertMutex.unlock(); +} + + RendererSection::RendererSection(RendererSectionData data) { + SyncVertices(); + glGenVertexArrays(1, &Vao); + glCheckError(); glGenBuffers(VBOCOUNT, Vbo); + glCheckError(); glBindVertexArray(Vao); { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Vbo[IBO]); + glCheckError(); + //Cube vertices GLuint VertAttribPos = 0; - glBindBuffer(GL_ARRAY_BUFFER, Vbo[VERTICES]); + glBindBuffer(GL_ARRAY_BUFFER, vertVbo); glVertexAttribPointer(VertAttribPos, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr); glEnableVertexAttribArray(VertAttribPos); - //Cube UVs + /*//Cube UVs GLuint UvAttribPos = 2; glBindBuffer(GL_ARRAY_BUFFER, Vbo[UV]); glVertexAttribPointer(UvAttribPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr); @@ -26,7 +64,6 @@ RendererSection::RendererSection(RendererSectionData data) { glBindBuffer(GL_ARRAY_BUFFER, Vbo[TEXTURES]); glVertexAttribPointer(textureAttribPos, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), nullptr); glEnableVertexAttribArray(textureAttribPos); - glCheckError(); //Color GLuint colorAttribPos = 12; @@ -38,35 +75,35 @@ RendererSection::RendererSection(RendererSectionData data) { GLuint lightAttribPos = 13; glBindBuffer(GL_ARRAY_BUFFER, Vbo[LIGHTS]); glVertexAttribPointer(lightAttribPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr); - glEnableVertexAttribArray(lightAttribPos); - - glBindBuffer(GL_ARRAY_BUFFER, 0); + glEnableVertexAttribArray(lightAttribPos);*/ } glBindVertexArray(0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glCheckError(); //Upload data to VRAM - 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[IBO]); + glBufferData(GL_ARRAY_BUFFER, data.indices.size() * sizeof(GLuint), data.indices.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[TEXTURES]); - glBufferData(GL_ARRAY_BUFFER, data.textures.size() * sizeof(glm::vec4), data.textures.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); glBindBuffer(GL_ARRAY_BUFFER, Vbo[LIGHTS]); - glBufferData(GL_ARRAY_BUFFER, data.lights.size() * sizeof(glm::vec2), data.lights.data(), GL_DYNAMIC_DRAW); + 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) { @@ -97,7 +134,7 @@ void swap(RendererSection & lhs, RendererSection & rhs) { void RendererSection::Render(RenderState &renderState) { renderState.SetActiveVao(Vao); - glDrawArrays(GL_TRIANGLES, 0, numOfFaces); + glDrawElements(GL_TRIANGLES, 6 * numOfFaces, GL_UNSIGNED_INT, nullptr); glCheckError(); } @@ -117,7 +154,8 @@ RendererSectionData::RendererSectionData(World * world, Vector 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; + glm::mat4 transform; auto sectionsList = world->GetSectionsList(); @@ -131,7 +169,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) const bool useNewMethod = true; - transform = glm::translate(baseOffset, Vector(x, y, z).glm()); + transform = glm::translate(glm::mat4(), Vector(x, y, z).glm()); const BlockModel* model = AssetManager::Instance().GetBlockModelByBlockId(block); if (model) { @@ -158,9 +196,12 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) } CreateVertices(); + + ReplaceVertices(); + models.clear(); - const int mul = 6; + /* const int mul = 6; textures.resize(textures.size() * mul); for (int i = textures.size()/mul - 1; i > 0; i--) { @@ -190,13 +231,13 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) 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(); + 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) { @@ -362,4 +403,10 @@ void RendererSectionData::CreateVertices() { uv.push_back(glm::vec2(uvs[8], uvs[9])); uv.push_back(glm::vec2(uvs[10], uvs[11])); } +} + +void RendererSectionData::ReplaceVertices() { + for (auto& it : vertices) { + indices.push_back(GetVertex(it)); + } }
\ No newline at end of file diff --git a/src/RendererSection.hpp b/src/RendererSection.hpp index 9ba694e..de042f2 100644 --- a/src/RendererSection.hpp +++ b/src/RendererSection.hpp @@ -14,6 +14,7 @@ #include "Renderer.hpp" struct RendererSectionData { + std::vector<GLuint> indices; std::vector<glm::vec3> vertices; std::vector<glm::vec2> uv; std::vector<glm::mat4> models; @@ -30,10 +31,14 @@ private: 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(); + + void ReplaceVertices(); }; + class RendererSection { enum Vbos { - VERTICES = 0, + //VERTICES = 0, + IBO = 0, UV, TEXTURES, COLORS, diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index a641cf2..282d97e 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -218,6 +218,7 @@ void RendererWorld::Render(RenderState & renderState) { GLint projectionLoc = glGetUniformLocation(blockShader->Program, "projection"); GLint viewLoc = glGetUniformLocation(blockShader->Program, "view"); GLint windowSizeLoc = glGetUniformLocation(blockShader->Program, "windowSize"); + GLint modelLoc = glGetUniformLocation(blockShader->Program, "model"); glm::mat4 projection = glm::perspective(45.0f, (float)renderState.WindowWidth / (float)renderState.WindowHeight, 0.1f, 10000000.0f); glm::mat4 view = gs->GetViewMatrix(); glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); @@ -257,6 +258,8 @@ void RendererWorld::Render(RenderState & renderState) { sectionsMutex.lock(); continue; } + glm::mat4 transform = glm::translate(glm::mat4(), (section.second.GetPosition() * 16).glm()); + glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(transform)); section.second.Render(renderState); sectionsMutex.lock(); } @@ -271,7 +274,7 @@ void RendererWorld::Render(RenderState & renderState) { glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); glCheckError(); - GLint modelLoc = glGetUniformLocation(entityShader->Program, "model"); + modelLoc = glGetUniformLocation(entityShader->Program, "model"); GLint colorLoc = glGetUniformLocation(entityShader->Program, "color"); for (auto& it : entities) { it.modelLoc = modelLoc; diff --git a/src/Shader.cpp b/src/Shader.cpp index 2b69c02..164da69 100644 --- a/src/Shader.cpp +++ b/src/Shader.cpp @@ -1,7 +1,5 @@ #include "Shader.hpp" -#include "Event.hpp" - Shader::Shader(const GLchar *vertexPath, const GLchar *fragmentPath, const GLchar *geometryPath) { vertex = vertexPath; fragment = fragmentPath; @@ -90,7 +88,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); - EventAgregator::PushEvent(EventType::Disconnect, DisconnectData{ "Shader linking failed" }); + LOG(FATAL) << "Shader program not linked: " << infoLog; } glDeleteShader(vertex); diff --git a/src/World.cpp b/src/World.cpp index 80ee4b9..8cff53a 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 { |