Add sorting of render groups by zdepth

This commit is contained in:
Doyle Thai 2016-11-30 18:18:36 +11:00
parent 68976bd6f8
commit fb254b06ec
7 changed files with 218 additions and 169 deletions

View File

@ -618,6 +618,7 @@ INTERNAL void gameUpdate(GameState *state, Memory *memory, f32 dt)
world->numStarP = 100; world->numStarP = 100;
world->starPList = world->starPList =
MEMORY_PUSH_ARRAY(&world->entityArena, world->numStarP, v2); MEMORY_PUSH_ARRAY(&world->entityArena, world->numStarP, v2);
world->starMinOpacity = 0.25f;
for (i32 i = 0; i < world->numStarP; i++) for (i32 i = 0; i < world->numStarP; i++)
{ {
@ -635,12 +636,34 @@ INTERNAL void gameUpdate(GameState *state, Memory *memory, f32 dt)
Radians starRotation = DEGREES_TO_RADIANS(45.0f); Radians starRotation = DEGREES_TO_RADIANS(45.0f);
v2 starSize = V2(2, 2); v2 starSize = V2(2, 2);
ASSERT(world->starMinOpacity >= 0.0f && world->starMinOpacity <= 1.0f);
f32 opacityFadeRateInS = 0.5f;
if (world->starFadeAway)
{
opacityFadeRateInS *= -1.0f;
}
if (world->starOpacity > 1.0f)
{
world->starOpacity = 1.0f;
world->starFadeAway = TRUE;
}
else if (world->starOpacity < world->starMinOpacity)
{
world->starOpacity = world->starMinOpacity;
world->starFadeAway = FALSE;
}
world->starOpacity += (opacityFadeRateInS * dt);
DEBUG_PUSH_VAR("Star Opacity: %5.2f", world->starOpacity, "f32");
for (i32 i = 0; i < world->numStarP; i++) for (i32 i = 0; i < world->numStarP; i++)
{ {
renderer_rect(&state->renderer, world->camera, world->starPList[i], renderer_rect(&state->renderer, world->camera, world->starPList[i],
starSize, V2(0, 0), starRotation, NULL, starSize, V2(0, 0), starRotation, NULL,
V4(1.0f, 1.0f, 0, 0.5f), V4(0.8f, 0.8f, 0.8f, world->starOpacity),
renderflag_no_texture | renderflag_wireframe); 0, renderflag_no_texture | renderflag_wireframe);
} }
if (platform_queryKey(&state->input.keys[keycode_left_square_bracket], if (platform_queryKey(&state->input.keys[keycode_left_square_bracket],
@ -713,7 +736,7 @@ INTERNAL void gameUpdate(GameState *state, Memory *memory, f32 dt)
renderer_rect(&state->renderer, world->camera, entity->pos, renderer_rect(&state->renderer, world->camera, entity->pos,
V2(5, 5), V2(0, 0), V2(5, 5), V2(0, 0),
DEGREES_TO_RADIANS(entity->rotation), NULL, DEGREES_TO_RADIANS(entity->rotation), NULL,
V4(1.0f, 1.0f, 1.0f, 1.0f), renderflag_no_texture); V4(1.0f, 1.0f, 1.0f, 1.0f), 0, renderflag_no_texture);
} }
else if (entity->type >= entitytype_asteroid_small && else if (entity->type >= entitytype_asteroid_small &&
entity->type <= entitytype_asteroid_large) entity->type <= entitytype_asteroid_large)
@ -984,7 +1007,7 @@ INTERNAL void gameUpdate(GameState *state, Memory *memory, f32 dt)
RenderFlags flags = renderflag_wireframe | renderflag_no_texture; RenderFlags flags = renderflag_wireframe | renderflag_no_texture;
renderer_entity(&state->renderer, &state->transientArena, world->camera, renderer_entity(&state->renderer, &state->transientArena, world->camera,
entity, V2(0, 0), 0, collideColor, flags); entity, V2(0, 0), 0, collideColor, 0, flags);
} }
for (i32 i = 0; i < world->numAudioRenderers; i++) for (i32 i = 0; i < world->numAudioRenderers; i++)
@ -1005,6 +1028,7 @@ INTERNAL void startMenuUpdate(GameState *state, Memory *memory, f32 dt)
StartMenuState *menuState = StartMenuState *menuState =
GET_STATE_DATA(state, &state->persistentArena, StartMenuState); GET_STATE_DATA(state, &state->persistentArena, StartMenuState);
i32 uiZDepth = 2;
if (!menuState->init) if (!menuState->init)
{ {
MemoryArena_ *persistentArena = &state->persistentArena; MemoryArena_ *persistentArena = &state->persistentArena;
@ -1063,6 +1087,7 @@ INTERNAL void startMenuUpdate(GameState *state, Memory *memory, f32 dt)
} }
else else
{ {
if (platform_queryKey(&inputBuffer->keys[keycode_enter], if (platform_queryKey(&inputBuffer->keys[keycode_enter],
readkeytype_one_shot, KEY_DELAY_NONE)) readkeytype_one_shot, KEY_DELAY_NONE))
{ {
@ -1114,7 +1139,7 @@ INTERNAL void startMenuUpdate(GameState *state, Memory *memory, f32 dt)
v2 p = v2_add(screenCenter, V2(0, textYOffset)); v2 p = v2_add(screenCenter, V2(0, textYOffset));
renderer_stringFixedCentered(renderer, transientArena, arial25, renderer_stringFixedCentered(renderer, transientArena, arial25,
title, p, V2(0, 0), 0, title, p, V2(0, 0), 0,
V4(1, 0, 1, 1), 0); V4(1, 1, 1, 1), uiZDepth, 0);
} }
{ // Resolution String Display { // Resolution String Display
@ -1125,7 +1150,7 @@ INTERNAL void startMenuUpdate(GameState *state, Memory *memory, f32 dt)
v2 p = v2_add(screenCenter, V2(0, 0)); v2 p = v2_add(screenCenter, V2(0, 0));
renderer_stringFixedCentered(renderer, transientArena, arial25, renderer_stringFixedCentered(renderer, transientArena, arial25,
resolutionLabel, p, V2(0, 0), 0, resolutionLabel, p, V2(0, 0), 0,
V4(1, 0, 1, 1), 0); V4(1, 1, 1, 1), uiZDepth, 0);
/* Draw label value */ /* Draw label value */
char *resStringToDisplay = char *resStringToDisplay =
@ -1134,9 +1159,13 @@ INTERNAL void startMenuUpdate(GameState *state, Memory *memory, f32 dt)
p = v2_add(screenCenter, V2(0, -textYOffset)); p = v2_add(screenCenter, V2(0, -textYOffset));
renderer_stringFixedCentered(renderer, transientArena, arial25, renderer_stringFixedCentered(renderer, transientArena, arial25,
resStringToDisplay, p, V2(0, 0), 0, resStringToDisplay, p, V2(0, 0), 0,
V4(1, 0, 1, 1), 0); V4(1, 1, 1, 1), uiZDepth, 0);
} }
} }
renderer_rectFixed(renderer, V2(100, 100), V2(1000, 500), V2(0, 0), 0,
NULL, V4(1.0f, 0.5f, 0.8f, 0.5f), uiZDepth - 1,
renderflag_no_texture);
} }
else else
{ {
@ -1144,7 +1173,8 @@ INTERNAL void startMenuUpdate(GameState *state, Memory *memory, f32 dt)
const char *const title = "Asteroids"; const char *const title = "Asteroids";
v2 p = v2_add(screenCenter, V2(0, 40)); v2 p = v2_add(screenCenter, V2(0, 40));
renderer_stringFixedCentered(renderer, transientArena, arial25, title, renderer_stringFixedCentered(renderer, transientArena, arial25, title,
p, V2(0, 0), 0, V4(1, 0, 0, 1), 0); p, V2(0, 0), 0, V4(1, 1, 1, 1), uiZDepth,
0);
/* Draw blinking start game prompt */ /* Draw blinking start game prompt */
menuState->startPromptBlinkTimer -= dt; menuState->startPromptBlinkTimer -= dt;
@ -1161,7 +1191,7 @@ INTERNAL void startMenuUpdate(GameState *state, Memory *memory, f32 dt)
v2 p = v2_add(screenCenter, V2(0, -40)); v2 p = v2_add(screenCenter, V2(0, -40));
renderer_stringFixedCentered(renderer, transientArena, arial25, renderer_stringFixedCentered(renderer, transientArena, arial25,
gameStart, p, V2(0, 0), 0, gameStart, p, V2(0, 0), 0,
V4(1, 1, 0, 1), 0); V4(1, 1, 1, 1), uiZDepth, 0);
} }
{ // Draw show options prompt { // Draw show options prompt
@ -1169,7 +1199,7 @@ INTERNAL void startMenuUpdate(GameState *state, Memory *memory, f32 dt)
v2 p = v2_add(screenCenter, V2(0, -120)); v2 p = v2_add(screenCenter, V2(0, -120));
renderer_stringFixedCentered(renderer, transientArena, arial25, renderer_stringFixedCentered(renderer, transientArena, arial25,
optionPrompt, p, V2(0, 0), 0, optionPrompt, p, V2(0, 0), 0,
V4(1, 1, 0, 1), 0); V4(1, 1, 1, 1), uiZDepth, 0);
} }
if (platform_queryKey(&inputBuffer->keys[keycode_enter], if (platform_queryKey(&inputBuffer->keys[keycode_enter],

View File

@ -225,7 +225,7 @@ INTERNAL void updateAndRenderDebugStack(Renderer *renderer, MemoryArena_ *arena,
v4 color = V4(1, 1, 1, 1); v4 color = V4(1, 1, 1, 1);
renderer_stringFixed( renderer_stringFixed(
renderer, arena, &GLOBAL_debug.font, GLOBAL_debug.debugStrings[i], renderer, arena, &GLOBAL_debug.font, GLOBAL_debug.debugStrings[i],
GLOBAL_debug.currStringP, V2(0, 0), rotate, color, 0); GLOBAL_debug.currStringP, V2(0, 0), rotate, color, 0, 0);
GLOBAL_debug.currStringP.y -= (0.9f * GLOBAL_debug.stringLineGap); GLOBAL_debug.currStringP.y -= (0.9f * GLOBAL_debug.stringLineGap);
} }
@ -257,7 +257,7 @@ INTERNAL void renderConsole(Renderer *renderer, MemoryArena_ *arena)
v4 color = V4(1.0f, 1.0f, 1.0f, 1.0f); v4 color = V4(1.0f, 1.0f, 1.0f, 1.0f);
renderer_stringFixed(renderer, arena, &GLOBAL_debug.font, renderer_stringFixed(renderer, arena, &GLOBAL_debug.font,
GLOBAL_debug.console[i], consoleStrP, GLOBAL_debug.console[i], consoleStrP,
V2(0, 0), rotate, color, 0); V2(0, 0), rotate, color, 0, 0);
consoleStrP.y -= (0.9f * GLOBAL_debug.stringLineGap); consoleStrP.y -= (0.9f * GLOBAL_debug.stringLineGap);
} }
} }
@ -290,6 +290,8 @@ void debug_drawUi(GameState *state, f32 dt)
} }
} }
DEBUG_PUSH_VAR("Num RenderGroups: %d", &state->renderer.groupsInUse,
"i32");
DEBUG_PUSH_VAR("Num Vertex: %d", DEBUG_PUSH_VAR("Num Vertex: %d",
GLOBAL_debug.callCount[debugcount_numVertex], "i32"); GLOBAL_debug.callCount[debugcount_numVertex], "i32");

