Set up a multipass shader

This commit is contained in:
2021-11-12 08:40:49 -08:00
parent 6d287e5ff8
commit 9a695813f9
5 changed files with 74 additions and 30 deletions

View File

@ -49,6 +49,10 @@ elseif(TARGET_TYPE STREQUAL game)
${ASSETS_SOURCE_DIR}/shared/shaders/textured.vert shaders/textured.vert
${ASSETS_SOURCE_DIR}/shared/shaders/textured.frag shaders/textured.frag
)
tool_copy(shader_test
${ASSETS_SOURCE_DIR}/shared/shaders/test.vert shaders/test.vert
${ASSETS_SOURCE_DIR}/shared/shaders/test.frag shaders/test.frag
)
# Fonts
tool_copy(font_opensans
@ -143,6 +147,7 @@ elseif(TARGET_TYPE STREQUAL game)
vn_sammy
shader_textured
shader_test
font_opensans
texture_test
)

View File

@ -10,11 +10,10 @@
void shaderInit(shader_t *shader,
char *vertexShaderSource, char* fragmentShaderSource
) {
int32_t isSuccess, maxLength, i;
int32_t isSuccess, maxLength, i, texture;
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 };
@ -77,14 +76,19 @@ void shaderInit(shader_t *shader,
// Extract uniforms
glGetProgramiv(shaderProgram, GL_ACTIVE_UNIFORMS, &shader->uniformCount);
texture = 0;
for(i = 0; i < shader->uniformCount; i++) {
shader->uniforms[i] = shader->uniformBuffer + (i * SHADER_UNIFORM_NAME_MAX);
shader->uniformNames[i] = (
shader->uniformBuffer + (i * SHADER_UNIFORM_NAME_MAX)
);
glGetActiveUniform(
shaderProgram, (GLuint)i, SHADER_UNIFORM_NAME_MAX,
&length, &size, &type, shader->uniforms[i]
&length, &size, shader->types + i, shader->uniformNames[i]
);
// TODO: Reset uniforms to zero.
if(shader->types[i] == GL_SAMPLER_2D) shader->textureSlots[i] = texture++;
}
// Bind the shader
@ -94,7 +98,7 @@ void shaderInit(shader_t *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;
if(strcmp(shader->uniformNames[i], name) == 0) return i;
}
return (shaderuniform_t)-1;
}
@ -112,8 +116,8 @@ void shaderUse(shader_t *shader) {
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
int32_t i = 0;
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, texture->id);
glUniform1i(uniform, i);

View File

