I am having some trouble building a buffer and sending the data over to the gpu in a single draw call instead of multiple draw calls.
I might be having issues in multiple places but the place that I am pretty sure is concatinating the smaller buffers into a larger one. vbo setup looks like this.
this is the quad and sprite data structures.
typedef struct {
vec3 angles;
GLshort vertex_count;
GLfloat vertices[12];
GLfloat colors[16];
GLshort indices[6];
GLfloat tex_coords[8];
} cg_quad;
typedef struct sprite {
cg_quad* quad;
vec3 scale;
vec3 pos;
vec3 angl;
mat4 m_mat;
GLuint texture_id;
}cg_sprite;
this is how i init my vbo and try to setup the gpu memory for the amount of sprites that I want to draw in one call.
v_buff = (float*)calloc(1, (sizeof(float*) * 48 * SPRITE_COUNT)); //allocating space for vertices
c_buff = (float*)calloc(1, (sizeof(float*) * 64 * SPRITE_COUNT));
t_buff = (float*)calloc(1, (sizeof(float*) * 32 * SPRITE_COUNT));
glGenBuffers(1, &vao);
glBindVertexArray(vao);
glEnableVertexAttribArray(0);
glGenBuffers(1, &vert_buff);
glBindBuffer(GL_ARRAY_BUFFER, vert_buff); //12 floats
glBufferData(GL_ARRAY_BUFFER, //SRITE_COUNT is 2 atm.
SPRITE_COUNT * sizeof(sprites[0]->quad->vertices), //is this the correct way to do this?
sprites[0]->quad->vertices, GL_STREAM_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat),
(GLvoid*)0);
glEnableVertexAttribArray(1);
glGenBuffers(1, &col_buff);
glBindBuffer(GL_ARRAY_BUFFER, col_buff);
glBufferData(GL_ARRAY_BUFFER,
SPRITE_COUNT * sizeof(sprites[0]->quad->colors), //16 floats
sprites[0]->quad->colors, GL_STREAM_DRAW);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat),
(GLvoid*)0);
glGenBuffers(1, &ind_buff);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ind_buff);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(sprites[0]->quad->indices), //4shorts
sprites[0]->quad->indices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
void copy_float_buffer(void* dest, size_t dest_index, void* src,
size_t src_index, size_t num_to_copy) {
memcpy(&dest[dest_index], &src[src_index], num_to_copy * sizeof(float));
}
for (int i = 0; i < SPRITE_COUNT; i++) {
vmathT3MakeIdentity(&rot);
vmathT3MakeIdentity(&scal);
vmathT3MakeIdentity(&trns);
vmathT3MakeIdentity(&tmp);
vmathT3MakeScale(&scal, &sprites[i]->scale);
vmathT3MakeRotationZYX(&rot, &sprites[i]->angl);
vmathT3MakeTranslation(&trns, &sprites[i]->pos);
vmathT3Mul(&tmp, &trns, &scal); // scale then trnslate
vmathT3Mul(&tmp, &tmp, &rot); // scale then translate then rotate
vmathM4MakeFromT3(&sprites[i]->m_mat, &tmp);
cg_quad_getquadverts(&iv0, &iv1, &iv2, &iv3, sprites[i]->quad); // here I grab each of the 4
vmathM4MulV4(&ov0, &sprites[i]->m_mat, &iv0); //vertex from the quad
vmathM4MulV4(&ov1, &sprites[i]->m_mat, &iv1); //then multiply them on the cpu
vmathM4MulV4(&ov2, &sprites[i]->m_mat, &iv2);
vmathM4MulV4(&ov3, &sprites[i]->m_mat, &iv3);
cg_quad_fillvertsbuff(v_buffer, &ov0, &ov1, &ov2, &ov3); //fill array puts the vertices in a temp
//array x,y,z,x1,y1,z1,ect
cg_quad_fillcolsbuff(c_buffer, sprites[i]->quad); //same for colors
cg_quad_filltexbuff(t_buffer, sprites[i]->quad); //and textures
copy_float_buffer(v_buff, (sizeof(float) * i) * 12, v_buffer, //i want to copy those 2 small buffers
(sizeof(float) * i) * 12, 12); //into the larger one to send to the gpu
copy_float_buffer(c_buff, (sizeof(float) * i) * 12, c_buffer,
(sizeof(float) * i) * 12, 12);
}
//then setup my matrices and send them over to the gpu
glUniformMatrix4fv(view_mat_loc, 1, GL_FALSE, vmathM4GetData(&v_mat));
glUniformMatrix4fv(proj_mat_loc, 1, GL_FALSE, vmathM4GetData(&p_mat));
glUniformMatrix4fv(mvp_matrix_loc, 1, GL_FALSE, vmathM4GetData(&mvp_mat));
glBindBuffer(GL_ARRAY_BUFFER, vert_buff);
glBufferData(GL_ARRAY_BUFFER,
SPRITE_COUNT * sizeof(sprites[0]->quad->vertices), v_buff,
GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, col_buff);
glBufferData(GL_ARRAY_BUFFER,
SPRITE_COUNT * sizeof(sprites[0]->quad->colors), c_buff,
GL_STREAM_DRAW);
//draw
glDrawElements(GL_TRIANGLES, SPRITE_COUNT * 6, GL_UNSIGNED_SHORT, 0);
glBindVertexArray(0);
currently this code, still only draws 1 quad. Can you take a look where I setup the size of the gpu memory, am I doing that part correctly? Could I make the large buffer in a more efficient way? Is my drawing command correct? Is anything standing out why it would still only be drawing 1 quad instead of two?