summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--RenderSection.cpp281
-rw-r--r--cwd/shaders/entity.fs2
-rw-r--r--cwd/shaders/face.fs4
-rw-r--r--cwd/shaders/face.vs3
-rw-r--r--cwd/shaders/sky.fs49
-rw-r--r--cwd/shaders/sky.vs17
-rw-r--r--src/AssetManager.cpp2
-rw-r--r--src/Block.cpp14
-rw-r--r--src/Block.hpp9
-rw-r--r--src/GameState.cpp6
-rw-r--r--src/GameState.hpp3
-rw-r--r--src/Network.cpp2
-rw-r--r--src/Packet.hpp18
-rw-r--r--src/Render.cpp11
-rw-r--r--src/Renderer.hpp5
-rw-r--r--src/RendererEntity.cpp17
-rw-r--r--src/RendererEntity.hpp7
-rw-r--r--src/RendererSection.cpp119
-rw-r--r--src/RendererSection.hpp3
-rw-r--r--src/RendererSky.cpp140
-rw-r--r--src/RendererSky.hpp16
-rw-r--r--src/RendererWorld.cpp78
-rw-r--r--src/RendererWorld.hpp15
-rw-r--r--src/Section.cpp87
-rw-r--r--src/Section.hpp2
-rw-r--r--src/ThreadGame.cpp2
-rw-r--r--src/World.cpp12
27 files changed, 467 insertions, 457 deletions
diff --git a/RenderSection.cpp b/RenderSection.cpp
deleted file mode 100644
index fec67b5..0000000
--- a/RenderSection.cpp
+++ /dev/null
@@ -1,281 +0,0 @@
-#include <graphics/RenderSection.hpp>
-
-const GLfloat vertices[] = {
- //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,
-
- //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,
-
- //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,
- -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,
- 0.5f, -0.5f, -0.5f,
- 0.5f, 0.5f, -0.5f,
-
- //Y+ 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,
-
- //Y- 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,
-};
-
-const GLfloat uv_coords[] = {
- //Z+
- 0.0f, 1.0f,
- 0.0f, 0.0f,
- 1.0f, 0.0f,
- 0.0f, 1.0f,
- 1.0f, 0.0f,
- 1.0f, 1.0f,
-
- //Z-
- 1.0f, 0.0f,
- 1.0f, 1.0f,
- 0.0f, 0.0f,
- 0.0f, 0.0f,
- 1.0f, 1.0f,
- 0.0f, 1.0f,
-
- //X+
- 0.0f, 0.0f,
- 1.0f, 0.0f,
- 0.0f, 1.0f,
- 0.0f, 1.0f,
- 1.0f, 0.0f,
- 1.0f, 1.0f,
-
- //X-
- 0.0f, 0.0f,
- 1.0f, 1.0f,
- 0.0f, 1.0f,
- 0.0f, 0.0f,
- 1.0f, 0.0f,
- 1.0f, 1.0f,
-
- //Y+
- 0.0f, 0.0f,
- 1.0f, 1.0f,
- 0.0f, 1.0f,
- 0.0f, 0.0f,
- 1.0f, 0.0f,
- 1.0f, 1.0f,
-
- //Y-
- 1.0f, 0.0f,
- 0.0f, 1.0f,
- 0.0f, 0.0f,
- 1.0f, 1.0f,
- 0.0f, 1.0f,
- 1.0f, 0.0f,
-};
-
-void RenderState::SetActiveVao(GLuint Vao) {
- if (Vao != ActiveVao) {
- glBindVertexArray(Vao);
- ActiveVao = Vao;
- }
-}
-
-void RenderState::SetActiveShader(GLuint Shader) {
- if (Shader != ActiveShader) {
- glUseProgram(Shader);
- ActiveShader = Shader;
- }
-}
-
-const GLuint magicUniqueConstant = 88375;
-GLuint RenderSection::VboVertices = magicUniqueConstant;
-GLuint RenderSection::VboUvs = magicUniqueConstant;
-std::map<GLuint, int> RenderSection::refCounterVbo;
-std::map<GLuint, int> RenderSection::refCounterVao;
-
-
-RenderSection::RenderSection(World *world, Vector position) : sectionPosition(position), world(world) {
-
- 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 ordinary blocks";
- }
-
- glGenBuffers(1, &VboBlocks);
- if (refCounterVbo.find(VboBlocks) == refCounterVbo.end())
- refCounterVbo[VboBlocks] = 0;
- refCounterVbo[VboBlocks]++;
-
- glGenBuffers(1, &VboModels);
- if (refCounterVbo.find(VboModels) == refCounterVbo.end())
- refCounterVbo[VboModels] = 0;
- refCounterVbo[VboModels]++;
-
- glGenVertexArrays(1, &Vao);
- if (refCounterVao.find(Vao) == refCounterVao.end())
- refCounterVao[Vao] = 0;
- refCounterVao[Vao]++;
-
- glBindVertexArray(Vao);
- {
- //Cube vertices
- glBindBuffer(GL_ARRAY_BUFFER, VboVertices);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);
- glEnableVertexAttribArray(0);
-
- //Cube UVs
- glBindBuffer(GL_ARRAY_BUFFER, VboUvs);
- glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr);
- glEnableVertexAttribArray(2);
-
- //Blocks ids
- glBindBuffer(GL_ARRAY_BUFFER, VboBlocks);
- glVertexAttribPointer(7, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr);
- glEnableVertexAttribArray(7);
- glVertexAttribDivisor(7, 1);
- glCheckError();
-
- //Blocks models
- size_t sizeOfMat4 = 4 * 4 * sizeof(GLfloat);
- glBindBuffer(GL_ARRAY_BUFFER, VboModels);
- glVertexAttribPointer(8 + 0, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, nullptr);
- glVertexAttribPointer(8 + 1, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, (void *) (1 * 4 * sizeof(GLfloat)));
- glVertexAttribPointer(8 + 2, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, (void *) (2 * 4 * sizeof(GLfloat)));
- glVertexAttribPointer(8 + 3, 4, GL_FLOAT, GL_FALSE, sizeOfMat4, (void *) (3 * 4 * sizeof(GLfloat)));
- glEnableVertexAttribArray(8 + 0);
- glEnableVertexAttribArray(8 + 1);
- glEnableVertexAttribArray(8 + 2);
- glEnableVertexAttribArray(8 + 3);
- glVertexAttribDivisor(8 + 0, 1);
- glVertexAttribDivisor(8 + 1, 1);
- glVertexAttribDivisor(8 + 2, 1);
- glVertexAttribDivisor(8 + 3, 1);
-
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- }
- glBindVertexArray(0);
- UpdateState();
- glCheckError();
-}
-
-RenderSection::~RenderSection() {
- refCounterVbo[VboBlocks]--;
- refCounterVbo[VboModels]--;
- refCounterVao[Vao]--;
- if (refCounterVbo[VboBlocks] <= 0)
- glDeleteBuffers(1, &VboBlocks);
- if (refCounterVbo[VboModels] <= 0)
- glDeleteBuffers(1, &VboBlocks);
- if (refCounterVao[Vao] <= 0)
- glDeleteVertexArrays(1, &Vao);
-}
-
-void RenderSection::UpdateState() {
- Section *section = &world->sections.find(sectionPosition)->second;
- std::vector<glm::mat4> models;
- std::vector<glm::vec2> blocks;
- for (int y = 0; y < 16; y++) {
- for (int z = 0; z < 16; z++) {
- for (int x = 0; x < 16; x++) {
- Block block = section->GetBlock(Vector(x, y, z));
- if (block.id == 0)
- continue;
-
- unsigned char isVisible = 0;
- if (x == 0 || x == 15 || y == 0 || y == 15 || z == 0 || z == 15) {
- isVisible = 0;
- } else {
- isVisible |= (section->GetBlock(Vector(x + 1, y, z)).id != 0) << 0;
- isVisible |= (section->GetBlock(Vector(x - 1, y, z)).id != 0) << 1;
- isVisible |= (section->GetBlock(Vector(x, y + 1, z)).id != 0) << 2;
- isVisible |= (section->GetBlock(Vector(x, y - 1, z)).id != 0) << 3;
- isVisible |= (section->GetBlock(Vector(x, y, z + 1)).id != 0) << 4;
- isVisible |= (section->GetBlock(Vector(x, y, z - 1)).id != 0) << 5;
- }
- if (isVisible == 0x3F)
- continue;
-
- glm::vec2 data(block.id, block.state);
- blocks.push_back(data);
- glm::mat4 model;
- model = glm::translate(model, glm::vec3(section->GetPosition().GetX() * 16,
- section->GetPosition().GetY() * 16,
- section->GetPosition().GetZ() * 16));
- model = glm::translate(model, glm::vec3(x, y, z));
- double size = 0.999;
- model = glm::scale(model, glm::vec3(size, size, size));
- models.push_back(model);
- }
- }
- }
- glBindBuffer(GL_ARRAY_BUFFER, VboBlocks);
- glBufferData(GL_ARRAY_BUFFER, blocks.size() * sizeof(glm::vec2), blocks.data(), GL_DYNAMIC_DRAW);
-
- glBindBuffer(GL_ARRAY_BUFFER, VboModels);
- glBufferData(GL_ARRAY_BUFFER, models.size() * sizeof(glm::mat4), models.data(), GL_DYNAMIC_DRAW);
-
- glBindBuffer(GL_ARRAY_BUFFER, 0);
-
- numOfBlocks = blocks.size();
-}
-
-void RenderSection::Render(RenderState &state) {
- state.SetActiveVao(Vao);
- glDrawArraysInstanced(GL_TRIANGLES, 0, 36, numOfBlocks);
- glCheckError();
-}
-
-Section *RenderSection::GetSection() {
- return &world->sections.find(sectionPosition)->second;
-}
-
-RenderSection::RenderSection(const RenderSection &other) {
- this->world = other.world;
- this->VboModels = other.VboModels;
- this->VboBlocks = other.VboBlocks;
- this->sectionPosition = other.sectionPosition;
- this->Vao = other.Vao;
- this->numOfBlocks = other.numOfBlocks;
-
- refCounterVbo[VboBlocks]++;
- refCounterVbo[VboModels]++;
- refCounterVao[Vao]++;
-}
diff --git a/cwd/shaders/entity.fs b/cwd/shaders/entity.fs
index 6935d1b..f0bb42f 100644
--- a/cwd/shaders/entity.fs
+++ b/cwd/shaders/entity.fs
@@ -4,6 +4,6 @@ uniform vec3 color;
in vec2 uvPos;
void main(){
- if (uvPos.x < 0.9 && uvPos.x > 0.1 && uvPos.y < 0.9 && uvPos.y > 0.1) discard;
+ //if (uvPos.x < 0.9 && uvPos.x > 0.1 && uvPos.y < 0.9 && uvPos.y > 0.1) discard;
gl_FragColor = vec4(color,1);
} \ No newline at end of file
diff --git a/cwd/shaders/face.fs b/cwd/shaders/face.fs
index 4710f90..6e9d8f0 100644
--- a/cwd/shaders/face.fs
+++ b/cwd/shaders/face.fs
@@ -4,6 +4,7 @@ in VS_OUT {
vec2 UvPosition;
vec4 Texture;
vec3 Color;
+ vec2 Light;
} fs_in;
uniform sampler2D textureAtlas;
@@ -44,4 +45,7 @@ void main() {
vec3 hsvColor = rgb2hsv(gl_FragColor.xyz);
hsvColor+=fs_in.Color;
gl_FragColor = vec4(hsv2rgb(hsvColor),1);
+// gl_FragColor = vec4(fs_in.Light.x / 16.0,0,fs_in.Light.y / 16.0,1);
+ float faceLight = clamp((fs_in.Light.x + fs_in.Light.y) / 16.0,0,16);
+ gl_FragColor = vec4(gl_FragColor.rgb * faceLight,gl_FragColor.a);
} \ No newline at end of file
diff --git a/cwd/shaders/face.vs b/cwd/shaders/face.vs
index 95818ef..9bf2639 100644
--- a/cwd/shaders/face.vs
+++ b/cwd/shaders/face.vs
@@ -4,11 +4,13 @@ layout (location = 2) in vec2 UvCoordinates;
layout (location = 7) in vec4 Texture;
layout (location = 8) in mat4 model;
layout (location = 12) in vec3 color;
+layout (location = 13) in vec2 light;
out VS_OUT {
vec2 UvPosition;
vec4 Texture;
vec3 Color;
+ vec2 Light;
} vs_out;
uniform mat4 view;
@@ -22,4 +24,5 @@ void main()
vs_out.UvPosition = vec2(UvCoordinates.x,UvCoordinates.y);
vs_out.Texture = Texture;
vs_out.Color = color;
+ vs_out.Light = light;
}
diff --git a/cwd/shaders/sky.fs b/cwd/shaders/sky.fs
new file mode 100644
index 0000000..549d463
--- /dev/null
+++ b/cwd/shaders/sky.fs
@@ -0,0 +1,49 @@
+#version 330 core
+
+in vec2 uvPos;
+in vec3 pos;
+
+uniform sampler2D textureAtlas;
+uniform vec4 skyTexture;
+uniform float DayTime;
+uniform vec4 sunTexture;
+uniform vec4 moonTexture;
+
+const vec4 DaySkyColor = vec4(0.49,0.66,1, 1);
+
+const vec3 SunPos = vec3(0,0.1,0.5);
+
+const vec3 MoonPos = vec3(0,0.1,-0.5);
+
+vec2 TransformTextureCoord(vec4 TextureAtlasCoords, vec2 UvCoords) {
+ float x = TextureAtlasCoords.x;
+ float y = TextureAtlasCoords.y;
+ float w = TextureAtlasCoords.z;
+ float h = TextureAtlasCoords.w;
+ vec2 A = vec2(x, 1 - y - h);
+ vec2 B = vec2(x + w, 1 - y);
+ return A + UvCoords * (B - A);
+}
+
+vec4 Sun() {
+ vec3 sunDelta = (pos - SunPos)*3.0f;
+ float distanceToSun = length(sunDelta);
+ vec4 sunColor = texture(textureAtlas,TransformTextureCoord(sunTexture,(vec2(sunDelta.xy)+0.5f)));
+ vec4 sun = mix(vec4(0,0,0,1),sunColor,clamp(1-distanceToSun*2.0f,0,1));
+ return sun;
+}
+
+vec4 Moon() {
+ vec3 moonDelta = (pos - MoonPos)*4.5f;
+ float distanceToMoon = length(moonDelta);
+ vec4 moonColor = texture(textureAtlas,TransformTextureCoord(moonTexture,(vec2(moonDelta.xy)+0.5f)));
+ vec4 moon = mix(vec4(0,0,0,1),moonColor,clamp(1-distanceToMoon*2.0f,0,1));
+ return moon;
+}
+
+void main() {
+ vec4 starColor = texture(textureAtlas,TransformTextureCoord(skyTexture,uvPos));
+ gl_FragColor = mix(starColor, DaySkyColor, DayTime);
+ gl_FragColor += Sun();
+ gl_FragColor += Moon();
+} \ No newline at end of file
diff --git a/cwd/shaders/sky.vs b/cwd/shaders/sky.vs
new file mode 100644
index 0000000..983e1f3
--- /dev/null
+++ b/cwd/shaders/sky.vs
@@ -0,0 +1,17 @@
+#version 330 core
+
+uniform mat4 view;
+uniform mat4 projection;
+uniform mat4 model;
+
+layout (location = 0) in vec3 position;
+layout (location = 1) in vec2 uvPosition;
+
+out vec2 uvPos;
+out vec3 pos;
+
+void main(){
+ uvPos = uvPosition;
+ pos = position;
+ gl_Position = projection*view*model*vec4(position,1);
+} \ No newline at end of file
diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp
index 28c08fa..22dac8d 100644
--- a/src/AssetManager.cpp
+++ b/src/AssetManager.cpp
@@ -20,7 +20,7 @@ void AssetManager::LoadIds() {
int id = it["type"].get<int>();
int state = it["meta"].get<int>();
std::string blockName = it["text_type"].get<std::string>();
- assetIds[blockName] = Block(id, state);
+ assetIds[blockName] = Block(id, state, 0, 0);
}
LOG(INFO) << "Loaded " << assetIds.size() << " ids";
}
diff --git a/src/Block.cpp b/src/Block.cpp
index e88068a..4c907d6 100644
--- a/src/Block.cpp
+++ b/src/Block.cpp
@@ -2,16 +2,6 @@
Block::~Block() {}
-Block::Block(unsigned short id, unsigned char state) : id(id), state(state) {}
+Block::Block(unsigned short id, unsigned char state, unsigned char light, unsigned char sky) : id(id), state(state), light(light), sky (sky) {}
-Block::Block() : id(0), state(0) {}
-
-bool operator<(const Block &lhs, const Block &rhs) {
- if (lhs.id < rhs.id)
- return true;
- if (lhs.id == rhs.id) {
- if (lhs.state != rhs.state)
- return lhs.state < rhs.state;
- }
- return false;
-}
+Block::Block() : id(0), state(0), light(0), sky(0) {} \ No newline at end of file
diff --git a/src/Block.hpp b/src/Block.hpp
index 2f823fe..367e559 100644
--- a/src/Block.hpp
+++ b/src/Block.hpp
@@ -3,13 +3,12 @@
struct Block {
Block();
- Block(unsigned short id, unsigned char state);
+ Block(unsigned short id, unsigned char state, unsigned char light, unsigned char sky);
~Block();
unsigned short id : 13;
unsigned char state : 4;
- //unsigned char light:4;
-};
-
-bool operator<(const Block &lhs, const Block &rhs); \ No newline at end of file
+ unsigned char light : 4;
+ unsigned char sky : 4;
+}; \ No newline at end of file
diff --git a/src/GameState.cpp b/src/GameState.cpp
index c49b332..e536df0 100644
--- a/src/GameState.cpp
+++ b/src/GameState.cpp
@@ -358,8 +358,12 @@ void GameState::UpdatePacket()
LOG(INFO) << "Spawn position is " << g_SpawnPosition.x << " " << g_SpawnPosition.y << " " << g_SpawnPosition.z;
break;
}
- case TimeUpdate:
+ case TimeUpdate: {
+ auto packet = std::static_pointer_cast<PacketTimeUpdate>(ptr);
+ WorldAge = packet->WorldAge;
+ TimeOfDay = packet->TimeOfDay;
break;
+ }
case Title:
break;
case SoundEffect:
diff --git a/src/GameState.hpp b/src/GameState.hpp
index f09e5ab..6dbb194 100644
--- a/src/GameState.hpp
+++ b/src/GameState.hpp
@@ -69,5 +69,8 @@ public:
double g_PlayerVelocityY = 0;
double g_PlayerVelocityZ = 0;
+ long long WorldAge = 0;
+ long long TimeOfDay = 0;
+
std::shared_ptr<GameState> gs;
};
diff --git a/src/Network.cpp b/src/Network.cpp
index 4ab85d3..71de943 100644
--- a/src/Network.cpp
+++ b/src/Network.cpp
@@ -205,7 +205,7 @@ std::shared_ptr<Packet> Network::ParsePacketPlay(PacketNamePlayCB id) {
case SpawnPosition:
return std::make_shared<PacketSpawnPosition>();
case TimeUpdate:
- break;
+ return std::make_shared<PacketTimeUpdate>();
case Title:
break;
case SoundEffect:
diff --git a/src/Packet.hpp b/src/Packet.hpp
index 6318eab..e3d09fd 100644
--- a/src/Packet.hpp
+++ b/src/Packet.hpp
@@ -801,4 +801,22 @@ struct PacketMultiBlockChange : Packet {
int BlockId;
};
std::vector<Record> Records;
+};
+
+struct PacketTimeUpdate : Packet {
+ void ToStream(StreamOutput *stream) override {
+
+ }
+
+ void FromStream(StreamInput *stream) override {
+ WorldAge = stream->ReadLong();
+ TimeOfDay = stream->ReadLong();
+ }
+
+ int GetPacketId() override {
+ return PacketNamePlayCB::TimeUpdate;
+ }
+
+ long long WorldAge;
+ long long TimeOfDay;
}; \ No newline at end of file
diff --git a/src/Render.cpp b/src/Render.cpp
index a175278..56541e9 100644
--- a/src/Render.cpp
+++ b/src/Render.cpp
@@ -5,7 +5,7 @@
#include "AssetManager.hpp"
#include "Event.hpp"
-Render::Render(unsigned int windowWidth, unsigned int windowHeight, std::string windowTitle) : timer(std::chrono::milliseconds(16)) {
+Render::Render(unsigned int windowWidth, unsigned int windowHeight, std::string windowTitle) : timer(std::chrono::milliseconds(0)) {
InitSfml(windowWidth, windowHeight, windowTitle);
glCheckError();
InitGlew();
@@ -47,20 +47,21 @@ void Render::InitGlew() {
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
- //glEnable(GL_BLEND);
- //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ glLineWidth(3.0);
glCheckError();
}
void Render::PrepareToRendering() {
//TextureAtlas texture
glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, AssetManager::Instance().GetTextureAtlas());
+ glBindTexture(GL_TEXTURE_2D, AssetManager::Instance().GetTextureAtlas());
AssetManager::Instance().GetTextureAtlasIndexes();
}
void Render::RenderFrame() {
- glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
+ glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (renderWorld)
diff --git a/src/Renderer.hpp b/src/Renderer.hpp
index cd0d7b0..53f6cc8 100644
--- a/src/Renderer.hpp
+++ b/src/Renderer.hpp
@@ -9,12 +9,13 @@ public:
void SetActiveVao(GLuint Vao);
void SetActiveShader(GLuint Shader);
unsigned int WindowWidth, WindowHeight;
+ long long TimeOfDay;
};
-struct Renderer {
+/*struct Renderer {
virtual ~Renderer() = default;
virtual void Render(RenderState& renderState) = 0;
virtual void PrepareResources() = 0;
virtual void PrepareRender() = 0;
virtual bool IsNeedResourcesPrepare() = 0;
-}; \ No newline at end of file
+};*/ \ No newline at end of file
diff --git a/src/RendererEntity.cpp b/src/RendererEntity.cpp
index 6a92cec..0bc2606 100644
--- a/src/RendererEntity.cpp
+++ b/src/RendererEntity.cpp
@@ -147,20 +147,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_TRIANGLES, 0, 36);
+ glDrawArrays(GL_LINE_STRIP, 0, 36);
glCheckError();
-}
-
-void RendererEntity::PrepareResources()
-{
-}
-
-void RendererEntity::PrepareRender()
-{
-}
-
-bool RendererEntity::IsNeedResourcesPrepare()
-{
- return false;
-}
+} \ No newline at end of file
diff --git a/src/RendererEntity.hpp b/src/RendererEntity.hpp
index 23fd029..578e24b 100644
--- a/src/RendererEntity.hpp
+++ b/src/RendererEntity.hpp
@@ -8,17 +8,14 @@
#include "Entity.hpp"
#include "World.hpp"
-class RendererEntity :Renderer {
+class RendererEntity {
unsigned int entityId;
World *world;
public:
RendererEntity(World *ptr, unsigned int id);
~RendererEntity();
- void Render(RenderState& renderState) override;
- void PrepareResources() override;
- void PrepareRender() override;
- bool IsNeedResourcesPrepare() override;
+ void Render(RenderState& renderState);
GLint modelLoc = 0;
GLint colorLoc = 0;
diff --git a/src/RendererSection.cpp b/src/RendererSection.cpp
index 609333c..0965453 100644
--- a/src/RendererSection.cpp
+++ b/src/RendererSection.cpp
@@ -32,15 +32,21 @@ RendererSection::~RendererSection() {
refCounterVbo[VboTextures]--;
refCounterVbo[VboModels]--;
refCounterVbo[VboColors]--;
+ refCounterVbo[VboLights]--;
refCounterVao[Vao]--;
+
if (refCounterVbo[VboTextures] <= 0)
glDeleteBuffers(1, &VboTextures);
if (refCounterVbo[VboModels] <= 0)
glDeleteBuffers(1, &VboTextures);
+
if (refCounterVbo[VboColors] <= 0)
glDeleteBuffers(1, &VboColors);
+ if (refCounterVbo[VboLights] <= 0)
+ glDeleteBuffers(1, &VboLights);
+
if (refCounterVao[Vao] <= 0)
glDeleteVertexArrays(1, &Vao);
}
@@ -93,6 +99,11 @@ RendererSection::RendererSection(RendererSectionData data) {
refCounterVbo[VboColors] = 0;
refCounterVbo[VboColors]++;
+ glGenBuffers(1, &VboLights);
+ if (refCounterVbo.find(VboLights) == refCounterVbo.end())
+ refCounterVbo[VboLights] = 0;
+ refCounterVbo[VboLights]++;
+
glGenVertexArrays(1, &Vao);
if (refCounterVao.find(Vao) == refCounterVao.end())
refCounterVao[Vao] = 0;
@@ -108,43 +119,50 @@ RendererSection::RendererSection(RendererSectionData data) {
//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, VboTextures);
-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, VboModels);
-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, VboColors);
-glVertexAttribPointer(colorAttribPos, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);
-glEnableVertexAttribArray(colorAttribPos);
-glVertexAttribDivisor(colorAttribPos, 1);
-
-glBindBuffer(GL_ARRAY_BUFFER, 0);
+ 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, VboTextures);
+ 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, VboModels);
+ 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, VboColors);
+ glVertexAttribPointer(colorAttribPos, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);
+ glEnableVertexAttribArray(colorAttribPos);
+ glVertexAttribDivisor(colorAttribPos, 1);
+
+ //Light
+ GLuint lightAttribPos = 13;
+ glBindBuffer(GL_ARRAY_BUFFER, VboLights);
+ glVertexAttribPointer(lightAttribPos, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), nullptr);
+ glEnableVertexAttribArray(lightAttribPos);
+ glVertexAttribDivisor(lightAttribPos, 1);
+
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
}
glBindVertexArray(0);
glCheckError();
@@ -160,6 +178,9 @@ glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, VboColors);
glBufferData(GL_ARRAY_BUFFER, data.colors.size() * sizeof(glm::vec3), data.colors.data(), GL_DYNAMIC_DRAW);
+ glBindBuffer(GL_ARRAY_BUFFER, VboLights);
+ glBufferData(GL_ARRAY_BUFFER, data.lights.size() * sizeof(glm::vec2), data.lights.data(), GL_DYNAMIC_DRAW);
+
glBindBuffer(GL_ARRAY_BUFFER, 0);
numOfFaces = data.textures.size();
@@ -171,6 +192,7 @@ RendererSection::RendererSection(const RendererSection &other) {
this->VboModels = other.VboModels;
this->VboTextures = other.VboTextures;
this->VboColors = other.VboColors;
+ this->VboLights = other.VboLights;
this->sectionPos = other.sectionPos;
this->Vao = other.Vao;
this->numOfFaces = other.numOfFaces;
@@ -179,6 +201,7 @@ RendererSection::RendererSection(const RendererSection &other) {
refCounterVbo[VboTextures]++;
refCounterVbo[VboModels]++;
refCounterVbo[VboColors]++;
+ refCounterVbo[VboLights]++;
refCounterVao[Vao]++;
}
@@ -193,7 +216,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition)
for (int x = 0; x < 16; x++) {
Block block = section.GetBlock(Vector(x, y, z));
if (block.id == 0)
- continue;
+ continue;
auto testBlockNonExist = [&](Vector block) -> bool {
Vector offset;
@@ -216,28 +239,22 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition)
offset = Vector(0, 0, 1);
block.z = 0;
}
- if (offset != Vector(0, 0, 0)) {
+ if (offset != Vector(0, 0, 0)) {
if (std::find(sectionsList.begin(), sectionsList.end(), sectionPosition + offset) == sectionsList.end())
return true;
const Section& blockSection = world->GetSection(sectionPosition + offset);
return blockSection.GetBlock(block).id == 0 || blockSection.GetBlock(block).id == 31 || blockSection.GetBlock(block).id == 18;
}
return section.GetBlock(block).id == 0 || section.GetBlock(block).id == 31 || section.GetBlock(block).id == 18;
- /*return block.x == -1 || block.x == 16 || block.y == -1 || block.y == 16 || block.z == -1 || block.z == 16 ||
- section.GetBlock(block).id == 0 || section.GetBlock(block).id == 31 || section.GetBlock(block).id == 18;*/
};
unsigned char isVisible = 0;
- /*if (x == 0 || x == 15 || y == 0 || y == 15 || z == 0 || z == 15) {
- isVisible = 0b1111'1111; //All faces is visible
- } else {*/
isVisible |= testBlockNonExist(Vector(x - 1, y, z)) << 0;
isVisible |= testBlockNonExist(Vector(x + 1, y, z)) << 1;
isVisible |= testBlockNonExist(Vector(x, y + 1, z)) << 2;
isVisible |= testBlockNonExist(Vector(x, y - 1, z)) << 3;
isVisible |= testBlockNonExist(Vector(x, y, z - 1)) << 4;
isVisible |= testBlockNonExist(Vector(x, y, z + 1)) << 5;
- //}
if (isVisible == 0x00)
continue;
@@ -255,6 +272,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition)
for (int i = 0; i < 4; i++) {
textures.push_back(texture->second);
colors.push_back(color);
+ lights.push_back(glm::vec2(block.light, block.sky));
}
glm::mat4 faceTransform = glm::translate(transform, glm::vec3(0.15f, 0, 0.15f));
faceTransform = glm::scale(faceTransform, glm::vec3(1.0f, 0.9f, 1.0f));
@@ -280,6 +298,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition)
textures.push_back(glm::vec4(0.0546875, 0.00442477876106194690,
0.0078125, 0.00442477876106194690)); //Fallback TNT texture
colors.push_back(color);
+ lights.push_back(glm::vec2(block.light, block.sky));
}
if (isVisible >> 1 & 0x1) { //west side X-
glm::mat4 faceTransform = glm::translate(transform, glm::vec3(1, 0, 0));
@@ -294,6 +313,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition)
textures.push_back(glm::vec4(0.0546875, 0.00442477876106194690,
0.0078125, 0.00442477876106194690)); //Fallback TNT texture
colors.push_back(color);
+ lights.push_back(glm::vec2(block.light, block.sky));
}
if (isVisible >> 2 & 0x1) { //Top side Y+
glm::mat4 faceTransform = glm::translate(transform, glm::vec3(0, 1, 0));
@@ -308,6 +328,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition)
colors.push_back(color);
else
colors.push_back(biomeColor);
+ lights.push_back(glm::vec2(block.light, block.sky));
}
if (isVisible >> 3 & 0x1) { //Bottom side Y-
glm::mat4 faceTransform = glm::translate(transform, glm::vec3(0, 0, 0));
@@ -321,6 +342,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition)
textures.push_back(glm::vec4(0.0546875, 0.00442477876106194690,
0.0078125, 0.00442477876106194690)); //Fallback TNT texture
colors.push_back(color);
+ lights.push_back(glm::vec2(block.light, block.sky));
}
if (isVisible >> 4 & 0x1) { //south side Z+
glm::mat4 faceTransform = glm::translate(transform, glm::vec3(1, 0, 0));
@@ -334,6 +356,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition)
textures.push_back(glm::vec4(0.0546875, 0.00442477876106194690,
0.0078125, 0.00442477876106194690)); //Fallback TNT texture
colors.push_back(color);
+ lights.push_back(glm::vec2(block.light, block.sky));
}
if (isVisible >> 5 & 0x1) { //north side Z-
glm::mat4 faceTransform = glm::translate(transform, glm::vec3(0, 0, 1));
@@ -350,6 +373,7 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition)
textures.push_back(glm::vec4(0.0546875, 0.00442477876106194690,
0.0078125, 0.00442477876106194690)); //Fallback TNT texture
colors.push_back(color);
+ lights.push_back(glm::vec2(block.light, block.sky));
}
}
}
@@ -359,4 +383,9 @@ RendererSectionData::RendererSectionData(World * world, Vector sectionPosition)
textures.shrink_to_fit();
models.shrink_to_fit();
colors.shrink_to_fit();
+
+ /*for (auto& it : lights) {
+ it.x = 8;
+ it.y = 16;
+ }*/
}
diff --git a/src/RendererSection.hpp b/src/RendererSection.hpp
index fe5e96b..9e5fd99 100644
--- a/src/RendererSection.hpp
+++ b/src/RendererSection.hpp
@@ -17,6 +17,7 @@ struct RendererSectionData {
std::vector<glm::mat4> models;
std::vector<glm::vec4> textures;
std::vector<glm::vec3> colors;
+ std::vector<glm::vec2> lights;
size_t hash;
Vector sectionPos;
@@ -24,7 +25,7 @@ struct RendererSectionData {
};
class RendererSection {
- GLuint Vao, VboTextures, VboModels, VboColors;
+ GLuint Vao, VboTextures, VboModels, VboColors, VboLights;
static GLuint VboVertices, VboUvs;
static std::map<GLuint, int> refCounterVbo;
diff --git a/src/RendererSky.cpp b/src/RendererSky.cpp
new file mode 100644
index 0000000..e0e367b
--- /dev/null
+++ b/src/RendererSky.cpp
@@ -0,0 +1,140 @@
+#include "RendererSky.hpp"
+
+const GLfloat vertices[] = {
+ //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,
+
+ //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,
+
+ //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,
+ -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,
+ 0.5f, -0.5f, 0.5f,
+ 0.5f, 0.5f, -0.5f,
+
+ //Y+ 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,
+
+ //Y- 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,
+};
+
+const GLfloat uv_coords[] = {
+ //Z+
+ 0.0f, 1.0f,
+ 0.0f, 0.0f,
+ 1.0f, 0.0f,
+ 0.0f, 1.0f,
+ 1.0f, 0.0f,
+ 1.0f, 1.0f,
+
+ //Z-
+ 1.0f, 0.0f,
+ 1.0f, 1.0f,
+ 0.0f, 0.0f,
+ 0.0f, 0.0f,
+ 1.0f, 1.0f,
+ 0.0f, 1.0f,
+
+ //X+
+ 0.0f, 0.0f,
+ 1.0f, 0.0f,
+ 0.0f, 1.0f,
+ 0.0f, 1.0f,
+ 1.0f, 0.0f,
+ 1.0f, 1.0f,
+
+ //X-
+ 0.0f, 0.0f,
+ 1.0f, 1.0f,
+ 0.0f, 1.0f,
+ 0.0f, 0.0f,
+ 1.0f, 0.0f,
+ 1.0f, 1.0f,
+
+ //Y+
+ 0.0f, 0.0f,
+ 1.0f, 1.0f,
+ 0.0f, 1.0f,
+ 0.0f, 0.0f,
+ 1.0f, 0.0f,
+ 1.0f, 1.0f,
+
+ //Y-
+ 1.0f, 0.0f,
+ 0.0f, 1.0f,
+ 0.0f, 0.0f,
+ 1.0f, 1.0f,
+ 0.0f, 1.0f,
+ 1.0f, 0.0f,
+};
+
+RendererSky::RendererSky() {
+ glGenBuffers(1, &VboVert);
+ glBindBuffer(GL_ARRAY_BUFFER, VboVert);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
+
+ glGenBuffers(1, &VboUv);
+ glBindBuffer(GL_ARRAY_BUFFER, VboUv);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(uv_coords), uv_coords, GL_STATIC_DRAW);
+ glGenVertexArrays(1, &Vao);
+
+ glBindVertexArray(Vao);
+ {
+ glBindBuffer(GL_ARRAY_BUFFER, VboVert);
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
+ glEnableVertexAttribArray(0);
+
+ glBindBuffer(GL_ARRAY_BUFFER, VboUv);
+ glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);
+ glEnableVertexAttribArray(1);
+ }
+ glBindVertexArray(0);
+ glCheckError();
+}
+
+RendererSky::~RendererSky()
+{
+ glDeleteBuffers(1, &VboVert);
+ glDeleteBuffers(1, &VboUv);
+ glDeleteVertexArrays(1, &Vao);
+ //glCheckError();
+}
+
+void RendererSky::Render(RenderState &renderState)
+{
+ renderState.SetActiveVao(Vao);
+ glDrawArrays(GL_TRIANGLES, 0, 36);
+ glCheckError();
+}
diff --git a/src/RendererSky.hpp b/src/RendererSky.hpp
new file mode 100644
index 0000000..4dd0892
--- /dev/null
+++ b/src/RendererSky.hpp
@@ -0,0 +1,16 @@
+#pragma once
+
+#include <glm/glm.hpp>
+
+#include "Utility.hpp"
+#include "Shader.hpp"
+#include "Renderer.hpp"
+#include "AssetManager.hpp"
+
+class RendererSky {
+ GLuint VboVert, VboUv, Vao;
+public:
+ RendererSky();
+ ~RendererSky();
+ void Render(RenderState &renderState);
+}; \ No newline at end of file
diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp
index 194ddd8..9bd2ccf 100644
--- a/src/RendererWorld.cpp
+++ b/src/RendererWorld.cpp
@@ -10,7 +10,7 @@ void RendererWorld::WorkerFunction(size_t workerId) {
Vector vec = data.Task;
sectionsMutex.lock();
- auto& result = sections.find(vec);
+ auto result = sections.find(vec);
if (result != sections.end()) {
if (result->second.GetHash() != gs->world.GetSection(result->first).GetHash()) {
sectionsMutex.unlock();
@@ -84,7 +84,7 @@ void RendererWorld::UpdateAllSections(VectorF playerPos)
}
RendererWorld::RendererWorld(std::shared_ptr<GameState> ptr):gs(ptr) {
- MaxRenderingDistance = 2;
+ MaxRenderingDistance = 10;
numOfWorkers = 4;
PrepareRender();
@@ -94,7 +94,7 @@ RendererWorld::RendererWorld(std::shared_ptr<GameState> ptr):gs(ptr) {
sectionsMutex.lock();
auto it = sections.find(vec);
if (it == sections.end()) {
- LOG(ERROR) << "Deleting wrong sectionRenderer";
+ //LOG(ERROR) << "Deleting wrong sectionRenderer";
sectionsMutex.unlock();
return;
}
@@ -178,6 +178,8 @@ RendererWorld::RendererWorld(std::shared_ptr<GameState> ptr):gs(ptr) {
for (int i = 0; i < numOfWorkers; i++)
workers.push_back(std::thread(&RendererWorld::WorkerFunction, this, i));
+
+ //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
RendererWorld::~RendererWorld() {
@@ -193,9 +195,11 @@ RendererWorld::~RendererWorld() {
workers[i].join();
delete blockShader;
delete entityShader;
+ delete skyShader;
}
void RendererWorld::Render(RenderState & renderState) {
+ renderState.TimeOfDay = gs->TimeOfDay;
renderState.SetActiveShader(blockShader->Program);
glCheckError();
@@ -250,7 +254,7 @@ void RendererWorld::Render(RenderState & renderState) {
renderState.SetActiveShader(entityShader->Program);
glCheckError();
projectionLoc = glGetUniformLocation(entityShader->Program, "projection");
- viewLoc = glGetUniformLocation(entityShader->Program, "view");
+ viewLoc = glGetUniformLocation(entityShader->Program, "view");
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glCheckError();
@@ -262,10 +266,55 @@ void RendererWorld::Render(RenderState & renderState) {
it.Render(renderState);
}
-}
-void RendererWorld::PrepareResources() {
- LOG(ERROR) << "Incorrect call";
+
+ renderState.SetActiveShader(skyShader->Program);
+ projectionLoc = glGetUniformLocation(skyShader->Program, "projection");
+ viewLoc = glGetUniformLocation(skyShader->Program, "view");
+ glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
+ glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
+ glm::mat4 model = glm::mat4();
+ model = glm::translate(model, gs->Position());
+ const float scale = 1000000.0f;
+ model = glm::scale(model, glm::vec3(scale, scale, scale));
+ float shift = gs->TimeOfDay / 24000.0f;
+ if (shift < 0)
+ shift *= -1.0f;
+ model = glm::rotate(model, glm::radians(90.0f), glm::vec3(0, 1.0f, 0.0f));
+ model = glm::rotate(model, glm::radians(360.0f * shift), glm::vec3(-1.0f, 0.0f, 0.0f));
+ modelLoc = glGetUniformLocation(skyShader->Program, "model");
+ glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
+
+ glCheckError();
+
+ const int sunriseMin = 22000;
+ const int sunriseMax = 23500;
+ const int moonriseMin = 12000;
+ const int moonriseMax = 13500;
+ const float sunriseLength = sunriseMax - sunriseMin;
+ const float moonriseLength = moonriseMax - moonriseMin;
+
+ float mixLevel=0;
+ int dayTime = gs->TimeOfDay;
+ if (dayTime < 0)
+ dayTime *= -1;
+ while (dayTime > 24000)
+ dayTime -= 24000;
+ if (dayTime > 0 && dayTime < moonriseMin || dayTime > sunriseMax) //day
+ mixLevel = 1.0;
+ if (dayTime > moonriseMax && dayTime < sunriseMin) //night
+ mixLevel = 0.0;
+ if (dayTime >= sunriseMin && dayTime <= sunriseMax) //sunrise
+ mixLevel = (dayTime - sunriseMin) / sunriseLength;
+ if (dayTime >= moonriseMin && dayTime <= moonriseMax) { //moonrise
+ float timePassed = (dayTime - moonriseMin);
+ mixLevel = 1.0 - (timePassed / moonriseLength);
+ }
+
+ glUniform1f(glGetUniformLocation(skyShader->Program,"DayTime"), mixLevel);
+
+ rendererSky.Render(renderState);
+ glCheckError();
}
void RendererWorld::PrepareRender() {
@@ -274,11 +323,18 @@ void RendererWorld::PrepareRender() {
glUniform1i(glGetUniformLocation(blockShader->Program, "textureAtlas"), 0);
entityShader = new Shader("./shaders/entity.vs", "./shaders/entity.fs");
-}
-bool RendererWorld::IsNeedResourcesPrepare() {
- LOG(ERROR) << "Incorrect call";
- return false;
+ skyShader = new Shader("./shaders/sky.vs", "./shaders/sky.fs");
+ skyShader->Use();
+ glUniform1i(glGetUniformLocation(skyShader->Program, "textureAtlas"), 0);
+ TextureCoordinates skyTexture = AssetManager::Instance().GetTextureByAssetName("minecraft/textures/entity/end_portal");
+ glUniform4f(glGetUniformLocation(skyShader->Program, "skyTexture"),skyTexture.x,skyTexture.y,skyTexture.w,skyTexture.h);
+ TextureCoordinates sunTexture = AssetManager::Instance().GetTextureByAssetName("minecraft/textures/environment/sun");
+ glUniform4f(glGetUniformLocation(skyShader->Program, "sunTexture"), sunTexture.x, sunTexture.y, sunTexture.w, sunTexture.h);
+ TextureCoordinates moonTexture = AssetManager::Instance().GetTextureByAssetName("minecraft/textures/environment/moon_phases");
+ moonTexture.w /= 4.0f; //First phase will be fine for now
+ moonTexture.h /= 2.0f;
+ glUniform4f(glGetUniformLocation(skyShader->Program, "moonTexture"), moonTexture.x, moonTexture.y, moonTexture.w, moonTexture.h);
}
void RendererWorld::Update(double timeToUpdate) {
diff --git a/src/RendererWorld.hpp b/src/RendererWorld.hpp
index e7bcdf0..5bc575c 100644
--- a/src/RendererWorld.hpp
+++ b/src/RendererWorld.hpp
@@ -2,12 +2,13 @@
#include "RendererSection.hpp"
#include "RendererEntity.hpp"
+#include "RendererSky.hpp"
#include "GameState.hpp"
#include "Shader.hpp"
#include <glm/gtc/type_ptr.hpp>
-class RendererWorld: public Renderer {
+class RendererWorld {
//General
std::shared_ptr<GameState> gs;
EventListener listener;
@@ -27,14 +28,18 @@ class RendererWorld: public Renderer {
//Entities
Shader *entityShader;
std::vector<RendererEntity> entities;
+ //Sky
+ Texture *skyTexture;
+ Shader *skyShader;
+ RendererSky rendererSky;
public:
RendererWorld(std::shared_ptr<GameState> ptr);
~RendererWorld();
- void Render(RenderState& renderState) override;
- void PrepareResources() override;
- void PrepareRender() override;
- bool IsNeedResourcesPrepare() override;
+ void Render(RenderState& renderState);
+ void PrepareResources();
+ void PrepareRender();
+ bool IsNeedResourcesPrepare();
double MaxRenderingDistance;
diff --git a/src/Section.cpp b/src/Section.cpp
index 3fb100f..7a9f947 100644
--- a/src/Section.cpp
+++ b/src/Section.cpp
@@ -9,69 +9,19 @@ Block &Section::GetBlock(Vector pos) {
Block Section::GetBlock(Vector pos) const
{
+ if (blocks.empty()) {
+ static Block fallback;
+ return fallback;
+ }
return blocks[pos.y * 256 + pos.z * 16 + pos.x];
}
double totalParsingTime = 0;
-//void Section::Parse() {
-// if (!m_blocks.empty())
-// return;
-//
-// long long *longArray = reinterpret_cast<long long *>(m_dataBlocks);
-// for (size_t i = 0; i < m_dataBlocksLen / 8; i++)
-// endswap(&longArray[i]);
-// std::vector<unsigned short> blocks;
-// blocks.reserve(4096);
-// {
-// auto begin = std::chrono::steady_clock::now();
-// int bitPos = 0;
-// unsigned short t = 0;
-// for (size_t i = 0; i < m_dataBlocksLen; i++) {
-// for (int j = 0; j < 8; j++) {
-// t |= (m_dataBlocks[i] & 0x01) ? 0x80 : 0x00;
-// t >>= 1;
-// m_dataBlocks[i] >>= 1;
-// bitPos++;
-// if (bitPos >= m_bitsPerBlock) {
-// bitPos = 0;
-// t >>= m_bitsPerBlock - 1;
-// blocks.push_back(t);
-// t = 0;
-// }
-// }
-// }
-// auto end = std::chrono::steady_clock::now();
-// std::chrono::duration<double, std::milli> time = end - begin;
-// totalParsingTime += time.count();
-// }
-// std::vector<byte> light;
-// light.reserve(4096);
-// for (int i = 0; i < 2048; i++) {
-// byte t = m_dataLight[i];
-// byte first = t & 0b11110000;
-// byte second = t >> 4;
-// light.push_back(first);
-// light.push_back(second);
-// }
-// for (int i = 0; i < 4096; i++) {
-// unsigned short blockId = m_palette.size() > 0 ? m_palette[blocks[i]] : blocks[i];
-// Block block(blockId >> 4, blockId & 0xF);
-// m_blocks.push_back(block);
-// }
-// delete[] m_dataBlocks;
-// m_dataBlocksLen = 0;
-// m_dataBlocks = nullptr;
-// delete[] m_dataLight;
-// m_dataLight = nullptr;
-// delete[] m_dataSkyLight;
-// m_dataSkyLight = nullptr;
-//
-// parseWaiter.notify_all();
-//}
-
Section::Section(PackedSection data)
{
+ if (data.blocks.empty())
+ return;
worldPosition = data.position;
long long *longArray = reinterpret_cast<long long *>(data.blocks.data());
@@ -105,14 +55,28 @@ Section::Section(PackedSection data)
light.reserve(4096);
for (int i = 0; i < 2048; i++) {
byte t = data.light[i];
- byte first = t & 0b11110000;
- byte second = t >> 4;
- light.push_back(first);
- light.push_back(second);
+ byte first = t & 0xF;
+ byte second = (t >> 4) & 0xF;
+ light.push_back(0);
+ light.push_back(0);
}
+
+ std::vector<byte> sky;
+ if (!data.sky.empty()) {
+ sky.reserve(4096);
+ for (int i = 0; i < 2048; i++) {
+ byte t = data.sky[i];
+ byte first = t & 0xF;
+ byte second = (t >> 4) & 0xF;
+ sky.push_back(first);
+ sky.push_back(0xF);
+ }
+ }
+
+
for (int i = 0; i < 4096; i++) {
unsigned short blockId = data.palette.size() > 0 ? data.palette[blocks[i]] : blocks[i];
- Block block(blockId >> 4, blockId & 0xF);
+ Block block(blockId >> 4, blockId & 0xF, light[i], sky.empty() ? 0 : sky[i]);
this->blocks.push_back(block);
}
}
@@ -130,7 +94,6 @@ void swap(Section &a, Section &b) {
Section::Section(const Section &other) {
worldPosition = other.worldPosition;
this->blocks = other.blocks;
- //std::copy(other.blocks.begin(), other.blocks.end(), std::back_inserter(blocks));
}
Vector Section::GetPosition() const {
diff --git a/src/Section.hpp b/src/Section.hpp
index 5422cf3..c7deeb1 100644
--- a/src/Section.hpp
+++ b/src/Section.hpp
@@ -28,6 +28,8 @@ struct PackedSection {
PackedSection(Vector position, byte *dataBlocks, size_t dataBlocksLength, byte *dataLight, byte *dataSky, byte bitsPerBlock,
std::vector<unsigned short> palette);
+
+ PackedSection() = default;
};
class Section {
diff --git a/src/ThreadGame.cpp b/src/ThreadGame.cpp
index ef7a95e..5e1d710 100644
--- a/src/ThreadGame.cpp
+++ b/src/ThreadGame.cpp
@@ -25,6 +25,8 @@ void ThreadGame::Execute() {
listener.RegisterHandler(EventType::Disconnected, [this](EventData eventData) {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
+ if (!gs)
+ return;
gs->gs.reset();
gs.reset();
});
diff --git a/src/World.cpp b/src/World.cpp
index 740c276..c79d1b7 100644
--- a/src/World.cpp
+++ b/src/World.cpp
@@ -130,12 +130,16 @@ std::vector<Vector> World::GetSectionsList() {
return sectionsList;
}
+static Section fallbackSection = Section(PackedSection());
+
const Section &World::GetSection(Vector sectionPos) {
auto result = cachedSections.find(sectionPos);
if (result == cachedSections.end()) {
auto it = sections.find(sectionPos);
- if (it == sections.end())
- LOG(ERROR) << "BAD";
+ if (it == sections.end()) {
+ LOG(ERROR) << "Accessed not loaded section " << sectionPos;
+ return fallbackSection;
+ }
Section section(it->second);
auto result = cachedSections.insert(std::make_pair(sectionPos, section));
return result.first->second;
@@ -216,7 +220,7 @@ void World::DeleteEntity(unsigned int EntityId)
void World::ParseChunkData(std::shared_ptr<PacketBlockChange> packet) {
Block& block = this->GetBlock(packet->Position);
- block = Block(packet->BlockId >> 4, packet->BlockId & 15);
+ block = Block(packet->BlockId >> 4, packet->BlockId & 15, block.light, block.sky);
Vector sectionPos(std::floor(packet->Position.x / 16.0), std::floor(packet->Position.y / 16.0), std::floor(packet->Position.z / 16.0));
EventAgregator::PushEvent(EventType::ChunkChanged, ChunkChangedData{ sectionPos });
}
@@ -229,7 +233,7 @@ void World::ParseChunkData(std::shared_ptr<PacketMultiBlockChange> packet) {
int z = (it.HorizontalPosition & 15) + (packet->ChunkZ * 16);
Vector worldPos(x, y, z);
Block& block = GetBlock(worldPos);
- block = Block(it.BlockId >> 4, it.BlockId & 15);
+ block = Block(it.BlockId >> 4, it.BlockId & 15, block.light, block.sky);
Vector sectionPos(packet->ChunkX, std::floor(it.YCoordinate / 16.0), packet->ChunkZ);
if (std::find(changedSections.begin(), changedSections.end(), sectionPos) == changedSections.end())