Working on the new refactor of primitive and shader

This commit is contained in:
2021-11-11 09:47:42 -08:00
parent ada6de9723
commit e4222866ef
49 changed files with 635 additions and 440 deletions

View File

@ -0,0 +1,63 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "common.h"
void shaderCommonInit(shadercommon_t *shader, char *vert, char *frag) {
shaderInit(&shader->shader, vert, frag);
shader->uniformProjection = shaderGetUniform(
&shader->shader, SHADER_COMMON_UNIFORM_PROJECTION
);
shader->uniformView = shaderGetUniform(
&shader->shader, SHADER_COMMON_UNIFORM_VIEW
);
shader->uniformModel = shaderGetUniform(
&shader->shader, SHADER_COMMON_UNIFORM_MODEL
);
shader->uniformColor = shaderGetUniform(
&shader->shader, SHADER_COMMON_UNIFORM_COLOR
);
shader->uniformTexture = shaderGetUniform(
&shader->shader, SHADER_COMMON_UNIFORM_TEXTURE
);
}
void shaderCommonUseCamera(shadercommon_t *shader, camera_t *camera) {
shaderUseCamera(
&shader->shader, shader->uniformView, shader->uniformProjection, camera
);
}
void shaderCommonUseTexture(shadercommon_t *shader, texture_t *texture) {
shaderUseTexture(&shader->shader, shader->uniformTexture, texture);
}
void shaderCommonUseColor(shadercommon_t *shader, pixel_t color) {
shaderUseColor(&shader->shader, shader->uniformColor, color);
}
void shaderCommonUsePosition(shadercommon_t *shader,
float x, float y, float z,
float pitch, float yaw, float roll
) {
shaderUsePosition(
&shader->shader, shader->uniformModel, x, y, z, pitch, yaw, roll
);
}
void shaderCommonUsePositionAndScale(
shadercommon_t *shader,
float x, float y, float z,
float pitch, float yaw, float roll,
float scaleX, float scaleY, float scaleZ
) {
shaderUsePositionAndScale(
&shader->shader, shader->uniformView,
x, y, z, pitch, yaw, roll, scaleX, scaleY, scaleZ
);
}

View File

@ -0,0 +1,40 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "shader.h"
#include "../texture.h"
#define SHADER_COMMON_UNIFORM_PROJECTION "u_Proj"
#define SHADER_COMMON_UNIFORM_VIEW "u_View"
#define SHADER_COMMON_UNIFORM_MODEL "u_Modl"
#define SHADER_COMMON_UNIFORM_COLOR "u_Colr"
#define SHADER_COMMON_UNIFORM_TEXTURE "u_Text"
typedef struct {
shader_t shader;
shaderuniform_t uniformProjection;
shaderuniform_t uniformView;
shaderuniform_t uniformModel;
shaderuniform_t uniformColor;
shaderuniform_t uniformTexture;
} shadercommon_t;
void shaderCommonInit(shadercommon_t *shader, char *vert, char *frag);
void shaderCommonUseCamera(shadercommon_t *shader, camera_t *camera);
void shaderCommonUseTexture(shadercommon_t *shader, texture_t *texture);
void shaderCommonUseColor(shadercommon_t *shader, pixel_t color);
void shaderCommonUsePosition(shadercommon_t *shader,
float x, float y, float z,
float pitch, float yaw, float roll
);
void shaderCommonUsePositionAndScale(
shadercommon_t *shader,
float x, float y, float z,
float pitch, float yaw, float roll,
float scaleX, float scaleY, float scaleZ
);

182
src/display/shader/shader.c Normal file
View File

