Working on the new refactor of primitive and shader

This commit is contained in:
2021-11-11 09:47:42 -08:00
parent ea5f68b479
commit 1e4ccc3e52
61 changed files with 635 additions and 440 deletions

View File

@ -8,8 +8,8 @@
#pragma once
#include "../libs.h"
#include "texture.h"
#include "primitive.h"
#include "primitives/quad.h"
#include "primitive/primitive.h"
#include "primitive/quad.h"
#include "../util/mem.h"
#include "../util/math.h"

View File

@ -5,7 +5,7 @@
#pragma once
#include "../../libs.h"
#include "../primitive.h"
#include "primitive.h"
#define CUBE_VERTICE_COUNT 8
#define CUBE_INDICE_COUNT 36

View File

@ -6,7 +6,7 @@
*/
#pragma once
#include "../libs.h"
#include "../../libs.h"
#define PRIMITIVE_POSITIONS_PER_VERTICE 3
#define PRIMITIVE_COORDINATES_PER_VERTICE 2

View File

@ -5,7 +5,7 @@
#pragma once
#include "../../libs.h"
#include "../primitive.h"
#include "primitive.h"
#define QUAD_VERTICE_COUNT 4
#define QUAD_INDICE_COUNT 6

View File

@ -6,7 +6,7 @@
#pragma once
#include "../../libs.h"
#include "../../util/math.h"
#include "../primitive.h"
#include "primitive.h"
/** How many slices in each cylinder. */
#define SKYWALL_SLICE_COUNT 40

View File

