diff options
author | LaG1924 <lag1924@gmail.com> | 2021-11-17 06:59:16 +0100 |
---|---|---|
committer | LaG1924 <lag1924@gmail.com> | 2021-11-17 06:59:16 +0100 |
commit | 513fe5ebe3f59aa8d85d9517db9fe0bfa531d3dc (patch) | |
tree | a54c7a24e93b96c8ba8fce248f4fcc60a44924d8 /src | |
parent | Changed section rendering to Gal (diff) | |
download | AltCraft-513fe5ebe3f59aa8d85d9517db9fe0bfa531d3dc.tar AltCraft-513fe5ebe3f59aa8d85d9517db9fe0bfa531d3dc.tar.gz AltCraft-513fe5ebe3f59aa8d85d9517db9fe0bfa531d3dc.tar.bz2 AltCraft-513fe5ebe3f59aa8d85d9517db9fe0bfa531d3dc.tar.lz AltCraft-513fe5ebe3f59aa8d85d9517db9fe0bfa531d3dc.tar.xz AltCraft-513fe5ebe3f59aa8d85d9517db9fe0bfa531d3dc.tar.zst AltCraft-513fe5ebe3f59aa8d85d9517db9fe0bfa531d3dc.zip |
Diffstat (limited to 'src')
-rw-r--r-- | src/Gal.hpp | 1 | ||||
-rw-r--r-- | src/GalOgl.cpp | 5 | ||||
-rw-r--r-- | src/RendererEntity.cpp | 131 | ||||
-rw-r--r-- | src/RendererEntity.hpp | 9 | ||||
-rw-r--r-- | src/RendererWorld.cpp | 270 | ||||
-rw-r--r-- | src/RendererWorld.hpp | 3 |
6 files changed, 197 insertions, 222 deletions
diff --git a/src/Gal.hpp b/src/Gal.hpp index 8089fd3..51d3bd6 100644 --- a/src/Gal.hpp +++ b/src/Gal.hpp @@ -78,6 +78,7 @@ namespace Gal { }; enum class Primitive { + Line, Triangle, TriangleStrip, TriangleFan, diff --git a/src/GalOgl.cpp b/src/GalOgl.cpp index 0452cab..5643b4d 100644 --- a/src/GalOgl.cpp +++ b/src/GalOgl.cpp @@ -392,6 +392,9 @@ public: virtual void Render(size_t offset = 0, size_t count = -1) override { GLenum vertexMode; switch (primitive) { + case Primitive::Line: + vertexMode = GL_LINES; + break; case Primitive::Triangle: vertexMode = GL_TRIANGLES; break; @@ -820,9 +823,7 @@ public: GLint location = glGetAttribLocation(program, name.c_str()); if (location < 0) { - glDeleteProgram(program); LOG(ERROR) << "Vertex attribute name \"" << name << "\" not found in shader"; - throw std::runtime_error("Invalid attribute"); } size_t attribSize = GalTypeGetSize(type); diff --git a/src/RendererEntity.cpp b/src/RendererEntity.cpp index fcbf79a..02a5f54 100644 --- a/src/RendererEntity.cpp +++ b/src/RendererEntity.cpp @@ -1,141 +1,22 @@ #include "RendererEntity.hpp" #include <glm/gtc/matrix_transform.hpp> -#include <glm/gtc/type_ptr.hpp> #include <optick.h> #include "Entity.hpp" -#include "GameState.hpp" -#include "Renderer.hpp" -#include "AssetManager.hpp" +#include "World.hpp" -const GLfloat vertices[] = { - -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, - 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, - 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, - -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, +RendererEntity::RendererEntity(unsigned int id): entityId(id) {} - //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, -}; - -const GLuint magic = 993214; -GLuint Vbo = magic,Vao = magic,Vbo2 = magic; - -GLuint RendererEntity::GetVao(){ - if (Vbo == magic) { - glGenBuffers(1, &Vbo); - glBindBuffer(GL_ARRAY_BUFFER, Vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - - glGenBuffers(1, &Vbo2); - glBindBuffer(GL_ARRAY_BUFFER, Vbo2); - glBufferData(GL_ARRAY_BUFFER, sizeof(uv_coords), uv_coords, GL_STATIC_DRAW); - - glGenVertexArrays(1, &Vao); - glBindVertexArray(Vao); - { - glBindBuffer(GL_ARRAY_BUFFER, Vbo); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0); - glEnableVertexAttribArray(0); - - glBindBuffer(GL_ARRAY_BUFFER, Vbo2); - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0); - glEnableVertexAttribArray(1); - } - glBindVertexArray(0); - } - return Vao; -} - -RendererEntity::RendererEntity(unsigned int id) -{ - entityId = id; -} - -RendererEntity::~RendererEntity() { -} - -void RendererEntity::Render(RenderState& renderState, const World *world) { - OPTICK_EVENT(); +void RendererEntity::Render(std::shared_ptr<Gal::Pipeline> pipeline, const World *world) { + OPTICK_EVENT(); glm::mat4 model = glm::mat4(1.0); const Entity &entity = world->GetEntity(entityId); model = glm::translate(model, entity.pos.glm()); model = glm::translate(model, glm::vec3(0, entity.height / 2.0, 0)); model = glm::scale(model, glm::vec3(entity.width, entity.height, entity.width)); - Shader *entityShader = AssetManager::GetAsset<AssetShader>("/altcraft/shaders/entity")->shader.get(); - entityShader->SetUniform("model", model); - entityShader->SetUniform("color", entity.renderColor); - glCheckError(); - glDrawArrays(GL_LINES, 0, 24); - - glCheckError(); + pipeline->SetShaderParameter("model", model); + pipeline->SetShaderParameter("color", entity.renderColor); } diff --git a/src/RendererEntity.hpp b/src/RendererEntity.hpp index 346f9fb..b9097bd 100644 --- a/src/RendererEntity.hpp +++ b/src/RendererEntity.hpp @@ -1,17 +1,14 @@ #pragma once -#include <GL/glew.h> +#include "Gal.hpp" -class RenderState; class World; class RendererEntity { unsigned int entityId; + std::shared_ptr<Gal::Pipeline> pipeline; public: RendererEntity(unsigned int id); - ~RendererEntity(); - void Render(RenderState& renderState, const World *world); - - static GLuint GetVao(); + void Render(std::shared_ptr<Gal::Pipeline> pipeline, const World *world); }; diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 2764405..03d575b 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -293,62 +293,47 @@ void RendererWorld::Render(RenderState & renderState) { glm::mat4 projView = projection * view; //Render Entities -#ifndef __APPLE__ - glLineWidth(3.0); -#endif - Shader *entityShader = AssetManager::GetAsset<AssetShader>("/altcraft/shaders/entity")->shader.get(); - entityShader->Activate(); - entityShader->SetUniform("projection", projection); - entityShader->SetUniform("view", view); - glCheckError(); - - renderState.SetActiveVao(RendererEntity::GetVao()); + entitiesPipeline->Activate(); + entitiesPipeline->SetShaderParameter("projection", projection); + entitiesPipeline->SetShaderParameter("view", view); + + entitiesPipelineInstance->Activate(); for (auto& it : entities) { - it.Render(renderState, &GetGameState()->GetWorld()); + it.Render(entitiesPipeline, &GetGameState()->GetWorld()); + entitiesPipelineInstance->Render(0, 24); } //Render selected block Vector selectedBlock = GetGameState()->GetSelectionStatus().selectedBlock; if (selectedBlock != Vector()) { -#ifndef __APPLE__ - glLineWidth(2.0f); -#endif { glm::mat4 model = glm::mat4(1.0); model = glm::translate(model, selectedBlock.glm()); model = glm::translate(model,glm::vec3(0.5f,0.5f,0.5f)); model = glm::scale(model,glm::vec3(1.01f,1.01f,1.01f)); - entityShader->SetUniform("model", model); - entityShader->SetUniform("color", glm::vec3(0, 0, 0)); - glCheckError(); - glDrawArrays(GL_LINES, 0, 24); + entitiesPipeline->SetShaderParameter("model", model); + entitiesPipeline->SetShaderParameter("color", glm::vec3(0, 0, 0)); + entitiesPipelineInstance->Render(0, 24); } } //Render raycast hit const bool renderHit = false; if (renderHit) { - VectorF hit = GetGameState()->GetSelectionStatus().raycastHit; -#ifndef __APPLE__ - glLineWidth(2.0f); -#endif + VectorF hit = GetGameState()->GetSelectionStatus().raycastHit; { glm::mat4 model; model = glm::translate(model, hit.glm()); - model = glm::scale(model,glm::vec3(0.3f,0.3f,0.3f)); - entityShader->SetUniform("model", model); + model = glm::scale(model, glm::vec3(0.3f, 0.3f, 0.3f)); + //entityShader->SetUniform("model", model); + entitiesPipeline->SetShaderParameter("model", model); if (selectedBlock == Vector()) - entityShader->SetUniform("color", glm::vec3(0.7f, 0, 0)); + entitiesPipeline->SetShaderParameter("color", glm::vec3(0.7f, 0.0f, 0.0f)); else - entityShader->SetUniform("color", glm::vec3(0, 0, 0.7f)); - glCheckError(); - glDrawArrays(GL_LINE_STRIP, 0, 36); + entitiesPipeline->SetShaderParameter("color", glm::vec3(0.0f, 0.0f, 0.7f)); + entitiesPipelineInstance->Render(0, 24); } } -#ifndef __APPLE__ - glLineWidth(1.0); -#endif - glCheckError(); //Render sky renderState.TimeOfDay = GetGameState()->GetTimeStatus().timeOfDay; @@ -399,80 +384,187 @@ void RendererWorld::Render(RenderState & renderState) { glCheckError(); //Render sections - auto rawGlobalTime = (std::chrono::high_resolution_clock::now() - globalTimeStart); - float globalTime = rawGlobalTime.count() / 1000000000.0f; - /*Shader* blockShader = AssetManager::GetAsset<AssetShader>("/altcraft/shaders/face")->shader.get(); - blockShader->Activate(); - blockShader->SetUniform("DayTime", mixLevel); - blockShader->SetUniform("projView", projView); - blockShader->SetUniform("GlobalTime", globalTime); - glCheckError(); - */ - sectionsPipeline->Activate(); - sectionsPipeline->SetShaderParameter("DayTime", mixLevel); - sectionsPipeline->SetShaderParameter("projView", projView); - - Frustum frustum(projView); + auto rawGlobalTime = (std::chrono::high_resolution_clock::now() - globalTimeStart); + float globalTime = rawGlobalTime.count() / 1000000000.0f; + sectionsPipeline->Activate(); + sectionsPipeline->SetShaderParameter("DayTime", mixLevel); + sectionsPipeline->SetShaderParameter("projView", projView); + + Frustum frustum(projView); size_t culledSections = sections.size(); - unsigned int renderedFaces = 0; + unsigned int renderedFaces = 0; for (auto& section : sections) { - glm::vec3 point{ - section.second.GetPosition().x * 16 + 8, - section.second.GetPosition().y * 16 + 8, - section.second.GetPosition().z * 16 + 8 - }; + glm::vec3 point{ + section.second.GetPosition().x * 16 + 8, + section.second.GetPosition().y * 16 + 8, + section.second.GetPosition().z * 16 + 8 + }; - bool isVisible = frustum.TestSphere(point, 16.0f); + bool isVisible = frustum.TestSphere(point, 16.0f); if (!isVisible) { culledSections--; continue; } section.second.Render(); - renderedFaces += section.second.numOfFaces; + renderedFaces += section.second.numOfFaces; } DebugInfo::culledSections = culledSections; - DebugInfo::renderFaces = renderedFaces; - glCheckError(); + DebugInfo::renderFaces = renderedFaces; } void RendererWorld::PrepareRender() { - std::string sectionVertexSource, sectionPixelSource; - { - auto vertAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/vert/face"); - sectionVertexSource = std::string((char*)vertAsset->data.data(), (char*)vertAsset->data.data() + vertAsset->data.size()); + std::string sectionVertexSource, sectionPixelSource; + { + auto vertAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/vert/face"); + sectionVertexSource = std::string((char*)vertAsset->data.data(), (char*)vertAsset->data.data() + vertAsset->data.size()); - auto pixelAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/face"); - sectionPixelSource = std::string((char*)pixelAsset->data.data(), (char*)pixelAsset->data.data() + pixelAsset->data.size()); - } + auto pixelAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/face"); + sectionPixelSource = std::string((char*)pixelAsset->data.data(), (char*)pixelAsset->data.data() + pixelAsset->data.size()); + } - auto gal = Gal::GetImplementation(); - { - auto sectionsPLC = gal->CreatePipelineConfig(); - sectionsPLC->SetTarget(gal->GetDefaultFramebuffer()); - sectionsPLC->AddShaderParameter("projView", Gal::Type::Mat4); - sectionsPLC->AddShaderParameter("DayTime", Gal::Type::Float); - sectionsPLC->AddShaderParameter("GlobalTime", Gal::Type::Float); - sectionsPLC->AddShaderParameter("MinLightLevel", Gal::Type::Float); - sectionsPLC->AddShaderParameter("textureAtlas", Gal::Type::Int32); - sectionsPLC->SetVertexShader(gal->LoadVertexShader(sectionVertexSource)); - sectionsPLC->SetPixelShader(gal->LoadPixelShader(sectionPixelSource)); - sectionsPLC->SetPrimitive(Gal::Primitive::TriangleFan); - sectionsBufferBinding = sectionsPLC->BindVertexBuffer({ - {"position", Gal::Type::Vec3, 4, 1}, - {"uv", Gal::Type::Vec2, 4, 1}, - {"uvLayer", Gal::Type::Float, 1, 1}, - {"animation", Gal::Type::Float, 1, 1}, - {"color", Gal::Type::Vec3, 1, 1}, - {"light", Gal::Type::Vec2, 1, 1}, - {"", Gal::Type::Uint8, 20, 1} - }); - sectionsPipeline = gal->BuildPipeline(sectionsPLC); - sectionsPipeline->SetShaderParameter("MinLightLevel", 0.2f); - sectionsPipeline->SetShaderParameter("textureAtlas", 0); - } - + std::string entitiesVertexSource, entitiesPixelSource; + { + auto vertAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/vert/entity"); + entitiesVertexSource = std::string((char*)vertAsset->data.data(), (char*)vertAsset->data.data() + vertAsset->data.size()); + + auto pixelAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/entity"); + entitiesPixelSource = std::string((char*)pixelAsset->data.data(), (char*)pixelAsset->data.data() + pixelAsset->data.size()); + } + + auto gal = Gal::GetImplementation(); + { + auto sectionsPLC = gal->CreatePipelineConfig(); + sectionsPLC->SetTarget(gal->GetDefaultFramebuffer()); + sectionsPLC->AddShaderParameter("projView", Gal::Type::Mat4); + sectionsPLC->AddShaderParameter("DayTime", Gal::Type::Float); + sectionsPLC->AddShaderParameter("GlobalTime", Gal::Type::Float); + sectionsPLC->AddShaderParameter("MinLightLevel", Gal::Type::Float); + sectionsPLC->AddShaderParameter("textureAtlas", Gal::Type::Int32); + sectionsPLC->SetVertexShader(gal->LoadVertexShader(sectionVertexSource)); + sectionsPLC->SetPixelShader(gal->LoadPixelShader(sectionPixelSource)); + sectionsPLC->SetPrimitive(Gal::Primitive::TriangleFan); + sectionsBufferBinding = sectionsPLC->BindVertexBuffer({ + {"position", Gal::Type::Vec3, 4, 1}, + {"uv", Gal::Type::Vec2, 4, 1}, + {"uvLayer", Gal::Type::Float, 1, 1}, + {"animation", Gal::Type::Float, 1, 1}, + {"color", Gal::Type::Vec3, 1, 1}, + {"light", Gal::Type::Vec2, 1, 1}, + {"", Gal::Type::Uint8, 20, 1} + }); + sectionsPipeline = gal->BuildPipeline(sectionsPLC); + sectionsPipeline->SetShaderParameter("MinLightLevel", 0.2f); + sectionsPipeline->SetShaderParameter("textureAtlas", 0); + } + + { + auto entitiesPLC = gal->CreatePipelineConfig(); + entitiesPLC->SetTarget(gal->GetDefaultFramebuffer()); + entitiesPLC->AddShaderParameter("view", Gal::Type::Mat4); + entitiesPLC->AddShaderParameter("projection", Gal::Type::Mat4); + entitiesPLC->AddShaderParameter("model", Gal::Type::Mat4); + entitiesPLC->AddShaderParameter("color", Gal::Type::Vec3); + entitiesPLC->SetVertexShader(gal->LoadVertexShader(entitiesVertexSource)); + entitiesPLC->SetPixelShader(gal->LoadPixelShader(entitiesPixelSource)); + entitiesPLC->SetPrimitive(Gal::Primitive::Line); + auto entitiesPosBB = entitiesPLC->BindVertexBuffer({ + {"position", Gal::Type::Vec3}, + }); + auto entitiesUvBB = entitiesPLC->BindVertexBuffer({ + {"uvPosition", Gal::Type::Vec2}, + }); + + entitiesPipeline = gal->BuildPipeline(entitiesPLC); + + constexpr float vertices[] = { + -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, + 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, + 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, + -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 + }; + + constexpr float uvs[] = { + //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, + }; + + entitiesPosBuffer = gal->CreateBuffer(); + entitiesPosBuffer->SetData({ reinterpret_cast<const std::byte*>(vertices), reinterpret_cast<const std::byte*>(vertices + sizeof(vertices)) }); + entitiesUvBuffer = gal->CreateBuffer(); + entitiesUvBuffer->SetData({ reinterpret_cast<const std::byte*>(uvs), reinterpret_cast<const std::byte*>(uvs + sizeof(uvs)) }); + + entitiesPipelineInstance = entitiesPipeline->CreateInstance({ + {entitiesPosBB, entitiesPosBuffer}, + {entitiesUvBB, entitiesUvBuffer}, + }); + } TextureCoord sunTexture = AssetManager::GetTexture("/minecraft/textures/environment/sun"); TextureCoord moonTexture = AssetManager::GetTexture("/minecraft/textures/environment/moon_phases"); diff --git a/src/RendererWorld.hpp b/src/RendererWorld.hpp index c0af0dc..80eadb3 100644 --- a/src/RendererWorld.hpp +++ b/src/RendererWorld.hpp @@ -48,6 +48,9 @@ class RendererWorld { std::shared_ptr<Gal::BufferBinding> sectionsBufferBinding; //Entities std::vector<RendererEntity> entities; + std::shared_ptr<Gal::Pipeline> entitiesPipeline; + std::shared_ptr<Gal::PipelineInstance> entitiesPipelineInstance; + std::shared_ptr<Gal::Buffer> entitiesPosBuffer, entitiesUvBuffer; //Sky Texture *skyTexture; RendererSky rendererSky; |