summaryrefslogtreecommitdiffstats
path: root/src/core/Core.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/Core.cpp')
-rw-r--r--src/core/Core.cpp255
1 files changed, 236 insertions, 19 deletions
diff --git a/src/core/Core.cpp b/src/core/Core.cpp
index 1481e36..ade043e 100644
--- a/src/core/Core.cpp
+++ b/src/core/Core.cpp
@@ -1,5 +1,39 @@
#include "Core.hpp"
+GLenum glCheckError_(const char *file, int line) {
+ GLenum errorCode;
+ while ((errorCode = glGetError()) != GL_NO_ERROR) {
+ std::string error;
+ switch (errorCode) {
+ case GL_INVALID_ENUM:
+ error = "INVALID_ENUM";
+ break;
+ case GL_INVALID_VALUE:
+ error = "INVALID_VALUE";
+ break;
+ case GL_INVALID_OPERATION:
+ error = "INVALID_OPERATION";
+ break;
+ case GL_STACK_OVERFLOW:
+ error = "STACK_OVERFLOW";
+ break;
+ case GL_STACK_UNDERFLOW:
+ error = "STACK_UNDERFLOW";
+ break;
+ case GL_OUT_OF_MEMORY:
+ error = "OUT_OF_MEMORY";
+ break;
+ case GL_INVALID_FRAMEBUFFER_OPERATION:
+ error = "INVALID_FRAMEBUFFER_OPERATION";
+ break;
+ }
+ LOG(ERROR) << "OpenGL error: " << error << " at " << file << ":" << line;
+ }
+ return errorCode;
+}
+
+#define glCheckError() glCheckError_(__FILE__, __LINE__)
+
const GLfloat vertices[] = {
//Z+ edge
-0.5f, 0.5f, 0.5f,
@@ -49,6 +83,56 @@ const GLfloat vertices[] = {
0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
};
+
+/*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,
@@ -102,7 +186,9 @@ const GLfloat uv_coords[] = {
Core::Core() {
LOG(INFO) << "Core initializing...";
InitSfml(1280, 720, "AltCraft");
+ glCheckError();
InitGlew();
+ glCheckError();
client = new NetworkClient("127.0.0.1", 25565, "HelloOne");
gameState = new GameState(client);
std::thread loop = std::thread(&Core::UpdateGameState, this);
@@ -110,6 +196,7 @@ Core::Core() {
assetManager = new AssetManager;
PrepareToWorldRendering();
LOG(INFO) << "Core is initialized";
+ glCheckError();
}
Core::~Core() {
@@ -118,10 +205,13 @@ Core::~Core() {
delete shader;
delete gameState;
delete client;
+ delete assetManager;
+ delete window;
LOG(INFO) << "Core is stopped";
}
void Core::Exec() {
+ LOG(INFO) << "Main loop is executing!";
isRunning = true;
while (isRunning) {
static sf::Clock clock, clock1;
@@ -145,8 +235,10 @@ void Core::Exec() {
HandleEvents();
if (isMouseCaptured)
HandleMouseCapture();
+ glCheckError();
RenderFrame();
+
}
}
@@ -162,11 +254,11 @@ void Core::RenderFrame() {
//RenderGui(LoadingScreen);
break;
case Playing:
- RenderWorld(gameState->world);
+ RenderWorld();
//RenderGui(HUD);
break;
case PauseMenu:
- RenderWorld(gameState->world);
+ RenderWorld();
//RenderGui(PauseGui);
break;
}
@@ -182,7 +274,8 @@ void Core::InitSfml(unsigned int WinWidth, unsigned int WinHeight, std::string W
contextSetting.attributeFlags = contextSetting.Core;
contextSetting.depthBits = 24;
window = new sf::Window(sf::VideoMode(WinWidth, WinHeight), WinTitle, sf::Style::Default, contextSetting);
- //window->setVerticalSyncEnabled(true);
+ glCheckError();
+ window->setVerticalSyncEnabled(true);
window->setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width / 2 - window->getSize().x / 2,
sf::VideoMode::getDesktopMode().height / 2 - window->getSize().y / 2));
@@ -193,6 +286,7 @@ void Core::InitGlew() {
LOG(INFO) << "Initializing GLEW";
glewExperimental = GL_TRUE;
GLenum glewStatus = glewInit();
+ glCheckError();
if (glewStatus != GLEW_OK) {
LOG(FATAL) << "Failed to initialize GLEW: " << glewGetErrorString(glewStatus);
}
@@ -201,6 +295,7 @@ void Core::InitGlew() {
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
+ glCheckError();
}
unsigned int Core::width() {
@@ -216,14 +311,18 @@ void Core::HandleEvents() {
while (window->pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
+ LOG(INFO) << "Received close event by window closing";
isRunning = false;
break;
case sf::Event::Resized:
glViewport(0, 0, width(), height());
break;
case sf::Event::KeyPressed:
+ if (!window->hasFocus())
+ break;
switch (event.key.code) {
case sf::Keyboard::Escape:
+ LOG(INFO) << "Received close event by esc";
isRunning = false;
break;
case sf::Keyboard::T:
@@ -239,21 +338,24 @@ void Core::HandleEvents() {
break;
}
case sf::Event::MouseWheelScrolled:
- //camera.ProcessMouseScroll(event.mouseWheelScroll.delta);
+ if (!window->hasFocus())
+ break;
+ camera.ProcessMouseScroll(event.mouseWheelScroll.delta);
break;
default:
break;
}
}
-
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
- camera.ProcessKeyboard(Camera_Movement::FORWARD, deltaTime);
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
- camera.ProcessKeyboard(Camera_Movement::BACKWARD, deltaTime);
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
- camera.ProcessKeyboard(Camera_Movement::LEFT, deltaTime);
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
- camera.ProcessKeyboard(Camera_Movement::RIGHT, deltaTime);
+ if (window->hasFocus()) {
+ if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
+ camera.ProcessKeyboard(Camera_Movement::FORWARD, deltaTime);
+ if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
+ camera.ProcessKeyboard(Camera_Movement::BACKWARD, deltaTime);
+ if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
+ camera.ProcessKeyboard(Camera_Movement::LEFT, deltaTime);
+ if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
+ camera.ProcessKeyboard(Camera_Movement::RIGHT, deltaTime);
+ }
}
void Core::HandleMouseCapture() {
@@ -265,23 +367,26 @@ void Core::HandleMouseCapture() {
}
void Core::RenderGui(Gui &Target) {
-
+ Target.WHY++;
}
-void Core::RenderWorld(World &Target) {
+void Core::RenderWorld() {
shader->Use();
+ glCheckError();
GLint modelLoc = glGetUniformLocation(shader->Program, "model");
GLint projectionLoc = glGetUniformLocation(shader->Program, "projection");
GLint viewLoc = glGetUniformLocation(shader->Program, "view");
GLint blockLoc = glGetUniformLocation(shader->Program, "block");
GLint timeLoc = glGetUniformLocation(shader->Program, "time");
- glm::mat4 projection = glm::perspective(camera.Zoom, (float) width() / (float) height(), 0.0001f, 1000.0f);
+ glm::mat4 projection = glm::perspective(camera.Zoom, (float) width() / (float) height(), 0.1f, 10000000.0f);
glm::mat4 view = camera.GetViewMatrix();
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniform1f(timeLoc, absTime);
+ glCheckError();
+
glBindVertexArray(VAO);
for (auto &sectionPos:toRender) {
@@ -290,7 +395,7 @@ void Core::RenderWorld(World &Target) {
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)
+ if (block.id == 0)
continue;
glm::mat4 model;
@@ -298,7 +403,6 @@ void Core::RenderWorld(World &Target) {
sectionPos.GetZ() * 16));
model = glm::translate(model, glm::vec3(x, y, z));
-
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniform1i(blockLoc, block.id);
@@ -308,6 +412,7 @@ void Core::RenderWorld(World &Target) {
}
}
glBindVertexArray(0);
+ glCheckError();
}
void Core::SetMouseCapture(bool IsCaptured) {
@@ -318,6 +423,7 @@ void Core::SetMouseCapture(bool IsCaptured) {
}
void Core::PrepareToWorldRendering() {
+ //Cube-rendering data
glGenBuffers(1, &VBO);
glGenBuffers(1, &VBO2);
glGenVertexArrays(1, &VAO);
@@ -336,19 +442,116 @@ void Core::PrepareToWorldRendering() {
}
glBindVertexArray(0);
+ glCheckError();
+
shader = new Shader("./shaders/block.vs", "./shaders/block.fs");
shader->Use();
+ LOG(INFO) << "Initializing texture atlas...";
+ //TextureAtlas texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, assetManager->GetTextureAtlas());
glUniform1i(glGetUniformLocation(shader->Program, "textureAtlas"), 0);
+ //TextureAtlas coordinates
+ std::vector<glm::vec4> textureCoordinates;
+ std::vector<GLint> indexes;
+ GLint totalTextures;
+ for (int id = 0; id < 4096; id++) {
+ bool isReachedEnd = true;
+ for (int state = 0; state < 16; state++) {
+ if (!assetManager->GetTextureByBlock(BlockTextureId(id, state, 6)) ||
+ !assetManager->GetTextureByBlock(BlockTextureId(id, state, 0))) {
+ continue;
+ }
+ isReachedEnd = false;
+ int side = assetManager->GetTextureByBlock(BlockTextureId(id, state, 6)) ? 6 : 0;
+ do {
+ int index = (side << 16) | (id << 4) | state;
+ TextureCoordinates tc = assetManager->GetTextureByBlock(BlockTextureId(id, state, side));
+ textureCoordinates.push_back(glm::vec4(tc.x, tc.y, tc.w, tc.h));
+ indexes.push_back(index);
+ /*LOG(ERROR) << "Encoded (" << side << " " << id << " " << state << ") as " << index << " ("
+ << std::bitset<20>(index) << ")";*/
+ /*LOG(FATAL)<<std::bitset<18>(index);
+ side = 0x7;
+ id = 0xFFF;
+ state = 0xF;
+ LOG(WARNING) << "side: " << side << " id: " << id << " state: " << state;
+ int i, si, st, index = 0;
+ si = side << 15;
+ i = id<<3;
+ st = state;
+ index = i | si | st;
+ LOG(FATAL) << std::bitset<18>(index) << " (" << index << "): " << std::bitset<18>(si) << " "
+ << std::bitset<18>(i) << " " << std::bitset<18>(st);*/
+ /*if (rand() == 73) //Almost impossible(Almost==1/32768)
+ {
+ int index = 393233;
+ LOG(WARNING) << std::bitset<20>(index) << "(" << index << ")";
+ int side = (index & 0xE0000) >> 16;
+ int id = (index & 0xFF0) >> 4;
+ int state = index & 0xF;
+ LOG(WARNING) << std::bitset<20>(side) << " " << std::bitset<20>(id) << " "
+ << std::bitset<20>(state);
+ LOG(FATAL) << side << " " << id << " " << state;
+ }*/
+ side++;
+ } while (side < 7);
+ }
+ if (isReachedEnd)
+ break;
+
+ }
+ totalTextures = indexes.size();
+ LOG(INFO) << "Created " << totalTextures << " texture indexes";
+ CHECK_EQ(indexes.size(), textureCoordinates.size()) << "Arrays of textureCoordinates and of indexes is not equals";
+ CHECK_LE(totalTextures, 2048) << "There is more texture indexes, than GLSL buffer allows";
+
+ for (auto& it:indexes){
+ LOG(WARNING)<<it;
+ }
+
+ indexes.insert(indexes.begin(), totalTextures);
+ indexes.resize(2048);
+
+
+
+ GLuint ubo = glGetUniformBlockIndex(shader->Program, "TextureIndexes");
+ glUniformBlockBinding(shader->Program, ubo, 0);
+ glGenBuffers(1, &UBO);
+ glBindBuffer(GL_UNIFORM_BUFFER, UBO);
+ glBufferData(GL_UNIFORM_BUFFER, indexes.size() * sizeof(GLint), NULL, GL_STATIC_DRAW);
+ glBindBufferRange(GL_UNIFORM_BUFFER, 0, UBO, 0, indexes.size() * sizeof(GLint));
+ glBufferSubData(GL_UNIFORM_BUFFER, 0, indexes.size() * sizeof(GLint), &indexes[0]);
+ glCheckError();
+
+ LOG(WARNING)<<"Uploaded "<<indexes.size() * sizeof(GLint)<<" bytes";
+
+ /*GLuint ubo2 = glGetUniformBlockIndex(shader->Program, "TextureData");
+ glUniformBlockBinding(shader->Program, ubo2, 1);
+ glGenBuffers(1, &UBO2);
+ glBindBuffer(GL_UNIFORM_BUFFER, UBO2);
+ glBufferData(GL_UNIFORM_BUFFER, sizeof(glm::vec4) * 1024, NULL, GL_STATIC_DRAW);
+ glBindBufferRange(GL_UNIFORM_BUFFER, 1, UBO2, 0, 1024 * sizeof(glm::vec4));
+ glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(glm::vec4) * textureCoordinates.size(), textureCoordinates.data());*/
+
+ /*
+ GLuint ubo3 = glGetUniformBlockIndex(shader->Program, "TextureData2");
+ glUniformBlockBinding(shader->Program, ubo3, 2);
+ glGenBuffers(1, &UBO3);
+ glBindBuffer(GL_UNIFORM_BUFFER, UBO3);
+ glBufferData(GL_UNIFORM_BUFFER, sizeof(glm::vec4) * 1024, NULL, GL_STATIC_DRAW);
+ glBindBufferRange(GL_UNIFORM_BUFFER, 2, UBO3, 0, 1024 * sizeof(glm::vec4));*/
+
+ glBindBuffer(GL_UNIFORM_BUFFER,0);
+ glCheckError();
}
void Core::UpdateChunksToRender() {
camera.Position = glm::vec3(gameState->g_PlayerX, gameState->g_PlayerY, gameState->g_PlayerZ);
toRender.clear();
- const float ChunkDistance = 1.3;
+ const float ChunkDistance = 1;
Vector playerChunk = Vector(floor(gameState->g_PlayerX / 16.0f), floor(gameState->g_PlayerY / 16.0f),
floor(gameState->g_PlayerZ / 16.0f));
for (auto &it:gameState->world.m_sections) {
@@ -359,12 +562,26 @@ void Core::UpdateChunksToRender() {
toRender.push_back(chunkPosition);
}
LOG(INFO) << "Chunks to render: " << toRender.size();
+
+ /*std::map<Block, int> totalBlocks;
+ for (auto &section:toRender)
+ for (int x = 0; x < 16; x++)
+ for (int y = 0; y < 16; y++)
+ for (int z = 0; z < 16; z++)
+ totalBlocks[gameState->world.m_sections.find(section)->second.GetBlock(Vector(x, y, z))]++;
+ for (auto &it:totalBlocks) {
+ LOG(WARNING) << it.first.id << ":" << (int) it.first.state << " = " << it.second << " ("
+ << std::bitset<13>(it.first.id) << ")";
+ }*/
}
void Core::UpdateGameState() {
+ el::Helpers::setThreadName("Game");
LOG(INFO) << "GameState thread is started";
while (isRunning) {
gameState->Update();
+ if (toRender.size() > 0)
+ break;
}
LOG(INFO) << "GameState thread is stopped";
}