frillrun/src/game/render/texture_quad.c
2024-08-24 00:47:58 -04:00

98 lines
2.6 KiB
C

#include "texture_quad.h"
void
texture_quad_draw
(
Texture* texture,
Renderer* renderer,
Shader* shader,
vec2 uvMin,
vec2 uvMax,
mat4 model,
mat4 view,
mat4 projection,
vec2 size,
vec4 color,
vec4 colorCover,
Flip flip
)
{
f32 vertices[4][5];
u32 indices[2][3] = {{1, 2, 3}, {0, 1, 3}};
f32 verticiesFlipNone[4][5] =
{
{0.0f, 0.0f, 0.0f, uvMin[0], uvMin[1]},
{0.0f, size[1], 0.0f, uvMin[0], uvMax[1]},
{size[0], size[1], 0.0f, uvMax[0], uvMax[1]},
{size[0], 0.0f, 0.0f, uvMax[0], uvMin[1]}
};
f32 verticiesFlipHorizontal[4][5] =
{
{size[0], 0.0f, 0.0f, uvMin[0], uvMin[1]},
{size[0], size[1], 0.0f, uvMin[0], uvMax[1]},
{0.0f, size[1], 0.0f, uvMax[0], uvMax[1]},
{0.0f, 0.0f, 0.0f, uvMax[0], uvMin[1]}
};
f32 verticiesFlipVertical[4][5] =
{
{size[0], size[1], 0.0f, uvMin[0], uvMin[1]},
{size[0], 0.0f, 0.0f, uvMin[0], uvMax[1]},
{0.0f, 0.0f, 0.0f, uvMax[0], uvMax[1]},
{0.0f, size[1], 0.0f, uvMax[0], uvMin[1]}
};
f32 verticiesFlipHorizontalVertical[4][5] =
{
{0.0f, size[1], 0.0f, uvMin[0], uvMin[1]},
{0.0f, 0.0f, 0.0f, uvMin[0], uvMax[1]},
{size[0], 0.0f, 0.0f, uvMax[0], uvMax[1]},
{size[0], size[1], 0.0f, uvMax[0], uvMin[1]}
};
switch (flip)
{
case FLIP_HORIZONTAL:
memcpy(vertices, verticiesFlipHorizontal, sizeof(vertices));
break;
case FLIP_VERTICAL:
memcpy(vertices, verticiesFlipVertical, sizeof(vertices));
break;
case FLIP_HORIZONTAL_VERTICAL:
memcpy(vertices, verticiesFlipHorizontalVertical, sizeof(vertices));
break;
case FLIP_NONE:
default:
memcpy(vertices, verticiesFlipNone, sizeof(vertices));
break;
}
vao_bind(&renderer->vao);
vbo_bind(&renderer->vbo);
vbo_buffer(&renderer->vbo, sizeof(vertices), vertices);
vbo_bind(&renderer->ebo);
vbo_buffer(&renderer->ebo, sizeof(indices), indices);
vertex_attribute_set(0, 3, GL_FLOAT, 5 * sizeof(f32), 0);
vertex_attribute_set(1, 2, GL_FLOAT, 5 * sizeof(f32), 3 * sizeof(f32));
renderer_shader_use(renderer, shader);
shader_uniform_mat4_set(shader, TEXTURE_QUAD_UNIFORM_PROJECTION, projection);
shader_uniform_mat4_set(shader, TEXTURE_QUAD_UNIFORM_VIEW, view);
shader_uniform_mat4_set(shader, TEXTURE_QUAD_UNIFORM_MODEL, model);
shader_uniform_vec2_set(shader, TEXTURE_QUAD_UNIFORM_SIZE, size);
shader_uniform_vec4_set(shader, TEXTURE_QUAD_UNIFORM_COLOR, color);
shader_uniform_vec4_set(shader, TEXTURE_QUAD_UNIFORM_COLOR_COVER, colorCover);
shader_uniform_texture_set(shader, TEXTURE_QUAD_UNIFORM_TEXTURE, texture, 0);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
vao_unbind();
}