Setup example shader.
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
#include "matrix.h"
|
||||
|
||||
void matrixIdentity(matrix_t *matrix) {
|
||||
ASSERT_NOT_NULL(matrix);
|
||||
glm_mat4_identity(matrix->internalMatrix);
|
||||
}
|
||||
|
||||
@@ -16,6 +17,7 @@ void matrixLookAt(matrix_t *matrix,
|
||||
float tx,float ty, float tz,
|
||||
float ux, float uy, float uz
|
||||
) {
|
||||
ASSERT_NOT_NULL(matrix);
|
||||
glm_lookat(
|
||||
(vec3){ x, y, z },
|
||||
(vec3){ tx, ty, tz },
|
||||
@@ -29,6 +31,7 @@ void matrixLook(matrix_t *matrix,
|
||||
float pitch, float yaw, float roll,
|
||||
float ux, float uy, float uz
|
||||
) {
|
||||
ASSERT_NOT_NULL(matrix);
|
||||
glm_look(
|
||||
(vec3){ x, y, z },
|
||||
(vec3){ pitch, yaw, roll },
|
||||
@@ -40,23 +43,48 @@ void matrixLook(matrix_t *matrix,
|
||||
void matrixPerspective(matrix_t *matrix,
|
||||
float fov, float aspect, float camNear, float camFar
|
||||
) {
|
||||
ASSERT_NOT_NULL(matrix);
|
||||
glm_perspective(fov, aspect, camNear, camFar, matrix->internalMatrix);
|
||||
}
|
||||
|
||||
void matrixOrtho(matrix_t *matrix,
|
||||
float left, float right, float bottom, float top, float camNear, float camFar
|
||||
) {
|
||||
ASSERT_NOT_NULL(matrix);
|
||||
glm_ortho(left, right, bottom, top, camNear, camFar, matrix->internalMatrix);
|
||||
}
|
||||
|
||||
void matrixTranslate(matrix_t *matrix, float x, float y, float z) {
|
||||
ASSERT_NOT_NULL(matrix);
|
||||
glm_translate(matrix->internalMatrix, (vec3){ x, y, z });
|
||||
}
|
||||
|
||||
void matrixRotate(matrix_t *matrix, float angle, float x, float y, float z) {
|
||||
ASSERT_NOT_NULL(matrix);
|
||||
glm_rotate(matrix->internalMatrix, angle, (vec3){ x, y, z });
|
||||
}
|
||||
|
||||
void matrixScale(matrix_t *matrix, float x, float y, float z) {
|
||||
ASSERT_NOT_NULL(matrix);
|
||||
glm_scale(matrix->internalMatrix, (vec3){ x, y, z });
|
||||
}
|
||||
|
||||
matrix_t matrixPositionAndScale(
|
||||
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);
|
||||
|
||||
return matrix;
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#pragma once
|
||||
#include "../libs.h"
|
||||
#include "../assert/assert.h"
|
||||
|
||||
/**
|
||||
* Representation for a matrix. Used as a highlevel wrapper for the math
|
||||
@@ -120,4 +121,25 @@ void matrixRotate(matrix_t *matrix, float angle, float x, float y, float z);
|
||||
* @param y Y vector to scale.
|
||||
* @param z Z vector to scale.
|
||||
*/
|
||||
void matrixScale(matrix_t *matrix, float x, float y, float z);
|
||||
void matrixScale(matrix_t *matrix, float x, float y, float z);
|
||||
|
||||
/**
|
||||
* Calculates a fairly standard position and scale matrix. Note that the matrix
|
||||
* is calculated in the order of Translate, Yaw, Pitch, Roll, Scale.
|
||||
*
|
||||
* @param x X translation to apply.
|
||||
* @param y Y translation to apply.
|
||||
* @param z Z translation to apply.
|
||||
* @param pitch Pitch rotation to apply.
|
||||
* @param yaw Yaw rotation to apply.
|
||||
* @param roll Roll rotation to apply.
|
||||
* @param scaleX X scale factor.
|
||||
* @param scaleY Y scale factor.
|
||||
* @param scaleZ Z scale factor.
|
||||
* @return A specially created transformation matrix.
|
||||
*/
|
||||
matrix_t matrixPositionAndScale(
|
||||
float x, float y, float z,
|
||||
float pitch, float yaw, float roll,
|
||||
float scaleX, float scaleY, float scaleZ
|
||||
);
|
||||
@@ -50,6 +50,7 @@ void renderListRender(renderlist_t *list, renderlistbackshader_t *backShader) {
|
||||
camera_t camera;
|
||||
int32_t i;
|
||||
renderpass_t *pass;
|
||||
matrix_t matrix;
|
||||
|
||||
// Setup the camera
|
||||
cameraLookAt(&camera, 0,0,1, 0,0,0);
|
||||
@@ -64,9 +65,9 @@ void renderListRender(renderlist_t *list, renderlistbackshader_t *backShader) {
|
||||
backShader->program, backShader->uniformView,
|
||||
backShader->uniformProjection, &camera
|
||||
);
|
||||
shaderUsePosition(backShader->program, backShader->uniformPosition,
|
||||
0,0,0, 0,0,0
|
||||
);
|
||||
|
||||
matrix = matrixPositionAndScale(0,0,0, 0,0,0, 1,1,1);
|
||||
shaderUseMatrix(backShader->program, backShader->uniformPosition, &matrix);
|
||||
|
||||
// Render each pass.
|
||||
for(i = 0; i < list->passCount; i++) {
|
||||
|
||||
@@ -103,42 +103,6 @@ void shaderUseMatrix(
|
||||
glUniformMatrix4fv(uniform, 1, GL_FALSE, matrix->internalMatrix[0]);
|
||||
}
|
||||
|
||||
void shaderUsePosition(shaderprogram_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(shaderprogram_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 shaderUseColor(
|
||||
shaderprogram_t *shader, shaderuniform_t uniform, pixel_t color
|
||||
) {
|
||||
|
||||
@@ -89,46 +89,6 @@ void shaderUseMatrix(
|
||||
shaderprogram_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 to set the position on to.
|
||||
* @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(shaderprogram_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 to set the position on to.
|
||||
* @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(shaderprogram_t *shader, shaderuniform_t uniform,
|
||||
float x, float y, float z,
|
||||
float pitch, float yaw, float roll,
|
||||
float scaleX, float scaleY, float scaleZ
|
||||
);
|
||||
|
||||
/**
|
||||
* Set a color on to the shader.
|
||||
*
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
# Copyright (c) 2021 Dominic Msters
|
||||
# Copyright (c) 2022 Dominic Msters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
# target_sources(${PROJECT_NAME}
|
||||
# PRIVATE
|
||||
# standardshader.c
|
||||
# shaderui.c
|
||||
# )
|
||||
target_sources(${PROJECT_NAME}
|
||||
PRIVATE
|
||||
standardshader.c
|
||||
)
|
||||
@@ -1,12 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "shaderui.h"
|
||||
|
||||
void shaderUiInit(shaderprogram_t *program) {
|
||||
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "../shaderprogram.h"
|
||||
#include "../texture.h"
|
||||
|
||||
typedef struct {
|
||||
shaderprogram_t program;
|
||||
texture_t texture;
|
||||
|
||||
shaderuniform_t uniProjection;
|
||||
shaderuniform_t uniModel;
|
||||
shaderuniform_t uniTexture;
|
||||
shaderuniform_t uniColor;
|
||||
} uishader_t;
|
||||
|
||||
void shaderUiInit(shaderprogram_t *program);
|
||||
@@ -7,5 +7,107 @@
|
||||
|
||||
#include "standardshader.h"
|
||||
|
||||
void standardShaderInit(standardshader_t *stds, char *vert, char *frag) {
|
||||
void _standardShaderUpdateUniforms(standardshader_t *stds) {
|
||||
shaderprogram_t *sp;
|
||||
ASSERT_NOT_NULL(stds);
|
||||
|
||||
sp = STANDARD_SHADER_SHADER(stds);
|
||||
stds->uniformView = shaderGetUniformByName(
|
||||
sp, STANDARD_SHADER_UNIFORM_NAME_VIEW
|
||||
);
|
||||
stds->uniformProjection = shaderGetUniformByName(
|
||||
sp, STANDARD_SHADER_UNIFORM_NAME_PROJ
|
||||
);
|
||||
stds->uniformTexture = shaderGetUniformByName(
|
||||
sp, STANDARD_SHADER_UNIFORM_NAME_TEXT
|
||||
);
|
||||
stds->uniformColor = shaderGetUniformByName(
|
||||
sp, STANDARD_SHADER_UNIFORM_NAME_COLOR
|
||||
);
|
||||
stds->uniformPosition = shaderGetUniformByName(
|
||||
sp, STANDARD_SHADER_UNIFORM_NAME_MODEL
|
||||
);
|
||||
}
|
||||
|
||||
bool _standardShaderOnAssetItemLoaded(void *u, event_t e, void *s[], int32_t c){
|
||||
standardshader_t *ss = (standardshader_t *)u;
|
||||
|
||||
ASSERT_NOT_NULL(ss);
|
||||
ASSERT_EQUAL(e, ASSET_MANAGER_EVENT_ITEM_LOADED);
|
||||
ASSERT_NOT_NULL(s);
|
||||
ASSERT_EQUAL(c, 2);
|
||||
|
||||
if(s[1] != ss->shaderProgram) return true;
|
||||
|
||||
eventManagerUnsubscribe(&((assetmanager_t *)s[0])->events, ss->onItemLoaded);
|
||||
_standardShaderUpdateUniforms(ss);
|
||||
return true;
|
||||
}
|
||||
|
||||
void standardShaderInit(standardshader_t *stds, assetmanager_t *assetManager) {
|
||||
ASSERT_NOT_NULL(stds);
|
||||
ASSERT_NOT_NULL(assetManager);
|
||||
|
||||
// Begin loading the shader.
|
||||
stds->holder = assetManagerHolderCreate(assetManager);
|
||||
stds->shaderProgram = assetManagerLoadShader(
|
||||
assetManager, stds->holder,
|
||||
STANDARD_SHARER_FILE_VERTEX, STANDARD_SHADER_FILE_FRAGMENT
|
||||
);
|
||||
|
||||
// Update uniforms
|
||||
if(assetManagerItemIsFinished(stds->shaderProgram)) {
|
||||
_standardShaderUpdateUniforms(stds);
|
||||
} else {
|
||||
stds->onItemLoaded = eventManagerSubscribe(
|
||||
&assetManager->events, ASSET_MANAGER_EVENT_ITEM_LOADED,
|
||||
stds, &_standardShaderOnAssetItemLoaded
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void standardShaderUse(standardshader_t *stds) {
|
||||
ASSERT_NOT_NULL(stds);
|
||||
if(!assetManagerItemIsFinished(stds->shaderProgram)) return;
|
||||
shaderUse(STANDARD_SHADER_SHADER(stds));
|
||||
}
|
||||
|
||||
void standardShaderSetCamera(standardshader_t *stds, camera_t *camera) {
|
||||
ASSERT_NOT_NULL(stds);
|
||||
ASSERT_NOT_NULL(camera);
|
||||
if(!assetManagerItemIsFinished(stds->shaderProgram)) return;
|
||||
|
||||
shaderUseCamera(STANDARD_SHADER_SHADER(stds),
|
||||
stds->uniformView, stds->uniformProjection, camera
|
||||
);
|
||||
}
|
||||
|
||||
void standardShaderSetTexture(standardshader_t *stds, texture_t *texture) {
|
||||
ASSERT_NOT_NULL(stds);
|
||||
ASSERT_NOT_NULL(texture);
|
||||
if(!assetManagerItemIsFinished(stds->shaderProgram)) return;
|
||||
|
||||
shaderUseTexture(STANDARD_SHADER_SHADER(stds), stds->uniformTexture, 0);
|
||||
}
|
||||
|
||||
void standardShaderSetColor(standardshader_t *stds, pixel_t color) {
|
||||
ASSERT_NOT_NULL(stds);
|
||||
if(!assetManagerItemIsFinished(stds->shaderProgram)) return;
|
||||
|
||||
shaderUseColor(STANDARD_SHADER_SHADER(stds), stds->uniformColor, color);
|
||||
}
|
||||
|
||||
void standardShaderSetPosition(standardshader_t *stds, matrix_t *matrix) {
|
||||
ASSERT_NOT_NULL(stds);
|
||||
ASSERT_NOT_NULL(matrix);
|
||||
if(!assetManagerItemIsFinished(stds->shaderProgram)) return;
|
||||
|
||||
shaderUseMatrix(STANDARD_SHADER_SHADER(stds), stds->uniformPosition, matrix);
|
||||
}
|
||||
|
||||
void standardShaderDispose(standardshader_t *stds, assetmanager_t *assMan) {
|
||||
ASSERT_NOT_NULL(stds);
|
||||
ASSERT_NOT_NULL(assMan);
|
||||
|
||||
assetManagerHolderRelease(assMan, stds->holder);
|
||||
}
|
||||
@@ -7,7 +7,10 @@
|
||||
|
||||
#pragma once
|
||||
#include "../../libs.h"
|
||||
#include "../../assert/assert.h"
|
||||
#include "../../engine/event.h"
|
||||
#include "../shaderprogram.h"
|
||||
#include "../../file/assetmanager.h"
|
||||
|
||||
#define STANDARD_SHADER_UNIFORM_NAME_VIEW "u_View"
|
||||
#define STANDARD_SHADER_UNIFORM_NAME_PROJ "u_Proj"
|
||||
@@ -15,8 +18,15 @@
|
||||
#define STANDARD_SHADER_UNIFORM_NAME_MODEL "u_Model"
|
||||
#define STANDARD_SHADER_UNIFORM_NAME_COLOR "u_Color"
|
||||
|
||||
#define STANDARD_SHARER_FILE_VERTEX "shaders/textured.vert"
|
||||
#define STANDARD_SHADER_FILE_FRAGMENT "shaders/textured.frag"
|
||||
|
||||
#define STANDARD_SHADER_SHADER(ss) (&ss->shaderProgram->data.shader.shader)
|
||||
|
||||
typedef struct {
|
||||
shaderprogram_t shader;
|
||||
assetmanageritem_t *shaderProgram;
|
||||
assetmanagerholder_t holder;
|
||||
eventlistener_t *onItemLoaded;
|
||||
|
||||
shaderuniform_t uniformView;
|
||||
shaderuniform_t uniformProjection;
|
||||
@@ -25,4 +35,10 @@ typedef struct {
|
||||
shaderuniform_t uniformColor;
|
||||
} standardshader_t;
|
||||
|
||||
void standardShaderInit(standardshader_t *stds, char *vert, char *frag);
|
||||
void standardShaderInit(standardshader_t *stds, assetmanager_t *assetManager);
|
||||
void standardShaderUse(standardshader_t *stds);
|
||||
void standardShaderSetCamera(standardshader_t *stds, camera_t *camera);
|
||||
void standardShaderSetTexture(standardshader_t *stds, texture_t *texture);
|
||||
void standardShaderSetColor(standardshader_t *stds, pixel_t color);
|
||||
void standardShaderSetPosition(standardshader_t *stds, matrix_t *matrix);
|
||||
void standardShaderDispose(standardshader_t *stds, assetmanager_t *assMan);
|
||||
Reference in New Issue
Block a user