Made sprite batch 2d only

dev
Shariq Shah 8 years ago
parent a976a9aa2e
commit 6728ac5f8c
  1. 4
      assets/shaders/sprite.vert
  2. 12
      src/libsymmetry/game.c
  3. 6
      src/libsymmetry/renderer.c
  4. 2
      src/libsymmetry/renderer.h
  5. 8
      src/libsymmetry/shader.c
  6. 8
      src/libsymmetry/shader.h
  7. 30
      src/libsymmetry/sprite.c
  8. 11
      src/libsymmetry/sprite.h

@ -2,7 +2,7 @@
uniform mat4 mvp; uniform mat4 mvp;
in vec3 vPosition; in vec2 vPosition;
in vec2 vUV; in vec2 vUV;
in vec4 vColor; in vec4 vColor;
@ -11,7 +11,7 @@ out vec4 color;
void main() void main()
{ {
gl_Position = mvp * vec4(vPosition, 1.0); gl_Position = mvp * vec4(vPosition, 0.0, 1.0);
uv = vUV; uv = vUV;
color = vColor; color = vColor;
} }

@ -405,12 +405,12 @@ void debug(float dt)
sprite_batch_begin(batch); sprite_batch_begin(batch);
static struct Sprite sprite; 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[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[1].position.z = 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[2].position.z = 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[3].position.z = 0.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[4].position.z = 0.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[5].position.z = 0.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[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; sprite.vertices[1].uv.x = 1.f; sprite.vertices[1].uv.y = 0.f;

@ -405,6 +405,8 @@ void renderer_draw(struct Entity* active_viewer)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
} }
shader_unbind();
/* Render 2D stuff */ /* Render 2D stuff */
shader_bind(sprite_batch->shader); shader_bind(sprite_batch->shader);
{ {
@ -480,6 +482,10 @@ int renderer_check_glerror(const char* context)
return error; return error;
} }
void im_render_box(float position, float length, float width, float height)
{
}
struct Sprite_Batch * get_batch(void) struct Sprite_Batch * get_batch(void)
{ {
return sprite_batch; return sprite_batch;

@ -43,6 +43,8 @@ void renderer_clearcolor_set(float r, float g, float b, float a);
void renderer_debug_draw_enabled(bool enabled); void renderer_debug_draw_enabled(bool enabled);
int renderer_check_glerror(const char* context); 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); struct Sprite_Batch* get_batch(void);
#endif #endif

@ -152,10 +152,10 @@ int shader_create(const char* vert_shader_name, const char* frag_shader_name)
glAttachShader(program, frag_shader); glAttachShader(program, frag_shader);
// Bind attribute locations // Bind attribute locations
glBindAttribLocation(program, AL_POSITION, "vPosition"); glBindAttribLocation(program, ATTRIB_LOC_POSITION, "vPosition");
glBindAttribLocation(program, AL_NORMAL, "vNormal"); glBindAttribLocation(program, ATTRIB_LOC_NORMAL, "vNormal");
glBindAttribLocation(program, AL_UV, "vUV"); glBindAttribLocation(program, ATRRIB_LOC_UV, "vUV");
glBindAttribLocation(program, AL_COLOR, "vColor"); glBindAttribLocation(program, ATTRIB_LOC_COLOR, "vColor");
renderer_check_glerror("shader:create"); renderer_check_glerror("shader:create");
glLinkProgram(program); glLinkProgram(program);

@ -6,10 +6,10 @@
// Constants for locations of attributes inside all shaders // Constants for locations of attributes inside all shaders
enum Attribute_Location enum Attribute_Location
{ {
AL_POSITION = 0, ATTRIB_LOC_POSITION = 0,
AL_NORMAL = 1, ATTRIB_LOC_NORMAL = 1,
AL_UV = 2, ATRRIB_LOC_UV = 2,
AL_COLOR = 3 ATTRIB_LOC_COLOR = 3
}; };
enum Uniform_Type enum Uniform_Type

@ -9,18 +9,6 @@
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
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) 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); assert(batch);
@ -32,27 +20,27 @@ void sprite_batch_create(struct Sprite_Batch* batch, const char* texture_name, c
glGenBuffers(1, &batch->vbo); glGenBuffers(1, &batch->vbo);
glBindBuffer(GL_ARRAY_BUFFER, batch->vbo); glBindBuffer(GL_ARRAY_BUFFER, batch->vbo);
glBufferData(GL_ARRAY_BUFFER, glBufferData(GL_ARRAY_BUFFER,
sizeof(struct Vertex) * MAX_SPRITE_VERTICES * SPRITE_BATCH_SIZE, sizeof(struct Sprite_Vertex) * MAX_SPRITE_VERTICES * SPRITE_BATCH_SIZE,
NULL, NULL,
GL_STREAM_DRAW); GL_STREAM_DRAW);
renderer_check_glerror("sprite_batch_create:glBufferData"); renderer_check_glerror("sprite_batch_create:glBufferData");
// Position // 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"); renderer_check_glerror("sprite_batch_create:glVertexAttribPointer");
glEnableVertexAttribArray(AL_POSITION); glEnableVertexAttribArray(ATTRIB_LOC_POSITION);
renderer_check_glerror("sprite_batch_create:glEnableVertexAttribPointer"); renderer_check_glerror("sprite_batch_create:glEnableVertexAttribPointer");
// Uvs // 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"); renderer_check_glerror("sprite_batch_create:glVertexAttribPointer");
glEnableVertexAttribArray(AL_UV); glEnableVertexAttribArray(ATRRIB_LOC_UV);
renderer_check_glerror("sprite_batch_create:glEnableVertexAttribPointer"); renderer_check_glerror("sprite_batch_create:glEnableVertexAttribPointer");
// Color // 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"); renderer_check_glerror("sprite_batch_create:glVertexAttribPointer");
glEnableVertexAttribArray(AL_COLOR); glEnableVertexAttribArray(ATTRIB_LOC_COLOR);
renderer_check_glerror("sprite_batch_create:glEnableVertexAttribPointer"); renderer_check_glerror("sprite_batch_create:glEnableVertexAttribPointer");
//glBindBuffer(GL_ARRAY_BUFFER, 0); //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); assert(batch);
if(batch->current_sprite_count < SPRITE_BATCH_SIZE) 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); glBindBuffer(GL_ARRAY_BUFFER, batch->vbo);
glBufferSubData(GL_ARRAY_BUFFER, glBufferSubData(GL_ARRAY_BUFFER,
0, 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]); &batch->sprites[0]);
renderer_check_glerror("sprite_batch_end:glBufferSubData"); renderer_check_glerror("sprite_batch_end:glBufferSubData");
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);

@ -7,16 +7,16 @@
#define SPRITE_BATCH_SIZE 1024 #define SPRITE_BATCH_SIZE 1024
#define MAX_SPRITE_VERTICES 6 #define MAX_SPRITE_VERTICES 6
struct Vertex struct Sprite_Vertex
{ {
vec3 position; vec2 position;
vec2 uv; vec2 uv;
vec4 color; vec4 color;
}; };
struct Sprite struct Sprite
{ {
struct Vertex vertices[MAX_SPRITE_VERTICES]; struct Sprite_Vertex vertices[MAX_SPRITE_VERTICES];
}; };
struct Sprite_Batch struct Sprite_Batch
@ -30,14 +30,11 @@ struct Sprite_Batch
int current_sprite_count; 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_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_remove(struct Sprite_Batch* batch);
void sprite_batch_begin(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(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_end(struct Sprite_Batch* batch);
void sprite_batch_render(struct Sprite_Batch* batch); void sprite_batch_render(struct Sprite_Batch* batch);

Loading…
Cancel
Save