summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2017-09-20 17:54:09 +0200
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2018-01-13 03:39:32 +0100
commit6bab0ab86a72881c3e0b88284c211e01fb22435a (patch)
tree3594305ad612f0e59ee630c2b5a59d974fc8bac5 /src
parent2017-09-19 (diff)
downloadAltCraft-6bab0ab86a72881c3e0b88284c211e01fb22435a.tar
AltCraft-6bab0ab86a72881c3e0b88284c211e01fb22435a.tar.gz
AltCraft-6bab0ab86a72881c3e0b88284c211e01fb22435a.tar.bz2
AltCraft-6bab0ab86a72881c3e0b88284c211e01fb22435a.tar.lz
AltCraft-6bab0ab86a72881c3e0b88284c211e01fb22435a.tar.xz
AltCraft-6bab0ab86a72881c3e0b88284c211e01fb22435a.tar.zst
AltCraft-6bab0ab86a72881c3e0b88284c211e01fb22435a.zip
Diffstat (limited to '')
-rw-r--r--src/Render.cpp4
-rw-r--r--src/RendererSection.cpp322
-rw-r--r--src/RendererSection.hpp23
-rw-r--r--src/RendererWorld.cpp14
4 files changed, 145 insertions, 218 deletions
diff --git a/src/Render.cpp b/src/Render.cpp
index cdbb480..1473ae0 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 1f0a0a3..59e3558 100644
--- a/src/RendererSection.cpp
+++ b/src/RendererSection.cpp
@@ -2,106 +2,136 @@
#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();
-}
-
+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;
+GLuint RendererSection::Vao;
+GLuint RendererSection::Vbo[VBOCOUNT];
+
+std::vector<glm::mat4> contentModels;
+std::vector<glm::vec4> contentTextures;
+std::vector<glm::vec3> contentColors;
+std::vector<glm::vec2> contentLights;
RendererSection::RendererSection(RendererSectionData data) {
- SyncVertices();
-
- glGenVertexArrays(1, &Vao);
- glCheckError();
-
- glGenBuffers(VBOCOUNT, Vbo);
- glCheckError();
-
- glBindVertexArray(Vao);
- {
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Vbo[IBO]);
- glCheckError();
+ if (VboVertices == magicUniqueConstant) {
+ glGenBuffers(1, &VboVertices);
+ glGenBuffers(1, &VboUvs);
//Cube vertices
- GLuint VertAttribPos = 0;
- glBindBuffer(GL_ARRAY_BUFFER, vertVbo);
- glVertexAttribPointer(VertAttribPos, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);
- glEnableVertexAttribArray(VertAttribPos);
-
- /*//Cube UVs
- GLuint UvAttribPos = 2;
- glBindBuffer(GL_ARRAY_BUFFER, Vbo[UV]);
- glVertexAttribPointer(UvAttribPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr);
- glEnableVertexAttribArray(UvAttribPos);
-
- //Textures
- GLuint textureAttribPos = 7;
- glBindBuffer(GL_ARRAY_BUFFER, Vbo[TEXTURES]);
- glVertexAttribPointer(textureAttribPos, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), nullptr);
- glEnableVertexAttribArray(textureAttribPos);
-
- //Color
- GLuint colorAttribPos = 12;
- glBindBuffer(GL_ARRAY_BUFFER, Vbo[COLORS]);
- glVertexAttribPointer(colorAttribPos, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);
- glEnableVertexAttribArray(colorAttribPos);
-
- //Light
- 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, 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);
+
+ glBindVertexArray(Vao);
+ {
+ //Cube vertices
+ GLuint VertAttribPos = 0;
+ glBindBuffer(GL_ARRAY_BUFFER, VboVertices);
+ glVertexAttribPointer(VertAttribPos, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);
+ glEnableVertexAttribArray(VertAttribPos);
+
+ //Cube UVs
+ GLuint UvAttribPos = 2;
+ glBindBuffer(GL_ARRAY_BUFFER, VboUvs);
+ glVertexAttribPointer(UvAttribPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr);
+ glEnableVertexAttribArray(UvAttribPos);
+
+ //Textures
+ GLuint textureAttribPos = 7;
+ 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);
+ }
+ glBindVertexArray(0);
+ glCheckError();
}
- glBindVertexArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- glCheckError();
-
//Upload data to VRAM
- 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);
+ contentModels.insert(contentModels.end(), data.models.begin(), data.models.end());
+ glBindBuffer(GL_ARRAY_BUFFER, Vbo[MODELS]);
+ glBufferData(GL_ARRAY_BUFFER, contentModels.size() * sizeof(glm::mat4), contentModels.data(), GL_DYNAMIC_DRAW);
+
+ contentTextures.insert(contentTextures.end(), data.textures.begin(), data.textures.end());
+ glBindBuffer(GL_ARRAY_BUFFER, Vbo[TEXTURES]);
+ glBufferData(GL_ARRAY_BUFFER, contentTextures.size() * sizeof(glm::vec4), contentTextures.data(), GL_DYNAMIC_DRAW);
+ contentColors.insert(contentColors.end(), data.colors.begin(), data.colors.end());
glBindBuffer(GL_ARRAY_BUFFER, Vbo[COLORS]);
- glBufferData(GL_ARRAY_BUFFER, data.colors.size() * sizeof(glm::vec3), data.colors.data(), GL_DYNAMIC_DRAW);
+ glBufferData(GL_ARRAY_BUFFER, contentColors.size() * sizeof(glm::vec3), contentColors.data(), GL_DYNAMIC_DRAW);
+ contentLights.insert(contentLights.end(), data.lights.begin(), data.lights.end());
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, contentLights.size() * sizeof(glm::vec2), contentLights.data(), GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
numOfFaces = data.textures.size();
+ offset = contentModels.size() - numOfFaces;
sectionPos = data.sectionPos;
hash = data.hash;
}
@@ -112,30 +142,13 @@ 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]);
- glBufferData(GL_ARRAY_BUFFER, 0, 0, GL_STATIC_DRAW);
- }
-
- glDeleteBuffers(VBOCOUNT, Vbo);
}
void swap(RendererSection & lhs, RendererSection & rhs) {
- std::swap(lhs.Vbo, rhs.Vbo);
- std::swap(lhs.Vao, rhs.Vao);
std::swap(lhs.hash, rhs.hash);
std::swap(lhs.numOfFaces, rhs.numOfFaces);
std::swap(lhs.sectionPos, rhs.sectionPos);
-}
-
-void RendererSection::Render(RenderState &renderState) {
- renderState.SetActiveVao(Vao);
- glDrawElements(GL_TRIANGLES, 6 * numOfFaces, GL_UNSIGNED_INT, nullptr);
- glCheckError();
+ std::swap(lhs.offset, rhs.offset);
}
Vector RendererSection::GetPosition()
@@ -148,14 +161,17 @@ size_t RendererSection::GetHash()
return hash;
}
-RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) {
+GLuint RendererSection::GetVao() {
+ return Vao;
+}
+
+RendererSectionData::RendererSectionData(World * world, Vector sectionPosition) {
const std::map<BlockTextureId, glm::vec4> &textureAtlas = AssetManager::Instance().GetTextureAtlasIndexes();
const Section &section = world->GetSection(sectionPosition);
hash = section.GetHash();
sectionPos = sectionPosition;
- //glm::mat4 baseOffset = glm::translate(glm::mat4(), (section.GetPosition() * 16).glm()), transform;
- glm::mat4 transform;
+ glm::mat4 baseOffset = glm::translate(glm::mat4(), (section.GetPosition() * 16).glm()),transform;
auto sectionsList = world->GetSectionsList();
@@ -169,7 +185,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition)
const bool useNewMethod = true;
- transform = glm::translate(glm::mat4(), Vector(x, y, z).glm());
+ transform = glm::translate(baseOffset, Vector(x, y, z).glm());
const BlockModel* model = AssetManager::Instance().GetBlockModelByBlockId(block);
if (model) {
@@ -177,13 +193,12 @@ 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);
@@ -194,50 +209,9 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition)
}
}
}
-
- CreateVertices();
-
- ReplaceVertices();
-
- 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> &sectionsList, World *world, Vector blockPos, const BlockModel &model, glm::mat4 transform, unsigned char light, unsigned char skyLight) {
@@ -363,50 +337,4 @@ bool RendererSectionData::TestBlockExists(const std::vector<Vector> &sectionsLis
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]));
- }
-}
-
-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 de042f2..fa4a389 100644
--- a/src/RendererSection.hpp
+++ b/src/RendererSection.hpp
@@ -14,9 +14,6 @@
#include "Renderer.hpp"
struct RendererSectionData {
- std::vector<GLuint> indices;
- 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;
@@ -29,25 +26,19 @@ private:
bool TestBlockExists(const std::vector<Vector> &sectionsList, World *world, Vector blockPos);
void AddFacesByBlockModel(const std::vector<Vector> &sectionsList, 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,
- IBO = 0,
- UV,
+ MODELS = 0,
TEXTURES,
COLORS,
LIGHTS,
VBOCOUNT,
};
- GLuint Vao = { 0 };
- GLuint Vbo[VBOCOUNT] = { 0 };
-
+
+ static GLuint Vao;
+ static GLuint Vbo[VBOCOUNT];
static GLuint VboVertices, VboUvs;
size_t hash;
@@ -61,13 +52,15 @@ public:
~RendererSection();
- void Render(RenderState &renderState);
-
Vector GetPosition();
size_t GetHash();
size_t numOfFaces;
+ size_t offset;
+
+ static GLuint GetVao();
+
friend void swap(RendererSection &lhs, RendererSection &rhs);
}; \ No newline at end of file
diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp
index 282d97e..f13011e 100644
--- a/src/RendererWorld.cpp
+++ b/src/RendererWorld.cpp
@@ -226,8 +226,13 @@ void RendererWorld::Render(RenderState & renderState) {
glUniform2f(windowSizeLoc, renderState.WindowWidth, renderState.WindowHeight);
glCheckError();
+ glBindVertexArray(RendererSection::GetVao());
+
+ size_t faces = 0;
+
sectionsMutex.lock();
- for (auto& section : sections) {
+ for (auto& section : sections) {
+ faces += section.second.numOfFaces;
sectionsMutex.unlock();
std::vector<Vector> sectionCorners = {
Vector(0, 0, 0),
@@ -258,12 +263,13 @@ 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);
+
+ //glDrawArraysInstanced(GL_TRIANGLES, section.second.offset, 6, section.second.numOfFaces);
sectionsMutex.lock();
}
sectionsMutex.unlock();
+ glBindVertexArray(RendererSection::GetVao());
+ glDrawArraysInstanced(GL_TRIANGLES, 0, 6, faces);
glCheckError();
glLineWidth(3.0);