Add sorting of render groups by zdepth
This commit is contained in:
parent
68976bd6f8
commit
fb254b06ec
@ -618,6 +618,7 @@ INTERNAL void gameUpdate(GameState *state, Memory *memory, f32 dt)
|
||||
world->numStarP = 100;
|
||||
world->starPList =
|
||||
MEMORY_PUSH_ARRAY(&world->entityArena, world->numStarP, v2);
|
||||
world->starMinOpacity = 0.25f;
|
||||
|
||||
for (i32 i = 0; i < world->numStarP; i++)
|
||||
{
|
||||
@ -633,14 +634,36 @@ INTERNAL void gameUpdate(GameState *state, Memory *memory, f32 dt)
|
||||
for (u32 i = world->asteroidCounter; i < world->numAsteroids; i++)
|
||||
addAsteroid(world, (rand() % asteroidsize_count));
|
||||
|
||||
Radians starRotation = DEGREES_TO_RADIANS(45.0f);
|
||||
v2 starSize = V2(2, 2);
|
||||
Radians starRotation = DEGREES_TO_RADIANS(45.0f);
|
||||
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++)
|
||||
{
|
||||
renderer_rect(&state->renderer, world->camera, world->starPList[i],
|
||||
starSize, V2(0, 0), starRotation, NULL,
|
||||
V4(1.0f, 1.0f, 0, 0.5f),
|
||||
renderflag_no_texture | renderflag_wireframe);
|
||||
V4(0.8f, 0.8f, 0.8f, world->starOpacity),
|
||||
0, renderflag_no_texture | renderflag_wireframe);
|
||||
}
|
||||
|
||||
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,
|
||||
V2(5, 5), V2(0, 0),
|
||||
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 &&
|
||||
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;
|
||||
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++)
|
||||
@ -1005,6 +1028,7 @@ INTERNAL void startMenuUpdate(GameState *state, Memory *memory, f32 dt)
|
||||
StartMenuState *menuState =
|
||||
GET_STATE_DATA(state, &state->persistentArena, StartMenuState);
|
||||
|
||||
i32 uiZDepth = 2;
|
||||
if (!menuState->init)
|
||||
{
|
||||
MemoryArena_ *persistentArena = &state->persistentArena;
|
||||
@ -1063,6 +1087,7 @@ INTERNAL void startMenuUpdate(GameState *state, Memory *memory, f32 dt)
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
if (platform_queryKey(&inputBuffer->keys[keycode_enter],
|
||||
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));
|
||||
renderer_stringFixedCentered(renderer, transientArena, arial25,
|
||||
title, p, V2(0, 0), 0,
|
||||
V4(1, 0, 1, 1), 0);
|
||||
V4(1, 1, 1, 1), uiZDepth, 0);
|
||||
}
|
||||
|
||||
{ // Resolution String Display
|
||||
@ -1125,7 +1150,7 @@ INTERNAL void startMenuUpdate(GameState *state, Memory *memory, f32 dt)
|
||||
v2 p = v2_add(screenCenter, V2(0, 0));
|
||||
renderer_stringFixedCentered(renderer, transientArena, arial25,
|
||||
resolutionLabel, p, V2(0, 0), 0,
|
||||
V4(1, 0, 1, 1), 0);
|
||||
V4(1, 1, 1, 1), uiZDepth, 0);
|
||||
|
||||
/* Draw label value */
|
||||
char *resStringToDisplay =
|
||||
@ -1134,9 +1159,13 @@ INTERNAL void startMenuUpdate(GameState *state, Memory *memory, f32 dt)
|
||||
p = v2_add(screenCenter, V2(0, -textYOffset));
|
||||
renderer_stringFixedCentered(renderer, transientArena, arial25,
|
||||
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
|
||||
{
|
||||
@ -1144,7 +1173,8 @@ INTERNAL void startMenuUpdate(GameState *state, Memory *memory, f32 dt)
|
||||
const char *const title = "Asteroids";
|
||||
v2 p = v2_add(screenCenter, V2(0, 40));
|
||||
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 */
|
||||
menuState->startPromptBlinkTimer -= dt;
|
||||
@ -1161,7 +1191,7 @@ INTERNAL void startMenuUpdate(GameState *state, Memory *memory, f32 dt)
|
||||
v2 p = v2_add(screenCenter, V2(0, -40));
|
||||
renderer_stringFixedCentered(renderer, transientArena, arial25,
|
||||
gameStart, p, V2(0, 0), 0,
|
||||
V4(1, 1, 0, 1), 0);
|
||||
V4(1, 1, 1, 1), uiZDepth, 0);
|
||||
}
|
||||
|
||||
{ // 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));
|
||||
renderer_stringFixedCentered(renderer, transientArena, arial25,
|
||||
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],
|
||||
|
@ -225,7 +225,7 @@ INTERNAL void updateAndRenderDebugStack(Renderer *renderer, MemoryArena_ *arena,
|
||||
v4 color = V4(1, 1, 1, 1);
|
||||
renderer_stringFixed(
|
||||
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);
|
||||
}
|
||||
|
||||
@ -257,7 +257,7 @@ INTERNAL void renderConsole(Renderer *renderer, MemoryArena_ *arena)
|
||||
v4 color = V4(1.0f, 1.0f, 1.0f, 1.0f);
|
||||
renderer_stringFixed(renderer, arena, &GLOBAL_debug.font,
|
||||
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);
|
||||
}
|
||||
}
|
||||
@ -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",
|
||||
GLOBAL_debug.callCount[debugcount_numVertex], "i32");
|
||||
|
||||
|
257
src/Renderer.c
257
src/Renderer.c
@ -6,14 +6,14 @@
|
||||
#include "Dengine/MemoryArena.h"
|
||||
#include "Dengine/OpenGL.h"
|
||||
|
||||
void shaderUniformSet1i(u32 shaderId, const GLchar *name,
|
||||
INTERNAL void shaderUniformSet1i(u32 shaderId, const GLchar *name,
|
||||
const GLuint data)
|
||||
{
|
||||
GLint uniformLoc = glGetUniformLocation(shaderId, name);
|
||||
glUniform1i(uniformLoc, data);
|
||||
}
|
||||
|
||||
void shaderUniformSetMat4fv(u32 shaderId, const GLchar *name,
|
||||
INTERNAL void shaderUniformSetMat4fv(u32 shaderId, const GLchar *name,
|
||||
mat4 data)
|
||||
{
|
||||
GLint uniformLoc = glGetUniformLocation(shaderId, name);
|
||||
@ -22,7 +22,7 @@ void shaderUniformSetMat4fv(u32 shaderId, const GLchar *name,
|
||||
GL_CHECK_ERROR();
|
||||
}
|
||||
|
||||
void shaderUniformSetVec4f(u32 shaderId, const GLchar *name,
|
||||
INTERNAL void shaderUniformSetVec4f(u32 shaderId, const GLchar *name,
|
||||
v4 data)
|
||||
{
|
||||
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)
|
||||
{
|
||||
@ -173,8 +173,8 @@ INTERNAL void applyRotationToVertexes(v2 pos, v2 pivotPoint, Radians rotate,
|
||||
}
|
||||
|
||||
INTERNAL void addVertexToRenderGroup_(Renderer *renderer, Texture *tex,
|
||||
v4 color, RenderVertex *vertexList,
|
||||
i32 numVertexes,
|
||||
v4 color, i32 zDepth,
|
||||
RenderVertex *vertexList, i32 numVertexes,
|
||||
enum RenderMode targetRenderMode,
|
||||
RenderFlags flags)
|
||||
{
|
||||
@ -197,45 +197,32 @@ INTERNAL void addVertexToRenderGroup_(Renderer *renderer, Texture *tex,
|
||||
/* If the textures match and have the same color modulation, we can
|
||||
* add these vertices to the current group */
|
||||
|
||||
b32 renderModeMatches = FALSE;
|
||||
if (group->mode == targetRenderMode) renderModeMatches = TRUE;
|
||||
if (!(group->mode == targetRenderMode)) continue;
|
||||
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 (v4_equals(group->color, color)) colorMatches = TRUE;
|
||||
|
||||
b32 flagsMatches = FALSE;
|
||||
if (group->flags == flags) flagsMatches = TRUE;
|
||||
|
||||
b32 texMatches = TRUE;
|
||||
if (!tex && !group->tex)
|
||||
if (tex && group->tex)
|
||||
{
|
||||
texMatches = TRUE;
|
||||
}
|
||||
else if (tex && group->tex)
|
||||
{
|
||||
if (group->tex->id == tex->id)
|
||||
{
|
||||
texMatches = TRUE;
|
||||
}
|
||||
if (!(group->tex->id == tex->id)) continue;
|
||||
}
|
||||
|
||||
if (texMatches && colorMatches && renderModeMatches && flagsMatches)
|
||||
groupIsValid = TRUE;
|
||||
groupIsValid = TRUE;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* New group, unused so initialise it */
|
||||
groupIsValid = TRUE;
|
||||
|
||||
group->init = TRUE;
|
||||
group->tex = tex;
|
||||
group->color = color;
|
||||
group->mode = targetRenderMode;
|
||||
group->flags = flags;
|
||||
group->init = TRUE;
|
||||
group->tex = tex;
|
||||
group->color = color;
|
||||
group->mode = targetRenderMode;
|
||||
group->flags = flags;
|
||||
group->zDepth = zDepth;
|
||||
|
||||
#ifdef DENGINE_DEBUG
|
||||
debug_countIncrement(debugcount_renderGroups);
|
||||
#endif
|
||||
renderer->groupsInUse++;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
/* Convert texture coordinates to normalised texture coordinates */
|
||||
@ -445,56 +419,6 @@ createDefaultTexQuad(Renderer *renderer, RenderTex *renderTex)
|
||||
|
||||
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)
|
||||
@ -505,8 +429,8 @@ RenderTex renderer_createNullRenderTex(AssetManager *const assetManager)
|
||||
}
|
||||
|
||||
void renderer_rect(Renderer *const renderer, Rect camera, v2 pos, v2 size,
|
||||
v2 pivotPoint, Radians rotate, RenderTex *renderTex, v4 color,
|
||||
RenderFlags flags)
|
||||
v2 pivotPoint, Radians rotate, RenderTex *renderTex,
|
||||
v4 color, i32 zDepth, RenderFlags flags)
|
||||
{
|
||||
// NOTE(doyle): Bottom left and top right position of quad in world space
|
||||
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);
|
||||
|
||||
beginVertexBatch(renderer);
|
||||
addVertexToRenderGroup_(renderer, renderTex->tex, color, quad.vertexList,
|
||||
ARRAY_COUNT(quad.vertexList), rendermode_quad,
|
||||
flags);
|
||||
addVertexToRenderGroup_(renderer, renderTex->tex, color, zDepth,
|
||||
quad.vertexList, ARRAY_COUNT(quad.vertexList),
|
||||
rendermode_quad, flags);
|
||||
endVertexBatch(renderer);
|
||||
}
|
||||
|
||||
void renderer_polygon(Renderer *const renderer, Rect camera,
|
||||
v2 *polygonPoints, i32 numPoints, v2 pivotPoint,
|
||||
Radians rotate, RenderTex *renderTex, v4 color,
|
||||
Radians rotate, RenderTex *renderTex, v4 color, i32 zDepth,
|
||||
RenderFlags flags)
|
||||
{
|
||||
ASSERT(numPoints >= 3);
|
||||
@ -614,9 +538,9 @@ void renderer_polygon(Renderer *const renderer, Rect camera,
|
||||
triangle[1].pos = vertexList[1];
|
||||
triangle[2].pos = vertexList[2];
|
||||
|
||||
addVertexToRenderGroup_(renderer, renderTex->tex, color, triangle,
|
||||
ARRAY_COUNT(triangle), rendermode_polygon,
|
||||
flags);
|
||||
addVertexToRenderGroup_(renderer, renderTex->tex, color, zDepth,
|
||||
triangle, ARRAY_COUNT(triangle),
|
||||
rendermode_polygon, flags);
|
||||
endVertexBatch(renderer);
|
||||
triangulationIndex++;
|
||||
}
|
||||
@ -624,7 +548,8 @@ void renderer_polygon(Renderer *const renderer, Rect camera,
|
||||
|
||||
void renderer_string(Renderer *const renderer, MemoryArena_ *arena, Rect camera,
|
||||
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);
|
||||
if (strLen <= 0) return;
|
||||
@ -673,7 +598,7 @@ void renderer_string(Renderer *const renderer, MemoryArena_ *arena, Rect camera,
|
||||
pivotPoint, rotate, renderTex);
|
||||
|
||||
beginVertexBatch(renderer);
|
||||
addVertexToRenderGroup_(renderer, tex, color, quad.vertexList,
|
||||
addVertexToRenderGroup_(renderer, tex, color, zDepth, quad.vertexList,
|
||||
ARRAY_COUNT(quad.vertexList),
|
||||
rendermode_quad, flags);
|
||||
endVertexBatch(renderer);
|
||||
@ -684,7 +609,7 @@ void renderer_string(Renderer *const renderer, MemoryArena_ *arena, Rect camera,
|
||||
|
||||
void renderer_entity(Renderer *renderer, MemoryArena_ *transientArena,
|
||||
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
|
||||
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,
|
||||
v2_add(entity->offset, pivotPoint), totalRotation,
|
||||
&renderTex, entity->color, flags);
|
||||
&renderTex, entity->color, zDepth, flags);
|
||||
}
|
||||
else if (entity->renderMode == rendermode_polygon)
|
||||
{
|
||||
@ -739,7 +664,7 @@ void renderer_entity(Renderer *renderer, MemoryArena_ *transientArena,
|
||||
renderer_polygon(renderer, camera, offsetVertexPoints,
|
||||
entity->numVertexPoints,
|
||||
v2_add(entity->offset, pivotPoint), totalRotation,
|
||||
&renderTex, renderColor, flags);
|
||||
&renderTex, renderColor, zDepth, flags);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -750,21 +675,105 @@ void renderer_entity(Renderer *renderer, MemoryArena_ *transientArena,
|
||||
// TODO(doyle): We have no notion of sort order!!
|
||||
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};
|
||||
cleanGroup.vertexList = currGroup->vertexList;
|
||||
*currGroup = cleanGroup;
|
||||
}
|
||||
else
|
||||
/* Sort the group by zdepth */
|
||||
b32 groupHasSwapped = TRUE;
|
||||
i32 numGroupsToCheck = renderer->groupsInUse - 1;
|
||||
while (groupHasSwapped)
|
||||
{
|
||||
groupHasSwapped = FALSE;
|
||||
for (i32 i = 0; i < numGroupsToCheck; i++)
|
||||
{
|
||||
break;
|
||||
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
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
29
src/Ui.c
29
src/Ui.c
@ -28,7 +28,7 @@ void ui_endState(UiState *state, InputBuffer *input)
|
||||
i32 ui_button(UiState *const uiState, MemoryArena_ *const arena,
|
||||
AssetManager *const assetManager, Renderer *const renderer,
|
||||
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))
|
||||
{
|
||||
@ -76,11 +76,12 @@ i32 ui_button(UiState *const uiState, MemoryArena_ *const arena,
|
||||
renderer_rectFixed(renderer,
|
||||
v2_add(V2(-2, -2), v2_add(buttonOffset, rect.min)),
|
||||
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,
|
||||
V2(0, 0), 0, NULL, buttonColor, renderflag_no_texture);
|
||||
V2(0, 0), 0, NULL, buttonColor, zDepth,
|
||||
renderflag_no_texture);
|
||||
|
||||
if (label)
|
||||
{
|
||||
@ -104,7 +105,7 @@ i32 ui_button(UiState *const uiState, MemoryArena_ *const arena,
|
||||
// button text into another render group
|
||||
labelPos = v2_add(labelPos, buttonOffset);
|
||||
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
|
||||
@ -145,7 +146,7 @@ i32 ui_button(UiState *const uiState, MemoryArena_ *const arena,
|
||||
i32 ui_scrollbar(UiState *const uiState, AssetManager *const assetManager,
|
||||
Renderer *const renderer, const InputBuffer input,
|
||||
const i32 id, const Rect scrollBarRect, i32 *const value,
|
||||
const i32 maxValue)
|
||||
const i32 maxValue, i32 zDepth)
|
||||
{
|
||||
#ifdef DENGINE_DEBUG
|
||||
ASSERT(*value <= maxValue);
|
||||
@ -176,12 +177,12 @@ i32 ui_scrollbar(UiState *const uiState, AssetManager *const assetManager,
|
||||
// Draw outline
|
||||
renderer_rectFixed(renderer, v2_add(V2(-2, -2), scrollBarRect.min),
|
||||
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
|
||||
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
|
||||
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);
|
||||
|
||||
renderer_rectFixed(renderer, sliderPos, sliderSize, V2(0, 0), 0, &renderTex,
|
||||
sliderColor, 0);
|
||||
sliderColor, zDepth, 0);
|
||||
|
||||
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,
|
||||
AssetManager *const assetManager, Renderer *const renderer,
|
||||
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);
|
||||
b32 changed = FALSE;
|
||||
@ -287,27 +288,27 @@ i32 ui_textfield(UiState *const uiState, MemoryArena_ *const arena,
|
||||
// Draw outline
|
||||
renderer_rectFixed(renderer, v2_add(V2(-2, -2), rect.min),
|
||||
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
|
||||
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)
|
||||
{
|
||||
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
|
||||
{
|
||||
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;
|
||||
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)
|
||||
{
|
||||
|
@ -39,6 +39,9 @@ typedef struct GameWorldState
|
||||
v2 *particleVertexCache;
|
||||
|
||||
v2 *starPList;
|
||||
f32 starOpacity;
|
||||
f32 starMinOpacity;
|
||||
b32 starFadeAway;
|
||||
i32 numStarP;
|
||||
|
||||
// TODO(doyle): Audio mixing instead of multiple renderers
|
||||
|
@ -46,6 +46,7 @@ typedef struct RenderGroup
|
||||
b32 init;
|
||||
RenderFlags flags;
|
||||
enum RenderMode mode;
|
||||
i32 zDepth;
|
||||
|
||||
// NOTE(doyle): Only for when adding singular triangles in triangle strip
|
||||
// mode
|
||||
@ -81,6 +82,7 @@ typedef struct Renderer
|
||||
v2 size;
|
||||
|
||||
RenderGroup groups[128];
|
||||
i32 groupsInUse;
|
||||
i32 groupCapacity;
|
||||
} Renderer;
|
||||
|
||||
@ -94,42 +96,44 @@ RenderTex renderer_createNullRenderTex(AssetManager *const assetManager);
|
||||
// Renderer::~Renderer() { glDeleteVertexArrays(1, &this->quadVAO); }
|
||||
void renderer_rect(Renderer *const renderer, Rect camera, v2 pos, v2 size,
|
||||
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,
|
||||
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,
|
||||
v2 pivotPoint, Radians rotate,
|
||||
RenderTex *renderTex, v4 color,
|
||||
RenderFlags flags)
|
||||
v2 pivotPoint, Radians rotate,
|
||||
RenderTex *renderTex, v4 color, i32 zDepth,
|
||||
RenderFlags flags)
|
||||
{
|
||||
Rect staticCamera = {V2(0, 0), renderer->size};
|
||||
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,
|
||||
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);
|
||||
|
||||
inline void renderer_stringFixed(Renderer *const renderer, MemoryArena_ *arena,
|
||||
Font *const font, const char *const string,
|
||||
v2 pos, v2 pivotPoint, Radians rotate,
|
||||
v4 color, RenderFlags flags)
|
||||
Font *const font, const char *const string,
|
||||
v2 pos, v2 pivotPoint, Radians rotate,
|
||||
v4 color, i32 zDepth, RenderFlags flags)
|
||||
{
|
||||
Rect staticCamera = {V2(0, 0), renderer->size};
|
||||
renderer_string(renderer, arena, staticCamera, font, string, pos,
|
||||
pivotPoint, rotate, color, flags);
|
||||
pivotPoint, rotate, color, zDepth, flags);
|
||||
}
|
||||
|
||||
inline void renderer_stringFixedCentered(Renderer *const renderer,
|
||||
MemoryArena_ *arena, Font *const font,
|
||||
const char *const string, v2 pos,
|
||||
v2 pivotPoint, Radians rotate,
|
||||
v4 color, RenderFlags flags)
|
||||
v4 color, i32 zDepth,
|
||||
enum RenderFlags flags)
|
||||
{
|
||||
Rect staticCamera = {V2(0, 0), renderer->size};
|
||||
|
||||
@ -138,12 +142,12 @@ inline void renderer_stringFixedCentered(Renderer *const renderer,
|
||||
pos = v2_sub(pos, halfDim);
|
||||
|
||||
renderer_string(renderer, arena, staticCamera, font, string, pos,
|
||||
pivotPoint, rotate, color, flags);
|
||||
pivotPoint, rotate, color, zDepth, flags);
|
||||
}
|
||||
|
||||
void renderer_entity(Renderer *renderer, MemoryArena_ *transientArena,
|
||||
Rect camera, Entity *entity, v2 pivotPoint, Degrees rotate,
|
||||
v4 color, RenderFlags flags);
|
||||
v4 color, i32 zDepth, RenderFlags flags);
|
||||
|
||||
void renderer_renderGroups(Renderer *renderer);
|
||||
|
||||
|
@ -64,15 +64,15 @@ void ui_endState(UiState *state, InputBuffer *input);
|
||||
i32 ui_button(UiState *const uiState, MemoryArena_ *const arena,
|
||||
AssetManager *const assetManager, Renderer *const renderer,
|
||||
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,
|
||||
AssetManager *const assetManager, Renderer *const renderer,
|
||||
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,
|
||||
Renderer *const renderer, const InputBuffer input,
|
||||
const i32 id, const Rect scrollBarRect, i32 *const value,
|
||||
const i32 maxValue);
|
||||
const i32 maxValue, i32 zDepth);
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user