Changes to shaders

This commit is contained in:
Tulpen 2023-06-14 21:59:21 +01:00
parent 205919e1a9
commit 0e6c5d63c3
No known key found for this signature in database
GPG key ID: 12294D73B907E784
5 changed files with 46 additions and 22 deletions

View file

@ -9,8 +9,6 @@ namespace hibis {
public: public:
Shader(std::string vertexShaderPath, std::string fragShaderPath); Shader(std::string vertexShaderPath, std::string fragShaderPath);
~Shader(); ~Shader();
// Vertex then Frag shader
unsigned int mShaderIDs[2] = {0, 0};
unsigned int mShaderProgram = 0; unsigned int mShaderProgram = 0;
unsigned int mShaderVAO = 0; unsigned int mShaderVAO = 0;
unsigned int mShaderVBO = 0; unsigned int mShaderVBO = 0;

View file

@ -47,7 +47,7 @@ namespace hibis::rglcore {
void RGLCore::clearScreen(Color col) { void RGLCore::clearScreen(Color col) {
float r = (float)(col.r) / 255, g = (float)(col.g) / 255, b = (float)(col.b) / 255, a = (float)(col.a) / 255; float r = (float)(col.r) / 255, g = (float)(col.g) / 255, b = (float)(col.b) / 255, a = (float)(col.a) / 255;
glClearColor(r, g, b, a); glClearColor(r, g, b, a);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
} }
void RGLCore::renderCurrent() { void RGLCore::renderCurrent() {
@ -110,32 +110,59 @@ namespace hibis::rglcore {
void RGLCore::compileShader(Shader* shader) { void RGLCore::compileShader(Shader* shader) {
if (shader->mShaderProgram != 0) return; if (shader->mShaderProgram != 0) return;
char infoLog[512];
int success;
unsigned int shaderIDs[2] = {0, 0};
mLogger(Information, fmt::format("Compiling shader {}", shader->mShaderPaths[0])); mLogger(Information, fmt::format("Compiling shader {}", shader->mShaderPaths[0]));
glGenBuffers(1, &shader->mShaderVBO); glGenBuffers(1, &shader->mShaderVBO);
glBindBuffer(GL_ARRAY_BUFFER, shader->mShaderVBO); glBindBuffer(GL_ARRAY_BUFFER, shader->mShaderVBO);
shader->mShaderIDs[0] = glCreateShader(GL_VERTEX_SHADER); shaderIDs[0] = glCreateShader(GL_VERTEX_SHADER);
const char* vertShader = loadFile(shader->mShaderPaths[0]); const std::string vertShaderStr = loadFile(shader->mShaderPaths[1]);
glShaderSource(shader->mShaderIDs[0], 1, &vertShader, NULL); const char* vertShader = vertShaderStr.c_str();
glCompileShader(shader->mShaderIDs[0]); glShaderSource(shaderIDs[0], 1, &vertShader, NULL);
glCompileShader(shaderIDs[0]);
glGetShaderiv(shaderIDs[0], GL_COMPILE_STATUS, &success);
if(!success) {
glGetShaderInfoLog(shaderIDs[0], 512, NULL, infoLog);
printf("%s", infoLog);
mLogger(Error, fmt::format("Couldn't compile shader '{}'! what: {}", shader->mShaderPaths[0], std::string(infoLog)));
}
mLogger(Information, fmt::format("Compiling shader {}", shader->mShaderPaths[1])); mLogger(Information, fmt::format("Compiling shader {}", shader->mShaderPaths[1]));
shader->mShaderIDs[1] = glCreateShader(GL_VERTEX_SHADER); shaderIDs[1] = glCreateShader(GL_FRAGMENT_SHADER);
const char* fragShader = loadFile(shader->mShaderPaths[1]); const std::string fragShaderStr = loadFile(shader->mShaderPaths[1]);
glShaderSource(shader->mShaderIDs[1], 1, &fragShader, NULL); const char* fragShader = fragShaderStr.c_str();
glCompileShader(shader->mShaderIDs[1]); glShaderSource(shaderIDs[1], 1, &fragShader, NULL);
glCompileShader(shaderIDs[1]);
glGetShaderiv(shaderIDs[1], GL_COMPILE_STATUS, &success);
if(!success) {
glGetShaderInfoLog(shaderIDs[1], 512, NULL, infoLog);
mLogger(Error, fmt::format("Couldn't compile shader '{}'! what: {}", shader->mShaderPaths[1], std::string(infoLog)));
}
shader->mShaderProgram = glCreateProgram(); shader->mShaderProgram = glCreateProgram();
glAttachShader(shader->mShaderProgram, shader->mShaderIDs[0]); glAttachShader(shader->mShaderProgram, shaderIDs[0]);
glAttachShader(shader->mShaderProgram, shader->mShaderIDs[1]); glAttachShader(shader->mShaderProgram, shaderIDs[1]);
glLinkProgram(shader->mShaderProgram); glLinkProgram(shader->mShaderProgram);
mLogger(Information, "Linked shader."); glGetProgramiv(shader->mShaderProgram, GL_LINK_STATUS, &success);
glDeleteShader(shader->mShaderIDs[0]); if (!success) {
glDeleteShader(shader->mShaderIDs[1]); TODO("what the fuck, this isn't helpful")
glGetShaderInfoLog(shader->mShaderProgram, 512, NULL, infoLog);
mLogger(Error, fmt::format("Couldn't link shader! what: {}", std::string(infoLog)));
}
else mLogger(Information, "Linked shader.");
glDeleteShader(shaderIDs[0]);
glDeleteShader(shaderIDs[1]);
mLogger(Information, "Cleaned up leftover shader objects."); mLogger(Information, "Cleaned up leftover shader objects.");
} }
@ -150,7 +177,7 @@ namespace hibis::rglcore {
glfwSetWindowTitle(mWindow, title.c_str()); glfwSetWindowTitle(mWindow, title.c_str());
} }
const char* RGLCore::loadFile(std::string path) { std::string RGLCore::loadFile(std::string path) {
std::string text, line; std::string text, line;
std::ifstream textFile(path); std::ifstream textFile(path);
if (textFile.is_open()) { if (textFile.is_open()) {
@ -163,8 +190,6 @@ namespace hibis::rglcore {
mLogger(Error, fmt::format("Couldn't load file '{}'", path)); mLogger(Error, fmt::format("Couldn't load file '{}'", path));
} }
const char* textChar = text.c_str(); return text;
return textChar;
} }
} }

View file

@ -41,6 +41,6 @@ namespace hibis::rglcore {
GLFWwindow* mWindow; GLFWwindow* mWindow;
std::unordered_map<Texture*, unsigned int> mTextures; std::unordered_map<Texture*, unsigned int> mTextures;
const char* loadFile(std::string path); std::string loadFile(std::string path);
}; };
} }

View file

@ -91,6 +91,7 @@ int main() {
// Clear screen then sleep for ~16ms // Clear screen then sleep for ~16ms
renderer.clearScreen(Color {red, 0, 0, 255}); renderer.clearScreen(Color {red, 0, 0, 255});
renderer.useShader(&shader, points); renderer.useShader(&shader, points);
renderer.stopUsingShaders();
//renderer.drawText(&font, "Testing Text", IntVec2 {0, 0}, Color {255, 255, 255, 255}); //renderer.drawText(&font, "Testing Text", IntVec2 {0, 0}, Color {255, 255, 255, 255});
//renderer.drawTexture(&image, 1.0f, IntVec2 {10, 10}); //renderer.drawTexture(&image, 1.0f, IntVec2 {10, 10});

View file

@ -2,5 +2,5 @@
out vec4 FragColor; out vec4 FragColor;
void main() { void main() {
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); FragColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);
} }