2024-04-29 22:41:13 +12:00
|
|
|
#include <init.h>
|
|
|
|
#include <debugging.h>
|
|
|
|
#include <shaders.h>
|
2024-04-30 23:02:19 +12:00
|
|
|
#include <textures.h>
|
2024-04-29 22:41:13 +12:00
|
|
|
#include <events.h>
|
2024-04-30 23:02:19 +12:00
|
|
|
#include <types.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#define MAX_VERTICES 1000
|
|
|
|
|
|
|
|
#define ARR_SIZE(arr) sizeof(arr)/sizeof(arr[0])
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int type;
|
|
|
|
size_t size;
|
|
|
|
int count;
|
|
|
|
} VertexAttrib;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
float3 Position;
|
|
|
|
float4 Color;
|
|
|
|
float2 TexCoord;
|
|
|
|
} QuadVertex;
|
|
|
|
|
|
|
|
VertexAttrib quad_attribs[] = {
|
|
|
|
{ .type = GL_FLOAT, .size = sizeof(float), .count = 3 }, // position
|
|
|
|
{ .type = GL_FLOAT, .size = sizeof(float), .count = 4 }, // color
|
|
|
|
{ .type = GL_FLOAT, .size = sizeof(float), .count = 2 }, // texcoord
|
|
|
|
};
|
|
|
|
|
|
|
|
uint32_t index_count = 0;
|
|
|
|
uint32_t vertex_count = 0;
|
|
|
|
uint32_t texture_shader, quad_vbo, quad_vao;
|
|
|
|
QuadVertex* quad_vertices;
|
|
|
|
QuadVertex* current_quad_vertex;
|
|
|
|
|
|
|
|
void load_default_shaders() {
|
|
|
|
texture_shader = load_shader_program(
|
|
|
|
compile_shader("texture.vert", GL_VERTEX_SHADER),
|
|
|
|
compile_shader("texture.frag", GL_FRAGMENT_SHADER), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
float3 quad_vertex_positions[4] = {
|
|
|
|
{ -0.5f, 0.5f, 0.0f },
|
|
|
|
{ -0.5f, -0.5f, 0.0f },
|
|
|
|
{ 0.5f, -0.5f, 0.0f },
|
|
|
|
{ 0.5f, 0.5f, 0.0f }
|
|
|
|
};
|
|
|
|
|
|
|
|
void draw_indexed(uint32_t vao, int count) {
|
|
|
|
glBindVertexArray(vao);
|
|
|
|
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, 0);
|
|
|
|
glBindVertexArray(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void flush() {
|
|
|
|
glNamedBufferSubData(quad_vbo, 0, sizeof(QuadVertex) * vertex_count, quad_vertices);
|
|
|
|
draw_indexed(quad_vao, index_count);
|
|
|
|
vertex_count = 0;
|
|
|
|
index_count = 0;
|
|
|
|
current_quad_vertex = quad_vertices;
|
|
|
|
}
|
|
|
|
|
|
|
|
void draw_texture(uint32_t texture, float4 color) {
|
|
|
|
if(vertex_count > MAX_VERTICES - 4)
|
|
|
|
flush();
|
|
|
|
|
|
|
|
float2 texcoords[] = {
|
|
|
|
{ 0.0f, 0.0f },
|
|
|
|
{ 1.0f, 0.0f },
|
|
|
|
{ 0.0f, 1.0f },
|
|
|
|
{ 1.0f, 1.0f }
|
|
|
|
};
|
|
|
|
|
|
|
|
for(int i = 0; i < 4; i++) {
|
|
|
|
current_quad_vertex->Position = quad_vertex_positions[i];
|
|
|
|
current_quad_vertex->Color = color;
|
|
|
|
current_quad_vertex->TexCoord = texcoords[i];
|
|
|
|
current_quad_vertex++;
|
|
|
|
|
|
|
|
vertex_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
index_count += 6;
|
|
|
|
}
|
2024-04-29 19:32:59 +12:00
|
|
|
|
2024-04-29 22:41:13 +12:00
|
|
|
int main() {
|
|
|
|
init_gl(4, 6);
|
2024-04-30 23:02:19 +12:00
|
|
|
Window window = create_window(800, 600, "gearlib");
|
2024-04-29 22:41:13 +12:00
|
|
|
enable_debugging();
|
2024-04-30 23:02:19 +12:00
|
|
|
load_default_shaders();
|
|
|
|
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
|
|
|
|
quad_vertices = malloc(sizeof(QuadVertex) * MAX_VERTICES);
|
|
|
|
current_quad_vertex = quad_vertices;
|
2024-04-29 19:32:59 +12:00
|
|
|
|
2024-04-30 23:02:19 +12:00
|
|
|
glCreateBuffers(1, &quad_vbo);
|
|
|
|
glNamedBufferStorage(quad_vbo, sizeof(QuadVertex) * MAX_VERTICES, NULL, GL_DYNAMIC_STORAGE_BIT);
|
|
|
|
|
|
|
|
uint32_t quad_indices[] = {
|
|
|
|
0, 1, 3,
|
|
|
|
1, 2, 3
|
2024-04-29 22:41:13 +12:00
|
|
|
};
|
2024-04-29 19:32:59 +12:00
|
|
|
|
2024-04-30 23:02:19 +12:00
|
|
|
uint32_t quad_ibo;
|
|
|
|
glCreateBuffers(1, &quad_ibo);
|
|
|
|
glNamedBufferStorage(quad_ibo, sizeof(quad_indices), quad_indices, GL_DYNAMIC_STORAGE_BIT);
|
|
|
|
|
|
|
|
glCreateVertexArrays(1, &quad_vao);
|
2024-04-29 19:32:59 +12:00
|
|
|
|
2024-04-30 23:02:19 +12:00
|
|
|
size_t stride = 0;
|
|
|
|
for(int i = 0; i < ARR_SIZE(quad_attribs); i++) {
|
|
|
|
VertexAttrib attr = quad_attribs[i];
|
2024-04-29 19:32:59 +12:00
|
|
|
|
2024-04-30 23:02:19 +12:00
|
|
|
glEnableVertexArrayAttrib(quad_vao, i);
|
|
|
|
glVertexArrayAttribFormat(quad_vao, i, attr.count, attr.type, GL_FALSE, stride);
|
|
|
|
glVertexArrayAttribBinding(quad_vao, i, 0);
|
|
|
|
|
|
|
|
stride += attr.size * attr.count;
|
|
|
|
}
|
|
|
|
|
|
|
|
glVertexArrayVertexBuffer(quad_vao, 0, quad_vbo, 0, stride);
|
|
|
|
glVertexArrayElementBuffer(quad_vao, quad_ibo);
|
|
|
|
|
|
|
|
uint32_t cat = load_texture("cat.png");
|
2024-04-29 19:32:59 +12:00
|
|
|
|
|
|
|
while (!glfwWindowShouldClose(window)) {
|
|
|
|
process_input(window);
|
|
|
|
|
|
|
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
2024-04-30 23:02:19 +12:00
|
|
|
glBindTextureUnit(0, cat);
|
|
|
|
glUseProgram(texture_shader);
|
|
|
|
|
|
|
|
for(int i = 0; i < 10000; i++) {
|
|
|
|
draw_texture(cat, (float4){ 1.0f, 1.0f, 1.0f, 1.0f });
|
|
|
|
}
|
|
|
|
|
|
|
|
flush();
|
2024-04-29 22:41:13 +12:00
|
|
|
|
2024-04-29 19:32:59 +12:00
|
|
|
glfwSwapBuffers(window);
|
|
|
|
glfwPollEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
glfwTerminate();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|