Simplify shaders, enable coloring of primitives

This commit is contained in:
Doyle Thai 2016-11-12 01:17:30 +11:00
parent 981d87a2d7
commit 339ae38b38
16 changed files with 96 additions and 82 deletions

View File

@ -126,6 +126,7 @@
<ClCompile Include="src\Common.c" /> <ClCompile Include="src\Common.c" />
<ClCompile Include="src\Debug.c" /> <ClCompile Include="src\Debug.c" />
<ClCompile Include="src\dengine.c" /> <ClCompile Include="src\dengine.c" />
<ClCompile Include="src\Entity.c" />
<ClCompile Include="src\MemoryArena.c" /> <ClCompile Include="src\MemoryArena.c" />
<ClCompile Include="src\Platform.c" /> <ClCompile Include="src\Platform.c" />
<ClCompile Include="src\Renderer.c" /> <ClCompile Include="src\Renderer.c" />

View File

@ -54,6 +54,9 @@
<ClCompile Include="src\Asteroid.c"> <ClCompile Include="src\Asteroid.c">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\Entity.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="data\shaders\default.vert.glsl" /> <None Include="data\shaders\default.vert.glsl" />

View File

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

View File

@ -1,19 +1,9 @@
#version 330 core #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; uniform mat4 projection;
void main() void main()
{ {
gl_Position = projection * view * model * vec4(position, 1.0f); gl_Position = projection * vec4(data.xy, 0.0f, 1.0f);
OurColor = color;
TexCoord = texCoord;
} }

View File

@ -0,0 +1,8 @@
#version 330 core
out vec4 color;
uniform vec4 spriteColor;
void main()
{
color = spriteColor;
}

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

View File

@ -918,40 +918,42 @@ INTERNAL GLuint createShaderFromPath(MemoryArena_ *arena, const char *const path
return result; return result;
} }
INTERNAL i32 shaderLoadProgram(Shader *const shader, const GLuint vertexShader, INTERNAL u32 shaderLoadProgram(const GLuint vertexShader,
const GLuint fragmentShader) const GLuint fragmentShader)
{ {
shader->id = glCreateProgram(); u32 result = glCreateProgram();
glAttachShader(shader->id, vertexShader); glAttachShader(result, vertexShader);
glAttachShader(shader->id, fragmentShader); glAttachShader(result, fragmentShader);
glLinkProgram(shader->id); glLinkProgram(result);
GL_CHECK_ERROR();
glDeleteShader(fragmentShader); glDeleteShader(fragmentShader);
glDeleteShader(vertexShader); glDeleteShader(vertexShader);
GL_CHECK_ERROR();
GLint success; GLint success;
GLchar infoLog[512]; GLchar infoLog[512];
glGetProgramiv(shader->id, GL_LINK_STATUS, &success); glGetProgramiv(result, GL_LINK_STATUS, &success);
if (!success) GL_CHECK_ERROR();
if (result == 0)
{ {
glGetProgramInfoLog(shader->id, 512, NULL, infoLog); glGetProgramInfoLog(result, 512, NULL, infoLog);
printf("glLinkProgram failed: %s\n", infoLog); printf("glLinkProgram failed: %s\n", infoLog);
return -1; ASSERT(TRUE);
}
GL_CHECK_ERROR();
return result;
} }
return 0; u32 asset_getShader(AssetManager *assetManager, const enum ShaderList type)
}
Shader *const asset_getShader(AssetManager *assetManager,
const enum ShaderList type)
{ {
if (type < shaderlist_count) if (type < shaderlist_count) return assetManager->shaders[type];
return &assetManager->shaders[type];
#ifdef DENGINE_DEBUG #ifdef DENGINE_DEBUG
ASSERT(INVALID_CODE_PATH); ASSERT(INVALID_CODE_PATH);
#endif #endif
return NULL; return -1;
} }
const i32 asset_loadShaderFiles(AssetManager *assetManager, MemoryArena_ *arena, const i32 asset_loadShaderFiles(AssetManager *assetManager, MemoryArena_ *arena,
@ -963,12 +965,10 @@ const i32 asset_loadShaderFiles(AssetManager *assetManager, MemoryArena_ *arena,
GLuint fragmentShader = GLuint fragmentShader =
createShaderFromPath(arena, fragmentPath, GL_FRAGMENT_SHADER); createShaderFromPath(arena, fragmentPath, GL_FRAGMENT_SHADER);
Shader shader; u32 shaderId = shaderLoadProgram(vertexShader, fragmentShader);
i32 result = shaderLoadProgram(&shader, vertexShader, fragmentShader); if (shaderId == 0) return -1;
if (result)
return result;
assetManager->shaders[type] = shader; assetManager->shaders[type] = shaderId;
return 0; return 0;
} }

View File

@ -32,9 +32,13 @@ void initAssetManager(GameState *state)
*tex = texture_gen(1, 1, 4, CAST(u8 *)(&bitmap)); *tex = texture_gen(1, 1, 4, CAST(u8 *)(&bitmap));
/* Load shaders */ /* Load shaders */
asset_loadShaderFiles(assetManager, arena, "data/shaders/sprite.vert.glsl", asset_loadShaderFiles(
"data/shaders/sprite.frag.glsl", assetManager, arena, "data/shaders/default_tex.vert.glsl",
shaderlist_sprite); "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 = i32 result =
asset_loadTTFont(assetManager, arena, "C:/Windows/Fonts/Arialbd.ttf"); 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 // NOTE(doyle): Value to map a screen coordinate to NDC coordinate
renderer->vertexNdcFactor = renderer->vertexNdcFactor =
V2(1.0f / renderer->size.w, 1.0f / renderer->size.h); 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 = const mat4 projection =
mat4_ortho(0.0f, renderer->size.w, 0.0f, renderer->size.h, 0.0f, 1.0f); 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(); GL_CHECK_ERROR();
/* Create buffers */ /* Create buffers */

View File

@ -285,12 +285,23 @@ createDefaultTexQuad(Renderer *renderer, RenderTex *renderTex)
INTERNAL void renderGLBufferedData(Renderer *renderer, RenderGroup *group) INTERNAL void renderGLBufferedData(Renderer *renderer, RenderGroup *group)
{ {
ASSERT(group->mode < rendermode_invalid); ASSERT(group->mode < rendermode_invalid);
/* Load transformation matrix */
shader_use(renderer->shader); u32 drawMethod = GL_TRIANGLE_STRIP;
GL_CHECK_ERROR(); 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 */ /* Set color modulation value */
shader_uniformSetVec4f(renderer->shader, "spriteColor", shader_uniformSetVec4f(renderer->activeShaderId, "spriteColor",
group->color); group->color);
GL_CHECK_ERROR(); GL_CHECK_ERROR();
@ -299,17 +310,10 @@ INTERNAL void renderGLBufferedData(Renderer *renderer, RenderGroup *group)
{ {
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex->id); glBindTexture(GL_TEXTURE_2D, tex->id);
shader_uniformSet1i(renderer->shader, "tex", 0); shader_uniformSet1i(renderer->activeShaderId, "tex", 0);
} }
glBindVertexArray(renderer->vao[group->mode]); glBindVertexArray(renderer->vao[group->mode]);
u32 drawMethod = GL_TRIANGLE_STRIP;
if (group->flags & renderflag_wireframe)
{
drawMethod = GL_LINE_LOOP;
}
glDrawArrays(drawMethod, 0, renderer->numVertexesInVbo); glDrawArrays(drawMethod, 0, renderer->numVertexesInVbo);
GL_CHECK_ERROR(); GL_CHECK_ERROR();
debug_countIncrement(debugcount_drawArrays); debug_countIncrement(debugcount_drawArrays);

View File

@ -1,25 +1,27 @@
#include "Dengine/Shader.h" #include "Dengine/Shader.h"
void shader_uniformSet1i(Shader *const shader, const GLchar *name, void shader_uniformSet1i(u32 shaderId, const GLchar *name,
const GLuint data) const GLuint data)
{ {
GLint uniformLoc = glGetUniformLocation(shader->id, name); GLint uniformLoc = glGetUniformLocation(shaderId, name);
glUniform1i(uniformLoc, data); glUniform1i(uniformLoc, data);
} }
void shader_uniformSetMat4fv(Shader *const shader, const GLchar *name, void shader_uniformSetMat4fv(u32 shaderId, const GLchar *name,
mat4 data) mat4 data)
{ {
GLint uniformLoc = glGetUniformLocation(shader->id, name); GLint uniformLoc = glGetUniformLocation(shaderId, name);
GL_CHECK_ERROR();
glUniformMatrix4fv(uniformLoc, 1, GL_FALSE, data.e[0]); 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) 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]); 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); }

