1
0
Fork 0
Univerxel/src/render/pass/MainProgram.cpp

90 lines
2.9 KiB
C++

#include "MainProgram.hpp"
#include "../Renderer.hpp"
MainProgram::MainProgram(const MainProgram::options& opts): Program() {
std::vector<std::string> flags;
if(opts.pbr)
flags.push_back("PBR");
if(opts.triplanar)
flags.push_back("TRIPLANAR");
if (opts.fog)
flags.push_back("FOG");
if (opts.blend)
flags.push_back("BLEND");
std::vector<Shader*> shaders;
shaders.push_back(loadShader(GL_VERTEX_SHADER, flags));
shaders.push_back(loadShader(GL_FRAGMENT_SHADER, flags));
if (opts.blend)
shaders.push_back(loadShader(GL_GEOMETRY_SHADER, flags));
load(shaders);
MVPMatrixID = glGetUniformLocation(ProgramID, "MVP");
ModelMatrixID = glGetUniformLocation(ProgramID, "Model");
ViewMatrixID = glGetUniformLocation(ProgramID, "View");
TextureID = glGetUniformLocation(ProgramID, "TextureAtlas");
NormalID = glGetUniformLocation(ProgramID, "NormalAtlas");
HOSID = glGetUniformLocation(ProgramID, "HOSAtlas");
LightInvDirID = glGetUniformLocation(ProgramID, "LightInvDirection_worldspace");
FogDepthID = glGetUniformLocation(ProgramID, "FogDepth");
FogColorID = glGetUniformLocation(ProgramID, "FogColor");
}
MainProgram::~MainProgram() { }
std::string MainProgram::getName() const {
return "Main";
}
void MainProgram::start(Renderer *renderer) {
bindTexture(renderer->getTextureAtlas());
bindNormal(renderer->getNormalAtlas());
bindHOS(renderer->getHOSAtlas());
setLightInvDir(&renderer->LightInvDir[0]);
setFog(&renderer->FogColor[0], renderer->FogDepth);
}
Buffer::params MainProgram::setup(Renderer *renderer, glm::mat4 modelMatrix) {
setModel(&modelMatrix[0][0]);
setView(&renderer->getViewMatrix()[0][0]);
const auto mvp = renderer->getProjectionMatrix() * renderer->getViewMatrix() * modelMatrix;
setMVP(&mvp[0][0]);
return Buffer::params{.vertexOnly = false};
}
void MainProgram::setMVP(const GLfloat *matrix) {
glUniformMatrix4fv(MVPMatrixID, 1, GL_FALSE, matrix);
}
void MainProgram::setModel(const GLfloat *matrix) {
glUniformMatrix4fv(ModelMatrixID, 1, GL_FALSE, matrix);
}
void MainProgram::setView(const GLfloat *matrix) {
glUniformMatrix4fv(ViewMatrixID, 1, GL_FALSE, matrix);
}
void MainProgram::bindTexture(GLuint textureID) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D_ARRAY, textureID);
glUniform1i(TextureID, 0);
}
void MainProgram::bindNormal(GLuint textureID) {
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D_ARRAY, textureID);
glUniform1i(NormalID, 1);
}
void MainProgram::bindHOS(GLuint textureID) {
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D_ARRAY, textureID);
glUniform1i(HOSID, 2);
}
void MainProgram::setLightInvDir(const GLfloat *pos) {
glUniform3fv(LightInvDirID, 1, pos);
}
void MainProgram::setFog(const GLfloat *color, const GLfloat depth) {
glUniform3fv(FogColorID, 1, color);
glUniform1f(FogDepthID, depth);
}