View File

@ -6,14 +6,14 @@
#include "Dengine/MemoryArena.h" #include "Dengine/MemoryArena.h"
#include "Dengine/OpenGL.h" #include "Dengine/OpenGL.h"
void shaderUniformSet1i(u32 shaderId, const GLchar *name, INTERNAL void shaderUniformSet1i(u32 shaderId, const GLchar *name,
const GLuint data) const GLuint data)
{ {
GLint uniformLoc = glGetUniformLocation(shaderId, name); GLint uniformLoc = glGetUniformLocation(shaderId, name);
glUniform1i(uniformLoc, data); glUniform1i(uniformLoc, data);
} }
void shaderUniformSetMat4fv(u32 shaderId, const GLchar *name, INTERNAL void shaderUniformSetMat4fv(u32 shaderId, const GLchar *name,
mat4 data) mat4 data)
{ {
GLint uniformLoc = glGetUniformLocation(shaderId, name); GLint uniformLoc = glGetUniformLocation(shaderId, name);
@ -22,7 +22,7 @@ void shaderUniformSetMat4fv(u32 shaderId, const GLchar *name,
GL_CHECK_ERROR(); GL_CHECK_ERROR();
} }
void shaderUniformSetVec4f(u32 shaderId, const GLchar *name, INTERNAL void shaderUniformSetVec4f(u32 shaderId, const GLchar *name,
v4 data) v4 data)
{ {
GLint uniformLoc = glGetUniformLocation(shaderId, name); GLint uniformLoc = glGetUniformLocation(shaderId, name);
@ -30,7 +30,7 @@ void shaderUniformSetVec4f(u32 shaderId, const GLchar *name,
} }
void shaderUse(u32 shaderId) { glUseProgram(shaderId); } INTERNAL void shaderUse(u32 shaderId) { glUseProgram(shaderId); }
void renderer_updateSize(Renderer *renderer, AssetManager *assetManager, v2 windowSize) void renderer_updateSize(Renderer *renderer, AssetManager *assetManager, v2 windowSize)
{ {
@ -173,8 +173,8 @@ INTERNAL void applyRotationToVertexes(v2 pos, v2 pivotPoint, Radians rotate,
} }
INTERNAL void addVertexToRenderGroup_(Renderer *renderer, Texture *tex, INTERNAL void addVertexToRenderGroup_(Renderer *renderer, Texture *tex,
v4 color, RenderVertex *vertexList, v4 color, i32 zDepth,
i32 numVertexes, RenderVertex *vertexList, i32 numVertexes,
enum RenderMode targetRenderMode, enum RenderMode targetRenderMode,
RenderFlags flags) RenderFlags flags)
{ {
@ -197,29 +197,17 @@ INTERNAL void addVertexToRenderGroup_(Renderer *renderer, Texture *tex,
/* If the textures match and have the same color modulation, we can /* If the textures match and have the same color modulation, we can
* add these vertices to the current group */ * add these vertices to the current group */
b32 renderModeMatches = FALSE; if (!(group->mode == targetRenderMode)) continue;
if (group->mode == targetRenderMode) renderModeMatches = TRUE; if (!(v4_equals(group->color, color))) continue;
if (!(group->flags == flags)) continue;
if (!(group->zDepth == zDepth)) continue;
if (!tex && group->tex) continue;
b32 colorMatches = FALSE; if (tex && group->tex)
if (v4_equals(group->color, color)) colorMatches = TRUE;
b32 flagsMatches = FALSE;
if (group->flags == flags) flagsMatches = TRUE;
b32 texMatches = TRUE;
if (!tex && !group->tex)
{ {
texMatches = TRUE; if (!(group->tex->id == tex->id)) continue;
}
else if (tex && group->tex)
{
if (group->tex->id == tex->id)
{
texMatches = TRUE;
}
} }
if (texMatches && colorMatches && renderModeMatches && flagsMatches)
groupIsValid = TRUE; groupIsValid = TRUE;
} }
else else
@ -232,10 +220,9 @@ INTERNAL void addVertexToRenderGroup_(Renderer *renderer, Texture *tex,
group->color = color; group->color = color;
group->mode = targetRenderMode; group->mode = targetRenderMode;
group->flags = flags; group->flags = flags;
group->zDepth = zDepth;
#ifdef DENGINE_DEBUG renderer->groupsInUse++;
debug_countIncrement(debugcount_renderGroups);
#endif
} }
if (groupIsValid) if (groupIsValid)
@ -340,19 +327,6 @@ INTERNAL inline void flipTexCoord(v4 *texCoords, b32 flipX, b32 flipY)
} }
} }
INTERNAL void bufferRenderGroupToGL(Renderer *renderer, RenderGroup *group)
{
RenderVertex *vertexList = group->vertexList;
i32 numVertex = group->vertexIndex;
// TODO(doyle): We assume that vbo and vao are assigned
renderer->numVertexesInVbo = numVertex;
glBindBuffer(GL_ARRAY_BUFFER, renderer->vbo[group->mode]);
glBufferData(GL_ARRAY_BUFFER, numVertex * sizeof(RenderVertex), vertexList,
GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
INTERNAL v4 getTexRectNormaliseDeviceCoords(RenderTex renderTex) INTERNAL v4 getTexRectNormaliseDeviceCoords(RenderTex renderTex)
{ {
/* Convert texture coordinates to normalised texture coordinates */ /* Convert texture coordinates to normalised texture coordinates */
@ -445,56 +419,6 @@ createDefaultTexQuad(Renderer *renderer, RenderTex *renderTex)
INTERNAL void renderGLBufferedData(Renderer *renderer, RenderGroup *group) INTERNAL void renderGLBufferedData(Renderer *renderer, RenderGroup *group)
{ {
ASSERT(group->mode < rendermode_invalid);
if (group->flags & renderflag_wireframe)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
else
{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
GL_CHECK_ERROR();
if (group->flags & renderflag_no_texture)
{
renderer->activeShaderId =
renderer->shaderList[shaderlist_default_no_tex];
shaderUse(renderer->activeShaderId);
}
else
{
renderer->activeShaderId = renderer->shaderList[shaderlist_default];
shaderUse(renderer->activeShaderId);
Texture *tex = group->tex;
if (tex)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex->id);
shaderUniformSet1i(renderer->activeShaderId, "tex", 0);
GL_CHECK_ERROR();
}
}
#if 0
glDisable(GL_CULL_FACE);
#endif
/* Set color modulation value */
shaderUniformSetVec4f(renderer->activeShaderId, "spriteColor",
group->color);
glBindVertexArray(renderer->vao[group->mode]);
glDrawArrays(GL_TRIANGLE_STRIP, 0, renderer->numVertexesInVbo);
GL_CHECK_ERROR();
debug_countIncrement(debugcount_drawArrays);
/* Unbind */
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
GL_CHECK_ERROR();
} }
RenderTex renderer_createNullRenderTex(AssetManager *const assetManager) RenderTex renderer_createNullRenderTex(AssetManager *const assetManager)
@ -505,8 +429,8 @@ RenderTex renderer_createNullRenderTex(AssetManager *const assetManager)
} }
void renderer_rect(Renderer *const renderer, Rect camera, v2 pos, v2 size, void renderer_rect(Renderer *const renderer, Rect camera, v2 pos, v2 size,
v2 pivotPoint, Radians rotate, RenderTex *renderTex, v4 color, v2 pivotPoint, Radians rotate, RenderTex *renderTex,
RenderFlags flags) v4 color, i32 zDepth, RenderFlags flags)
{ {
// NOTE(doyle): Bottom left and top right position of quad in world space // NOTE(doyle): Bottom left and top right position of quad in world space
v2 posInCameraSpace = v2_sub(pos, camera.min); v2 posInCameraSpace = v2_sub(pos, camera.min);
@ -522,15 +446,15 @@ void renderer_rect(Renderer *const renderer, Rect camera, v2 pos, v2 size,
pivotPoint, rotate, *renderTex); pivotPoint, rotate, *renderTex);
beginVertexBatch(renderer); beginVertexBatch(renderer);
addVertexToRenderGroup_(renderer, renderTex->tex, color, quad.vertexList, addVertexToRenderGroup_(renderer, renderTex->tex, color, zDepth,
ARRAY_COUNT(quad.vertexList), rendermode_quad, quad.vertexList, ARRAY_COUNT(quad.vertexList),
flags); rendermode_quad, flags);
endVertexBatch(renderer); endVertexBatch(renderer);
} }
void renderer_polygon(Renderer *const renderer, Rect camera, void renderer_polygon(Renderer *const renderer, Rect camera,
v2 *polygonPoints, i32 numPoints, v2 pivotPoint, v2 *polygonPoints, i32 numPoints, v2 pivotPoint,
Radians rotate, RenderTex *renderTex, v4 color, Radians rotate, RenderTex *renderTex, v4 color, i32 zDepth,
RenderFlags flags) RenderFlags flags)
{ {
ASSERT(numPoints >= 3); ASSERT(numPoints >= 3);
@ -614,9 +538,9 @@ void renderer_polygon(Renderer *const renderer, Rect camera,
triangle[1].pos = vertexList[1]; triangle[1].pos = vertexList[1];
triangle[2].pos = vertexList[2]; triangle[2].pos = vertexList[2];
addVertexToRenderGroup_(renderer, renderTex->tex, color, triangle, addVertexToRenderGroup_(renderer, renderTex->tex, color, zDepth,
ARRAY_COUNT(triangle), rendermode_polygon, triangle, ARRAY_COUNT(triangle),
flags); rendermode_polygon, flags);
endVertexBatch(renderer); endVertexBatch(renderer);
triangulationIndex++; triangulationIndex++;
} }
@ -624,7 +548,8 @@ void renderer_polygon(Renderer *const renderer, Rect camera,
void renderer_string(Renderer *const renderer, MemoryArena_ *arena, Rect camera, void renderer_string(Renderer *const renderer, MemoryArena_ *arena, Rect camera,
Font *const font, const char *const string, v2 pos, Font *const font, const char *const string, v2 pos,
v2 pivotPoint, Radians rotate, v4 color, RenderFlags flags) v2 pivotPoint, Radians rotate, v4 color, i32 zDepth,
RenderFlags flags)
{ {
i32 strLen = common_strlen(string); i32 strLen = common_strlen(string);
if (strLen <= 0) return; if (strLen <= 0) return;
@ -673,7 +598,7 @@ void renderer_string(Renderer *const renderer, MemoryArena_ *arena, Rect camera,
pivotPoint, rotate, renderTex); pivotPoint, rotate, renderTex);
beginVertexBatch(renderer); beginVertexBatch(renderer);
addVertexToRenderGroup_(renderer, tex, color, quad.vertexList, addVertexToRenderGroup_(renderer, tex, color, zDepth, quad.vertexList,
ARRAY_COUNT(quad.vertexList), ARRAY_COUNT(quad.vertexList),
rendermode_quad, flags); rendermode_quad, flags);
endVertexBatch(renderer); endVertexBatch(renderer);
@ -684,7 +609,7 @@ void renderer_string(Renderer *const renderer, MemoryArena_ *arena, Rect camera,
void renderer_entity(Renderer *renderer, MemoryArena_ *transientArena, void renderer_entity(Renderer *renderer, MemoryArena_ *transientArena,
Rect camera, Entity *entity, v2 pivotPoint, Degrees rotate, Rect camera, Entity *entity, v2 pivotPoint, Degrees rotate,
v4 color, RenderFlags flags) v4 color, i32 zDepth, RenderFlags flags)
{ {
// TODO(doyle): Add early exit on entities out of camera bounds // TODO(doyle): Add early exit on entities out of camera bounds
Radians totalRotation = DEGREES_TO_RADIANS((entity->rotation + rotate)); Radians totalRotation = DEGREES_TO_RADIANS((entity->rotation + rotate));
@ -726,7 +651,7 @@ void renderer_entity(Renderer *renderer, MemoryArena_ *transientArena,
{ {
renderer_rect(renderer, camera, entity->pos, entity->size, renderer_rect(renderer, camera, entity->pos, entity->size,
v2_add(entity->offset, pivotPoint), totalRotation, v2_add(entity->offset, pivotPoint), totalRotation,
&renderTex, entity->color, flags); &renderTex, entity->color, zDepth, flags);
} }
else if (entity->renderMode == rendermode_polygon) else if (entity->renderMode == rendermode_polygon)
{ {
@ -739,7 +664,7 @@ void renderer_entity(Renderer *renderer, MemoryArena_ *transientArena,
renderer_polygon(renderer, camera, offsetVertexPoints, renderer_polygon(renderer, camera, offsetVertexPoints,
entity->numVertexPoints, entity->numVertexPoints,
v2_add(entity->offset, pivotPoint), totalRotation, v2_add(entity->offset, pivotPoint), totalRotation,
&renderTex, renderColor, flags); &renderTex, renderColor, zDepth, flags);
} }
else else
{ {
@ -750,21 +675,105 @@ void renderer_entity(Renderer *renderer, MemoryArena_ *transientArena,
// TODO(doyle): We have no notion of sort order!! // TODO(doyle): We have no notion of sort order!!
void renderer_renderGroups(Renderer *renderer) void renderer_renderGroups(Renderer *renderer)
{ {
for (i32 i = 0; i < ARRAY_COUNT(renderer->groups); i++)
{
RenderGroup *currGroup = &renderer->groups[i];
if (currGroup->init)
{
bufferRenderGroupToGL(renderer, currGroup);
renderGLBufferedData(renderer, currGroup);
RenderGroup cleanGroup = {0}; /* Sort the group by zdepth */
cleanGroup.vertexList = currGroup->vertexList; b32 groupHasSwapped = TRUE;
*currGroup = cleanGroup; i32 numGroupsToCheck = renderer->groupsInUse - 1;
while (groupHasSwapped)
{
groupHasSwapped = FALSE;
for (i32 i = 0; i < numGroupsToCheck; i++)
{
RenderGroup *group = &renderer->groups[i];
RenderGroup *checkGroup = &renderer->groups[i + 1];
if (checkGroup->zDepth < group->zDepth)
{
RenderGroup tmp = *group;
*group = *checkGroup;
*checkGroup = tmp;
groupHasSwapped = TRUE;
}
}
numGroupsToCheck--;
}
/* Render groups */
for (i32 i = 0; i < renderer->groupsInUse; i++)
{
RenderGroup *group = &renderer->groups[i];
{ // Buffer render group to OpenGL
RenderVertex *vertexList = group->vertexList;
i32 numVertex = group->vertexIndex;
// TODO(doyle): We assume that vbo and vao are assigned
renderer->numVertexesInVbo = numVertex;
glBindBuffer(GL_ARRAY_BUFFER, renderer->vbo[group->mode]);
glBufferData(GL_ARRAY_BUFFER, numVertex * sizeof(RenderVertex),
vertexList, GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
{ // Render buffered data in OpenGl
ASSERT(group->mode < rendermode_invalid);
if (group->flags & renderflag_wireframe)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
} }
else else
{ {
break; glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
GL_CHECK_ERROR();
if (group->flags & renderflag_no_texture)
{
renderer->activeShaderId =
renderer->shaderList[shaderlist_default_no_tex];
shaderUse(renderer->activeShaderId);
}
else
{
renderer->activeShaderId =
renderer->shaderList[shaderlist_default];
shaderUse(renderer->activeShaderId);
Texture *tex = group->tex;
if (tex)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex->id);
shaderUniformSet1i(renderer->activeShaderId, "tex", 0);
GL_CHECK_ERROR();
} }
} }
#if 0
glDisable(GL_CULL_FACE);
#endif
/* Set color modulation value */
shaderUniformSetVec4f(renderer->activeShaderId, "spriteColor",
group->color);
glBindVertexArray(renderer->vao[group->mode]);
glDrawArrays(GL_TRIANGLE_STRIP, 0, renderer->numVertexesInVbo);
GL_CHECK_ERROR();
debug_countIncrement(debugcount_drawArrays);
/* Unbind */
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
GL_CHECK_ERROR();
}
RenderGroup cleanGroup = {0};
cleanGroup.vertexList = group->vertexList;
*group = cleanGroup;
}
renderer->groupsInUse = 0;
} }

View File

@ -28,7 +28,7 @@ void ui_endState(UiState *state, InputBuffer *input)
i32 ui_button(UiState *const uiState, MemoryArena_ *const arena, i32 ui_button(UiState *const uiState, MemoryArena_ *const arena,
AssetManager *const assetManager, Renderer *const renderer, AssetManager *const assetManager, Renderer *const renderer,
Font *const font, const InputBuffer input, const i32 id, Font *const font, const InputBuffer input, const i32 id,
const Rect rect, const char *const label) const Rect rect, const char *const label, i32 zDepth)
{ {
if (math_rectContainsP(rect, input.mouseP)) if (math_rectContainsP(rect, input.mouseP))
{ {
@ -76,11 +76,12 @@ i32 ui_button(UiState *const uiState, MemoryArena_ *const arena,
renderer_rectFixed(renderer, renderer_rectFixed(renderer,
v2_add(V2(-2, -2), v2_add(buttonOffset, rect.min)), v2_add(V2(-2, -2), v2_add(buttonOffset, rect.min)),
v2_add(V2(4, 4), rectSize), V2(0, 0), 0, NULL, v2_add(V2(4, 4), rectSize), V2(0, 0), 0, NULL,
buttonColor, renderflag_no_texture); buttonColor, zDepth, renderflag_no_texture);
} }
renderer_rectFixed(renderer, v2_add(buttonOffset, rect.min), rectSize, renderer_rectFixed(renderer, v2_add(buttonOffset, rect.min), rectSize,
V2(0, 0), 0, NULL, buttonColor, renderflag_no_texture); V2(0, 0), 0, NULL, buttonColor, zDepth,
renderflag_no_texture);
if (label) if (label)
{ {
@ -104,7 +105,7 @@ i32 ui_button(UiState *const uiState, MemoryArena_ *const arena,
// button text into another render group // button text into another render group
labelPos = v2_add(labelPos, buttonOffset); labelPos = v2_add(labelPos, buttonOffset);
renderer_stringFixed(renderer, arena, font, label, labelPos, V2(0, 0), renderer_stringFixed(renderer, arena, font, label, labelPos, V2(0, 0),
0, V4(0.9f, 0.9f, 0.9f, 0.9f), 0); 0, V4(0.9f, 0.9f, 0.9f, 0.9f), zDepth, 0);
} }
// After renderering before click check, see if we need to process keys // After renderering before click check, see if we need to process keys
@ -145,7 +146,7 @@ i32 ui_button(UiState *const uiState, MemoryArena_ *const arena,
i32 ui_scrollbar(UiState *const uiState, AssetManager *const assetManager, i32 ui_scrollbar(UiState *const uiState, AssetManager *const assetManager,
Renderer *const renderer, const InputBuffer input, Renderer *const renderer, const InputBuffer input,
const i32 id, const Rect scrollBarRect, i32 *const value, const i32 id, const Rect scrollBarRect, i32 *const value,
const i32 maxValue) const i32 maxValue, i32 zDepth)
{ {
#ifdef DENGINE_DEBUG #ifdef DENGINE_DEBUG
ASSERT(*value <= maxValue); ASSERT(*value <= maxValue);
@ -176,12 +177,12 @@ i32 ui_scrollbar(UiState *const uiState, AssetManager *const assetManager,
// Draw outline // Draw outline
renderer_rectFixed(renderer, v2_add(V2(-2, -2), scrollBarRect.min), renderer_rectFixed(renderer, v2_add(V2(-2, -2), scrollBarRect.min),
v2_add(V2(4, 4), rectSize), V2(0, 0), 0, &renderTex, v2_add(V2(4, 4), rectSize), V2(0, 0), 0, &renderTex,
V4(1, 0, 0, 1), 0); V4(1, 0, 0, 1), zDepth, 0);
} }
// Render scroll bar background // Render scroll bar background
renderer_rectFixed(renderer, scrollBarRect.min, rectSize, V2(0, 0), 0, renderer_rectFixed(renderer, scrollBarRect.min, rectSize, V2(0, 0), 0,
&renderTex, V4(0.75f, 0.5f, 0.5f, 1), 0); &renderTex, V4(0.75f, 0.5f, 0.5f, 1), zDepth, 0);
// Render scroll bar slider // Render scroll bar slider
v2 sliderSize = V2(16, 16); v2 sliderSize = V2(16, 16);
@ -198,7 +199,7 @@ i32 ui_scrollbar(UiState *const uiState, AssetManager *const assetManager,
sliderColor = V4(0.0f, 1.0f, 0, 1); sliderColor = V4(0.0f, 1.0f, 0, 1);
renderer_rectFixed(renderer, sliderPos, sliderSize, V2(0, 0), 0, &renderTex, renderer_rectFixed(renderer, sliderPos, sliderSize, V2(0, 0), 0, &renderTex,
sliderColor, 0); sliderColor, zDepth, 0);
if (uiState->kbdItem == id) if (uiState->kbdItem == id)
{ {
@ -259,7 +260,7 @@ i32 ui_scrollbar(UiState *const uiState, AssetManager *const assetManager,
i32 ui_textfield(UiState *const uiState, MemoryArena_ *const arena, i32 ui_textfield(UiState *const uiState, MemoryArena_ *const arena,
AssetManager *const assetManager, Renderer *const renderer, AssetManager *const assetManager, Renderer *const renderer,
Font *const font, InputBuffer input, const i32 id, Font *const font, InputBuffer input, const i32 id,
const Rect rect, char *const string) const Rect rect, char *const string, i32 zDepth)
{ {
i32 strLen = common_strlen(string); i32 strLen = common_strlen(string);
b32 changed = FALSE; b32 changed = FALSE;
@ -287,27 +288,27 @@ i32 ui_textfield(UiState *const uiState, MemoryArena_ *const arena,
// Draw outline // Draw outline
renderer_rectFixed(renderer, v2_add(V2(-2, -2), rect.min), renderer_rectFixed(renderer, v2_add(V2(-2, -2), rect.min),
v2_add(V2(4, 4), rectSize), V2(0, 0), 0, NULL, v2_add(V2(4, 4), rectSize), V2(0, 0), 0, NULL,
V4(1.0f, 0, 0, 1), 0); V4(1.0f, 0, 0, 1), zDepth, 0);
} }
// Render text field // Render text field
renderer_rectFixed(renderer, rect.min, rectSize, V2(0, 0), 0, NULL, renderer_rectFixed(renderer, rect.min, rectSize, V2(0, 0), 0, NULL,
V4(0.75f, 0.5f, 0.5f, 1), 0); V4(0.75f, 0.5f, 0.5f, 1), zDepth, 0);
if (uiState->activeItem == id || uiState->hotItem == id) if (uiState->activeItem == id || uiState->hotItem == id)
{ {
renderer_rectFixed(renderer, rect.min, rectSize, V2(0, 0), 0, NULL, renderer_rectFixed(renderer, rect.min, rectSize, V2(0, 0), 0, NULL,
V4(0.75f, 0.75f, 0.0f, 1), 0); V4(0.75f, 0.75f, 0.0f, 1), zDepth, 0);
} }
else else
{ {
renderer_rectFixed(renderer, rect.min, rectSize, V2(0, 0), 0, NULL, renderer_rectFixed(renderer, rect.min, rectSize, V2(0, 0), 0, NULL,
V4(0.5f, 0.5f, 0.5f, 1), 0); V4(0.5f, 0.5f, 0.5f, 1), zDepth, 0);
} }
v2 strPos = rect.min; v2 strPos = rect.min;
renderer_stringFixed(renderer, arena, font, string, strPos, V2(0, 0), 0, renderer_stringFixed(renderer, arena, font, string, strPos, V2(0, 0), 0,
V4(0, 0, 0, 1), 0); V4(0, 0, 0, 1), zDepth, 0);
if (uiState->kbdItem == id) if (uiState->kbdItem == id)
{ {

View File

@ -39,6 +39,9 @@ typedef struct GameWorldState
v2 *particleVertexCache; v2 *particleVertexCache;
v2 *starPList; v2 *starPList;
f32 starOpacity;
f32 starMinOpacity;
b32 starFadeAway;
i32 numStarP; i32 numStarP;
// TODO(doyle): Audio mixing instead of multiple renderers // TODO(doyle): Audio mixing instead of multiple renderers

View File

@ -46,6 +46,7 @@ typedef struct RenderGroup
b32 init; b32 init;
RenderFlags flags; RenderFlags flags;
enum RenderMode mode; enum RenderMode mode;
i32 zDepth;
// NOTE(doyle): Only for when adding singular triangles in triangle strip // NOTE(doyle): Only for when adding singular triangles in triangle strip
// mode // mode
@ -81,6 +82,7 @@ typedef struct Renderer
v2 size; v2 size;
RenderGroup groups[128]; RenderGroup groups[128];
i32 groupsInUse;
i32 groupCapacity; i32 groupCapacity;
} Renderer; } Renderer;
@ -94,42 +96,44 @@ RenderTex renderer_createNullRenderTex(AssetManager *const assetManager);
// Renderer::~Renderer() { glDeleteVertexArrays(1, &this->quadVAO); } // Renderer::~Renderer() { glDeleteVertexArrays(1, &this->quadVAO); }
void renderer_rect(Renderer *const renderer, Rect camera, v2 pos, v2 size, void renderer_rect(Renderer *const renderer, Rect camera, v2 pos, v2 size,
v2 pivotPoint, Radians rotate, RenderTex *renderTex, v2 pivotPoint, Radians rotate, RenderTex *renderTex,
v4 color, RenderFlags flags); v4 color, i32 zDepth, RenderFlags flags);
void renderer_polygon(Renderer *const renderer, Rect camera, v2 *polygonPoints, void renderer_polygon(Renderer *const renderer, Rect camera, v2 *polygonPoints,
i32 numPoints, v2 pivotPoint, Radians rotate, i32 numPoints, v2 pivotPoint, Radians rotate,
RenderTex *renderTex, v4 color, RenderFlags flags); RenderTex *renderTex, v4 color, i32 zDepth,
RenderFlags flags);
inline void renderer_rectFixed(Renderer *const renderer, v2 pos, v2 size, inline void renderer_rectFixed(Renderer *const renderer, v2 pos, v2 size,
v2 pivotPoint, Radians rotate, v2 pivotPoint, Radians rotate,
RenderTex *renderTex, v4 color, RenderTex *renderTex, v4 color, i32 zDepth,
RenderFlags flags) RenderFlags flags)
{ {
Rect staticCamera = {V2(0, 0), renderer->size}; Rect staticCamera = {V2(0, 0), renderer->size};
renderer_rect(renderer, staticCamera, pos, size, pivotPoint, rotate, renderer_rect(renderer, staticCamera, pos, size, pivotPoint, rotate,
renderTex, color, flags); renderTex, color, zDepth, flags);
} }
void renderer_string(Renderer *const renderer, MemoryArena_ *arena, Rect camera, void renderer_string(Renderer *const renderer, MemoryArena_ *arena, Rect camera,
Font *const font, const char *const string, v2 pos, Font *const font, const char *const string, v2 pos,
v2 pivotPoint, Radians rotate, v4 color, v2 pivotPoint, Radians rotate, v4 color, i32 zDepth,
RenderFlags flags); RenderFlags flags);
inline void renderer_stringFixed(Renderer *const renderer, MemoryArena_ *arena, inline void renderer_stringFixed(Renderer *const renderer, MemoryArena_ *arena,
Font *const font, const char *const string, Font *const font, const char *const string,
v2 pos, v2 pivotPoint, Radians rotate, v2 pos, v2 pivotPoint, Radians rotate,
v4 color, RenderFlags flags) v4 color, i32 zDepth, RenderFlags flags)
{ {
Rect staticCamera = {V2(0, 0), renderer->size}; Rect staticCamera = {V2(0, 0), renderer->size};
renderer_string(renderer, arena, staticCamera, font, string, pos, renderer_string(renderer, arena, staticCamera, font, string, pos,
pivotPoint, rotate, color, flags); pivotPoint, rotate, color, zDepth, flags);
} }
inline void renderer_stringFixedCentered(Renderer *const renderer, inline void renderer_stringFixedCentered(Renderer *const renderer,
MemoryArena_ *arena, Font *const font, MemoryArena_ *arena, Font *const font,
const char *const string, v2 pos, const char *const string, v2 pos,
v2 pivotPoint, Radians rotate, v2 pivotPoint, Radians rotate,
v4 color, RenderFlags flags) v4 color, i32 zDepth,
enum RenderFlags flags)
{ {
Rect staticCamera = {V2(0, 0), renderer->size}; Rect staticCamera = {V2(0, 0), renderer->size};
@ -138,12 +142,12 @@ inline void renderer_stringFixedCentered(Renderer *const renderer,
pos = v2_sub(pos, halfDim); pos = v2_sub(pos, halfDim);
renderer_string(renderer, arena, staticCamera, font, string, pos, renderer_string(renderer, arena, staticCamera, font, string, pos,
pivotPoint, rotate, color, flags); pivotPoint, rotate, color, zDepth, flags);
} }
void renderer_entity(Renderer *renderer, MemoryArena_ *transientArena, void renderer_entity(Renderer *renderer, MemoryArena_ *transientArena,
Rect camera, Entity *entity, v2 pivotPoint, Degrees rotate, Rect camera, Entity *entity, v2 pivotPoint, Degrees rotate,
v4 color, RenderFlags flags); v4 color, i32 zDepth, RenderFlags flags);
void renderer_renderGroups(Renderer *renderer); void renderer_renderGroups(Renderer *renderer);

View File

@ -64,15 +64,15 @@ void ui_endState(UiState *state, InputBuffer *input);
i32 ui_button(UiState *const uiState, MemoryArena_ *const arena, i32 ui_button(UiState *const uiState, MemoryArena_ *const arena,
AssetManager *const assetManager, Renderer *const renderer, AssetManager *const assetManager, Renderer *const renderer,
Font *const font, const InputBuffer input, const i32 id, Font *const font, const InputBuffer input, const i32 id,
const Rect rect, const char *const label); const Rect rect, const char *const label, i32 zDepth);
i32 ui_textfield(UiState *const uiState, MemoryArena_ *const arena, i32 ui_textfield(UiState *const uiState, MemoryArena_ *const arena,
AssetManager *const assetManager, Renderer *const renderer, AssetManager *const assetManager, Renderer *const renderer,
Font *const font, InputBuffer input, const i32 id, Font *const font, InputBuffer input, const i32 id,
const Rect rect, char *const string); const Rect rect, char *const string, i32 zDepth);
i32 ui_scrollbar(UiState *const uiState, AssetManager *const assetManager, i32 ui_scrollbar(UiState *const uiState, AssetManager *const assetManager,
Renderer *const renderer, const InputBuffer input, Renderer *const renderer, const InputBuffer input,
const i32 id, const Rect scrollBarRect, i32 *const value, const i32 id, const Rect scrollBarRect, i32 *const value,
const i32 maxValue); const i32 maxValue, i32 zDepth);
#endif #endif