Restore color modulation in render groups

This commit is contained in:
Doyle Thai 2016-09-23 19:19:47 +10:00
parent 3894d33485
commit 72d30b6320
3 changed files with 55 additions and 35 deletions

View File

@ -8,9 +8,9 @@
#include "Dengine/Shader.h" #include "Dengine/Shader.h"
#include "Dengine/Texture.h" #include "Dengine/Texture.h"
#define RENDER_BOUNDING_BOX TRUE #define RENDER_BOUNDING_BOX FALSE
INTERNAL void addVertexToRenderGroup(Renderer *renderer, Texture *texture, INTERNAL void addVertexToRenderGroup(Renderer *renderer, Texture *tex, v4 color,
Vertex *vertexList, i32 numVertexes) Vertex *vertexList, i32 numVertexes)
{ {
/* Find vacant/matching render group */ /* Find vacant/matching render group */
@ -18,18 +18,34 @@ INTERNAL void addVertexToRenderGroup(Renderer *renderer, Texture *texture,
for (i32 i = 0; i < ARRAY_COUNT(renderer->groups); i++) for (i32 i = 0; i < ARRAY_COUNT(renderer->groups); i++)
{ {
RenderGroup *group = &renderer->groups[i]; RenderGroup *group = &renderer->groups[i];
if (group->tex == NULL || group->tex->id == texture->id) b32 groupIsValid = FALSE;
if (group->tex)
{
/* If the textures match and have the same color modulation, we can
* add these vertices to the current group */
if (group->tex->id == tex->id &&
v4_equals(group->color, color))
{
groupIsValid = TRUE;
}
}
else
{
/* New group, unused so initialise it */
groupIsValid = TRUE;
// NOTE(doyle): Mark first vertex as degenerate vertex
group->vertexIndex++;
group->tex = tex;
group->color = color;
}
if (groupIsValid)
{ {
i32 freeVertexSlots = renderer->groupCapacity - group->vertexIndex; i32 freeVertexSlots = renderer->groupCapacity - group->vertexIndex;
if (numVertexes < freeVertexSlots) if (numVertexes < freeVertexSlots)
{ {
if (!group->tex)
{
// NOTE(doyle): Mark first vertex as degenerate vertex
group->vertexIndex++;
group->tex = texture;
}
targetGroup = &renderer->groups[i]; targetGroup = &renderer->groups[i];
break; break;
} }
@ -59,7 +75,7 @@ INTERNAL void addVertexToRenderGroup(Renderer *renderer, Texture *texture,
INTERNAL inline void addRenderQuadToRenderGroup(Renderer *renderer, INTERNAL inline void addRenderQuadToRenderGroup(Renderer *renderer,
RenderQuad_ quad, RenderQuad_ quad,
RenderTex renderTex) Texture *tex, v4 color)
{ {
/* /*
NOTE(doyle): Entity rendering is always done in two pairs of NOTE(doyle): Entity rendering is always done in two pairs of
@ -84,7 +100,7 @@ INTERNAL inline void addRenderQuadToRenderGroup(Renderer *renderer,
*/ */
Vertex vertexList[6] = {quad.vertex[0], quad.vertex[0], quad.vertex[1], Vertex vertexList[6] = {quad.vertex[0], quad.vertex[0], quad.vertex[1],
quad.vertex[2], quad.vertex[3], quad.vertex[3]}; quad.vertex[2], quad.vertex[3], quad.vertex[3]};
addVertexToRenderGroup(renderer, renderTex.tex, vertexList, addVertexToRenderGroup(renderer, tex, color, vertexList,
ARRAY_COUNT(vertexList)); ARRAY_COUNT(vertexList));
}; };
@ -213,15 +229,15 @@ createDefaultTexQuad(Renderer *renderer, RenderTex renderTex)
return result; return result;
} }
INTERNAL void renderObject(Renderer *renderer, v2 pos, v2 size, v2 pivotPoint, INTERNAL void renderGLBufferedData(Renderer *renderer, RenderGroup *renderGroup)
f32 rotate, v4 color, Texture *tex)
{ {
/* Load transformation matrix */ /* Load transformation matrix */
shader_use(renderer->shader); shader_use(renderer->shader);
GL_CHECK_ERROR(); GL_CHECK_ERROR();
/* Set color modulation value */ /* Set color modulation value */
shader_uniformSetVec4f(renderer->shader, "spriteColor", color); shader_uniformSetVec4f(renderer->shader, "spriteColor",
renderGroup->color);
/* Send draw calls */ /* Send draw calls */
#if RENDER_BOUNDING_BOX #if RENDER_BOUNDING_BOX
@ -230,6 +246,7 @@ INTERNAL void renderObject(Renderer *renderer, v2 pos, v2 size, v2 pivotPoint,
glBindVertexArray(0); glBindVertexArray(0);
#endif #endif
Texture *tex = renderGroup->tex;
if (tex) if (tex)
{ {
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
@ -264,7 +281,7 @@ void renderer_rect(Renderer *const renderer, Rect camera, v2 pos, v2 size,
v2 posInCameraSpace = v2_sub(pos, camera.pos); v2 posInCameraSpace = v2_sub(pos, camera.pos);
RenderQuad_ quad = createRenderQuad(renderer, posInCameraSpace, size, RenderQuad_ quad = createRenderQuad(renderer, posInCameraSpace, size,
pivotPoint, rotate, renderTex); pivotPoint, rotate, renderTex);
addRenderQuadToRenderGroup(renderer, quad, renderTex); addRenderQuadToRenderGroup(renderer, quad, renderTex.tex, color);
} }
void renderer_string(Renderer *const renderer, MemoryArena *arena, Rect camera, void renderer_string(Renderer *const renderer, MemoryArena *arena, Rect camera,
@ -325,7 +342,8 @@ void renderer_string(Renderer *const renderer, MemoryArena *arena, Rect camera,
pos.x += metric.advance; pos.x += metric.advance;
} }
addVertexToRenderGroup(renderer, tex, vertexList, numVertexesToAlloc); addVertexToRenderGroup(renderer, tex, color, vertexList,
numVertexesToAlloc);
PLATFORM_MEM_FREE(arena, vertexList, PLATFORM_MEM_FREE(arena, vertexList,
sizeof(Vertex) * numVertexesToAlloc); sizeof(Vertex) * numVertexesToAlloc);
} }
@ -374,12 +392,15 @@ void renderer_renderGroups(Renderer *renderer)
if (currGroup->tex) if (currGroup->tex)
{ {
bufferRenderGroupToGL(renderer, currGroup); bufferRenderGroupToGL(renderer, currGroup);
renderObject(renderer, V2(0.0f, 0.0f), renderer->size, V2(0, 0), renderGLBufferedData(renderer, currGroup);
0, V4(1, 1, 1, 1), currGroup->tex);
RenderGroup cleanGroup = {0}; RenderGroup cleanGroup = {0};
cleanGroup.vertexList = currGroup->vertexList; cleanGroup.vertexList = currGroup->vertexList;
*currGroup = cleanGroup; *currGroup = cleanGroup;
} }
else
{
break;
}
} }
} }

View File

@ -69,10 +69,9 @@ i32 userInterface_button(UiState *const uiState,
if (uiState->kbdItem == id) if (uiState->kbdItem == id)
{ {
// Draw outline // Draw outline
renderer_staticRect(renderer, renderer_staticRect(
v2_add(V2(-2, -2), v2_add(buttonOffset, rect.pos)), renderer, v2_add(V2(-2, -2), v2_add(buttonOffset, rect.pos)),
v2_add(V2(4, 4), rect.size), V2(0, 0), 0, renderTex, v2_add(V2(4, 4), rect.size), V2(0, 0), 0, renderTex, buttonColor);
V4(1.0f, 0, 0, 1));
} }
renderer_staticRect(renderer, v2_add(buttonOffset, rect.pos), rect.size, renderer_staticRect(renderer, v2_add(buttonOffset, rect.pos), rect.size,
@ -172,7 +171,7 @@ i32 userInterface_scrollbar(UiState *const uiState,
// Draw outline // Draw outline
renderer_staticRect(renderer, v2_add(V2(-2, -2), scrollBarRect.pos), renderer_staticRect(renderer, v2_add(V2(-2, -2), scrollBarRect.pos),
v2_add(V2(4, 4), scrollBarRect.size), V2(0, 0), 0, v2_add(V2(4, 4), scrollBarRect.size), V2(0, 0), 0,
renderTex, V4(1.0f, 0, 0, 1)); renderTex, V4(1, 0, 0, 1));
} }
// Render scroll bar background // Render scroll bar background

View File

@ -18,21 +18,21 @@ typedef struct Vertex
v2 texCoord; v2 texCoord;
} Vertex; } Vertex;
typedef struct RenderTex
{
Texture *tex;
v4 texRect;
} RenderTex;
typedef struct RenderQuad typedef struct RenderQuad
{ {
Vertex vertex[4]; Vertex vertex[4];
} RenderQuad_; } RenderQuad_;
typedef struct RenderTex
{
Texture *tex;
// TODO(doyle): Switch to rect
v4 texRect;
} RenderTex;
typedef struct RenderGroup typedef struct RenderGroup
{ {
Texture *tex; Texture *tex;
v4 color;
Vertex *vertexList; Vertex *vertexList;
i32 vertexIndex; i32 vertexIndex;
@ -47,7 +47,7 @@ typedef struct Renderer
v2 vertexNdcFactor; v2 vertexNdcFactor;
v2 size; v2 size;
RenderGroup groups[16]; RenderGroup groups[128];
i32 groupCapacity; i32 groupCapacity;
} Renderer; } Renderer;