@ -0,0 +1,182 @@
/**
* Copyright (c) 2021 Dominic Msters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "shader.h"
void shaderInit(shader_t *shader,
char *vertexShaderSource, char* fragmentShaderSource
) {
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 };
// Load the vertex shader first
shaderVertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(shaderVertex, 1, filesVertex, 0);
glCompileShader(shaderVertex);
// Validate
glGetShaderiv(shaderVertex, GL_COMPILE_STATUS, &isSuccess);
if(!isSuccess) {
glGetShaderiv(shaderVertex, GL_INFO_LOG_LENGTH, &maxLength);
error = malloc(maxLength);
glGetShaderInfoLog(shaderVertex, maxLength, &maxLength, error);
printf("Failed to compile vertex shader %s\n", error);
free(error);
return;
}
// Now load the Frag shader
shaderFragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(shaderFragment, 1, filesFragment, 0);
glCompileShader(shaderFragment);
glGetShaderiv(shaderFragment, GL_COMPILE_STATUS, &isSuccess);
if(!isSuccess) {
glGetShaderiv(shaderFragment, GL_INFO_LOG_LENGTH, &maxLength);
error = malloc(maxLength);
glGetShaderInfoLog(shaderFragment, maxLength, &maxLength, error);
printf("Failed to compile fragment shader %s\n", error);
free(error);
glDeleteShader(shaderVertex);
return;
}
// Now create the shader program.
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, shaderVertex);
glAttachShader(shaderProgram, shaderFragment);
//Bind, Verify & Use the shader program
glLinkProgram(shaderProgram);
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &isSuccess);
if(!isSuccess) {
glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH, &maxLength);
error = malloc(maxLength);
glGetProgramInfoLog(shaderProgram, maxLength, &maxLength, error);
printf("Failed to load shader program %s\n", error);
free(error);
glDeleteShader(shaderVertex);
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);
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]
);
// TODO: Reset uniforms to zero.
}
// 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->uniforms[i], name) == 0) return i;
}
return (shaderuniform_t)-1;
}
void shaderDispose(shader_t *shader) {
glDeleteProgram(shader->shaderProgram);
glDeleteShader(shader->shaderVertex);
glDeleteShader(shader->shaderFrag);
}
void shaderUse(shader_t *shader) {
glUseProgram(shader->shaderProgram);
}
void shaderUseTexture(
shader_t *shader, shaderuniform_t uniform, texture_t *texture
) {
// TODO: I need to be able to get the texture ID
int32_t i = 0;
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, texture->id);
glUniform1i(uniform, i);
}
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,
float x, float y, float z,
float pitch, float yaw, float roll
) {
matrix_t matrix;
matrixIdentity(&matrix);
matrixTranslate(&matrix, x, y, z);
// Rotation (YZX order)
matrixRotate(&matrix, yaw, 0, 1, 0);
matrixRotate(&matrix, roll, 0, 0, 1);
matrixRotate(&matrix, pitch, 1, 0, 0);
shaderUseMatrix(shader, uniform, &matrix);
}
void shaderUsePositionAndScale(
shader_t *shader, shaderuniform_t uniform,
float x, float y, float z,
float pitch, float yaw, float roll,
float scaleX, float scaleY, float scaleZ
) {
matrix_t matrix;
matrixIdentity(&matrix);
matrixTranslate(&matrix, x, y, z);
// Rotation (YZX order)
matrixRotate(&matrix, yaw, 0, 1, 0);
matrixRotate(&matrix, roll, 0, 0, 1);
matrixRotate(&matrix, pitch, 1, 0, 0);
matrixScale(&matrix, scaleX, scaleY, scaleZ);
shaderUseMatrix(shader, uniform, &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);
}

158
src/display/shader/shader.h Normal file
View File

@ -0,0 +1,158 @@
/**
* Copyright (c) 2021 Dominic Msters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "../../libs.h"
#include "../matrix.h"
#include "../camera.h"
#include "../texture.h"
#include "../../util/array.h"
#define SHADER_UNIFORM_NAME_MAX 24
#define SHADER_UNIFORM_COUNT 8
/** Representation of a shader uniform */
typedef GLuint shaderuniform_t;
/**
* Structure containing information about an OpenGL Shader. For simplicity sake
* we demand certain uninforms to be present on the shader target.
*/
typedef struct {
/** Pointer to an uploaded vertex shader program */
shaderuniform_t shaderVertex;
/** Pointer to an uploaded fragment shader program */
shaderuniform_t shaderFrag;
/** Pointer to an uploaded shader program linked */
shaderuniform_t shaderProgram;
char uniformBuffer[SHADER_UNIFORM_NAME_MAX * SHADER_UNIFORM_COUNT];
char *uniforms[SHADER_UNIFORM_COUNT];
int32_t uniformCount;
} shader_t;
/**
* Compiles a shader from vertex and fragment shader code.
* @param shader Shader to compile into.
* @param vertexShaderSource The raw vertex shader code.
* @param fragmentShaderSource The raw fragment shader code.
*/
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
*/
void shaderDispose(shader_t *shader);
/**
* Attaches the supplied shader as the current shader.
* @param shader The shader to attach
*/
void shaderUse(shader_t *shader);
/**
* 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
);
/**
* Set's a specific shader uniform to a matrix.
*
* @param shader Shader to apply to.
* @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);
/**
* 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).
* @param pitch Pitch of the object (local space).
* @param yaw Yaw of the object (local space).
* @param roll Roll of the object (local space).
*/
void shaderUsePosition(
shader_t *shader, shaderuniform_t uniform,
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).
* @param pitch Pitch of the object (local space).
* @param yaw Yaw of the object (local space).
* @param roll Roll of the object (local space).
* @param scaleX X scale of model (1 being 100% scaled).
* @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,
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
);