@ -13,26 +13,29 @@ void renderListInit(renderlist_t *list,int32_t width, int32_t height) {
list->passCount = 0;
}
renderpass_t * renderListGetPass(renderlist_t *list, int32_t pass) {
renderpass_t * renderListGetPass(renderlist_t *list, uint8_t pass) {
return list->passes + pass;
}
int32_t renderPassAdd(renderlist_t *list) {
int32_t i = list->passCount++;
renderpass_t *pass = renderListGetPass(list, i);
uint8_t renderPassAdd(renderlist_t *list, shader_t *shader) {
uint8_t i;
renderpass_t *pass;
i = list->passCount++;
pass = renderListGetPass(list, i);
pass->shader = shader;
frameBufferInit(&pass->frame,
list->frame.texture.width, list->frame.texture.height
);
return i;
}
void renderListRenderPass(
renderlist_t *list, engine_t *engine,
camera_t *camera, int32_t pass, renderitem_t *items, int32_t itemCount
renderpass_t * renderListRenderPass(
renderlist_t *list, engine_t *engine, uint8_t pass
) {
int32_t i;
renderpass_t *renderPass;
renderitem_t *item;
renderPass = renderListGetPass(list, pass);
@ -40,17 +43,14 @@ void renderListRenderPass(
frameBufferUse(&renderPass->frame, true);
shaderUse(renderPass->shader);
// "Uniforms"
shaderUseCamera(renderPass->shader, camera);
// Render list
for(i = 0; i < itemCount; i++) {
item = items + i;
item->onRender(list, renderPass, engine, i);
}
return renderPass;
}
void renderListRender(renderlist_t *list, shader_t *shader) {
void renderListRender(
renderlist_t *list, shader_t *shader,
shaderuniform_t uniformView, shaderuniform_t uniformProjection,
shaderuniform_t uniformModel, shaderuniform_t uniformTextures[]
) {
camera_t camera;
int32_t i;
renderpass_t *pass;
@ -64,19 +64,21 @@ void renderListRender(renderlist_t *list, shader_t *shader) {
// Set the shader
shaderUse(shader);
shaderUsePosition(shader, 0,0,0, 0,0,0);
shaderUseCamera(shader, &camera);
shaderUsePosition(shader, uniformModel, 0,0,0, 0,0,0);
shaderUseCamera(shader, uniformView, uniformProjection, &camera);
// Render each pass.
for(i = 0; i < list->passCount; i++) {
pass = renderListGetPass(list, i);
shaderUseTexture(shader, &pass->frame.texture);
shaderUseTexture(shader, uniformTextures[i], &pass->frame.texture);
primitiveDraw(&list->quad, 0, -1);
}
}
void renderListAsBackbuffer(
renderlist_t *list, engine_t *engine, shader_t *shader
renderlist_t *list, engine_t *engine, shader_t *shader,
shaderuniform_t uniformView, shaderuniform_t uniformProjection,
shaderuniform_t uniformModel, shaderuniform_t uniformTexture
) {
camera_t camera;
@ -89,10 +91,38 @@ void renderListAsBackbuffer(
// Set up the shader.
shaderUse(shader);
shaderUseTexture(shader, &list->frame.texture);
shaderUseCamera(shader, &camera);
shaderUsePosition(shader, 0,0,0, 0,0,0);
shaderUseTexture(shader, uniformTexture, &list->frame.texture);
shaderUseCamera(shader, uniformView, uniformProjection, &camera);
shaderUsePosition(shader, uniformModel, 0,0,0, 0,0,0);
// Render the quad to the back buffer.
primitiveDraw(&list->quad, 0, -1);
}
void renderListResize(renderlist_t *list, int32_t width, int32_t height) {
uint8_t i;
if(
width == list->frame.texture.width &&
height == list->frame.texture.height
) return;
frameBufferDispose(&list->frame);
frameBufferInit(&list->frame, width, height);
for(i = 0; i < list->passCount; i++) {
frameBufferDispose(&list->passes[i].frame);
frameBufferInit(&list->passes[i].frame, width, height);
}
}
void renderListDispose(renderlist_t *list) {
uint8_t i;
for(i = 0; i < list->passCount; i++) {
frameBufferDispose(&list->passes[i].frame);
}
frameBufferDispose(&list->frame);
primitiveDispose(&list->quad);
}

View File

@ -8,11 +8,11 @@
#pragma once
#include "../libs.h"
#include "framebuffer.h"
#include "primitive.h"
#include "shader.h"
#include "primitive/primitive.h"
#include "shader/shader.h"
#include "camera.h"
#include "../engine/engine.h"
#include "primitives/quad.h"
#include "primitive/quad.h"
#include "../util/dynarray.h"
#include "render.h"
@ -28,28 +28,108 @@ typedef struct {
primitive_t quad;
renderpass_t passes[RENDER_PASSES_MAX];
int32_t passCount;
void *user;
uint8_t passCount;
} renderlist_t;
typedef void renderitemcallback_t(
renderlist_t *list, renderpass_t *pass, engine_t *engine, int32_t i
);
typedef struct {
renderitemcallback_t *onRender;
} renderitem_t;
shader_t shader;
shaderuniform_t uniformTexture;
shaderuniform_t uniformView;
shaderuniform_t uniformProjection;
} renderlistbackbuffershader_t;
/**
* Initialize a render pass list.
*
* @param list List to initialize.
* @param width Width of the frame buffer(s).
* @param height Height of the frame buffer(s).
*/
void renderListInit(renderlist_t *list, int32_t width, int32_t height);
renderpass_t * renderListGetPass(renderlist_t *list, int32_t pass);
int32_t renderPassAdd(renderlist_t *list);
void renderListRenderPass(
renderlist_t *list, engine_t *engine,
camera_t *camera, int32_t pass, renderitem_t *items, int32_t itemCount
/**
* Retrieve the render pass at the given index.
*
* @param list List to get the pass from.
* @param pass Render pass index to get.
* @return The render pass at the given index.
*/
renderpass_t * renderListGetPass(renderlist_t *list, uint8_t pass);
/**
* Adds a render pass to the render list.
*
* @param list Render list to add the pass to.
* @param shader Shader to use for the render pass.
* @return The render pass index.
*/
uint8_t renderPassAdd(renderlist_t *list, shader_t *shader);
/**
* Prepare the rendering for a specific render pass. This will set up the
* render pass framebuffer, shader and prepare it for rendering.
*
* @param list List to get the render pass from.
* @param engine Game engine for the render pass.
* @param pass Pass index to render.
* @return The pointer to the render pass that is now active.
*/
renderpass_t * renderListRenderPass(
renderlist_t *list, engine_t *engine, uint8_t pass
);
void renderListRender(renderlist_t *list, shader_t *shader);
/**
* Render a render list. The render list will provide the sum of the textures to
* the given shader as uniforms and then call a single render against a textured
* quad. The given shader will need to decide how to multiply the provided
* texture indexes.
*
* @param list List to render.
* @param shader Shader to use while rendering.
* @param uniformView Shader uniform to receive the view matrix.
* @param uniformProjection Shader uniform to receive the projection matirx.
* @param uniformModel Shader uniform to receive the model matrix.
* @param uniformTextures Array of uniforms for each rendered pass as textures.
*/
void renderListRender(
renderlist_t *list, shader_t *shader,
shaderuniform_t uniformView, shaderuniform_t uniformProjection,
shaderuniform_t uniformModel, shaderuniform_t uniformTextures[]
);
/**
* Takes a previously rendered render list and renders it to the backbuffer.
* You could do this manually, but this method makes the assumption that the
* render list is the only thing to be rendered to the backbuffer (currently).
*
* @param list Render list to render to the backbuffer
* @param engine Engine to use when rendering.
* @param shader Shader to use to render to the backbuffer.
* @param uniformView Shader uniform to receive the view matrix.
* @param uniformProjection Shader uniform to receive the projection matirx.
* @param uniformModel Shader uniform to receive the model matrix.
* @param uniformTexture Shader uniform to receive the texture.
*/
void renderListAsBackbuffer(
renderlist_t *list, engine_t *engine, shader_t *shader
);
renderlist_t *list, engine_t *engine, shader_t *shader,
shaderuniform_t uniformView, shaderuniform_t uniformProjection,
shaderuniform_t uniformModel, shaderuniform_t uniformTexture
);
/**
* Resize an existing render list and all its render pass frame buffers. This
* will dispose (and clear) the existing frame buffers for both the list and
* each of the passes. Resizing to the same size won't cause an update to occur.
*
* @param list List to update.
* @param width New render list width.
* @param height New render list height.
*/
void renderListResize(renderlist_t *list, int32_t width, int32_t height);
/**
* Dispose a render list entirely.
*
* @param list Render list to dispose.
*/
void renderListDispose(renderlist_t *list);

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
);

View File

@ -83,21 +83,12 @@ void shaderInit(shader_t *shader,
shaderProgram, (GLuint)i, SHADER_UNIFORM_NAME_MAX,
&length, &size, &type, shader->uniforms[i]
);
// TODO: Reset uniforms to zero.
}
// 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);
// Reset position
shaderUsePosition(shader, 0, 0, 0, 0, 0, 0);
shaderUseColor(shader, PIXEL_COLOR_WHITE);
}
shaderuniform_t shaderGetUniform(shader_t *shader, char *name) {
@ -118,23 +109,33 @@ void shaderUse(shader_t *shader) {
glUseProgram(shader->shaderProgram);
}
void shaderUseCamera(shader_t *shader, camera_t *camera) {
shaderUseMatrix(shader, shader->uniView, &camera->view);
shaderUseMatrix(shader, shader->uniProj, &camera->projection);
}
void shaderUseTexture(shader_t *shader, texture_t *texture) {
// OpenGL requires us to set the active texure.
glActiveTexture(GL_TEXTURE0);
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(shader->uniText, 0);
glUniform1i(uniform, i);
}
void shaderUseMatrix(shader_t *shader,shaderuniform_t uniform,matrix_t *matrix){
void shaderUseMatrix(
shader_t *shader, shaderuniform_t uniform, matrix_t *matrix
) {
glUniformMatrix4fv(uniform, 1, GL_FALSE, matrix->internalMatrix[0]);
}
void shaderUsePosition(shader_t *shader,
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
) {
@ -147,10 +148,11 @@ void shaderUsePosition(shader_t *shader,
matrixRotate(&matrix, yaw, 0, 1, 0);
matrixRotate(&matrix, roll, 0, 0, 1);
matrixRotate(&matrix, pitch, 1, 0, 0);
shaderUseMatrix(shader, shader->uniModl, &matrix);
shaderUseMatrix(shader, uniform, &matrix);
}
void shaderUsePositionAndScale(shader_t *shader,
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
@ -167,14 +169,14 @@ void shaderUsePositionAndScale(shader_t *shader,
matrixScale(&matrix, scaleX, scaleY, scaleZ);
shaderUseMatrix(shader, shader->uniModl, &matrix);
shaderUseMatrix(shader, uniform, &matrix);
}
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
);
void shaderUseCamera(
shader_t *shader,
shaderuniform_t uniformView, shaderuniform_t uniformProjection,
camera_t *camera
) {
shaderUseMatrix(shader, uniformView, &camera->view);
shaderUseMatrix(shader, uniformProjection, &camera->projection);
}

View File

@ -6,17 +6,11 @@
*/
#pragma once
#include "../libs.h"
#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"
#define SHADER_UNI_TEXT "u_Text"
#define SHADER_UNI_MODL "u_Modl"
#define SHADER_UNI_COLR "u_Colr"
#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
@ -38,21 +32,6 @@ typedef struct {
/** Pointer to an uploaded shader program linked */
shaderuniform_t shaderProgram;
/** Matrix for the view matrix */
shaderuniform_t uniView;
/** Matrix for the projection matrix */
shaderuniform_t uniProj;
/** Uniform for the current texture */
shaderuniform_t uniText;
/** Uniform for the current model world position */
shaderuniform_t uniModl;
/** 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;
@ -90,19 +69,17 @@ 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, texture_t *texture);
void shaderUseTexture(
shader_t *shader, shaderuniform_t uniform, texture_t *texture
);
/**
* Set's a specific shader uniform to a matrix.
@ -111,12 +88,25 @@ void shaderUseTexture(shader_t *shader, texture_t *texture);
* @param uniform Uniform on the shader to set.
* @param matrix Matrix to apply.
*/
void shaderUseMatrix(shader_t *shader,shaderuniform_t uniform,matrix_t *matrix);
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).
@ -124,17 +114,18 @@ void shaderUseMatrix(shader_t *shader,shaderuniform_t uniform,matrix_t *matrix);
* @param yaw Yaw of the object (local space).
* @param roll Roll of the object (local space).
*/
void shaderUsePosition(shader_t *shader,
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).
@ -145,11 +136,23 @@ void shaderUsePosition(shader_t *shader,
* @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,
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
);
void shaderUseColor(shader_t *shader, pixel_t color);
/**
* 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
);

View File

@ -6,8 +6,8 @@
#pragma once
#include "../libs.h"
#include "../util/math.h"
#include "primitive.h"
#include "primitives/quad.h"
#include "primitive/primitive.h"
#include "primitive/quad.h"
/** Definition of a Sprite Batch. */
typedef struct {