@ -13,7 +13,7 @@
#include "../../util/array.h"
#define SHADER_UNIFORM_NAME_MAX 24
#define SHADER_UNIFORM_COUNT 8
#define SHADER_UNIFORM_COUNT 16
/** Representation of a shader uniform */
typedef GLuint shaderuniform_t;
@ -27,14 +27,23 @@ typedef struct {
shaderuniform_t shaderVertex;
/** Pointer to an uploaded fragment shader program */
shaderuniform_t shaderFrag;
GLuint shaderFrag;
/** Pointer to an uploaded shader program linked */
shaderuniform_t shaderProgram;
GLuint shaderProgram;
/** Buffer of chars where we store the uniform names */
char uniformBuffer[SHADER_UNIFORM_NAME_MAX * SHADER_UNIFORM_COUNT];
char *uniforms[SHADER_UNIFORM_COUNT];
/** Array of strings (pointers to the above buffer) of the uniform names */
char *uniformNames[SHADER_UNIFORM_COUNT];
int32_t uniformCount;
/** Type of each uniform */
GLenum types[SHADER_UNIFORM_COUNT];
/** Texture Slots (which texture slot for GL to use for each uniform) */
uint8_t textureSlots[SHADER_UNIFORM_COUNT];
} shader_t;
/**

View File

@ -17,6 +17,9 @@ bool sandboxGameInit(sandboxgame_t *game) {
assetManagerLoadShader(&game->engine.assetManager, game->scene.assetOwner,
&game->shader, "shaders/textured.vert", "shaders/textured.frag"
);
assetManagerLoadShader(&game->engine.assetManager, game->scene.assetOwner,
&game->shader2, "shaders/test.vert", "shaders/test.frag"
);
// Load the texture
assetManagerLoadScaledTexture(
@ -28,8 +31,16 @@ bool sandboxGameInit(sandboxgame_t *game) {
&game->st, &game->texture, 0
);
quadInit(&game->quad, 0, 0,0,0,0, 500,500,1,1);
assetManagerLoadTexture(
&game->engine.assetManager, game->scene.assetOwner,
&game->texture, "textures/test_texture.png"
);
cubeInit(&game->quad, 1, 1, 1);
i = renderPassAdd(&game->scene.renderList, &game->shader);
i = renderPassAdd(&game->scene.renderList, &game->shader);
return true;
}
@ -40,34 +51,48 @@ void sandboxGameUpdate(sandboxgame_t *game) {
n = assetManagerProgressGet(&game->engine.assetManager);
if(n < 1.0f) return;
// First pass
sceneRenderStart(&game->scene);
renderListRenderPass(&game->scene.renderList, &game->engine, 0);
cameraOrtho(&camera,
0, game->engine.render.width,
game->engine.render.height, 0,
0.01f, 1000.0f
);
cameraLookAt(&camera, 0,0,10, 0,0,0);
// First pass
shaderuniform_t uniView = shaderGetUniform(&game->shader, "u_View");
shaderuniform_t uniProj = shaderGetUniform(&game->shader, "u_Proj");
shaderuniform_t uniModel = shaderGetUniform(&game->shader, "u_Modl");
shaderuniform_t uniColor = shaderGetUniform(&game->shader, "u_Colr");
shaderuniform_t uniTexture = shaderGetUniform(&game->shader, "u_Text");
renderpass_t *pass =renderListRenderPass(
&game->scene.renderList, &game->engine, 0
);
cameraPerspective(&camera, 45.0f,
game->engine.render.width/game->engine.render.height, 0.001f, 1000.0f
);
cameraLookAt(&camera, 3,3,3, 0,0,0);
shaderUse(&game->shader);
shaderUseCamera(&game->shader, uniView, uniProj, &camera);
shaderUseColor(&game->shader, uniColor, PIXEL_COLOR_WHITE);
shaderUseTexture(&game->shader, uniTexture, &game->texture);
shaderUsePosition(&game->shader, uniModel, 0,0,0, 0,0,0);
shaderUseCamera(pass->shader, uniView, uniProj, &camera);
shaderUseColor(pass->shader, uniColor, PIXEL_COLOR_WHITE);
shaderUseTexture(pass->shader, uniTexture, &game->texture);
shaderUsePosition(pass->shader, uniModel, 0,0,0,
game->engine.time.current/2.0f, game->engine.time.current, 0
);
primitiveDraw(&game->quad, 0, -1);
sceneRenderEnd(&game->scene, &game->shader,
uniView, uniProj, uniModel, &uniTexture,
&game->shader,
uniView, uniProj, uniModel, uniTexture
pass = renderListRenderPass(&game->scene.renderList, &game->engine, 1);
shaderUsePosition(pass->shader, uniModel, 0,0,0,
-game->engine.time.current/2.0f, -game->engine.time.current, 0
);
primitiveDraw(&game->quad, 0, -1);
shaderuniform_t uniTextureSet[2];
uniTextureSet[0] = shaderGetUniform(&game->shader2, "u_Text0");
uniTextureSet[1] = shaderGetUniform(&game->shader2, "u_Text1");
sceneRenderEnd(&game->scene, &game->shader2,
shaderGetUniform(&game->shader2, "u_View"),
shaderGetUniform(&game->shader2, "u_Proj"),
shaderGetUniform(&game->shader2, "u_Modl"),
uniTextureSet,
&game->shader, uniView, uniProj, uniModel, uniTexture
);
}

View File

@ -12,6 +12,7 @@
#include "../../display/shader/shader.h"
#include "../../display/primitive/primitive.h"
#include "../../display/primitive/quad.h"
#include "../../display/primitive/cube.h"
#include "../../display/scaledtexture.h"
#include "../../file/asset.h"
#include "../../ui/label.h"
@ -20,13 +21,13 @@
#include "../../file/xml.h"
#include "../../engine/engine.h"
#include "../../display/renderlist.h"
#include "../scene/scene.h"
typedef struct {
engine_t engine;
scene_t scene;
shader_t shader;
shader_t shader2;
texture_t texture;
primitive_t quad;
scaledtexture_t st;