Added function to compile multiple shaders and prospective code for multiple programs (WIP)

This commit is contained in:
JonasJan2 2024-06-22 17:16:51 +02:00
parent 7142ed2d6a
commit a44757cbd6
3 changed files with 67 additions and 12 deletions

View File

@ -71,6 +71,9 @@ void handleInputs(double deltaTime) {
if (glfwGetKey(window, GLFW_KEY_4) == GLFW_PRESS) {
cameraPosition = (vec3){-0.6f, 2.1f, -4.5f};
}
if (glfwGetKey(window, GLFW_KEY_5) == GLFW_PRESS) {
cameraPosition = (vec3){-10.6f, 22.1f, -4.5f};
}
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
cameraPosition.z += deltaTime * 10;
}
@ -161,8 +164,6 @@ void renderNode(SceneNode* node) {
void init(void) {
//IDEE: shader in array speichern und dann so kompilieren etc
GLuint vertexShader;
GLuint fragmentShader;
GLint status;
Shader shaders[2] = {
@ -170,8 +171,13 @@ void init(void) {
{GL_FRAGMENT_SHADER, 0}
};
compileShader(vertexShader_glsl, &shaders[0]);
compileShader(fragmentShader_glsl, &shaders[1]);
const GLchar* shaderSources[2] = {
vertexShader_glsl,
fragmentShader_glsl
};
int numShaders = sizeof(shaders) / sizeof(Shader);
compileShaders(shaders, shaderSources, numShaders);
program = glCreateProgram();

View File

@ -1,13 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
typedef struct {
GLenum type;
GLuint id;
} Shader;
#include "shader.h"
void compileShader(const GLchar *shaderSource, Shader *shader) {
shader->id = glCreateShader(shader->type);
@ -27,3 +21,50 @@ void compileShader(const GLchar *shaderSource, Shader *shader) {
exit(1);
}
}
void compileShaders(Shader* shaders, const GLchar** glslSources, int numShaders) {
for (int i = 0; i < numShaders; i++) {
compileShader(glslSources[i], &shaders[i]);
}
}
ShaderProgram createShaderProgram(const GLchar* vertexSource, const GLchar* fragmentSource) {
Shader vertexShader = { GL_VERTEX_SHADER, 0 };
Shader fragmentShader = { GL_FRAGMENT_SHADER, 0 };
const GLchar* shaderSources[2] = { vertexSource, fragmentSource };
Shader shaders[2] = { vertexShader, fragmentShader };
compileShaders(shaders, shaderSources, 2);
GLuint program = glCreateProgram();
glAttachShader(program, shaders[0].id);
glAttachShader(program, shaders[1].id);
glLinkProgram(program);
GLint status;
glGetProgramiv(program, GL_LINK_STATUS, &status);
if (!status) {
printf("Error linking program: ");
GLchar infoLog[1024];
glGetProgramInfoLog(program, 1024, NULL, infoLog);
printf("%s\n", infoLog);
exit(1);
}
glValidateProgram(program);
glGetProgramiv(program, GL_VALIDATE_STATUS, &status);
if (!status) {
printf("Error validating program: ");
GLchar infoLog[1024];
glGetProgramInfoLog(program, 1024, NULL, infoLog);
printf("%s\n", infoLog);
exit(1);
}
ShaderProgram shaderProgram = { program, vertexSource, fragmentSource };
return shaderProgram;
}

View File

@ -8,6 +8,14 @@ typedef struct {
GLuint id;
} Shader;
typedef struct {
GLuint program;
const GLchar* vertexSource;
const GLchar* fragmentSource;
} ShaderProgram;
void compileShader(const GLchar *shaderSource, Shader *shader);
void compileShaders(Shader* shaders, const GLchar** glslSources, int numShaders);
ShaderProgram createShaderProgram(const GLchar* vertexSource, const GLchar* fragmentSource);
#endif