commit df0c6303e21e671f55372b148d0d0753022f1cae
parent 8a8349d0e50d68a9c80b8ef6fdbbc68739205412
Author: amin <dev@aminmesbah.com>
Date: Thu, 4 Jul 2019 19:28:31 +0000
Render a quad with a test XOR texture
FossilOrigin-Name: e677daee49d7a65402d64f3f1b79af2e9e49468d9cdb22836ee5b475b0f2c2ea
Diffstat:
6 files changed, 80 insertions(+), 10 deletions(-)
diff --git a/shader/main_f.glsl b/shader/main_f.glsl
@@ -6,10 +6,14 @@ precision highp float;
// TODO: dynamically instert the version header after loading the file
in vec4 vertex_color;
+in vec2 tex_coord;
out vec4 frag_color;
+uniform float interp;
+uniform sampler2D main_texture;
+
void main()
{
- frag_color = vertex_color;
+ frag_color = mix(vertex_color, texture(main_texture, tex_coord), interp);
}
diff --git a/shader/main_v.glsl b/shader/main_v.glsl
@@ -3,8 +3,10 @@
// TODO: dynamically instert the version header after loading the file
layout (location = 0) in vec2 position;
+layout (location = 1) in vec2 a_tex_coords;
out vec4 vertex_color;
+out vec2 tex_coord;
uniform mat4 model;
uniform mat4 view;
@@ -15,4 +17,5 @@ void main()
{
gl_Position = projection * view * model * vec4(position, 0.0f, 1.0f);
vertex_color = vec4(color, 1.0f);
+ tex_coord = a_tex_coords;
}
diff --git a/src/game.c b/src/game.c
@@ -167,8 +167,6 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer)
}
}
- renderer_init(&game_state->renderer, &game_state->world_allocator);
-
// game_shader_load
{
struct PlatformApi platform = game_memory->platform;
@@ -183,6 +181,8 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer)
platform.platform_memory_free(f_source);
game_state->renderer.shader = main_shader;
}
+
+ renderer_init(&game_state->renderer, &game_state->world_allocator);
}
// NOTE(amin): For now updating and rendering are interleaved. We simulate the
@@ -192,6 +192,7 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
{
assert(sizeof(struct GameState) <= game_memory->buffer_size);
struct GameState *game_state = (struct GameState *)game_memory->buffer;
+ f32 dt = game_input->dt;
rect viewport = {0};
f32 ppm = 0.0f;
@@ -224,6 +225,7 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
m4 view = math_m4_init_id();
// World origin is in the lower left
+ //view = math_scale(view, (v3) {1.0f, -1.0f, 1.0f});
view = math_translate(view, (v3) {0.0f, framebuffer.height, 0.0f});
view = math_scale(view, (v3) {1.0f, -1.0f, 1.0f});
view = math_translate(view, (v3) {viewport.min.x, viewport.min.y, 0.0f});
@@ -252,7 +254,6 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
{
struct Entity *player = &game_state->player;
move_mode_print(player->move_mode);
- f32 dt = game_input->dt;
u32 button_states = game_input->button_states;
if (dt >= 0.5f)
{
@@ -589,6 +590,28 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
renderer_jobs_sort(&game_state->renderer, &game_state->world_allocator);
renderer_jobs_draw(&game_state->renderer);
+
+ // texture test
+ {
+ m4 model = math_m4_init_id();
+ model = math_translate(model, (v3) {12.5f, 5.0f, 0.0f});
+ model = math_scale(model, (v3) {10.0f, 10.0f, 10.0f});
+ static f32 bork = 0;
+ bork += dt;
+ f32 scale_factor = fabsf(1.0f * sinf(bork));
+ model = math_scale(model, (v3) {scale_factor, scale_factor, 0.0f});
+
+ shader_setm4(&game_state->renderer.shader, SHADER_UNIFORM_MODEL, &model);
+ shader_setm4(&game_state->renderer.shader, SHADER_UNIFORM_PROJECTION, &game_state->renderer.projection);
+ shader_setm4(&game_state->renderer.shader, SHADER_UNIFORM_VIEW, &game_state->renderer.view);
+ v3 color = {1.0f, 1.0f, 1.0f};
+ shader_setv3(&game_state->renderer.shader, SHADER_UNIFORM_COLOR, &color);
+ shader_setf(&game_state->renderer.shader, SHADER_UNIFORM_INTERP, 1.0f);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, game_state->renderer.quad_ebo);
+ glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
+
+ shader_setf(&game_state->renderer.shader, SHADER_UNIFORM_INTERP, 0.0f);
+ }
}
internal void game_cleanup(struct GameMemory *game_memory)
diff --git a/src/render.c b/src/render.c
@@ -1,10 +1,10 @@
internal void renderer_init(struct RendererState *renderer, struct StackAllocator *allocator)
{
GLfloat quad_vertices[] = {
- 0.5f, 0.5f,
- 0.5f, -0.5f,
- -0.5f, -0.5f,
- -0.5f, 0.5f,
+ 0.5f, 0.5f, 1.0f, 1.0f,
+ 0.5f, -0.5f, 1.0f, 0.0f,
+ -0.5f, -0.5f, 0.0f, 0.0f,
+ -0.5f, 0.5f, 0.0f, 1.0f,
};
GLuint quad_elements[] = { 0, 1, 3, 1, 2, 3 };
@@ -30,11 +30,15 @@ internal void renderer_init(struct RendererState *renderer, struct StackAllocato
2, // Number of values
GL_FLOAT, // Data type
GL_FALSE, // Normalize data
- 2 * sizeof(GLfloat), // Stride
+ 4 * sizeof(GLfloat), // Stride
(GLvoid*)0 // Position data offset (0)
);
-
glEnableVertexAttribArray(0);
+
+ // texture coordinates
+ glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)(2 * sizeof(GLfloat)));
+ glEnableVertexAttribArray(1);
+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(quad_elements), quad_elements, GL_STATIC_DRAW);
@@ -49,6 +53,38 @@ internal void renderer_init(struct RendererState *renderer, struct StackAllocato
renderer->rect_ebo = rect_ebo;
renderer->queue = mem_st_alloc_buffer(allocator, struct RenderJob, RENDER_QUEUE_SIZE);
renderer->queue_count = 0;
+
+ u32 texture_id;
+ {
+ // TODO: flip textures
+#define TEX_DIM 32
+ u32 test_texture[TEX_DIM][TEX_DIM];
+
+ for (u32 y = 0; y < TEX_DIM; ++y)
+ {
+ for (u32 x = 0; x < TEX_DIM; ++x)
+ {
+ u32 xor_value = (x * 256 / TEX_DIM) ^ (y * 256 / TEX_DIM);
+ u32 r = xor_value << 16;
+ u32 g = xor_value << 8;
+ u32 b = xor_value;
+ test_texture[y][x] = r | g | b;
+ printf("%X\n", test_texture[y][x]);
+ }
+ }
+
+ glGenTextures(1, &texture_id);
+ glBindTexture(GL_TEXTURE_2D, texture_id);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEX_DIM, TEX_DIM, 0, GL_RGBA, GL_UNSIGNED_BYTE, test_texture);
+ glGenerateMipmap(GL_TEXTURE_2D);
+
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ glActiveTexture(GL_TEXTURE0);
+#undef TEX_DIM
+ }
}
internal void renderer_jobs_draw(struct RendererState *renderer)
diff --git a/src/shader.c b/src/shader.c
@@ -168,6 +168,9 @@ char *shader_uniform_get_name(enum ShaderUniform u)
case SHADER_UNIFORM_COLOR:
name = "color";
break;
+ case SHADER_UNIFORM_INTERP:
+ name = "interp";
+ break;
case NUM_SHADER_UNIFORMS:
// fallthrough
default:
diff --git a/src/shader.h b/src/shader.h
@@ -4,6 +4,7 @@ enum ShaderUniform
SHADER_UNIFORM_VIEW,
SHADER_UNIFORM_PROJECTION,
SHADER_UNIFORM_COLOR,
+ SHADER_UNIFORM_INTERP,
NUM_SHADER_UNIFORMS,
};