Working on shader code.

This commit is contained in:
2021-11-09 09:25:15 -08:00
parent 2d05d9e60e
commit 66e8240ff7
2 changed files with 46 additions and 7 deletions

View File

@ -10,9 +10,12 @@
void shaderInit(shader_t *shader,
char *vertexShaderSource, char* fragmentShaderSource
) {
int isSuccess, maxLength;
int32_t isSuccess, maxLength, i;
char *error;
GLuint shaderVertex, shaderFragment, shaderProgram;
GLint size; // size of the variable
GLenum type; // type of the variable (float, vec3 or mat4, etc)
GLsizei length; // name length
GLchar const* filesVertex[] = { vertexShaderSource };
GLchar const* filesFragment[] = { fragmentShaderSource };
@ -66,17 +69,28 @@ void shaderInit(shader_t *shader,
glDeleteShader(shaderFragment);
return;
}
// Everything is okay, let's create the encapsulated shader.
shader->shaderVertex = shaderVertex;
shader->shaderFrag = shaderFragment;
shader->shaderProgram = shaderProgram;
shader->uniProj = glGetUniformLocation(shader->shaderProgram,SHADER_UNI_PROJ);
shader->uniView = glGetUniformLocation(shader->shaderProgram,SHADER_UNI_VIEW);
shader->uniText = glGetUniformLocation(shader->shaderProgram,SHADER_UNI_TEXT);
shader->uniModl = glGetUniformLocation(shader->shaderProgram,SHADER_UNI_MODL);
shader->uniColr = glGetUniformLocation(shader->shaderProgram,SHADER_UNI_COLR);
// Extract uniforms
glGetProgramiv(shaderProgram, GL_ACTIVE_UNIFORMS, &shader->uniformCount);
for(i = 0; i < shader->uniformCount; i++) {
shader->uniforms[i] = shader->uniformBuffer + (i * SHADER_UNIFORM_NAME_MAX);
glGetActiveUniform(
shaderProgram, (GLuint)i, SHADER_UNIFORM_NAME_MAX,
&length, &size, &type, shader->uniforms[i]
);
}
// Hold off on this for now
shader->uniProj = shaderGetUniform(shader, SHADER_UNI_PROJ);
shader->uniView = shaderGetUniform(shader, SHADER_UNI_VIEW);
shader->uniText = shaderGetUniform(shader, SHADER_UNI_TEXT);
shader->uniModl = shaderGetUniform(shader, SHADER_UNI_MODL);
shader->uniColr = shaderGetUniform(shader, SHADER_UNI_COLR);
// Bind the shader
shaderUse(shader);
@ -86,6 +100,14 @@ void shaderInit(shader_t *shader,
shaderUseColor(shader, PIXEL_COLOR_WHITE);
}
shaderuniform_t shaderGetUniform(shader_t *shader, char *name) {
int32_t i;
for(i = 0; i < shader->uniformCount; i++) {
if(strcmp(shader->uniforms[i], name) == 0) return i;
}
return (shaderuniform_t)-1;
}
void shaderDispose(shader_t *shader) {
glDeleteProgram(shader->shaderProgram);
glDeleteShader(shader->shaderVertex);

View File

@ -10,6 +10,7 @@
#include "matrix.h"
#include "camera.h"
#include "texture.h"
#include "../util/array.h"
#define SHADER_UNI_VIEW "u_View"
#define SHADER_UNI_PROJ "u_Proj"
@ -17,6 +18,9 @@
#define SHADER_UNI_MODL "u_Modl"
#define SHADER_UNI_COLR "u_Colr"
#define SHADER_UNIFORM_NAME_MAX 24
#define SHADER_UNIFORM_COUNT 8
/** Representation of a shader uniform */
typedef GLuint shaderuniform_t;
@ -48,6 +52,10 @@ typedef struct {
/** Uniform for the color multiplier */
shaderuniform_t uniColr;
char uniformBuffer[SHADER_UNIFORM_NAME_MAX * SHADER_UNIFORM_COUNT];
char *uniforms[SHADER_UNIFORM_COUNT];
int32_t uniformCount;
} shader_t;
/**
@ -60,6 +68,15 @@ void shaderInit(shader_t *shader,
char *vertexShaderSource, char* fragmentShaderSource
);
/**
* Return the shaderuniform_t for a given shader uniform name.
*
* @param shader Shader to get from
* @param name Name to look for
* @return The shader uniform, or -1 if not found.
*/
shaderuniform_t shaderGetUniform(shader_t *shader, char *name);
/**
* Cleanup and unload a previously loaded shader.
* @param shader The shader to unload