From 6728ac5f8c54801e5fa4d499fd6a1ba11e17775b Mon Sep 17 00:00:00 2001 From: Shariq Shah Date: Wed, 3 Jan 2018 17:07:09 +1100 Subject: [PATCH] Made sprite batch 2d only --- assets/shaders/sprite.vert | 4 ++-- src/libsymmetry/game.c | 12 ++++++------ src/libsymmetry/renderer.c | 6 ++++++ src/libsymmetry/renderer.h | 2 ++ src/libsymmetry/shader.c | 8 ++++---- src/libsymmetry/shader.h | 8 ++++---- src/libsymmetry/sprite.c | 30 +++++++++--------------------- src/libsymmetry/sprite.h | 11 ++++------- 8 files changed, 37 insertions(+), 44 deletions(-) diff --git a/assets/shaders/sprite.vert b/assets/shaders/sprite.vert index 1d00706..2ed83c9 100644 --- a/assets/shaders/sprite.vert +++ b/assets/shaders/sprite.vert @@ -2,7 +2,7 @@ uniform mat4 mvp; -in vec3 vPosition; +in vec2 vPosition; in vec2 vUV; in vec4 vColor; @@ -11,7 +11,7 @@ out vec4 color; void main() { - gl_Position = mvp * vec4(vPosition, 1.0); + gl_Position = mvp * vec4(vPosition, 0.0, 1.0); uv = vUV; color = vColor; } diff --git a/src/libsymmetry/game.c b/src/libsymmetry/game.c index 8b6d910..6acd0d9 100644 --- a/src/libsymmetry/game.c +++ b/src/libsymmetry/game.c @@ -405,12 +405,12 @@ void debug(float dt) sprite_batch_begin(batch); static struct Sprite sprite; - sprite.vertices[0].position.x = 0.f; sprite.vertices[0].position.y = 0.f; sprite.vertices[0].position.z = 0.f; - sprite.vertices[1].position.x = 50.f; sprite.vertices[1].position.y = 0.f; sprite.vertices[1].position.z = 0.f; - sprite.vertices[2].position.x = 0.f; sprite.vertices[2].position.y = 50.f; sprite.vertices[2].position.z = 0.f; - sprite.vertices[3].position.x = 0.f; sprite.vertices[3].position.y = 50.f; sprite.vertices[3].position.z = 0.f; - sprite.vertices[4].position.x = 50.f; sprite.vertices[4].position.y = 50.f; sprite.vertices[4].position.z = 0.f; - sprite.vertices[5].position.x = 50.f; sprite.vertices[5].position.y = 0.f; sprite.vertices[5].position.z = 0.f; + sprite.vertices[0].position.x = 0.f; sprite.vertices[0].position.y = 0.f; + sprite.vertices[1].position.x = 50.f; sprite.vertices[1].position.y = 0.f; + sprite.vertices[2].position.x = 0.f; sprite.vertices[2].position.y = 50.f; + sprite.vertices[3].position.x = 0.f; sprite.vertices[3].position.y = 50.f; + sprite.vertices[4].position.x = 50.f; sprite.vertices[4].position.y = 50.f; + sprite.vertices[5].position.x = 50.f; sprite.vertices[5].position.y = 0.f; sprite.vertices[0].uv.x = 0.f; sprite.vertices[0].uv.y = 0.f; sprite.vertices[1].uv.x = 1.f; sprite.vertices[1].uv.y = 0.f; diff --git a/src/libsymmetry/renderer.c b/src/libsymmetry/renderer.c index 474b457..4b1edb3 100644 --- a/src/libsymmetry/renderer.c +++ b/src/libsymmetry/renderer.c @@ -405,6 +405,8 @@ void renderer_draw(struct Entity* active_viewer) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); } + shader_unbind(); + /* Render 2D stuff */ shader_bind(sprite_batch->shader); { @@ -480,6 +482,10 @@ int renderer_check_glerror(const char* context) return error; } +void im_render_box(float position, float length, float width, float height) +{ +} + struct Sprite_Batch * get_batch(void) { return sprite_batch; diff --git a/src/libsymmetry/renderer.h b/src/libsymmetry/renderer.h index b73c304..134f86f 100644 --- a/src/libsymmetry/renderer.h +++ b/src/libsymmetry/renderer.h @@ -43,6 +43,8 @@ void renderer_clearcolor_set(float r, float g, float b, float a); void renderer_debug_draw_enabled(bool enabled); int renderer_check_glerror(const char* context); +void im_render_box(float position, float length, float width, float height); + struct Sprite_Batch* get_batch(void); #endif diff --git a/src/libsymmetry/shader.c b/src/libsymmetry/shader.c index 4c07183..04d1260 100644 --- a/src/libsymmetry/shader.c +++ b/src/libsymmetry/shader.c @@ -152,10 +152,10 @@ int shader_create(const char* vert_shader_name, const char* frag_shader_name) glAttachShader(program, frag_shader); // Bind attribute locations - glBindAttribLocation(program, AL_POSITION, "vPosition"); - glBindAttribLocation(program, AL_NORMAL, "vNormal"); - glBindAttribLocation(program, AL_UV, "vUV"); - glBindAttribLocation(program, AL_COLOR, "vColor"); + glBindAttribLocation(program, ATTRIB_LOC_POSITION, "vPosition"); + glBindAttribLocation(program, ATTRIB_LOC_NORMAL, "vNormal"); + glBindAttribLocation(program, ATRRIB_LOC_UV, "vUV"); + glBindAttribLocation(program, ATTRIB_LOC_COLOR, "vColor"); renderer_check_glerror("shader:create"); glLinkProgram(program); diff --git a/src/libsymmetry/shader.h b/src/libsymmetry/shader.h index 7921fc7..a3039ed 100644 --- a/src/libsymmetry/shader.h +++ b/src/libsymmetry/shader.h @@ -6,10 +6,10 @@ // Constants for locations of attributes inside all shaders enum Attribute_Location { - AL_POSITION = 0, - AL_NORMAL = 1, - AL_UV = 2, - AL_COLOR = 3 + ATTRIB_LOC_POSITION = 0, + ATTRIB_LOC_NORMAL = 1, + ATRRIB_LOC_UV = 2, + ATTRIB_LOC_COLOR = 3 }; enum Uniform_Type diff --git a/src/libsymmetry/sprite.c b/src/libsymmetry/sprite.c index ee5b1d0..89159c2 100644 --- a/src/libsymmetry/sprite.c +++ b/src/libsymmetry/sprite.c @@ -9,18 +9,6 @@ #include #include -static int sprite_count = 0; - -void sprite_init(void) -{ - -} - -void sprite_cleanup(void) -{ - -} - void sprite_batch_create(struct Sprite_Batch* batch, const char* texture_name, const char* vert_shader, const char* frag_shader, int draw_mode) { assert(batch); @@ -32,27 +20,27 @@ void sprite_batch_create(struct Sprite_Batch* batch, const char* texture_name, c glGenBuffers(1, &batch->vbo); glBindBuffer(GL_ARRAY_BUFFER, batch->vbo); glBufferData(GL_ARRAY_BUFFER, - sizeof(struct Vertex) * MAX_SPRITE_VERTICES * SPRITE_BATCH_SIZE, + sizeof(struct Sprite_Vertex) * MAX_SPRITE_VERTICES * SPRITE_BATCH_SIZE, NULL, GL_STREAM_DRAW); renderer_check_glerror("sprite_batch_create:glBufferData"); // Position - glVertexAttribPointer(AL_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), 0); + glVertexAttribPointer(ATTRIB_LOC_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(struct Sprite_Vertex), 0); renderer_check_glerror("sprite_batch_create:glVertexAttribPointer"); - glEnableVertexAttribArray(AL_POSITION); + glEnableVertexAttribArray(ATTRIB_LOC_POSITION); renderer_check_glerror("sprite_batch_create:glEnableVertexAttribPointer"); // Uvs - glVertexAttribPointer(AL_UV, 2, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), sizeof(vec3)); + glVertexAttribPointer(ATRRIB_LOC_UV, 2, GL_FLOAT, GL_FALSE, sizeof(struct Sprite_Vertex), sizeof(vec2)); renderer_check_glerror("sprite_batch_create:glVertexAttribPointer"); - glEnableVertexAttribArray(AL_UV); + glEnableVertexAttribArray(ATRRIB_LOC_UV); renderer_check_glerror("sprite_batch_create:glEnableVertexAttribPointer"); // Color - glVertexAttribPointer(AL_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), sizeof(vec3) + sizeof(vec2)); + glVertexAttribPointer(ATTRIB_LOC_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof(struct Sprite_Vertex), sizeof(vec2) + sizeof(vec2)); renderer_check_glerror("sprite_batch_create:glVertexAttribPointer"); - glEnableVertexAttribArray(AL_COLOR); + glEnableVertexAttribArray(ATTRIB_LOC_COLOR); renderer_check_glerror("sprite_batch_create:glEnableVertexAttribPointer"); //glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -113,7 +101,7 @@ void sprite_batch_add_sprite(struct Sprite_Batch* batch, struct Sprite* sprite) } } -void sprite_batch_add_sprite_new(struct Sprite_Batch* batch, int texture, struct Vertex vertices[MAX_SPRITE_VERTICES]) +void sprite_batch_add_sprite_new(struct Sprite_Batch* batch, int texture, struct Sprite_Vertex vertices[MAX_SPRITE_VERTICES]) { assert(batch); if(batch->current_sprite_count < SPRITE_BATCH_SIZE) @@ -133,7 +121,7 @@ void sprite_batch_end(struct Sprite_Batch* batch) glBindBuffer(GL_ARRAY_BUFFER, batch->vbo); glBufferSubData(GL_ARRAY_BUFFER, 0, - sizeof(struct Vertex) * MAX_SPRITE_VERTICES * batch->current_sprite_count, + sizeof(struct Sprite_Vertex) * MAX_SPRITE_VERTICES * batch->current_sprite_count, &batch->sprites[0]); renderer_check_glerror("sprite_batch_end:glBufferSubData"); glBindBuffer(GL_ARRAY_BUFFER, 0); diff --git a/src/libsymmetry/sprite.h b/src/libsymmetry/sprite.h index 8d30499..6a8d167 100644 --- a/src/libsymmetry/sprite.h +++ b/src/libsymmetry/sprite.h @@ -7,16 +7,16 @@ #define SPRITE_BATCH_SIZE 1024 #define MAX_SPRITE_VERTICES 6 -struct Vertex +struct Sprite_Vertex { - vec3 position; + vec2 position; vec2 uv; vec4 color; }; struct Sprite { - struct Vertex vertices[MAX_SPRITE_VERTICES]; + struct Sprite_Vertex vertices[MAX_SPRITE_VERTICES]; }; struct Sprite_Batch @@ -30,14 +30,11 @@ struct Sprite_Batch int current_sprite_count; }; -void sprite_init(void); -void sprite_cleanup(void); - void sprite_batch_create(struct Sprite_Batch* batch, const char* texture_name, const char* vert_shader, const char* frag_shader, int draw_mode); void sprite_batch_remove(struct Sprite_Batch* batch); void sprite_batch_begin(struct Sprite_Batch* batch); void sprite_batch_add_sprite(struct Sprite_Batch* batch, struct Sprite* sprite); -void sprite_batch_add_sprite_new(struct Sprite_Batch* batch, int texture, struct Vertex vertices[MAX_SPRITE_VERTICES]); +void sprite_batch_add_sprite_new(struct Sprite_Batch* batch, int texture, struct Sprite_Vertex vertices[MAX_SPRITE_VERTICES]); void sprite_batch_end(struct Sprite_Batch* batch); void sprite_batch_render(struct Sprite_Batch* batch);