Cleaned up VBO handling
This commit is contained in:
parent
ea924e716d
commit
31d6665d21
4 changed files with 68 additions and 104 deletions
|
@ -68,9 +68,9 @@ void draw_fbo_viewport(FBO *fbo) {
|
|||
|
||||
glBindTexture(GL_TEXTURE_2D, fbo->tex);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, _quadvbo);
|
||||
// glBindBuffer(GL_ARRAY_BUFFER, _vbo);
|
||||
glDrawArrays(GL_QUADS, 4, 4);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
// glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
glDisable(GL_TEXTURE_2D);
|
||||
|
||||
|
|
|
@ -44,7 +44,7 @@ void taisei_shutdown() {
|
|||
delete_fbo(&resources.fbg[1]);
|
||||
delete_fbo(&resources.fsec);
|
||||
printf("-- freeing VBOs\n");
|
||||
delete_vbo(&_quadvbo);
|
||||
delete_vbo(&_vbo);
|
||||
printf("-- freeing shaders\n");
|
||||
delete_shaders();
|
||||
|
||||
|
|
127
src/vbo.c
127
src/vbo.c
|
@ -9,103 +9,66 @@
|
|||
#include <string.h>
|
||||
#include "taisei_err.h"
|
||||
|
||||
GLuint _quadvbo;
|
||||
VBO _vbo;
|
||||
|
||||
/*void init_partbuf(PartBuffer *buf) {
|
||||
memset(buf, 0, sizeof(buf));
|
||||
}
|
||||
void init_vbo(VBO *vbo, int size) {
|
||||
memset(vbo, 0, sizeof(VBO));
|
||||
vbo->size = size;
|
||||
|
||||
glGenBuffers(1, &vbo->vbo);
|
||||
|
||||
void destroy_partbuf(PartBuffer *buf){
|
||||
free(buf->poss);
|
||||
free(buf->texcs);
|
||||
free(buf->clrs);
|
||||
}
|
||||
|
||||
void partbuf_add_batch(PartBuffer *buf, Matrix m, Vector tex, Color *clr) {
|
||||
if(buf->cursor >= buf->size) {
|
||||
buf->size += 10;
|
||||
buf->poss = realloc(buf->poss, buf->size*sizeof(Matrix));
|
||||
buf->texcs = realloc(buf->texcs, buf->size*sizeof(Vector));
|
||||
buf->clrs = realloc(buf->clrs, buf->size*sizeof(Color));
|
||||
}
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo->vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*size, NULL, GL_STATIC_DRAW);
|
||||
|
||||
memcpy(&buf->poss[buf->cursor], m, sizeof(Matrix));
|
||||
memcpy(&buf->texcs[buf->cursor], tex, sizeof(Vector));
|
||||
|
||||
if(clr != NULL) {
|
||||
memcpy(&buf->clrs[buf->cursor], clr, sizeof(Color));
|
||||
} else {
|
||||
memset(&buf->clrs[buf->cursor], 0, sizeof(Color));
|
||||
}
|
||||
|
||||
buf->cursor++;
|
||||
}
|
||||
|
||||
void partbuf_clear(PartBuffer *buf) {
|
||||
buf->cursor = 0;
|
||||
}
|
||||
|
||||
void partbuf_draw(PartBuffer *buf, GLuint shader, char *posname, char *tcname, char *clrname) {
|
||||
glUniformMatrix4fv(glGetUniformLocation(shader, posname), buf->cursor, 0, (GLfloat *)buf->poss);
|
||||
glUniform3fv(glGetUniformLocation(shader, tcname), buf->cursor, (GLfloat *)buf->texcs);
|
||||
glUniform4fv(glGetUniformLocation(shader, clrname), buf->cursor, (GLfloat *)buf->clrs);
|
||||
|
||||
glDrawArraysInstanced(GL_QUADS, 0, buf->cursor-1, buf->cursor);
|
||||
}
|
||||
|
||||
void partbuf_add(PartBuffer *buf, Matrix m, Texture *tex, Color *clr) {
|
||||
Vector texscale = {1,1,0};
|
||||
|
||||
Matrix m2;
|
||||
matscale(m2, m, tex->w, tex->h, tex->gltex);
|
||||
// Vector scale = {tex->w, tex->h, tex->gltex};
|
||||
|
||||
partbuf_add_batch(buf, m2, texscale, clr);
|
||||
}*/
|
||||
|
||||
void init_quadvbo() {
|
||||
glGenBuffers(1, &_quadvbo);
|
||||
|
||||
Vector verts[] = {
|
||||
{-0.5,-0.5,0}, {0,0,0},
|
||||
{-0.5,0.5,0}, {0,1,0},
|
||||
{0.5,0.5,0}, {1,1,0},
|
||||
{0.5,-0.5,0}, {1,0,0},
|
||||
|
||||
// Alternative quad for FBO
|
||||
{-0.5,-0.5,0}, {0,1,0},
|
||||
{-0.5,0.5,0}, {0,0,0},
|
||||
{0.5,0.5,0}, {1,0,0},
|
||||
{0.5,-0.5,0}, {1,1,0}
|
||||
};
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, _quadvbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(Vector)*16, verts, GL_STATIC_DRAW);
|
||||
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(float)*6, NULL);
|
||||
glTexCoordPointer(3, GL_FLOAT, sizeof(float)*6, NULL + 3*sizeof(float));
|
||||
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), NULL);
|
||||
glNormalPointer(GL_FLOAT, sizeof(Vertex), NULL + sizeof(Vector));
|
||||
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), NULL + 2*sizeof(Vector));
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
glEnableClientState(GL_VERTEX_ARRAY);
|
||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
glEnableClientState(GL_NORMAL_ARRAY);
|
||||
}
|
||||
|
||||
void delete_vbo(GLuint *vbo) {
|
||||
glDeleteBuffers(1, vbo);
|
||||
void vbo_add_verts(VBO *vbo, Vertex *verts, int count) {
|
||||
if(vbo->offset + count > vbo->size)
|
||||
errx(-1, "vbo_add_verts():\n !- Cannot add Vertices: VBO too small!\n");
|
||||
|
||||
glBufferSubData(GL_ARRAY_BUFFER, sizeof(Vertex)*vbo->offset, sizeof(Vertex)*count, verts);
|
||||
|
||||
vbo->offset += count;
|
||||
}
|
||||
|
||||
void init_quadvbo() {
|
||||
Vertex verts[] = {
|
||||
{{-0.5,-0.5,0},{0,0,1},0,0},
|
||||
{{-0.5,0.5,0},{0,0,1},0,1},
|
||||
{{0.5,0.5,0},{0,0,1},1,1},
|
||||
{{0.5,-0.5,0},{0,0,1},1,0},
|
||||
|
||||
// Alternative quad for FBO
|
||||
{{-0.5,-0.5,0},{0,0,1},0,1},
|
||||
{{-0.5,0.5,0},{0,0,1},0,0},
|
||||
{{0.5,0.5,0},{0,0,1},1,0},
|
||||
{{0.5,-0.5,0},{0,0,1},1,1}
|
||||
};
|
||||
|
||||
init_vbo(&_vbo, VBO_SIZE);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, _vbo.vbo);
|
||||
|
||||
vbo_add_verts(&_vbo, verts, 8);
|
||||
}
|
||||
|
||||
void delete_vbo(VBO *vbo) {
|
||||
glDeleteBuffers(1, &vbo->vbo);
|
||||
}
|
||||
|
||||
void draw_quad() {
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, _quadvbo);
|
||||
|
||||
// glEnableClientState(GL_VERTEX_ARRAY);
|
||||
// glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
// glBindBuffer(GL_ARRAY_BUFFER, _vbo.vbo);
|
||||
|
||||
glDrawArrays(GL_QUADS, 0, 4);
|
||||
|
||||
// glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
// glDisableClientState(GL_VERTEX_ARRAY);
|
||||
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
// glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
}
|
39
src/vbo.h
39
src/vbo.h
|
@ -13,31 +13,32 @@
|
|||
#include "resource/shader.h"
|
||||
#include <GL/glew.h>
|
||||
|
||||
/* PartBuffer for fast instanced Particle/whatever (maybe everything) rendering (for now rocket science)*/
|
||||
/*
|
||||
typedef struct {
|
||||
Matrix *poss;
|
||||
Vector *texcs;
|
||||
Color *clrs;
|
||||
|
||||
int cursor;
|
||||
enum {
|
||||
VBO_SIZE = 128, // * sizeof(Vertex)
|
||||
};
|
||||
|
||||
typedef struct VBO VBO;
|
||||
struct VBO {
|
||||
GLuint vbo;
|
||||
int offset;
|
||||
int size;
|
||||
} PartBuffer;
|
||||
};
|
||||
|
||||
extern VBO _vbo;
|
||||
|
||||
void init_partbuf(PartBuffer *buf);
|
||||
void destroy_partbuf(PartBuffer *buf);
|
||||
typedef struct Vertex Vertex;
|
||||
struct Vertex {
|
||||
Vector x;
|
||||
Vector n;
|
||||
float s;
|
||||
float t;
|
||||
};
|
||||
|
||||
void partbuf_add_batch(PartBuffer *buf, Matrix m, Vector v, Color *clr);
|
||||
void partbuf_clear(PartBuffer *buf);
|
||||
void partbuf_draw(PartBuffer *buf, GLuint shader, char *posname, char *tcname, char *clrname);
|
||||
void partbuf_add(PartBuffer *buf, Matrix m, Texture *tex, Color *clr);
|
||||
*/
|
||||
|
||||
extern GLuint _quadvbo;
|
||||
void init_vbo(VBO *vbo, int size);
|
||||
void vbo_add_verts(VBO *vbo, Vertex *verts, int count);
|
||||
|
||||
void init_quadvbo();
|
||||
void draw_quad();
|
||||
void delete_vbo(GLuint *vbo);
|
||||
void delete_vbo(VBO *vbo);
|
||||
|
||||
#endif
|
Loading…
Reference in a new issue