Final commit pre C++

This commit is contained in:
2021-11-25 08:31:35 -08:00
parent a0ff2909b6
commit a57e978f6c
36 changed files with 266 additions and 354 deletions

View File

@ -46,9 +46,7 @@ renderpass_t * renderListRenderPass(
return renderPass;
}
void renderListRender(
renderlist_t *list, shader_t *shader, shaderuniform_t *uniforms
) {
void renderListRender(renderlist_t *list, shader_t *shader) {
camera_t camera;
int32_t i;
renderpass_t *pass;
@ -62,13 +60,13 @@ void renderListRender(
// Set the shader
shaderUse(shader);
shaderUseCamera(shader, uniforms[0], uniforms[1], &camera);
shaderUsePosition(shader, uniforms[2], 0,0,0, 0,0,0);
shaderUseCamera(shader, &camera);
shaderUsePosition(shader, 0,0,0, 0,0,0);
// Render each pass.
for(i = 0; i < list->passCount; i++) {
pass = renderListGetPass(list, i);
shaderUseTexture(shader, uniforms[3+i], &pass->frame.texture);
shaderUseTexture(shader, &pass->frame.texture);
primitiveDraw(&list->quad, 0, -1);
}
}
@ -88,9 +86,9 @@ void renderListAsBackbuffer(
// Set up the shader.
shaderUse(shader);
shaderUseCamera(shader, uniforms[0], uniforms[1], &camera);
shaderUsePosition(shader, uniforms[2], 0,0,0, 0,0,0);
shaderUseTexture(shader, uniforms[3], &list->frame.texture);
shaderUseCamera(shader, &camera);
shaderUsePosition(shader, 0,0,0, 0,0,0);
shaderUseTexture(shader, &list->frame.texture);
// Render the quad to the back buffer.
primitiveDraw(&list->quad, 0, -1);

View File

@ -10,11 +10,9 @@
void shaderInit(shader_t *shader,
char *vertexShaderSource, char* fragmentShaderSource
) {
int32_t isSuccess, maxLength, i, texture;
int isSuccess, maxLength;
char *error;
GLuint shaderVertex, shaderFragment, shaderProgram;
GLint size; // size of the variable
GLsizei length; // name length
GLchar const* filesVertex[] = { vertexShaderSource };
GLchar const* filesFragment[] = { fragmentShaderSource };
@ -68,49 +66,24 @@ 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;
// Extract uniforms
glGetProgramiv(shaderProgram, GL_ACTIVE_UNIFORMS, &shader->uniformCount);
texture = 0;
for(i = 0; i < shader->uniformCount; i++) {
shader->uniformNames[i] = (
shader->uniformBuffer + (i * SHADER_UNIFORM_NAME_MAX)
);
glGetActiveUniform(
shaderProgram, (GLuint)i, SHADER_UNIFORM_NAME_MAX,
&length, &size, shader->types + i, shader->uniformNames[i]
);
// TODO: Reset uniforms to zero.
if(shader->types[i] == GL_SAMPLER_2D) shader->textureSlots[i] = texture++;
}
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);
// Bind the shader
shaderUse(shader);
}
shaderuniform_t shaderGetUniform(shader_t *shader, char *name) {
int32_t i;
for(i = 0; i < shader->uniformCount; i++) {
if(strcmp(shader->uniformNames[i], name) == 0) return i;
}
return (shaderuniform_t)0xFFFFFFFF;
}
void shaderGetUniformArray(
shader_t *shader, shaderuniform_t *uniformSet,
char **uniforms, int32_t uniformCount
) {
int32_t i;
for(i = 0; i < uniformCount; i++) {
uniformSet[i] = shaderGetUniform(shader, uniforms[i]);
}
// Reset position
shaderUsePosition(shader, 0, 0, 0, 0, 0, 0);
shaderUseColor(shader, PIXEL_COLOR_WHITE);
}
void shaderDispose(shader_t *shader) {
@ -123,33 +96,23 @@ void shaderUse(shader_t *shader) {
glUseProgram(shader->shaderProgram);
}
void shaderUseTexture(
shader_t *shader, shaderuniform_t uniform, texture_t *texture
) {
int32_t i = shader->textureSlots[(int32_t)uniform];
// TODO: I need to be able to get the texture ID
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, texture->id);
glUniform1i(uniform, i);
void shaderUseCamera(shader_t *shader, camera_t *camera) {
shaderUseMatrix(shader, shader->uniView, &camera->view);
shaderUseMatrix(shader, shader->uniProj, &camera->projection);
}
void shaderUseMatrix(
shader_t *shader, shaderuniform_t uniform, matrix_t *matrix
) {
void shaderUseTexture(shader_t *shader, texture_t *texture) {
// OpenGL requires us to set the active texure.
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture->id);
glUniform1i(shader->uniText, 0);
}
void shaderUseMatrix(shader_t *shader,shaderuniform_t uniform,matrix_t *matrix){
glUniformMatrix4fv(uniform, 1, GL_FALSE, matrix->internalMatrix[0]);
}
void shaderUseColor(shader_t *shader, shaderuniform_t uniform, pixel_t color) {
glUniform4f(uniform,
(float)color.r / 255.0f,
(float)color.g / 255.0f,
(float)color.b / 255.0f,
(float)color.a / 255.0f
);
}
void shaderUsePosition(
shader_t *shader, shaderuniform_t uniform,
void shaderUsePosition(shader_t *shader,
float x, float y, float z,
float pitch, float yaw, float roll
) {
@ -162,11 +125,10 @@ void shaderUsePosition(
matrixRotate(&matrix, yaw, 0, 1, 0);
matrixRotate(&matrix, roll, 0, 0, 1);
matrixRotate(&matrix, pitch, 1, 0, 0);
shaderUseMatrix(shader, uniform, &matrix);
shaderUseMatrix(shader, shader->uniModl, &matrix);
}
void shaderUsePositionAndScale(
shader_t *shader, shaderuniform_t uniform,
void shaderUsePositionAndScale(shader_t *shader,
float x, float y, float z,
float pitch, float yaw, float roll,
float scaleX, float scaleY, float scaleZ
@ -183,14 +145,14 @@ void shaderUsePositionAndScale(
matrixScale(&matrix, scaleX, scaleY, scaleZ);
shaderUseMatrix(shader, uniform, &matrix);
shaderUseMatrix(shader, shader->uniModl, &matrix);
}
void shaderUseCamera(
shader_t *shader,
shaderuniform_t uniformView, shaderuniform_t uniformProjection,
camera_t *camera
) {
shaderUseMatrix(shader, uniformView, &camera->view);
shaderUseMatrix(shader, uniformProjection, &camera->projection);
}
void shaderUseColor(shader_t *shader, pixel_t color) {
glUniform4f(shader->uniColr,
(float)color.r / 255.0f,
(float)color.g / 255.0f,
(float)color.b / 255.0f,
(float)color.a / 255.0f
);
}

View File

@ -10,10 +10,12 @@
#include "matrix.h"
#include "camera.h"
#include "texture.h"
#include "../util/array.h"
#define SHADER_UNIFORM_NAME_MAX 24
#define SHADER_UNIFORM_COUNT 16
#define SHADER_UNI_VIEW "u_View"
#define SHADER_UNI_PROJ "u_Proj"
#define SHADER_UNI_TEXT "u_Text"
#define SHADER_UNI_MODL "u_Model"
#define SHADER_UNI_COLR "u_Colr"
/** Representation of a shader uniform */
typedef GLuint shaderuniform_t;
@ -24,26 +26,28 @@ typedef GLuint shaderuniform_t;
*/
typedef struct {
/** Pointer to an uploaded vertex shader program */
GLuint shaderVertex;
shaderuniform_t shaderVertex;
/** Pointer to an uploaded fragment shader program */
GLuint shaderFrag;
shaderuniform_t shaderFrag;
/** Pointer to an uploaded shader program linked */
GLuint shaderProgram;
shaderuniform_t shaderProgram;
/** Buffer of chars where we store the uniform names */
char uniformBuffer[SHADER_UNIFORM_NAME_MAX * SHADER_UNIFORM_COUNT];
/** Matrix for the view matrix */
shaderuniform_t uniView;
/** Array of strings (pointers to the above buffer) of the uniform names */
char *uniformNames[SHADER_UNIFORM_COUNT];
int32_t uniformCount;
/** Matrix for the projection matrix */
shaderuniform_t uniProj;
/** Type of each uniform */
GLenum types[SHADER_UNIFORM_COUNT];
/** Uniform for the current texture */
shaderuniform_t uniText;
/** Texture Slots (which texture slot for GL to use for each uniform) */
uint8_t textureSlots[SHADER_UNIFORM_COUNT];
/** Uniform for the current model world position */
shaderuniform_t uniModl;
/** Uniform for the color multiplier */
shaderuniform_t uniColr;
} shader_t;
/**
@ -56,28 +60,6 @@ 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);
/**
* Return an array of shaderuniform_t's into an array for a given string array.
*
* @param shader Shader to get the uniforms from.
* @param uniformSet Uniform array to get.
* @param uniforms Uniform strings to get.
* @param uniformCount Count of uniforms you're getting.
*/
void shaderGetUniformArray(
shader_t *shader, shaderuniform_t *uniformSet,
char **uniforms, int32_t uniformCount
);
/**
* Cleanup and unload a previously loaded shader.
* @param shader The shader to unload
@ -90,16 +72,20 @@ void shaderDispose(shader_t *shader);
*/
void shaderUse(shader_t *shader);
/**
* Attaches a camera to the shader.
* @param shader Shader to attach to.
* @param camera Camera to attach.
*/
void shaderUseCamera(shader_t *shader, camera_t *camera);
/**
* Attaches a texture to the shader.
*
* @param shader Shader to attach to.
* @param uniform Uniform on the shader to set.
* @param texture Texture to attach.
*/
void shaderUseTexture(
shader_t *shader, shaderuniform_t uniform, texture_t *texture
);
void shaderUseTexture(shader_t *shader, texture_t *texture);
/**
* Set's a specific shader uniform to a matrix.
@ -108,25 +94,12 @@ void shaderUseTexture(
* @param uniform Uniform on the shader to set.
* @param matrix Matrix to apply.
*/
void shaderUseMatrix(
shader_t *shader, shaderuniform_t uniform, matrix_t *matrix
);
/**
* Set's a specific shader uniform to a color.
*
* @param shader Shader to apply to.
* @param uniform Uniform on the shader to set.
* @param color Color to set on to the uniform.
*/
void shaderUseColor(shader_t *shader, shaderuniform_t uniform, pixel_t color);
void shaderUseMatrix(shader_t *shader,shaderuniform_t uniform,matrix_t *matrix);
/**
* Set's the current translation matrix onto the shader for the next
* render to use. Rotation order is set to YZX.
*
* @param shader Shader to attach to.
* @param uniform Uniform on the shader to set.
* @param x X coordinate (world space).
* @param y Y coordinate (world space).
* @param z Z coordinate (world space).
@ -134,18 +107,17 @@ void shaderUseColor(shader_t *shader, shaderuniform_t uniform, pixel_t color);
* @param yaw Yaw of the object (local space).
* @param roll Roll of the object (local space).
*/
void shaderUsePosition(
shader_t *shader, shaderuniform_t uniform,
void shaderUsePosition(shader_t *shader,
float x, float y, float z,
float pitch, float yaw, float roll
);
/**
* Set's the current translation matrix onto the shader for the next
* render to use. Also provides scaling controls.
*
* @param shader Shader to attach to.
* @param uniform Uniform on the shader to set.
* @param x X coordinate (world space).
* @param y Y coordinate (world space).
* @param z Z coordinate (world space).
@ -156,23 +128,11 @@ void shaderUsePosition(
* @param scaleY Y scale of model (1 being 100% scaled).
* @param scaleZ Z scale of model (1 being 100% scaled).
*/
void shaderUsePositionAndScale(
shader_t *shader, shaderuniform_t uniform,
void shaderUsePositionAndScale(shader_t *shader,
float x, float y, float z,
float pitch, float yaw, float roll,
float scaleX, float scaleY, float scaleZ
);
/**
* Attaches a camera to the shader.
*
* @param shader Shader to attach to.
* @param uniformView Shader Uniform for the view matrix.
* @param uniformProjection Shader Uniform for the view matrix.
* @param camera Camera to attach.
*/
void shaderUseCamera(
shader_t *shader,
shaderuniform_t uniformView, shaderuniform_t uniformProjection,
camera_t *camera
);
void shaderUseColor(shader_t *shader, pixel_t color);