About to start shaders
This commit is contained in:
14
src/duskgl/display/shader/entityshader/CMakeLists.txt
Normal file
14
src/duskgl/display/shader/entityshader/CMakeLists.txt
Normal file
@ -0,0 +1,14 @@
|
||||
# Copyright (c) 2025 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Shaders
|
||||
glsltool(entity_vert.glsl)
|
||||
glsltool(entity_frag.glsl)
|
||||
|
||||
# Sources
|
||||
target_sources(${DUSK_TARGET_NAME}
|
||||
PRIVATE
|
||||
entityshader.c
|
||||
)
|
27
src/duskgl/display/shader/entityshader/entity.glsl
Normal file
27
src/duskgl/display/shader/entityshader/entity.glsl
Normal file
@ -0,0 +1,27 @@
|
||||
#include "../fragments/header.glsl"
|
||||
#include "../fragments/transforms.glsl"
|
||||
|
||||
#define ENTITY_TILE_OFFSET_X (MAP_TILE_WIDTH - ENTITY_WIDTH) / 2
|
||||
#define ENTITY_TILE_OFFSET_Y (MAP_TILE_HEIGHT - ENTITY_HEIGHT) / 2
|
||||
|
||||
struct Entity {
|
||||
int x;
|
||||
int y;
|
||||
int tileset;
|
||||
int tile;
|
||||
};
|
||||
|
||||
layout(std140) uniform b_Data {
|
||||
Entity entities[MAP_ENTITY_COUNT_MAX];
|
||||
int entityCount;
|
||||
};
|
||||
|
||||
vec4 entityGetCoordinates(Entity ent) {
|
||||
int vx = (ent.x * MAP_TILE_WIDTH) + ENTITY_TILE_OFFSET_X;
|
||||
int vy = (ent.y * MAP_TILE_HEIGHT) + ENTITY_TILE_OFFSET_Y;
|
||||
return vec4(vx, vy, vx + ENTITY_WIDTH, vy + ENTITY_HEIGHT);
|
||||
}
|
||||
|
||||
vec4 entityGetUVs(Entity ent) {
|
||||
return tilesetGetUVsByIndex(ent.tileset, ent.tile);
|
||||
}
|
18
src/duskgl/display/shader/entityshader/entity_frag.glsl
Normal file
18
src/duskgl/display/shader/entityshader/entity_frag.glsl
Normal file
@ -0,0 +1,18 @@
|
||||
#include "entity.glsl"
|
||||
|
||||
in vec2 v_TextureCoord;
|
||||
flat in int v_Entity;
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
void main() {
|
||||
Entity e = entities[v_Entity];
|
||||
vec4 textureColor = tilesetGetColor(e.tileset, v_TextureCoord);
|
||||
|
||||
// We remove the pink color as transparent
|
||||
if(textureColor.r == 1.0 && textureColor.g == 0.0 && textureColor.b == 1.0) {
|
||||
discard;
|
||||
}
|
||||
|
||||
FragColor = textureColor;
|
||||
}
|
35
src/duskgl/display/shader/entityshader/entity_vert.glsl
Normal file
35
src/duskgl/display/shader/entityshader/entity_vert.glsl
Normal file
@ -0,0 +1,35 @@
|
||||
#include "entity.glsl"
|
||||
|
||||
// Outputs to fragment shader
|
||||
out vec2 v_TextureCoord;
|
||||
flat out int v_Entity;
|
||||
|
||||
void main() {
|
||||
int entityIndex = gl_InstanceID;
|
||||
int indiceIndex = gl_VertexID % 6;
|
||||
|
||||
Entity entity = entities[entityIndex];
|
||||
|
||||
vec4 quadPos = entityGetCoordinates(entity);
|
||||
vec4 quadUVs = entityGetUVs(entity);
|
||||
|
||||
vec2 vertex;
|
||||
vec2 uv;
|
||||
if(indiceIndex == 0 || indiceIndex == 4) {
|
||||
vertex = quadPos.xy;
|
||||
uv = quadUVs.xy;
|
||||
} else if(indiceIndex == 1) {
|
||||
vertex = quadPos.zy;
|
||||
uv = quadUVs.zy;
|
||||
} else if(indiceIndex == 2 || indiceIndex == 5) {
|
||||
vertex = quadPos.zw;
|
||||
uv = quadUVs.zw;
|
||||
} else if(indiceIndex == 3) {
|
||||
vertex = quadPos.xw;
|
||||
uv = quadUVs.xw;
|
||||
}
|
||||
|
||||
gl_Position = transforms.projection * transforms.view * vec4(vertex, 0.0, 1.0);
|
||||
v_TextureCoord = uv;
|
||||
v_Entity = entityIndex;
|
||||
}
|
32
src/duskgl/display/shader/entityshader/entityshader.c
Normal file
32
src/duskgl/display/shader/entityshader/entityshader.c
Normal file
@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#include "assert/assert.h"
|
||||
#include "assert/assertgl.h"
|
||||
#include "entityshader.h"
|
||||
#include "entity_vert.glsl.h"
|
||||
#include "entity_frag.glsl.h"
|
||||
|
||||
entityshader_t ENTITY_SHADER;
|
||||
|
||||
void entityShaderInit() {
|
||||
memset(&ENTITY_SHADER, 0, sizeof(entityshader_t));
|
||||
|
||||
shaderInit(
|
||||
&ENTITY_SHADER.shader,
|
||||
entity_vertShaderSource,
|
||||
entity_fragShaderSource
|
||||
);
|
||||
|
||||
// Uniform buffers
|
||||
|
||||
// Uniforms
|
||||
}
|
||||
|
||||
void entityShaderDispose() {
|
||||
shaderDispose(&ENTITY_SHADER.shader);
|
||||
}
|
31
src/duskgl/display/shader/entityshader/entityshader.h
Normal file
31
src/duskgl/display/shader/entityshader/entityshader.h
Normal file
@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "display/shader/shader.h"
|
||||
|
||||
typedef struct {
|
||||
shader_t shader;
|
||||
|
||||
// GLuint dataBlock;
|
||||
// GLuint transformsBlock;
|
||||
// GLuint tilesetsBlock;
|
||||
|
||||
// GLuint uniformTilesetTextures;
|
||||
} entityshader_t;
|
||||
|
||||
extern entityshader_t ENTITY_SHADER;
|
||||
|
||||
/**
|
||||
* Initializes the entity shader.
|
||||
*/
|
||||
void entityShaderInit();
|
||||
|
||||
/**
|
||||
* Disposes of the entity shader.
|
||||
*/
|
||||
void entityShaderDispose();
|
Reference in New Issue
Block a user