This commit is contained in:
sam 2024-05-05 17:59:02 +12:00
parent 8bca227b73
commit 9c0bd98918
10 changed files with 258 additions and 230 deletions

6
.gitignore vendored
View file

@ -1,3 +1,3 @@
libgearlib.a *.a
obj/ obj/
test/*.exe examples/*.exe

View file

@ -1,25 +1,25 @@
LIBRARY=libgearlib.a LIBRARY=libgearlib.a
CC:=gcc CC:=gcc
AR=ar AR=ar
CFLAGS=-O3 -Iinclude -g CFLAGS=-O3 -Iinclude -g
LDFLAGS=-lglfw -lm LDFLAGS=-lglfw -lm
CFILES=$(shell cd src && mingw32-find -L * -type f -name '*.c') CFILES=$(shell cd src && find -L * -type f -name '*.c')
OBJDIR=obj OBJDIR=obj
OBJ=$(addprefix $(OBJDIR)/, $(CFILES:.c=.o)) OBJ=$(addprefix $(OBJDIR)/, $(CFILES:.c=.o))
$(LIBRARY): $(OBJ) Makefile $(LIBRARY): $(OBJ) Makefile
$(AR) rcs $(LIBRARY) $(OBJ) $(AR) rcs $(LIBRARY) $(OBJ)
obj/%.o: src/%.c obj/%.o: src/%.c
mkdir -p $(OBJDIR) mkdir -p $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $@ $(CC) $(CFLAGS) -c $< -o $@
run: $(LIBRARY) run: $(LIBRARY)
make -C examples test make -C examples test
cd examples && ./test cd examples && ./test
clean: clean:
rm -rf $(OBJ) $(LIBRARY) rm -rf $(OBJ) $(LIBRARY)
make -C test clean make -C examples clean

View file

@ -1,16 +1,16 @@
CC=gcc CC=gcc
CFLAGS=-O3 -I../include CFLAGS = -O3 -I../include
LDFLAGS=C:\msys64\ucrt64\lib\libglfw3.a -L ../ -lgdi32 -lgearlib -lm LDFLAGS = -L ../ -lglfw -lgearlib -lm
CFILES=$(shell mingw32-find -L * -type f -name '*.c') CFILES=$(shell find -L * -type f -name '*.c')
BINARIES=$(CFILES:.c=) BINARIES=$(CFILES:.c=)
all: $(BINARIES) all: $(BINARIES)
%: %.c %: %.c
$(CC) $< -o $@ $(CFLAGS) $(LDFLAGS) $(CC) $< -o $@ $(CFLAGS) $(LDFLAGS)
clean: clean:
rm -rf $(BINARIES) rm -rf $(BINARIES)

Binary file not shown.

View file

@ -1,35 +1,44 @@
#include <gearlib.h> #include <gearlib.h>
int main() { int main() {
Window window = create_window(800, 600, "test"); Window window = create_window(800, 600, "test");
glfwSwapInterval(0); // fps unlock glfwSwapInterval(0); // fps unlock
enable_debugging(); enable_debugging();
setup_textures(); setup_textures();
Camera* camera = create_camera(vec2(0.0f, 0.0f)); Camera* camera = create_camera(vec2(0.0f, 0.0f));
UniformBuffer* camera_buffer = create_uniform_buffer(sizeof(CameraMatrices)); UniformBuffer* camera_buffer = create_uniform_buffer(sizeof(CameraMatrices));
Texture cat = load_texture("assets/cat.png"); Texture cat = load_texture("assets/cat.png");
while (!glfwWindowShouldClose(window)) { double time = glfwGetTime();
process_input(window);
update_camera(camera, window); while (!glfwWindowShouldClose(window)) {
set_uniform_data(camera_buffer, camera->m); process_input(window);
update_camera(camera, window);
glClearColor(vec4_spread(BLACK)); set_uniform_data(camera_buffer, camera->m);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(vec4_spread(BLACK));
for(int i = 0; i < 32; i++) { glClear(GL_COLOR_BUFFER_BIT);
draw_texture(cat, vec2(rand() % 600, rand() % 600), vec2(500.0f, 500.0f), WHITE);
} for(int i = 0; i < 32; i++) {
draw_texture(cat, vec2(rand() % 600, rand() % 600), vec2(500.0f, 500.0f), WHITE);
flush(); }
glfwSwapBuffers(window); flush();
glfwPollEvents();
} double end_time = glfwGetTime();
double frame_time = end_time - time;
glfwDestroyWindow(window); time = end_time;
glfwTerminate();
return 0; double fps = 1.0 / frame_time;
} printf("frame_time: %lf fps: %lf\n", frame_time, fps);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}

Binary file not shown.

Binary file not shown.

View file

@ -1,32 +1,41 @@
#include <gearlib.h> #include <gearlib.h>
int main() { int main() {
Window window = create_window(800, 600, "textures"); Window window = create_window(800, 600, "textures");
glfwSwapInterval(0); // fps unlock glfwSwapInterval(0); // fps unlock
setup_textures(); setup_textures();
Camera* camera = create_camera(vec2(0.0f, 0.0f)); Camera* camera = create_camera(vec2(0.0f, 0.0f));
UniformBuffer* camera_buffer = create_uniform_buffer(sizeof(CameraMatrices)); UniformBuffer* camera_buffer = create_uniform_buffer(sizeof(CameraMatrices));
Texture cat = load_texture("assets/cat.png"); Texture cat = load_texture("assets/cat.png");
while (!glfwWindowShouldClose(window)) { double time = glfwGetTime();
process_input(window);
update_camera(camera, window); while (!glfwWindowShouldClose(window)) {
set_uniform_data(camera_buffer, camera->m); process_input(window);
update_camera(camera, window);
glClearColor(vec4_spread(BLACK)); set_uniform_data(camera_buffer, camera->m);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(vec4_spread(BLUE));
draw_texture(cat, vec2(250.0f, 250.0f), vec2(500.0f, 500.0f), WHITE); glClear(GL_COLOR_BUFFER_BIT);
flush(); draw_texture(cat, vec2(250.0f, 250.0f), vec2(500.0f, 500.0f), WHITE);
glfwSwapBuffers(window); flush();
glfwPollEvents();
} double end_time = glfwGetTime();
double frame_time = end_time - time;
glfwDestroyWindow(window); time = end_time;
glfwTerminate();
return 0; double fps = 1.0 / frame_time;
} printf("frame_time: %lf fps: %lf\n", frame_time, fps);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}

BIN
src/.textures.c.swp Normal file

Binary file not shown.

View file

@ -1,119 +1,129 @@
#include <textures.h> #include <textures.h>
#include <stb_image.h> #include <stb_image.h>
#include <opengl.h> #include <opengl.h>
#include <stdio.h> #include <stdio.h>
#include <assert.h> #include <assert.h>
RenderBatch* texture_quad_batch = NULL; RenderBatch* texture_quad_batch = NULL;
int max_textures; int max_textures;
void setup_textures() { void setup_textures() {
texture_quad_batch = create_texture_quad_batch(); texture_quad_batch = create_texture_quad_batch();
glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_textures); glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &max_textures);
if(max_textures > 32) max_textures = 32; if(max_textures > 32) max_textures = 32;
} }
uint32_t load_texture(const char* path) { uint32_t load_texture(const char* path) {
uint32_t id; uint32_t id;
glCreateTextures(GL_TEXTURE_2D, 1, &id); glCreateTextures(GL_TEXTURE_2D, 1, &id);
glTextureParameteri(id, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); uint8_t default_texture[] = {
glTextureParameteri(id, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 0, 0, 0, 255, 255, 0, 255, 255,
glTextureParameteri(id, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 255, 0, 255, 255, 0, 0, 0, 255 };
glTextureParameteri(id, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
int width = 2, height = 2, channels;
int width, height, channels; uint8_t* pixels = stbi_load(path, &width, &height, &channels, 4);
unsigned char* pixels = stbi_load(path, &width, &height, &channels, 4); if(pixels) {
if(pixels) { glTextureParameteri(id, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTextureStorage2D(id, 1, GL_RGBA8, width, height); glTextureParameteri(id, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTextureSubImage2D(id, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); glTextureParameteri(id, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
printf("[Texture %d, %s] Loaded successfully (%dx%d, %d channels)\n", id, path, width, height, channels); glTextureParameteri(id, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
} else {
printf("[Texture %d, %s] Failed to load image data: %s\n", id, path, stbi_failure_reason()); glTextureStorage2D(id, 1, GL_RGBA8, width, height);
exit(-1); glTextureSubImage2D(id, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
} printf("[Texture %d, %s] Loaded successfully (%dx%d, %d channels)\n", id, path, width, height, channels);
} else {
return id; glTextureParameteri(id, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
} glTextureParameteri(id, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTextureParameteri(id, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
vec2 texture_quad_texcoords[] = { glTextureParameteri(id, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
{ 0.0f, 1.0f },
{ 0.0f, 0.0f }, glTextureStorage2D(id, 1, GL_RGBA8, width, height);
{ 1.0f, 0.0f }, glTextureSubImage2D(id, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, default_texture);
printf("[Texture %d, %s] Failed to load image data: %s\n", id, path, stbi_failure_reason());
{ 0.0f, 1.0f }, }
{ 1.0f, 0.0f },
{ 1.0f, 1.0f } return id;
}; }
void draw_texture(Texture id, vec2 pos, vec2 size, vec4 tint) { vec2 texture_quad_texcoords[] = {
mat4 transform = mat4Multiply( { 0.0f, 1.0f },
mat4Scale(size.x, size.y, 1.0f), { 0.0f, 0.0f },
mat4Translate(pos.x, pos.y, 0.0f)); { 1.0f, 0.0f },
draw_texture_trans(id, transform, tint); { 0.0f, 1.0f },
} { 1.0f, 0.0f },
{ 1.0f, 1.0f }
void draw_texture_trans(Texture id, mat4 transform, vec4 tint) { };
assert(texture_quad_batch != NULL && "texture_quad_batch is null, was setup_textures() called?");
batch_draw_texture(texture_quad_batch, id, transform, tint); void draw_texture(Texture id, vec2 pos, vec2 size, vec4 tint) {
} mat4 transform = mat4Multiply(
mat4Scale(size.x, size.y, 1.0f),
void batch_draw_texture(RenderBatch* batch, Texture texture, mat4 transform, vec4 color) { mat4Translate(pos.x, pos.y, 0.0f));
TextureQuadBatchData* batch_data = batch->data;
uint32_t vertex_add = 6; draw_texture_trans(id, transform, tint);
}
if(batch_needs_flush(batch, vertex_add) || batch_data->texture_index >= max_textures)
flush_batch(batch); void draw_texture_trans(Texture id, mat4 transform, vec4 tint) {
assert(texture_quad_batch != NULL && "texture_quad_batch is null, was setup_textures() called?");
uint32_t tex_id = batch_data->texture_index++; batch_draw_texture(texture_quad_batch, id, transform, tint);
glBindTextureUnit(tex_id, texture); }
for(int i = 0; i < vertex_add; i++) { void batch_draw_texture(RenderBatch* batch, Texture texture, mat4 transform, vec4 color) {
TextureQuadVertex* vertex = batch->vertex_ptr; TextureQuadBatchData* batch_data = batch->data;
uint32_t vertex_add = 6;
vertex->Position = vec3Transform(quad_vertex_positions[i], transform);
vertex->Tint = color; if(batch_needs_flush(batch, vertex_add) || batch_data->texture_index >= max_textures)
vertex->TexCoord = texture_quad_texcoords[i]; flush_batch(batch);
vertex->TexID = tex_id;
uint32_t tex_id = batch_data->texture_index++;
batch->vertex_ptr += batch->vertex_size; glBindTextureUnit(tex_id, texture);
batch->vertex_count++;
} for(int i = 0; i < vertex_add; i++) {
} TextureQuadVertex* vertex = batch->vertex_ptr;
void texture_flush_callback(RenderBatch* batch) { vertex->Position = vec3Transform(quad_vertex_positions[i], transform);
TextureQuadBatchData* data = batch->data; vertex->Tint = color;
data->texture_index = 0; vertex->TexCoord = texture_quad_texcoords[i];
} vertex->TexID = tex_id;
RenderBatch* create_texture_quad_batch() { batch->vertex_ptr += batch->vertex_size;
RenderBatch* texture_quad_batch = create_batch(sizeof(TextureQuadVertex), MAX_VERTICES); batch->vertex_count++;
texture_quad_batch->shader = load_shader_program( }
compile_shader("assets/texture.vert", GL_VERTEX_SHADER), }
compile_shader("assets/texture.frag", GL_FRAGMENT_SHADER), 0);
void texture_flush_callback(RenderBatch* batch) {
texture_quad_batch->data = calloc(sizeof(TextureQuadBatchData), 1); TextureQuadBatchData* data = batch->data;
texture_quad_batch->flush_callback = &texture_flush_callback; data->texture_index = 0;
}
batch_add_attrib(texture_quad_batch, (VertexAttrib){
.type = GL_FLOAT, .size = sizeof(float), .count = 3 RenderBatch* create_texture_quad_batch() {
}); // pos RenderBatch* texture_quad_batch = create_batch(sizeof(TextureQuadVertex), MAX_VERTICES);
texture_quad_batch->shader = load_shader_program(
batch_add_attrib(texture_quad_batch, (VertexAttrib){ compile_shader("assets/texture.vert", GL_VERTEX_SHADER),
.type = GL_FLOAT, .size = sizeof(float), .count = 4 compile_shader("assets/texture.frag", GL_FRAGMENT_SHADER), 0);
}); // color
texture_quad_batch->data = calloc(sizeof(TextureQuadBatchData), 1);
batch_add_attrib(texture_quad_batch, (VertexAttrib){ texture_quad_batch->flush_callback = &texture_flush_callback;
.type = GL_FLOAT, .size = sizeof(float), .count = 2
}); // texcoord batch_add_attrib(texture_quad_batch, (VertexAttrib){
.type = GL_FLOAT, .size = sizeof(float), .count = 3
batch_add_attrib(texture_quad_batch, (VertexAttrib){ }); // pos
.type = GL_FLOAT, .size = sizeof(float), .count = 1
}); // texid batch_add_attrib(texture_quad_batch, (VertexAttrib){
.type = GL_FLOAT, .size = sizeof(float), .count = 4
batch_bind_attribs(texture_quad_batch); }); // color
return texture_quad_batch; batch_add_attrib(texture_quad_batch, (VertexAttrib){
} .type = GL_FLOAT, .size = sizeof(float), .count = 2
}); // texcoord
batch_add_attrib(texture_quad_batch, (VertexAttrib){
.type = GL_FLOAT, .size = sizeof(float), .count = 1
}); // texid
batch_bind_attribs(texture_quad_batch);
return texture_quad_batch;
}