Simplify shaders, enable coloring of primitives
This commit is contained in:
parent
981d87a2d7
commit
339ae38b38
@ -126,6 +126,7 @@
|
||||
<ClCompile Include="src\Common.c" />
|
||||
<ClCompile Include="src\Debug.c" />
|
||||
<ClCompile Include="src\dengine.c" />
|
||||
<ClCompile Include="src\Entity.c" />
|
||||
<ClCompile Include="src\MemoryArena.c" />
|
||||
<ClCompile Include="src\Platform.c" />
|
||||
<ClCompile Include="src\Renderer.c" />
|
||||
|
@ -54,6 +54,9 @@
|
||||
<ClCompile Include="src\Asteroid.c">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\Entity.c">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="data\shaders\default.vert.glsl" />
|
||||
|
@ -1,12 +0,0 @@
|
||||
#version 330 core
|
||||
in vec3 OurColor;
|
||||
in vec2 TexCoord;
|
||||
|
||||
out vec4 color;
|
||||
|
||||
uniform sampler2D ourTexture1;
|
||||
|
||||
void main()
|
||||
{
|
||||
color = texture(ourTexture1, TexCoord);
|
||||
}
|
@ -1,19 +1,9 @@
|
||||
#version 330 core
|
||||
layout(location = 0) in vec4 data; // (vec2)pos, (vec2)texCoord
|
||||
|
||||
layout (location = 0) in vec3 position;
|
||||
layout (location = 1) in vec3 color;
|
||||
layout (location = 2) in vec2 texCoord;
|
||||
|
||||
out vec3 OurColor;
|
||||
out vec2 TexCoord;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * view * model * vec4(position, 1.0f);
|
||||
OurColor = color;
|
||||
TexCoord = texCoord;
|
||||
gl_Position = projection * vec4(data.xy, 0.0f, 1.0f);
|
||||
}
|
||||
|
8
data/shaders/default_no_tex.frag.glsl
Normal file
8
data/shaders/default_no_tex.frag.glsl
Normal file
@ -0,0 +1,8 @@
|
||||
#version 330 core
|
||||
out vec4 color;
|
||||
uniform vec4 spriteColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
color = spriteColor;
|
||||
}
|
9
data/shaders/default_no_tex.vert.glsl
Normal file
9
data/shaders/default_no_tex.vert.glsl
Normal file
@ -0,0 +1,9 @@
|
||||
#version 330 core
|
||||
layout(location = 0) in vec4 data; // (vec2)pos, (vec2)texCoord
|
||||
|
||||
uniform mat4 projection;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * vec4(data.xy, 0.0f, 1.0f);
|
||||
}
|
@ -918,40 +918,42 @@ INTERNAL GLuint createShaderFromPath(MemoryArena_ *arena, const char *const path
|
||||
return result;
|
||||
}
|
||||
|
||||
INTERNAL i32 shaderLoadProgram(Shader *const shader, const GLuint vertexShader,
|
||||
INTERNAL u32 shaderLoadProgram(const GLuint vertexShader,
|
||||
const GLuint fragmentShader)
|
||||
{
|
||||
shader->id = glCreateProgram();
|
||||
glAttachShader(shader->id, vertexShader);
|
||||
glAttachShader(shader->id, fragmentShader);
|
||||
glLinkProgram(shader->id);
|
||||
u32 result = glCreateProgram();
|
||||
glAttachShader(result, vertexShader);
|
||||
glAttachShader(result, fragmentShader);
|
||||
glLinkProgram(result);
|
||||
GL_CHECK_ERROR();
|
||||
|
||||
glDeleteShader(fragmentShader);
|
||||
glDeleteShader(vertexShader);
|
||||
GL_CHECK_ERROR();
|
||||
|
||||
GLint success;
|
||||
GLchar infoLog[512];
|
||||
glGetProgramiv(shader->id, GL_LINK_STATUS, &success);
|
||||
if (!success)
|
||||
glGetProgramiv(result, GL_LINK_STATUS, &success);
|
||||
GL_CHECK_ERROR();
|
||||
if (result == 0)
|
||||
{
|
||||
glGetProgramInfoLog(shader->id, 512, NULL, infoLog);
|
||||
glGetProgramInfoLog(result, 512, NULL, infoLog);
|
||||
printf("glLinkProgram failed: %s\n", infoLog);
|
||||
return -1;
|
||||
ASSERT(TRUE);
|
||||
}
|
||||
GL_CHECK_ERROR();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Shader *const asset_getShader(AssetManager *assetManager,
|
||||
const enum ShaderList type)
|
||||
u32 asset_getShader(AssetManager *assetManager, const enum ShaderList type)
|
||||
{
|
||||
if (type < shaderlist_count)
|
||||
return &assetManager->shaders[type];
|
||||
if (type < shaderlist_count) return assetManager->shaders[type];
|
||||
|
||||
#ifdef DENGINE_DEBUG
|
||||
ASSERT(INVALID_CODE_PATH);
|
||||
#endif
|
||||
return NULL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
const i32 asset_loadShaderFiles(AssetManager *assetManager, MemoryArena_ *arena,
|
||||
@ -963,12 +965,10 @@ const i32 asset_loadShaderFiles(AssetManager *assetManager, MemoryArena_ *arena,
|
||||
GLuint fragmentShader =
|
||||
createShaderFromPath(arena, fragmentPath, GL_FRAGMENT_SHADER);
|
||||
|
||||
Shader shader;
|
||||
i32 result = shaderLoadProgram(&shader, vertexShader, fragmentShader);
|
||||
if (result)
|
||||
return result;
|
||||
u32 shaderId = shaderLoadProgram(vertexShader, fragmentShader);
|
||||
if (shaderId == 0) return -1;
|
||||
|
||||
assetManager->shaders[type] = shader;
|
||||
assetManager->shaders[type] = shaderId;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -32,9 +32,13 @@ void initAssetManager(GameState *state)
|
||||
*tex = texture_gen(1, 1, 4, CAST(u8 *)(&bitmap));
|
||||
|
||||
/* Load shaders */
|
||||
asset_loadShaderFiles(assetManager, arena, "data/shaders/sprite.vert.glsl",
|
||||
"data/shaders/sprite.frag.glsl",
|
||||
shaderlist_sprite);
|
||||
asset_loadShaderFiles(
|
||||
assetManager, arena, "data/shaders/default_tex.vert.glsl",
|
||||
"data/shaders/default_tex.frag.glsl", shaderlist_default);
|
||||
|
||||
asset_loadShaderFiles(
|
||||
assetManager, arena, "data/shaders/default_no_tex.vert.glsl",
|
||||
"data/shaders/default_no_tex.frag.glsl", shaderlist_default_no_tex);
|
||||
|
||||
i32 result =
|
||||
asset_loadTTFont(assetManager, arena, "C:/Windows/Fonts/Arialbd.ttf");
|
||||
@ -52,12 +56,19 @@ void initRenderer(GameState *state, v2 windowSize) {
|
||||
// NOTE(doyle): Value to map a screen coordinate to NDC coordinate
|
||||
renderer->vertexNdcFactor =
|
||||
V2(1.0f / renderer->size.w, 1.0f / renderer->size.h);
|
||||
renderer->shader = asset_getShader(assetManager, shaderlist_sprite);
|
||||
shader_use(renderer->shader);
|
||||
|
||||
const mat4 projection =
|
||||
mat4_ortho(0.0f, renderer->size.w, 0.0f, renderer->size.h, 0.0f, 1.0f);
|
||||
shader_uniformSetMat4fv(renderer->shader, "projection", projection);
|
||||
for (i32 i = 0; i < shaderlist_count; i++)
|
||||
{
|
||||
renderer->shaderList[i] = asset_getShader(assetManager, i);
|
||||
shader_use(renderer->shaderList[i]);
|
||||
shader_uniformSetMat4fv(renderer->shaderList[i], "projection",
|
||||
projection);
|
||||
GL_CHECK_ERROR();
|
||||
}
|
||||
|
||||
renderer->activeShaderId = renderer->shaderList[shaderlist_default];
|
||||
GL_CHECK_ERROR();
|
||||
|
||||
/* Create buffers */
|
||||
|
@ -285,12 +285,23 @@ createDefaultTexQuad(Renderer *renderer, RenderTex *renderTex)
|
||||
INTERNAL void renderGLBufferedData(Renderer *renderer, RenderGroup *group)
|
||||
{
|
||||
ASSERT(group->mode < rendermode_invalid);
|
||||
/* Load transformation matrix */
|
||||
shader_use(renderer->shader);
|
||||
GL_CHECK_ERROR();
|
||||
|
||||
u32 drawMethod = GL_TRIANGLE_STRIP;
|
||||
if (group->flags & renderflag_wireframe)
|
||||
{
|
||||
drawMethod = GL_LINE_LOOP;
|
||||
renderer->activeShaderId =
|
||||
renderer->shaderList[shaderlist_default_no_tex];
|
||||
}
|
||||
else
|
||||
{
|
||||
renderer->activeShaderId = renderer->shaderList[shaderlist_default];
|
||||
}
|
||||
|
||||
shader_use(renderer->activeShaderId);
|
||||
|
||||
/* Set color modulation value */
|
||||
shader_uniformSetVec4f(renderer->shader, "spriteColor",
|
||||
shader_uniformSetVec4f(renderer->activeShaderId, "spriteColor",
|
||||
group->color);
|
||||
GL_CHECK_ERROR();
|
||||
|
||||
@ -299,17 +310,10 @@ INTERNAL void renderGLBufferedData(Renderer *renderer, RenderGroup *group)
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, tex->id);
|
||||
shader_uniformSet1i(renderer->shader, "tex", 0);
|
||||
shader_uniformSet1i(renderer->activeShaderId, "tex", 0);
|
||||
}
|
||||
|
||||
glBindVertexArray(renderer->vao[group->mode]);
|
||||
|
||||
u32 drawMethod = GL_TRIANGLE_STRIP;
|
||||
if (group->flags & renderflag_wireframe)
|
||||
{
|
||||
drawMethod = GL_LINE_LOOP;
|
||||
}
|
||||
|
||||
glDrawArrays(drawMethod, 0, renderer->numVertexesInVbo);
|
||||
GL_CHECK_ERROR();
|
||||
debug_countIncrement(debugcount_drawArrays);
|
||||
|
16
src/Shader.c
16
src/Shader.c
@ -1,25 +1,27 @@
|
||||
#include "Dengine/Shader.h"
|
||||
|
||||
void shader_uniformSet1i(Shader *const shader, const GLchar *name,
|
||||
void shader_uniformSet1i(u32 shaderId, const GLchar *name,
|
||||
const GLuint data)
|
||||
{
|
||||
GLint uniformLoc = glGetUniformLocation(shader->id, name);
|
||||
GLint uniformLoc = glGetUniformLocation(shaderId, name);
|
||||
glUniform1i(uniformLoc, data);
|
||||
}
|
||||
|
||||
void shader_uniformSetMat4fv(Shader *const shader, const GLchar *name,
|
||||
void shader_uniformSetMat4fv(u32 shaderId, const GLchar *name,
|
||||
mat4 data)
|
||||
{
|
||||
GLint uniformLoc = glGetUniformLocation(shader->id, name);
|
||||
GLint uniformLoc = glGetUniformLocation(shaderId, name);
|
||||
GL_CHECK_ERROR();
|
||||
glUniformMatrix4fv(uniformLoc, 1, GL_FALSE, data.e[0]);
|
||||
GL_CHECK_ERROR();
|
||||
}
|
||||
|
||||
void shader_uniformSetVec4f(Shader *const shader, const GLchar *name,
|
||||
void shader_uniformSetVec4f(u32 shaderId, const GLchar *name,
|
||||
v4 data)
|
||||
{
|
||||
GLint uniformLoc = glGetUniformLocation(shader->id, name);
|
||||
GLint uniformLoc = glGetUniformLocation(shaderId, name);
|
||||
glUniform4f(uniformLoc, data.e[0], data.e[1], data.e[2], data.e[3]);
|
||||
}
|
||||
|
||||
|
||||
void shader_use(const Shader *const shader) { glUseProgram(shader->id); }
|
||||
void shader_use(u32 shaderId) { glUseProgram(shaderId); }
|
||||
|
@ -18,7 +18,7 @@ typedef struct AssetManager
|
||||
HashTable audio;
|
||||
|
||||
/* Primitive Array */
|
||||
Shader shaders[2];
|
||||
u32 shaders[shaderlist_count];
|
||||
Font font;
|
||||
} AssetManager;
|
||||
|
||||
@ -72,7 +72,7 @@ const i32 asset_loadXmlFile(AssetManager *const assetManager,
|
||||
MemoryArena_ *const arena,
|
||||
const PlatformFileRead *const fileRead);
|
||||
|
||||
Shader *const asset_getShader(AssetManager *assetManager, const enum ShaderList type);
|
||||
u32 asset_getShader(AssetManager *assetManager, const enum ShaderList type);
|
||||
const i32 asset_loadShaderFiles(AssetManager *assetManager, MemoryArena_ *arena,
|
||||
const char *const vertexPath,
|
||||
const char *const fragmentPath,
|
||||
|
@ -44,7 +44,8 @@ typedef struct XmlNode
|
||||
|
||||
enum ShaderList
|
||||
{
|
||||
shaderlist_sprite,
|
||||
shaderlist_default,
|
||||
shaderlist_default_no_tex,
|
||||
shaderlist_count,
|
||||
};
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
|
||||
#include "Dengine/Common.h"
|
||||
#include "Dengine/Math.h"
|
||||
#include "Dengine/Assets.h"
|
||||
|
||||
/* Forward Declaration */
|
||||
typedef struct AssetManager AssetManager;
|
||||
@ -57,7 +58,8 @@ typedef struct RenderGroup
|
||||
|
||||
typedef struct Renderer
|
||||
{
|
||||
Shader *shader;
|
||||
u32 shaderList[shaderlist_count];
|
||||
u32 activeShaderId;
|
||||
|
||||
u32 vao[rendermode_count];
|
||||
u32 vbo[rendermode_count];
|
||||
|
@ -4,18 +4,13 @@
|
||||
#include "Dengine/Math.h"
|
||||
#include "Dengine/OpenGL.h"
|
||||
|
||||
typedef struct Shader
|
||||
{
|
||||
GLuint id;
|
||||
} Shader;
|
||||
|
||||
void shader_uniformSet1i(Shader *const shader, const GLchar *name,
|
||||
void shader_uniformSet1i(u32 shaderId, const GLchar *name,
|
||||
const GLuint data);
|
||||
void shader_uniformSetMat4fv(Shader *const shader, const GLchar *name,
|
||||
void shader_uniformSetMat4fv(u32 shaderId, const GLchar *name,
|
||||
mat4 data);
|
||||
void shader_uniformSetVec4f(Shader *const shader, const GLchar *name,
|
||||
void shader_uniformSetVec4f(u32 shaderId, const GLchar *name,
|
||||
v4 data);
|
||||
|
||||
void shader_use(const Shader *const shader);
|
||||
void shader_use(u32 shaderId);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user