View File

@ -18,7 +18,7 @@ typedef struct AssetManager
HashTable audio; HashTable audio;
/* Primitive Array */ /* Primitive Array */
Shader shaders[2]; u32 shaders[shaderlist_count];
Font font; Font font;
} AssetManager; } AssetManager;
@ -72,7 +72,7 @@ const i32 asset_loadXmlFile(AssetManager *const assetManager,
MemoryArena_ *const arena, MemoryArena_ *const arena,
const PlatformFileRead *const fileRead); 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 i32 asset_loadShaderFiles(AssetManager *assetManager, MemoryArena_ *arena,
const char *const vertexPath, const char *const vertexPath,
const char *const fragmentPath, const char *const fragmentPath,

View File

@ -44,7 +44,8 @@ typedef struct XmlNode
enum ShaderList enum ShaderList
{ {
shaderlist_sprite, shaderlist_default,
shaderlist_default_no_tex,
shaderlist_count, shaderlist_count,
}; };

View File

@ -3,6 +3,7 @@
#include "Dengine/Common.h" #include "Dengine/Common.h"
#include "Dengine/Math.h" #include "Dengine/Math.h"
#include "Dengine/Assets.h"
/* Forward Declaration */ /* Forward Declaration */
typedef struct AssetManager AssetManager; typedef struct AssetManager AssetManager;
@ -57,7 +58,8 @@ typedef struct RenderGroup
typedef struct Renderer typedef struct Renderer
{ {
Shader *shader; u32 shaderList[shaderlist_count];
u32 activeShaderId;
u32 vao[rendermode_count]; u32 vao[rendermode_count];
u32 vbo[rendermode_count]; u32 vbo[rendermode_count];

View File

@ -4,18 +4,13 @@
#include "Dengine/Math.h" #include "Dengine/Math.h"
#include "Dengine/OpenGL.h" #include "Dengine/OpenGL.h"
typedef struct Shader void shader_uniformSet1i(u32 shaderId, const GLchar *name,
{
GLuint id;
} Shader;
void shader_uniformSet1i(Shader *const shader, const GLchar *name,
const GLuint data); const GLuint data);
void shader_uniformSetMat4fv(Shader *const shader, const GLchar *name, void shader_uniformSetMat4fv(u32 shaderId, const GLchar *name,
mat4 data); mat4 data);
void shader_uniformSetVec4f(Shader *const shader, const GLchar *name, void shader_uniformSetVec4f(u32 shaderId, const GLchar *name,
v4 data); v4 data);
void shader_use(const Shader *const shader); void shader_use(u32 shaderId);
#endif #endif