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/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)
{
/* 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++)
{
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;
if (numVertexes < freeVertexSlots)
{
if (!group->tex)
{
// NOTE(doyle): Mark first vertex as degenerate vertex
group->vertexIndex++;
group->tex = texture;
}
targetGroup = &renderer->groups[i];
break;
}
@ -59,7 +75,7 @@ INTERNAL void addVertexToRenderGroup(Renderer *renderer, Texture *texture,
INTERNAL inline void addRenderQuadToRenderGroup(Renderer *renderer,
RenderQuad_ quad,
RenderTex renderTex)
Texture *tex, v4 color)
{
/*
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],
quad.vertex[2], quad.vertex[3], quad.vertex[3]};
addVertexToRenderGroup(renderer, renderTex.tex, vertexList,
addVertexToRenderGroup(renderer, tex, color, vertexList,
ARRAY_COUNT(vertexList));
};
@ -213,15 +229,15 @@ createDefaultTexQuad(Renderer *renderer, RenderTex renderTex)
return result;
}
INTERNAL void renderObject(Renderer *renderer, v2 pos, v2 size, v2 pivotPoint,
f32 rotate, v4 color, Texture *tex)
INTERNAL void renderGLBufferedData(Renderer *renderer, RenderGroup *renderGroup)
{
/* Load transformation matrix */
shader_use(renderer->shader);
GL_CHECK_ERROR();
/* Set color modulation value */
shader_uniformSetVec4f(renderer->shader, "spriteColor", color);
shader_uniformSetVec4f(renderer->shader, "spriteColor",
renderGroup->color);
/* Send draw calls */
#if RENDER_BOUNDING_BOX
@ -230,6 +246,7 @@ INTERNAL void renderObject(Renderer *renderer, v2 pos, v2 size, v2 pivotPoint,
glBindVertexArray(0);
#endif
Texture *tex = renderGroup->tex;
if (tex)
{
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);
RenderQuad_ quad = createRenderQuad(renderer, posInCameraSpace, size,
pivotPoint, rotate, renderTex);
addRenderQuadToRenderGroup(renderer, quad, renderTex);
addRenderQuadToRenderGroup(renderer, quad, renderTex.tex, color);
}
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;
}
addVertexToRenderGroup(renderer, tex, vertexList, numVertexesToAlloc);
addVertexToRenderGroup(renderer, tex, color, vertexList,
numVertexesToAlloc);
PLATFORM_MEM_FREE(arena, vertexList,
sizeof(Vertex) * numVertexesToAlloc);
}
@ -374,12 +392,15 @@ void renderer_renderGroups(Renderer *renderer)
if (currGroup->tex)
{
bufferRenderGroupToGL(renderer, currGroup);
renderObject(renderer, V2(0.0f, 0.0f), renderer->size, V2(0, 0),
0, V4(1, 1, 1, 1), currGroup->tex);
renderGLBufferedData(renderer, currGroup);
RenderGroup cleanGroup = {0};
cleanGroup.vertexList = currGroup->vertexList;
*currGroup = cleanGroup;
}
else
{
break;
}
}
}

View File

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

View File

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