Compare commits
2 commits
8cbe81177b
...
b3ee3f3357
Author | SHA1 | Date | |
---|---|---|---|
b3ee3f3357 | |||
e50d043952 |
18 changed files with 2821 additions and 47 deletions
3
Makefile
3
Makefile
|
@ -19,6 +19,9 @@ objdir:
|
||||||
run: $(BINARY)
|
run: $(BINARY)
|
||||||
./$(BINARY)
|
./$(BINARY)
|
||||||
|
|
||||||
|
debug: $(BINARY)
|
||||||
|
gdb $(BINARY)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(OBJ) $(BINARY)
|
rm -rf $(OBJ) $(BINARY)
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
#version 460 core
|
#version 460 core
|
||||||
|
|
||||||
layout(location = 0) in vec3 a_Position;
|
layout(location = 0) in vec3 a_Position;
|
||||||
layout(location = 1) in mat4 a_Transform;
|
layout(location = 1) in vec4 a_Color;
|
||||||
layout(location = 2) in vec4 a_Color;
|
|
||||||
|
|
||||||
struct VertexOutput {
|
struct VertexOutput {
|
||||||
vec4 Color;
|
vec4 Color;
|
||||||
|
@ -10,8 +9,13 @@ struct VertexOutput {
|
||||||
|
|
||||||
layout(location = 0) out VertexOutput Output;
|
layout(location = 0) out VertexOutput Output;
|
||||||
|
|
||||||
|
layout(std140, binding = 0) uniform Camera {
|
||||||
|
mat4 view;
|
||||||
|
mat4 projection;
|
||||||
|
};
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
Output.Color = a_Color;
|
Output.Color = a_Color;
|
||||||
|
|
||||||
gl_Position = vec4(a_Position, 1.0) * a_Transform;
|
gl_Position = projection * view * vec4(a_Position, 1.0);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,10 +13,15 @@ struct VertexOutput {
|
||||||
layout(location = 0) out VertexOutput Output;
|
layout(location = 0) out VertexOutput Output;
|
||||||
layout(location = 2) out flat float TexID;
|
layout(location = 2) out flat float TexID;
|
||||||
|
|
||||||
|
layout(std140, binding = 0) uniform Camera {
|
||||||
|
mat4 view;
|
||||||
|
mat4 projection;
|
||||||
|
};
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
Output.Tint = a_Tint;
|
Output.Tint = a_Tint;
|
||||||
Output.TexCoord = a_TexCoord;
|
Output.TexCoord = a_TexCoord;
|
||||||
TexID = a_TexID;
|
TexID = a_TexID;
|
||||||
|
|
||||||
gl_Position = vec4(a_Position, 1.0);
|
gl_Position = projection * view * vec4(a_Position, 1.0);
|
||||||
}
|
}
|
||||||
|
|
BIN
implementations.o
Normal file
BIN
implementations.o
Normal file
Binary file not shown.
|
@ -6,6 +6,7 @@
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include <gearlib.h>
|
#include <gearlib.h>
|
||||||
|
#include <slibs.h>
|
||||||
|
|
||||||
typedef struct BatchStats {
|
typedef struct BatchStats {
|
||||||
uint32_t draw_calls;
|
uint32_t draw_calls;
|
||||||
|
@ -13,9 +14,10 @@ typedef struct BatchStats {
|
||||||
} BatchStats;
|
} BatchStats;
|
||||||
|
|
||||||
typedef struct RenderBatch {
|
typedef struct RenderBatch {
|
||||||
uint32_t vbo;
|
uint32_t vbo, vao;
|
||||||
uint32_t vao;
|
|
||||||
uint32_t shader;
|
uint32_t shader;
|
||||||
|
|
||||||
void* vertices;
|
void* vertices;
|
||||||
void* vertex_ptr;
|
void* vertex_ptr;
|
||||||
size_t vertex_size;
|
size_t vertex_size;
|
||||||
|
@ -29,6 +31,10 @@ typedef struct RenderBatch {
|
||||||
void (*flush_callback)(struct RenderBatch*);
|
void (*flush_callback)(struct RenderBatch*);
|
||||||
} RenderBatch;
|
} RenderBatch;
|
||||||
|
|
||||||
|
typedef sl_vec(RenderBatch*) BatchList;
|
||||||
|
|
||||||
|
extern BatchList batches;
|
||||||
|
|
||||||
RenderBatch* create_batch(size_t vert_size, uint32_t max_verts);
|
RenderBatch* create_batch(size_t vert_size, uint32_t max_verts);
|
||||||
void flush();
|
void flush();
|
||||||
void flush_batch(RenderBatch* batch);
|
void flush_batch(RenderBatch* batch);
|
||||||
|
|
11
include/camera.h
Normal file
11
include/camera.h
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
#ifndef __CAMERA_H__
|
||||||
|
#define __CAMERA_H__
|
||||||
|
|
||||||
|
typedef struct Camera {
|
||||||
|
mat4 view;
|
||||||
|
mat4 projection;
|
||||||
|
} Camera;
|
||||||
|
|
||||||
|
void set_active_camera(Camera* camera);
|
||||||
|
|
||||||
|
#endif
|
12
include/colors.h
Normal file
12
include/colors.h
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
#ifndef __COLORS_H__
|
||||||
|
#define __COLORS_H__
|
||||||
|
|
||||||
|
#define WHITE vec4(1.0f, 1.0f, 1.0f, 1.0f)
|
||||||
|
#define BLACK vec4(0.0f, 0.0f, 0.0f, 1.0f)
|
||||||
|
#define RED vec4(1.0f, 0.0f, 0.0f, 1.0f)
|
||||||
|
#define GREEN vec4(0.0f, 1.0f, 0.0f, 1.0f)
|
||||||
|
#define BLUE vec4(0.0f, 0.0f, 1.0f, 1.0f)
|
||||||
|
|
||||||
|
#define BLANK vec4(0.0f, 0.0f, 0.0f, 0.0f)
|
||||||
|
|
||||||
|
#endif
|
|
@ -1,8 +1,9 @@
|
||||||
#ifndef __GEARLIB_H__
|
#ifndef __GEARLIB_H__
|
||||||
#define __GEARLIB_H__
|
#define __GEARLIB_H__
|
||||||
|
|
||||||
#define MAX_VERTICES 1000
|
#define MAX_VERTICES 2500
|
||||||
|
|
||||||
|
#include <colors.h>
|
||||||
#include <raymath.h>
|
#include <raymath.h>
|
||||||
#include <init.h>
|
#include <init.h>
|
||||||
#include <shaders.h>
|
#include <shaders.h>
|
||||||
|
@ -10,6 +11,7 @@
|
||||||
#include <debugging.h>
|
#include <debugging.h>
|
||||||
#include <vertex.h>
|
#include <vertex.h>
|
||||||
#include <renderer.h>
|
#include <renderer.h>
|
||||||
|
#include <camera.h>
|
||||||
#include <batch.h>
|
#include <batch.h>
|
||||||
#include <quad.h>
|
#include <quad.h>
|
||||||
#include <textures.h>
|
#include <textures.h>
|
||||||
|
|
2554
include/oldmath.h
Normal file
2554
include/oldmath.h
Normal file
File diff suppressed because it is too large
Load diff
|
@ -3,9 +3,16 @@
|
||||||
|
|
||||||
#include <gearlib.h>
|
#include <gearlib.h>
|
||||||
|
|
||||||
|
void setup_quads();
|
||||||
|
void draw_quad(vec2 pos, vec2 size, vec4 color);
|
||||||
|
void draw_quad_trans(mat4 transform, vec4 color);
|
||||||
|
void batch_draw_quad(RenderBatch* batch, mat4 transform, vec4 color);
|
||||||
|
RenderBatch* create_quad_batch();
|
||||||
|
|
||||||
|
extern RenderBatch* quad_batch;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
vec3 Position;
|
vec3 Position;
|
||||||
mat4 Transform;
|
|
||||||
vec4 Color;
|
vec4 Color;
|
||||||
} QuadVertex;
|
} QuadVertex;
|
||||||
|
|
||||||
|
|
|
@ -115,6 +115,11 @@ typedef struct vec2 {
|
||||||
float x;
|
float x;
|
||||||
float y;
|
float y;
|
||||||
} vec2;
|
} vec2;
|
||||||
|
|
||||||
|
#define vec2(...) (vec2){ __VA_ARGS__ }
|
||||||
|
|
||||||
|
#define VEC2 "%f, %f"
|
||||||
|
|
||||||
#define RL_VECTOR2_TYPE
|
#define RL_VECTOR2_TYPE
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -125,6 +130,18 @@ typedef struct vec3 {
|
||||||
float y;
|
float y;
|
||||||
float z;
|
float z;
|
||||||
} vec3;
|
} vec3;
|
||||||
|
|
||||||
|
#define vec3_1(x) (vec3){ x, x, x }
|
||||||
|
#define vec3_2(v2, z) (vec3){ v2.x, v2.y, z }
|
||||||
|
#define vec3_3(x, y, z) (vec3){ x, y, z }
|
||||||
|
|
||||||
|
#define vec3_MACRO(_1, _2, _3, NAME, ...) NAME
|
||||||
|
#define vec3(...) vec3_MACRO(__VA_ARGS__, vec3_3, vec3_2, vec3_1)(__VA_ARGS__)
|
||||||
|
|
||||||
|
#define vec3_spread(v) v.x, v.y, v.z
|
||||||
|
|
||||||
|
#define VEC3 "%f, %f, %f"
|
||||||
|
|
||||||
#define RL_VECTOR3_TYPE
|
#define RL_VECTOR3_TYPE
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -136,6 +153,19 @@ typedef struct vec4 {
|
||||||
float z;
|
float z;
|
||||||
float w;
|
float w;
|
||||||
} vec4;
|
} vec4;
|
||||||
|
|
||||||
|
#define vec4_1(x) (vec4){ x, x, x, x }
|
||||||
|
#define vec4_2(v3, w) (vec4){ v3.x, v3.y, v3.z, w }
|
||||||
|
#define vec4_3(v2, z, w) (vec4){ v2.x, v2.y, z, w }
|
||||||
|
#define vec4_4(x, y, z, w) (vec4){ x, y, z, w }
|
||||||
|
|
||||||
|
#define vec4_MACRO(_1, _2, _3, _4, NAME, ...) NAME
|
||||||
|
#define vec4(...) vec4_MACRO(__VA_ARGS__, vec4_4, vec4_3, vec4_2, vec4_1)(__VA_ARGS__)
|
||||||
|
|
||||||
|
#define vec4_spread(v) v.x, v.y, v.z, v.w
|
||||||
|
|
||||||
|
#define VEC4 "%f, %f, %f, %f"
|
||||||
|
|
||||||
#define RL_VECTOR4_TYPE
|
#define RL_VECTOR4_TYPE
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -4,10 +4,13 @@
|
||||||
#include <gearlib.h>
|
#include <gearlib.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef uint32_t Texture;
|
||||||
|
|
||||||
void setup_textures();
|
void setup_textures();
|
||||||
uint32_t load_texture(const char* path);
|
Texture load_texture(const char* path);
|
||||||
void draw_texture(uint32_t id, vec3 pos, vec4 color);
|
void draw_texture(Texture id, vec2 pos, vec2 size, vec4 tint);
|
||||||
void batch_draw_texture(RenderBatch* batch, uint32_t id, vec3 pos, vec4 color);
|
void draw_texture_trans(Texture id, mat4 transform, vec4 tint);
|
||||||
|
void batch_draw_texture(RenderBatch* batch, Texture id, mat4 transform, vec4 tint);
|
||||||
RenderBatch* create_texture_quad_batch();
|
RenderBatch* create_texture_quad_batch();
|
||||||
|
|
||||||
extern RenderBatch* texture_quad_batch;
|
extern RenderBatch* texture_quad_batch;
|
||||||
|
|
10
src/batch.c
10
src/batch.c
|
@ -2,7 +2,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <slibs.h>
|
#include <slibs.h>
|
||||||
|
|
||||||
sl_vec(RenderBatch*) batches = { 0 };
|
BatchList batches = { 0 };
|
||||||
|
|
||||||
RenderBatch* create_batch(size_t vert_size, uint32_t max_verts) {
|
RenderBatch* create_batch(size_t vert_size, uint32_t max_verts) {
|
||||||
RenderBatch* batch = calloc(sizeof(RenderBatch), 1);
|
RenderBatch* batch = calloc(sizeof(RenderBatch), 1);
|
||||||
|
@ -36,6 +36,13 @@ void flush_batch(RenderBatch* batch) {
|
||||||
|
|
||||||
glUseProgram(batch->shader);
|
glUseProgram(batch->shader);
|
||||||
|
|
||||||
|
/*glUniformMatrix4fv(0, 1, GL_FALSE,
|
||||||
|
mat4ToFloat(mat4Multiply(batch->camera->view, batch->camera->projection)));*/
|
||||||
|
|
||||||
|
|
||||||
|
//glUniformMatrix4fv(1, 1, GL_FALSE, mat4ToFloat(batch->camera->view));
|
||||||
|
//glUniformMatrix4fv(2, 1, GL_FALSE, mat4ToFloat(batch->camera->projection));
|
||||||
|
|
||||||
if(batch->flush_callback != NULL)
|
if(batch->flush_callback != NULL)
|
||||||
batch->flush_callback(batch);
|
batch->flush_callback(batch);
|
||||||
|
|
||||||
|
@ -69,6 +76,5 @@ void batch_bind_attribs(RenderBatch* batch) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool batch_needs_flush(RenderBatch* batch, uint32_t vertex_add) {
|
bool batch_needs_flush(RenderBatch* batch, uint32_t vertex_add) {
|
||||||
//return true;
|
|
||||||
return batch->vertex_size * (batch->vertex_count + vertex_add) >= batch->vertex_size * batch->max_vertices;
|
return batch->vertex_size * (batch->vertex_count + vertex_add) >= batch->vertex_size * batch->max_vertices;
|
||||||
}
|
}
|
||||||
|
|
7
src/camera.c
Normal file
7
src/camera.c
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
#include <gearlib.h>
|
||||||
|
|
||||||
|
void set_active_camera(Camera* camera) {
|
||||||
|
/*for(int i = 0; i < batches.size; i++) {
|
||||||
|
batches.data[i]->camera = camera;
|
||||||
|
}*/
|
||||||
|
}
|
|
@ -3,6 +3,8 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <opengl.h>
|
#include <opengl.h>
|
||||||
|
|
||||||
|
bool gl_initialized = false;
|
||||||
|
|
||||||
void init_gl(int major, int minor) {
|
void init_gl(int major, int minor) {
|
||||||
if(!glfwInit()) {
|
if(!glfwInit()) {
|
||||||
printf("[GLFW] Failed to initialize\n");
|
printf("[GLFW] Failed to initialize\n");
|
||||||
|
@ -14,9 +16,15 @@ void init_gl(int major, int minor) {
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, major);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, major);
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, minor);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, minor);
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
|
|
||||||
|
printf("[OPENGL] Using OpenGL %d.%d\n", major, minor);
|
||||||
|
|
||||||
|
gl_initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Window create_window(int width, int height, const char* title) {
|
Window create_window(int width, int height, const char* title) {
|
||||||
|
if(!gl_initialized) init_gl(4, 6);
|
||||||
|
|
||||||
Window window = glfwCreateWindow(width, height, title, NULL, NULL);
|
Window window = glfwCreateWindow(width, height, title, NULL, NULL);
|
||||||
if(!window) {
|
if(!window) {
|
||||||
printf("[GLFW] Failed to create window\n");
|
printf("[GLFW] Failed to create window\n");
|
||||||
|
|
102
src/main.c
102
src/main.c
|
@ -1,45 +1,127 @@
|
||||||
#include <gearlib.h>
|
#include <gearlib.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <raymath.h>
|
Camera* create_camera(vec2 pos) {
|
||||||
|
Camera* camera = calloc(sizeof(Camera), 1);
|
||||||
|
camera->view = mat4Transpose(mat4Translate(pos.x, pos.y, -3.0f));
|
||||||
|
|
||||||
|
return camera;
|
||||||
|
}
|
||||||
|
|
||||||
|
void update_camera(Camera* camera, Window window) {
|
||||||
|
int width, height;
|
||||||
|
glfwGetWindowSize(window, &width, &height);
|
||||||
|
|
||||||
|
assert(camera != NULL);
|
||||||
|
camera->projection = mat4Transpose(mat4Ortho(0.0f, width, height, 0.0f, 0.0f, 1000.0f));
|
||||||
|
//camera->projection = mat4Transpose(mat4Perspective(45.0f * DEG2RAD, ((double)width / (double)height), 0.1f, 100.0f));
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct CameraData {
|
||||||
|
mat4 view_projection;
|
||||||
|
} CameraData;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
init_gl(4, 6);
|
|
||||||
Window window = create_window(800, 600, "gearlib");
|
Window window = create_window(800, 600, "gearlib");
|
||||||
glfwSwapInterval(0);
|
glfwSwapInterval(0);
|
||||||
|
|
||||||
enable_debugging();
|
enable_debugging();
|
||||||
|
setup_quads();
|
||||||
setup_textures();
|
setup_textures();
|
||||||
|
|
||||||
uint32_t cat = load_texture("assets/cat.png");
|
Camera* camera = create_camera(vec2(0.0f, 0.0f));
|
||||||
|
set_active_camera(camera);
|
||||||
|
|
||||||
|
Texture cat = load_texture("assets/cat.png");
|
||||||
|
|
||||||
|
/*uint32_t cat = load_texture("assets/cat.png");
|
||||||
uint32_t cuddle = load_texture("assets/cuddle.png");
|
uint32_t cuddle = load_texture("assets/cuddle.png");
|
||||||
uint32_t parrots = load_texture("assets/parrots.png");
|
uint32_t parrots = load_texture("assets/parrots.png");*/
|
||||||
|
|
||||||
double time = glfwGetTime();
|
double time = glfwGetTime();
|
||||||
|
/*double total_time = 0;
|
||||||
|
int frames = 0;
|
||||||
|
int fps = 0;*/
|
||||||
|
|
||||||
|
uint32_t ubo;
|
||||||
|
glCreateBuffers(1, &ubo);
|
||||||
|
glNamedBufferStorage(ubo, sizeof(Camera), NULL, GL_DYNAMIC_STORAGE_BIT);
|
||||||
|
glBindBufferBase(GL_UNIFORM_BUFFER, 0, ubo);
|
||||||
|
|
||||||
|
float angle = 0;
|
||||||
|
|
||||||
while (!glfwWindowShouldClose(window)) {
|
while (!glfwWindowShouldClose(window)) {
|
||||||
process_input(window);
|
process_input(window);
|
||||||
|
update_camera(camera, window);
|
||||||
|
|
||||||
|
glNamedBufferSubData(ubo, 0, sizeof(Camera), camera);
|
||||||
|
|
||||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
draw_texture(parrots, (vec3){ -0.4f, 0.4f, 0.0f }, (vec4){ 1.0f, 1.0f, 1.0f, 1.0f });
|
/*for(int i = 0; i < 33; i++) {
|
||||||
draw_texture(cat, (vec3){ 0.0f, 0.0f, 0.0f }, (vec4){ 1.0f, 1.0f, 1.0f, 1.0f });
|
draw_texture(parrots, vec3(-0.4f, 0.4f, 0.0f), WHITE);
|
||||||
draw_texture(cuddle, (vec3){ 0.0f, -0.4f, 0.0f }, (vec4){ 1.0f, 1.0f, 1.0f, 1.0f });
|
draw_texture(cat, vec3(0.0f, 0.0f, 0.0f), WHITE);
|
||||||
|
draw_texture(cuddle, vec3(0.0f, -0.4f, 0.0f), WHITE);
|
||||||
|
}*/
|
||||||
|
|
||||||
|
//draw_texture(cat, vec2(300, 400), vec2(700, 700), WHITE);
|
||||||
|
|
||||||
|
//for(int i = 0; i < 255; i++)
|
||||||
|
//draw_quad(vec2(i, i), vec2(10, 10), vec4(i / 255.0f, i / 255.0f, i / 255.0f, 1.0f));
|
||||||
|
|
||||||
|
mat4 transform = mat4Multiply(
|
||||||
|
mat4Multiply(
|
||||||
|
mat4Scale(100, 100, 1),
|
||||||
|
mat4Identity()),
|
||||||
|
/*mat4Rotate(vec3(1.0f, 0.5f, 0.0f), angle)),*/
|
||||||
|
mat4Translate(0, 0, 1));
|
||||||
|
transform = mat4Multiply(mat4Translate(0.5, 0.5f, 0.0f), transform);
|
||||||
|
|
||||||
|
draw_texture_trans(cat, transform, WHITE);
|
||||||
|
|
||||||
flush();
|
flush();
|
||||||
|
|
||||||
double end_time = glfwGetTime();
|
double end_time = glfwGetTime();
|
||||||
double frame_time = end_time - time;
|
double frame_time = end_time - time;
|
||||||
|
//total_time += frame_time;
|
||||||
double fps = 1 / frame_time;
|
double fps = 1 / frame_time;
|
||||||
|
|
||||||
|
angle += frame_time;
|
||||||
|
|
||||||
time = end_time;
|
time = end_time;
|
||||||
|
|
||||||
//printf("%lf, %lf" /*", %d, %d"*/ "\n", frame_time, fps/*, stats.draw_calls, stats.total_verts*/);
|
/*frames++;
|
||||||
|
if(total_time > 1.0) {
|
||||||
|
fps = frames;
|
||||||
|
frames = 0;
|
||||||
|
total_time = 0;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
//printf("%lf, %lf\n", frame_time, fps);
|
||||||
|
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
glfwDestroyWindow(window);
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
double time = glfwGetTime();
|
||||||
|
...
|
||||||
|
double end_time = glfwGetTime();
|
||||||
|
double frame_time = end_time - time;
|
||||||
|
double fps = 1 / frame_time;
|
||||||
|
time = end_time;
|
||||||
|
|
||||||
|
printf("%lf, %lf, %d, %d\n", frame_time, fps, 0, 0);
|
||||||
|
*/
|
||||||
|
|
37
src/quad.c
37
src/quad.c
|
@ -1,4 +1,5 @@
|
||||||
#include <gearlib.h>
|
#include <gearlib.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
vec3 quad_vertex_positions[6] = {
|
vec3 quad_vertex_positions[6] = {
|
||||||
{ -0.5f, 0.5f, 0.0f },
|
{ -0.5f, 0.5f, 0.0f },
|
||||||
|
@ -10,14 +11,36 @@ vec3 quad_vertex_positions[6] = {
|
||||||
{ 0.5f, 0.5f, 0.0f },
|
{ 0.5f, 0.5f, 0.0f },
|
||||||
};
|
};
|
||||||
|
|
||||||
void draw_rectangle(RenderBatch* batch, mat4 transform, vec4 color) {
|
RenderBatch* quad_batch = NULL;
|
||||||
if(batch_needs_flush(batch, 6))
|
|
||||||
|
void setup_quads() {
|
||||||
|
quad_batch = create_quad_batch();
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw_quad(vec2 pos, vec2 size, vec4 color) {
|
||||||
|
mat4 transform = mat4Multiply(
|
||||||
|
mat4Scale(size.x, size.y, 1.0f),
|
||||||
|
mat4Translate(pos.x, pos.y, 0.0f));
|
||||||
|
|
||||||
|
draw_quad_trans(transform, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw_quad_trans(mat4 transform, vec4 color) {
|
||||||
|
assert(quad_batch != NULL && "quad_batch is null, was setup_quads() called?");
|
||||||
|
|
||||||
|
batch_draw_quad(quad_batch, transform, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
void batch_draw_quad(RenderBatch* batch, mat4 transform, vec4 color) {
|
||||||
|
uint32_t vertex_add = 6;
|
||||||
|
|
||||||
|
if(batch_needs_flush(batch, vertex_add))
|
||||||
flush_batch(batch);
|
flush_batch(batch);
|
||||||
|
|
||||||
for(int i = 0; i < 6; i++) {
|
for(int i = 0; i < vertex_add; i++) {
|
||||||
QuadVertex* vertex = batch->vertex_ptr;
|
QuadVertex* vertex = batch->vertex_ptr;
|
||||||
|
|
||||||
vertex->Position = quad_vertex_positions[i];
|
vertex->Position = vec3Transform(quad_vertex_positions[i], transform);
|
||||||
vertex->Color = color;
|
vertex->Color = color;
|
||||||
|
|
||||||
batch->vertex_ptr += batch->vertex_size;
|
batch->vertex_ptr += batch->vertex_size;
|
||||||
|
@ -25,11 +48,11 @@ void draw_rectangle(RenderBatch* batch, mat4 transform, vec4 color) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup_quad() {
|
RenderBatch* create_quad_batch() {
|
||||||
RenderBatch* quad_batch = create_batch(sizeof(QuadVertex), MAX_VERTICES);
|
RenderBatch* quad_batch = create_batch(sizeof(QuadVertex), MAX_VERTICES);
|
||||||
quad_batch->shader = load_shader_program(
|
quad_batch->shader = load_shader_program(
|
||||||
compile_shader("assets/quad.vert", GL_VERTEX_SHADER),
|
compile_shader("assets/quad.vert", GL_VERTEX_SHADER),
|
||||||
compile_shader("assets/quad.frag", GL_FRAGMENT_SHADER), 0);;
|
compile_shader("assets/quad.frag", GL_FRAGMENT_SHADER), 0);
|
||||||
|
|
||||||
batch_add_attrib(quad_batch, (VertexAttrib){
|
batch_add_attrib(quad_batch, (VertexAttrib){
|
||||||
.type = GL_FLOAT, .size = sizeof(float), .count = 3
|
.type = GL_FLOAT, .size = sizeof(float), .count = 3
|
||||||
|
@ -40,4 +63,6 @@ void setup_quad() {
|
||||||
}); // color
|
}); // color
|
||||||
|
|
||||||
batch_bind_attribs(quad_batch);
|
batch_bind_attribs(quad_batch);
|
||||||
|
|
||||||
|
return quad_batch;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include <stb_image.h>
|
#include <stb_image.h>
|
||||||
#include <opengl.h>
|
#include <opengl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
RenderBatch* texture_quad_batch = NULL;
|
RenderBatch* texture_quad_batch = NULL;
|
||||||
int max_textures;
|
int max_textures;
|
||||||
|
@ -14,8 +15,6 @@ void setup_textures() {
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t load_texture(const char* path) {
|
uint32_t load_texture(const char* path) {
|
||||||
stbi_set_flip_vertically_on_load(1);
|
|
||||||
|
|
||||||
uint32_t id;
|
uint32_t id;
|
||||||
glCreateTextures(GL_TEXTURE_2D, 1, &id);
|
glCreateTextures(GL_TEXTURE_2D, 1, &id);
|
||||||
|
|
||||||
|
@ -38,10 +37,6 @@ uint32_t load_texture(const char* path) {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw_texture(uint32_t id, vec3 pos, vec4 color) {
|
|
||||||
batch_draw_texture(texture_quad_batch, id, pos, color);
|
|
||||||
}
|
|
||||||
|
|
||||||
vec2 texture_quad_texcoords[] = {
|
vec2 texture_quad_texcoords[] = {
|
||||||
{ 0.0f, 1.0f },
|
{ 0.0f, 1.0f },
|
||||||
{ 0.0f, 0.0f },
|
{ 0.0f, 0.0f },
|
||||||
|
@ -52,11 +47,24 @@ vec2 texture_quad_texcoords[] = {
|
||||||
{ 1.0f, 1.0f }
|
{ 1.0f, 1.0f }
|
||||||
};
|
};
|
||||||
|
|
||||||
void batch_draw_texture(RenderBatch* batch, uint32_t texture, vec3 pos, vec4 color) {
|
void draw_texture(Texture id, vec2 pos, vec2 size, vec4 tint) {
|
||||||
|
mat4 transform = mat4Multiply(
|
||||||
|
mat4Scale(size.x, size.y, 1.0f),
|
||||||
|
mat4Translate(pos.x, pos.y, 0.0f));
|
||||||
|
|
||||||
|
draw_texture_trans(id, transform, tint);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 batch_draw_texture(RenderBatch* batch, Texture texture, mat4 transform, vec4 color) {
|
||||||
TextureQuadBatchData* batch_data = batch->data;
|
TextureQuadBatchData* batch_data = batch->data;
|
||||||
uint32_t vertex_add = 6;
|
uint32_t vertex_add = 6;
|
||||||
|
|
||||||
if(batch_needs_flush(batch, vertex_add) || batch_data->texture_index > max_textures)
|
if(batch_needs_flush(batch, vertex_add) || batch_data->texture_index >= max_textures)
|
||||||
flush_batch(batch);
|
flush_batch(batch);
|
||||||
|
|
||||||
uint32_t tex_id = batch_data->texture_index++;
|
uint32_t tex_id = batch_data->texture_index++;
|
||||||
|
@ -65,7 +73,7 @@ void batch_draw_texture(RenderBatch* batch, uint32_t texture, vec3 pos, vec4 col
|
||||||
for(int i = 0; i < vertex_add; i++) {
|
for(int i = 0; i < vertex_add; i++) {
|
||||||
TextureQuadVertex* vertex = batch->vertex_ptr;
|
TextureQuadVertex* vertex = batch->vertex_ptr;
|
||||||
|
|
||||||
vertex->Position = vec3Add(quad_vertex_positions[i], pos);
|
vertex->Position = vec3Transform(quad_vertex_positions[i], transform);
|
||||||
vertex->Tint = color;
|
vertex->Tint = color;
|
||||||
vertex->TexCoord = texture_quad_texcoords[i];
|
vertex->TexCoord = texture_quad_texcoords[i];
|
||||||
vertex->TexID = tex_id;
|
vertex->TexID = tex_id;
|
||||||
|
@ -75,7 +83,7 @@ void batch_draw_texture(RenderBatch* batch, uint32_t texture, vec3 pos, vec4 col
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void texture_quad_batch_flush_callback(RenderBatch* batch) {
|
void texture_flush_callback(RenderBatch* batch) {
|
||||||
TextureQuadBatchData* data = batch->data;
|
TextureQuadBatchData* data = batch->data;
|
||||||
data->texture_index = 0;
|
data->texture_index = 0;
|
||||||
}
|
}
|
||||||
|
@ -85,8 +93,9 @@ RenderBatch* create_texture_quad_batch() {
|
||||||
texture_quad_batch->shader = load_shader_program(
|
texture_quad_batch->shader = load_shader_program(
|
||||||
compile_shader("assets/texture.vert", GL_VERTEX_SHADER),
|
compile_shader("assets/texture.vert", GL_VERTEX_SHADER),
|
||||||
compile_shader("assets/texture.frag", GL_FRAGMENT_SHADER), 0);
|
compile_shader("assets/texture.frag", GL_FRAGMENT_SHADER), 0);
|
||||||
|
|
||||||
texture_quad_batch->data = calloc(sizeof(TextureQuadBatchData), 1);
|
texture_quad_batch->data = calloc(sizeof(TextureQuadBatchData), 1);
|
||||||
texture_quad_batch->flush_callback = &texture_quad_batch_flush_callback;
|
texture_quad_batch->flush_callback = &texture_flush_callback;
|
||||||
|
|
||||||
batch_add_attrib(texture_quad_batch, (VertexAttrib){
|
batch_add_attrib(texture_quad_batch, (VertexAttrib){
|
||||||
.type = GL_FLOAT, .size = sizeof(float), .count = 3
|
.type = GL_FLOAT, .size = sizeof(float), .count = 3
|
||||||
|
|
Loading…
Reference in a new issue