1
0
Fork 0
Univerxel/src/client/render/gl/Pipeline.cpp

128 lines
4.4 KiB
C++
Raw Normal View History

2020-09-14 16:03:21 +00:00
#include "Pipeline.hpp"
2020-07-10 17:49:16 +00:00
2020-09-14 16:03:21 +00:00
#include "../../../core/world/materials.hpp"
#include "../../control/Camera.hpp"
#include <TracyOpenGL.hpp>
2020-07-10 17:49:16 +00:00
2020-09-14 16:03:21 +00:00
using namespace render::gl;
Pipeline::Pipeline(const Pipeline::options& options):
2020-08-30 16:35:45 +00:00
IndicatorCubeBuffer(GL_LINES, 24, {
glm::vec3(0, 0, 0), glm::vec3(0, 0, 1),
glm::vec3(0, 0, 1), glm::vec3(0, 1, 1),
glm::vec3(0, 1, 1), glm::vec3(0, 1, 0),
glm::vec3(0, 1, 0), glm::vec3(0, 0, 0),
glm::vec3(1, 0, 0), glm::vec3(1, 0, 1),
glm::vec3(1, 0, 1), glm::vec3(1, 1, 1),
glm::vec3(1, 1, 1), glm::vec3(1, 1, 0),
glm::vec3(1, 1, 0), glm::vec3(1, 0, 0),
glm::vec3(0, 0, 0), glm::vec3(1, 0, 0),
glm::vec3(0, 0, 1), glm::vec3(1, 0, 1),
glm::vec3(0, 1, 1), glm::vec3(1, 1, 1),
glm::vec3(0, 1, 0), glm::vec3(1, 1, 0),
}, {
glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1),
glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1),
glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1),
glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1),
glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1),
glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1),
glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1),
glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1),
glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1),
glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1),
glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1),
glm::vec4(1, 1, 1, 1), glm::vec4(1, 1, 1, 1),
}) {
2020-07-10 17:49:16 +00:00
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
2020-08-30 16:35:45 +00:00
reloadShaders(options.voxel);
SkyPass = std::make_unique<pass::SkyProgram>();
2020-07-13 16:45:08 +00:00
SkyEnable = options.skybox;
2020-08-30 16:35:45 +00:00
IndicatorPass = std::make_unique<pass::ColorProgram>();
2020-07-10 17:49:16 +00:00
2020-07-12 13:46:51 +00:00
FogColor = glm::vec3(options.clear_color.x, options.clear_color.y, options.clear_color.z);
loadTextures(options.textures, options.mipMapLOD, options.anisotropy);
2020-07-10 17:49:16 +00:00
}
2020-09-14 16:03:21 +00:00
Pipeline::~Pipeline() {
2020-07-10 17:49:16 +00:00
unloadTextures();
glDeleteVertexArrays(1, &VertexArrayID);
}
2020-09-14 16:03:21 +00:00
void Pipeline::beginFrame() {
TracyGpuZone("Render");
2020-07-13 16:45:08 +00:00
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2020-07-10 17:49:16 +00:00
}
2020-08-30 16:35:45 +00:00
2020-09-14 16:03:21 +00:00
std::function<buffer::params(glm::mat4)> Pipeline::beginWorldPass() {
2020-08-30 16:35:45 +00:00
WorldPass->useIt();
WorldPass->start(this);
return [&](glm::mat4 model) {
return WorldPass->setup(this, model);
};
}
2020-09-14 16:03:21 +00:00
std::function<buffer::params(const std::vector<glm::mat4> &)> Pipeline::beginEntityPass() {
2020-08-30 16:35:45 +00:00
EntityPass->useIt();
EntityPass->start(this);
return [&](const std::vector<glm::mat4>& models) {
return EntityPass->setup(this, models);
};
}
2020-09-14 16:03:21 +00:00
size_t Pipeline::drawIndicatorCube(glm::mat4 model) {
2020-08-30 16:35:45 +00:00
IndicatorPass->useIt();
return IndicatorCubeBuffer.draw(IndicatorPass->setup(this, model));
}
2020-09-14 16:03:21 +00:00
void Pipeline::endPass() {
2020-07-13 16:45:08 +00:00
if(SkyEnable) {
SkyPass->draw(this);
}
}
2020-07-10 17:49:16 +00:00
2020-09-14 16:03:21 +00:00
void Pipeline::swapBuffer(GLFWwindow* ptr) {
TracyGpuZone("Swap");
glfwSwapBuffers(ptr);
TracyGpuCollect;
}
void Pipeline::reloadShaders(const pass::VoxelProgram::options& options) {
2020-08-30 16:35:45 +00:00
WorldPass = std::make_unique<pass::WorldProgram>(options);
EntityPass = std::make_unique<pass::EntityProgram>(options);
2020-07-10 17:49:16 +00:00
}
2020-09-14 16:03:21 +00:00
void Pipeline::reloadTextures(const std::string& texturePath, float mipMapLOD, float anisotropy) {
2020-07-10 17:49:16 +00:00
unloadTextures();
loadTextures(texturePath, mipMapLOD, anisotropy);
2020-07-10 17:49:16 +00:00
}
2020-09-14 16:03:21 +00:00
void Pipeline::unloadTextures() {
2020-07-10 17:49:16 +00:00
glDeleteTextures(1, &HOSAtlas);
glDeleteTextures(1, &NormalAtlas);
glDeleteTextures(1, &TextureAtlas);
}
2020-09-14 16:03:21 +00:00
void Pipeline::loadTextures(const std::string& texturePath, float mipMapLOD, float anisotropy) {
2020-07-13 16:45:08 +00:00
std::vector<std::string> terrainTextures;
2020-08-07 17:08:02 +00:00
for(const auto& texture: world::materials::textures) {
2020-07-31 17:09:44 +00:00
terrainTextures.emplace_back(texturePath + "/terrain/" + texture);
2020-07-10 17:49:16 +00:00
}
2020-09-14 16:03:21 +00:00
const auto ani = anisotropy >= 1 ? (1 << (static_cast<int>(anisotropy)-1)) : 0;
TextureAtlas = pass::Program::loadTextureArray(terrainTextures, "", mipMapLOD, ani);
NormalAtlas = pass::Program::loadTextureArray(terrainTextures, ".nrm", mipMapLOD, ani);
HOSAtlas = pass::Program::loadTextureArray(terrainTextures, ".hos", mipMapLOD, ani);
2020-07-13 16:45:08 +00:00
2020-07-25 16:45:03 +00:00
Skybox = pass::Program::loadTextureCube(texturePath + "/sky/Space_tray");
2020-07-10 17:49:16 +00:00
}
2020-09-14 16:03:21 +00:00
void Pipeline::lookFrom(const Camera& camera) {
2020-07-10 17:49:16 +00:00
ProjectionMatrix = camera.getProjectionMatrix();
ViewMatrix = camera.getViewMatrix();
2020-07-10 19:37:49 +00:00
FogDepth = camera.getDepth();
2020-09-14 16:03:21 +00:00
}
void Pipeline::setClearColor(glm::vec4 c) {
FogColor = c;
glClearColor(c.r, c.g, c.b, c.a);
2020-07-10 17:49:16 +00:00
}