Group texture & texture rect together in renderer

This commit is contained in:
Doyle Thai 2016-07-16 20:21:49 +10:00
parent 0d5725eb97
commit 2d9fded235
3 changed files with 30 additions and 24 deletions

View File

@ -18,8 +18,8 @@ INTERNAL void updateBufferObject(Renderer *const renderer,
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
} }
INTERNAL RenderQuad createTexQuad(Renderer *renderer, v4 quadRect, v4 texRect, INTERNAL RenderQuad createTexQuad(Renderer *renderer, v4 quadRect,
Texture *tex) RenderTex renderTex)
{ {
// NOTE(doyle): Draws a series of triangles (three-sided polygons) using // NOTE(doyle): Draws a series of triangles (three-sided polygons) using
// vertices v0, v1, v2, then v2, v1, v3 (note the order) // vertices v0, v1, v2, then v2, v1, v3 (note the order)
@ -33,10 +33,11 @@ INTERNAL RenderQuad createTexQuad(Renderer *renderer, v4 quadRect, v4 texRect,
quadRectNdc.e[3] *= renderer->vertexNdcFactor.h; quadRectNdc.e[3] *= renderer->vertexNdcFactor.h;
/* Convert texture coordinates to normalised texture coordinates */ /* Convert texture coordinates to normalised texture coordinates */
v4 texRectNdc = texRect; v4 texRectNdc = renderTex.texRect;
if (tex) if (renderTex.tex)
{ {
v2 texNdcFactor = V2(1.0f / tex->width, 1.0f / tex->height); v2 texNdcFactor =
V2(1.0f / renderTex.tex->width, 1.0f / renderTex.tex->height);
texRectNdc.e[0] *= texNdcFactor.w; texRectNdc.e[0] *= texNdcFactor.w;
texRectNdc.e[1] *= texNdcFactor.h; texRectNdc.e[1] *= texNdcFactor.h;
texRectNdc.e[2] *= texNdcFactor.w; texRectNdc.e[2] *= texNdcFactor.w;
@ -55,20 +56,12 @@ INTERNAL RenderQuad createTexQuad(Renderer *renderer, v4 quadRect, v4 texRect,
return result; return result;
} }
INTERNAL inline RenderQuad createQuad(Renderer *renderer, v4 quadRect)
{
v4 texRect = V4(0, 0, 0, 0);
RenderQuad result =
createTexQuad(renderer, quadRect, texRect, NULL);
return result;
}
INTERNAL inline RenderQuad INTERNAL inline RenderQuad
createDefaultTexQuad(Renderer *renderer, v4 texRect, Texture *tex) createDefaultTexQuad(Renderer *renderer, RenderTex renderTex)
{ {
RenderQuad result = {0}; RenderQuad result = {0};
v4 defaultQuad = V4(0.0f, renderer->size.h, renderer->size.w, 0.0f); v4 defaultQuad = V4(0.0f, renderer->size.h, renderer->size.w, 0.0f);
result = createTexQuad(renderer, defaultQuad, texRect, tex); result = createTexQuad(renderer, defaultQuad, renderTex);
return result; return result;
} }
@ -125,15 +118,16 @@ INTERNAL void renderObject(Renderer *renderer, v2 pos, v2 size, f32 rotate,
} }
void renderer_rect(Renderer *const renderer, v4 cameraBounds, v2 pos, v2 size, void renderer_rect(Renderer *const renderer, v4 cameraBounds, v2 pos, v2 size,
f32 rotate, Texture *tex, v4 texRect, v4 color) f32 rotate, RenderTex renderTex, v4 color)
{ {
RenderQuad quad = createDefaultTexQuad(renderer, texRect, tex); RenderQuad quad = createDefaultTexQuad(renderer, renderTex);
updateBufferObject(renderer, &quad, 1); updateBufferObject(renderer, &quad, 1);
// NOTE(doyle): Get the origin of cameraBounds in world space, bottom left // NOTE(doyle): Get the origin of cameraBounds in world space, bottom left
v2 offsetFromCamOrigin = V2(cameraBounds.x, cameraBounds.w); v2 offsetFromCamOrigin = V2(cameraBounds.x, cameraBounds.w);
v2 rectRelativeToCamera = v2_sub(pos, offsetFromCamOrigin); v2 rectRelativeToCamera = v2_sub(pos, offsetFromCamOrigin);
renderObject(renderer, rectRelativeToCamera, size, rotate, color, tex); renderObject(renderer, rectRelativeToCamera, size, rotate, color,
renderTex.tex);
} }
void renderer_string(Renderer *const renderer, v4 cameraBounds, void renderer_string(Renderer *const renderer, v4 cameraBounds,
@ -181,9 +175,10 @@ void renderer_string(Renderer *const renderer, v4 cameraBounds,
/* Get texture out */ /* Get texture out */
v4 charTexRect = font->atlas->texRect[relativeIndex]; v4 charTexRect = font->atlas->texRect[relativeIndex];
renderer_flipTexCoord(&charTexRect, FALSE, TRUE); renderer_flipTexCoord(&charTexRect, FALSE, TRUE);
RenderTex renderTex = {font->tex, charTexRect};
RenderQuad charQuad = createTexQuad(renderer, charRectOnScreen, RenderQuad charQuad =
charTexRect, font->tex); createTexQuad(renderer, charRectOnScreen, renderTex);
stringQuads[quadIndex++] = charQuad; stringQuads[quadIndex++] = charQuad;
} }
@ -227,8 +222,9 @@ void renderer_entity(Renderer *renderer, v4 cameraBounds, Entity *entity,
// NOTE(doyle): Flip the x coordinates to flip the tex // NOTE(doyle): Flip the x coordinates to flip the tex
renderer_flipTexCoord(&texRect, TRUE, FALSE); renderer_flipTexCoord(&texRect, TRUE, FALSE);
} }
RenderTex renderTex = {entity->tex, texRect};
RenderQuad entityQuad = RenderQuad entityQuad =
createDefaultTexQuad(renderer, texRect, entity->tex); createDefaultTexQuad(renderer, renderTex);
updateBufferObject(renderer, &entityQuad, 1); updateBufferObject(renderer, &entityQuad, 1);
v2 offsetFromCamOrigin = V2(cameraBounds.x, cameraBounds.w); v2 offsetFromCamOrigin = V2(cameraBounds.x, cameraBounds.w);

View File

@ -500,8 +500,10 @@ void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt)
Texture *emptyTex = Texture *emptyTex =
asset_getTexture(assetManager, texlist_empty); asset_getTexture(assetManager, texlist_empty);
v2 heroCenter = v2_add(hero->pos, v2_scale(hero->size, 0.5f)); v2 heroCenter = v2_add(hero->pos, v2_scale(hero->size, 0.5f));
RenderTex renderTex = {emptyTex, V4(0, 1, 1, 0)};
renderer_rect(renderer, cameraBounds, heroCenter, renderer_rect(renderer, cameraBounds, heroCenter,
V2(distance, 10.0f), 0, emptyTex, V4(0, 1, 1, 0), V2(distance, 10.0f), 0, renderTex,
V4(1, 0, 0, 0.5f)); V4(1, 0, 0, 0.5f));
} }
} }
@ -574,8 +576,10 @@ void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt)
v2 heroAvatarSize = math_getRectSize(heroAvatarTexRect); v2 heroAvatarSize = math_getRectSize(heroAvatarTexRect);
v2 heroAvatarP = v2 heroAvatarP =
V2(10.0f, (renderer->size.h * 0.5f) - (0.5f * heroAvatarSize.h)); V2(10.0f, (renderer->size.h * 0.5f) - (0.5f * heroAvatarSize.h));
RenderTex heroRenderTex = {hero->tex, heroAvatarTexRect};
renderer_rect(renderer, cameraBounds, heroAvatarP, heroAvatarSize, 0, renderer_rect(renderer, cameraBounds, heroAvatarP, heroAvatarSize, 0,
hero->tex, heroAvatarTexRect, V4(1, 1, 1, 1)); heroRenderTex, V4(1, 1, 1, 1));
v4 color = V4(0, 0, 1.0f, 1); v4 color = V4(0, 0, 1.0f, 1);
char *heroAvatarStr = "HP: 100/100"; char *heroAvatarStr = "HP: 100/100";

View File

@ -14,6 +14,12 @@ typedef struct Renderer
v2 size; v2 size;
} Renderer; } Renderer;
typedef struct RenderTex
{
Texture *tex;
v4 texRect;
} RenderTex;
typedef struct RenderQuad typedef struct RenderQuad
{ {
v4 vertex[4]; v4 vertex[4];
@ -23,7 +29,7 @@ typedef struct RenderQuad
// Renderer::~Renderer() { glDeleteVertexArrays(1, &this->quadVAO); } // Renderer::~Renderer() { glDeleteVertexArrays(1, &this->quadVAO); }
void renderer_rect(Renderer *const renderer, v4 cameraBounds, v2 pos, v2 size, void renderer_rect(Renderer *const renderer, v4 cameraBounds, v2 pos, v2 size,
f32 rotate, Texture *tex, v4 texRect, v4 color); f32 rotate, RenderTex renderTex, v4 color);
void renderer_string(Renderer *const renderer, v4 cameraBounds, void renderer_string(Renderer *const renderer, v4 cameraBounds,
Font *const font, const char *const string, v2 pos, Font *const font, const char *const string, v2 pos,