Allow color modulation of sprites, refactor renderer init

This commit is contained in:
Doyle Thai 2016-07-09 16:08:03 +10:00
parent ea2656c0bd
commit ea50aeb44e
7 changed files with 57 additions and 40 deletions

Binary file not shown.

View File

@ -4,8 +4,9 @@ in vec2 texCoord;
out vec4 color; out vec4 color;
uniform sampler2D tex; uniform sampler2D tex;
uniform vec4 spriteColor;
void main() void main()
{ {
color = texture(tex, texCoord); color = spriteColor * texture(tex, texCoord);
} }

View File

@ -21,7 +21,7 @@ INTERNAL void updateBufferObject(Renderer *const renderer,
void renderer_string(Renderer *const renderer, Font *const font, void renderer_string(Renderer *const renderer, Font *const font,
const char *const string, v2 pos, f32 rotate, const char *const string, v2 pos, f32 rotate,
v3 color) v4 color)
{ {
i32 quadIndex = 0; i32 quadIndex = 0;
i32 strLen = common_strlen(string); i32 strLen = common_strlen(string);
@ -74,14 +74,14 @@ void renderer_debugString(Renderer *const renderer, Font *const font,
} }
f32 rotate = 0; f32 rotate = 0;
v3 color = V3(0, 0, 0); v4 color = V4(0, 0, 0, 1);
renderer_string(renderer, font, string, debugRenderer.stringPos, rotate, renderer_string(renderer, font, string, debugRenderer.stringPos, rotate,
color); color);
debugRenderer.stringPos.y -= (0.9f * asset_getVFontSpacing(font->metrics)); debugRenderer.stringPos.y -= (0.9f * asset_getVFontSpacing(font->metrics));
} }
void renderer_entity(Renderer *renderer, v4 cameraBounds, Entity *entity, f32 dt, f32 rotate, void renderer_entity(Renderer *renderer, v4 cameraBounds, Entity *entity, f32 dt, f32 rotate,
v3 color) v4 color)
{ {
// TODO(doyle): Batch into render groups // TODO(doyle): Batch into render groups
@ -124,7 +124,7 @@ void renderer_entity(Renderer *renderer, v4 cameraBounds, Entity *entity, f32 dt
} }
void renderer_object(Renderer *renderer, v2 pos, v2 size, f32 rotate, v3 color, void renderer_object(Renderer *renderer, v2 pos, v2 size, f32 rotate, v4 color,
Texture *tex) Texture *tex)
{ {
shader_use(renderer->shader); shader_use(renderer->shader);
@ -138,12 +138,10 @@ void renderer_object(Renderer *renderer, v2 pos, v2 size, f32 rotate, v3 color,
mat4 scaleMatrix = mat4_scale(size.x, size.y, 1.0f); mat4 scaleMatrix = mat4_scale(size.x, size.y, 1.0f);
mat4 model = mat4_mul(transMatrix, mat4_mul(rotateMatrix, scaleMatrix)); mat4 model = mat4_mul(transMatrix, mat4_mul(rotateMatrix, scaleMatrix));
//mat4 model = mat4_mul(transMatrix, rotateMatrix);
shader_uniformSetMat4fv(renderer->shader, "model", model); shader_uniformSetMat4fv(renderer->shader, "model", model);
glCheckError(); glCheckError();
// TODO(doyle): Unimplemented shader_uniformSetVec4f(renderer->shader, "spriteColor", color);
// this->shader->uniformSetVec3f("spriteColor", color);
#if RENDER_BOUNDING_BOX #if RENDER_BOUNDING_BOX
glBindVertexArray(renderer->vao); glBindVertexArray(renderer->vao);

View File

@ -38,4 +38,12 @@ void shader_uniformSetMat4fv(Shader *const shader, const GLchar *name,
glUniformMatrix4fv(uniformLoc, 1, GL_FALSE, data.e[0]); glUniformMatrix4fv(uniformLoc, 1, GL_FALSE, data.e[0]);
} }
void shader_uniformSetVec4f(Shader *const shader, const GLchar *name,
v4 data)
{
GLint uniformLoc = glGetUniformLocation(shader->id, name);
glUniform4f(uniformLoc, data.e[0], data.e[1], data.e[2], data.e[3]);
}
void shader_use(const Shader *const shader) { glUseProgram(shader->id); } void shader_use(const Shader *const shader) { glUseProgram(shader->id); }

View File

@ -48,36 +48,11 @@ INTERNAL void addAnim(Entity *entity, v4 *rects, i32 numRects, f32 duration)
entity->anim[entity->freeAnimIndex++] = result; entity->anim[entity->freeAnimIndex++] = result;
} }
void worldTraveller_gameInit(GameState *state, v2i windowSize) INTERNAL void rendererInit(GameState *state, v2i windowSize)
{ {
AssetManager *assetManager = &state->assetManager; AssetManager *assetManager = &state->assetManager;
/* Initialise assets */ Renderer *renderer = &state->renderer;
asset_loadTextureImage(assetManager, renderer->size = V2(CAST(f32) windowSize.x, CAST(f32) windowSize.y);
"data/textures/WorldTraveller/TerraSprite1024.png",
texlist_hero);
asset_loadTextureImage(assetManager,
"data/textures/WorldTraveller/Terrain.png",
texlist_terrain);
TexAtlas *terrainAtlas =
asset_getTextureAtlas(assetManager, texlist_terrain);
f32 atlasTileSize = 128.0f;
terrainAtlas->texRect[terraincoords_ground] =
V4(384.0f, 512.0f, 384.0f + atlasTileSize, 512.0f + atlasTileSize);
asset_loadShaderFiles(assetManager, "data/shaders/sprite.vert.glsl",
"data/shaders/sprite.frag.glsl", shaderlist_sprite);
asset_loadTTFont(assetManager, "C:/Windows/Fonts/Arialbd.ttf");
glCheckError();
state->state = state_active;
state->currWorldIndex = 0;
state->tileSize = 64;
/* Init renderer */
Renderer *renderer = &state->renderer;
renderer->size = V2(CAST(f32) windowSize.x, CAST(f32) windowSize.y);
// NOTE(doyle): Value to map a screen coordinate to NDC coordinate // NOTE(doyle): Value to map a screen coordinate to NDC coordinate
renderer->vertexNdcFactor = renderer->vertexNdcFactor =
V2(1.0f / renderer->size.w, 1.0f / renderer->size.h); V2(1.0f / renderer->size.w, 1.0f / renderer->size.h);
@ -111,6 +86,38 @@ void worldTraveller_gameInit(GameState *state, v2i windowSize)
glBindVertexArray(0); glBindVertexArray(0);
glCheckError(); glCheckError();
}
void worldTraveller_gameInit(GameState *state, v2i windowSize)
{
AssetManager *assetManager = &state->assetManager;
/* Initialise assets */
asset_loadTextureImage(assetManager,
"data/textures/WorldTraveller/TerraSprite1024.png",
texlist_hero);
asset_loadTextureImage(assetManager,
"data/textures/WorldTraveller/Terrain.png",
texlist_terrain);
TexAtlas *terrainAtlas =
asset_getTextureAtlas(assetManager, texlist_terrain);
f32 atlasTileSize = 128.0f;
terrainAtlas->texRect[terraincoords_ground] =
V4(384.0f, 512.0f, 384.0f + atlasTileSize, 512.0f + atlasTileSize);
asset_loadShaderFiles(assetManager, "data/shaders/sprite.vert.glsl",
"data/shaders/sprite.frag.glsl", shaderlist_sprite);
asset_loadTTFont(assetManager, "C:/Windows/Fonts/Arialbd.ttf");
glCheckError();
state->state = state_active;
state->currWorldIndex = 0;
state->tileSize = 64;
/* Init renderer */
rendererInit(state, windowSize);
/* Init world */ /* Init world */
const i32 targetWorldWidth = 500 * METERS_TO_PIXEL; const i32 targetWorldWidth = 500 * METERS_TO_PIXEL;
const i32 targetWorldHeight = 15 * METERS_TO_PIXEL; const i32 targetWorldHeight = 15 * METERS_TO_PIXEL;
@ -167,6 +174,7 @@ void worldTraveller_gameInit(GameState *state, v2i windowSize)
/* Init hero entity */ /* Init hero entity */
world->heroIndex = world->freeEntityIndex; world->heroIndex = world->freeEntityIndex;
Renderer *renderer = &state->renderer;
v2 size = V2(58.0f, 98.0f); v2 size = V2(58.0f, 98.0f);
v2 pos = V2(((renderer->size.w * 0.5f) - (size.w * 0.5f)), v2 pos = V2(((renderer->size.w * 0.5f) - (size.w * 0.5f)),
CAST(f32) state->tileSize); CAST(f32) state->tileSize);
@ -401,7 +409,7 @@ void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt)
{ {
Entity *const entity = &world->entities[i]; Entity *const entity = &world->entities[i];
renderer_entity(&state->renderer, cameraBounds, entity, dt, 0.0f, renderer_entity(&state->renderer, cameraBounds, entity, dt, 0.0f,
V3(0, 0, 0)); V4(1, 1, 1, 1));
} }
// TODO(doyle): Clean up lines // TODO(doyle): Clean up lines

View File

@ -35,15 +35,15 @@ void renderer_backgroundTiles(Renderer *const renderer, const v2 tileSize,
#endif #endif
void renderer_string(Renderer *const renderer, Font *const font, void renderer_string(Renderer *const renderer, Font *const font,
const char *const string, v2 pos, f32 rotate, v3 color); const char *const string, v2 pos, f32 rotate, v4 color);
void renderer_debugString(Renderer *const renderer, Font *const font, void renderer_debugString(Renderer *const renderer, Font *const font,
const char *const string); const char *const string);
void renderer_entity(Renderer *renderer, v4 cameraBounds, Entity *entity, void renderer_entity(Renderer *renderer, v4 cameraBounds, Entity *entity,
f32 dt, f32 rotate, v3 color); f32 dt, f32 rotate, v4 color);
void renderer_object(Renderer *renderer, v2 pos, v2 size, f32 rotate, v3 color, void renderer_object(Renderer *renderer, v2 pos, v2 size, f32 rotate, v4 color,
Texture *tex); Texture *tex);
RenderQuad renderer_createQuad(Renderer *renderer, v4 quadRect, v4 texRect, RenderQuad renderer_createQuad(Renderer *renderer, v4 quadRect, v4 texRect,

View File

@ -17,6 +17,8 @@ void shader_uniformSet1i(Shader *const shader, const GLchar *name,
const GLuint data); const GLuint data);
void shader_uniformSetMat4fv(Shader *const shader, const GLchar *name, void shader_uniformSetMat4fv(Shader *const shader, const GLchar *name,
mat4 data); mat4 data);
void shader_uniformSetVec4f(Shader *const shader, const GLchar *name,
v4 data);
void shader_use(const Shader *const shader); void shader_use(const Shader *const shader);