vrheights

changeset 14:25cab9e20c9c

mesh vbos
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 09 Oct 2014 01:29:28 +0300
parents 0168104ec568
children ffb62c8db542
files src/mesh.cc src/mesh.h
diffstat 2 files changed, 55 insertions(+), 12 deletions(-) [+]
line diff
     1.1 --- a/src/mesh.cc	Sat Oct 04 03:41:59 2014 +0300
     1.2 +++ b/src/mesh.cc	Thu Oct 09 01:29:28 2014 +0300
     1.3 @@ -246,62 +246,103 @@
     1.4  	cur_tang_valid = true;
     1.5  }
     1.6  
     1.7 +void Mesh::update_buffers()
     1.8 +{
     1.9 +	for(int i=0; i<NUM_MESH_ATTRIBS; i++) {
    1.10 +		if(attrib[i].vbo_valid || attrib[i].data.empty()) {
    1.11 +			continue;
    1.12 +		}
    1.13 +		int count = (int)attrib[i].data.size();
    1.14 +
    1.15 +		float *ptr = &attrib[i].data[0];
    1.16 +		// TODO: check previous size and don't resize if it's not needed
    1.17 +		attrib[i].vbo_size = count * attrib[i].nelems * sizeof(float);
    1.18 +
    1.19 +		if(!attrib[i].vbo) {
    1.20 +			glGenBuffers(1, &attrib[i].vbo);
    1.21 +		}
    1.22 +
    1.23 +		glBindBuffer(GL_ARRAY_BUFFER, attrib[i].vbo);
    1.24 +		glBufferData(GL_ARRAY_BUFFER, attrib[i].vbo_size, ptr, vbo_usage);
    1.25 +		attrib[i].vbo_valid = true;
    1.26 +	}
    1.27 +
    1.28 +	if(!ibo_valid && !index.empty()) {
    1.29 +		unsigned int *ptr = &index[0];
    1.30 +		ibo_size = index.size() * sizeof *ptr;	// TODO: see above
    1.31 +
    1.32 +		if(!ibo) {
    1.33 +			glGenBuffers(1, &ibo);
    1.34 +		}
    1.35 +
    1.36 +		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    1.37 +		glBufferData(GL_ELEMENT_ARRAY_BUFFER, ibo_size, ptr, ibo_usage);
    1.38 +		ibo_valid = true;
    1.39 +	}
    1.40 +}
    1.41 +
    1.42  void Mesh::draw() const
    1.43  {
    1.44  	if(attrib[MESH_VERTEX].data.empty()) {
    1.45  		return;
    1.46  	}
    1.47  
    1.48 +	((Mesh*)this)->update_buffers();
    1.49 +
    1.50  	bool use_norm = !attrib[MESH_NORMAL].data.empty();
    1.51  	bool use_tc = !attrib[MESH_TEXCOORD].data.empty();
    1.52  	bool use_tang = !attrib[MESH_TANGENT].data.empty();
    1.53  	int norm_loc, tc_loc, tang_loc;
    1.54  
    1.55 -	const float *ptr = &attrib[MESH_VERTEX].data[0];
    1.56 +	glBindBuffer(GL_ARRAY_BUFFER, attrib[MESH_VERTEX].vbo);
    1.57  	int loc = attrib[MESH_VERTEX].sdrloc;
    1.58  	if(loc == -1) {
    1.59  		glEnableClientState(GL_VERTEX_ARRAY);
    1.60 -		glVertexPointer(attrib[MESH_VERTEX].nelems, GL_FLOAT, 0, ptr);
    1.61 +		glVertexPointer(attrib[MESH_VERTEX].nelems, GL_FLOAT, 0, 0);
    1.62  	} else {
    1.63  		glEnableVertexAttribArray(loc);
    1.64 -		glVertexAttribPointer(loc, attrib[MESH_VERTEX].nelems, GL_FLOAT, 0, 0, ptr);
    1.65 +		glVertexAttribPointer(loc, attrib[MESH_VERTEX].nelems, GL_FLOAT, 0, 0, 0);
    1.66  	}
    1.67  
    1.68  	if(use_norm) {
    1.69 -		const float *ptr = &attrib[MESH_NORMAL].data[0];
    1.70 +		glBindBuffer(GL_ARRAY_BUFFER, attrib[MESH_NORMAL].vbo);
    1.71  		norm_loc = attrib[MESH_NORMAL].sdrloc;
    1.72  		if(norm_loc == -1) {
    1.73  			glEnableClientState(GL_NORMAL_ARRAY);
    1.74 -			glNormalPointer(GL_FLOAT, 0, ptr);
    1.75 +			glNormalPointer(GL_FLOAT, 0, 0);
    1.76  		} else {
    1.77  			glEnableVertexAttribArray(norm_loc);
    1.78 -			glVertexAttribPointer(norm_loc, attrib[MESH_NORMAL].nelems, GL_FLOAT, 0, 0, ptr);
    1.79 +			glVertexAttribPointer(norm_loc, attrib[MESH_NORMAL].nelems, GL_FLOAT, 0, 0, 0);
    1.80  		}
    1.81  	}
    1.82  
    1.83  	if(use_tc) {
    1.84 -		const float *ptr = &attrib[MESH_TEXCOORD].data[0];
    1.85 +		glBindBuffer(GL_ARRAY_BUFFER, attrib[MESH_TEXCOORD].vbo);
    1.86  		tc_loc = attrib[MESH_TEXCOORD].sdrloc;
    1.87  		if(tc_loc == -1) {
    1.88  			glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    1.89 -			glTexCoordPointer(attrib[MESH_TEXCOORD].nelems, GL_FLOAT, 0, ptr);
    1.90 +			glTexCoordPointer(attrib[MESH_TEXCOORD].nelems, GL_FLOAT, 0, 0);
    1.91  		} else {
    1.92  			glEnableVertexAttribArray(tc_loc);
    1.93 -			glVertexAttribPointer(tc_loc, attrib[MESH_TEXCOORD].nelems, GL_FLOAT, 0, 0, ptr);
    1.94 +			glVertexAttribPointer(tc_loc, attrib[MESH_TEXCOORD].nelems, GL_FLOAT, 0, 0, 0);
    1.95  		}
    1.96  	}
    1.97  
    1.98  	if(!attrib[MESH_TANGENT].data.empty()) {
    1.99 -		const float *ptr = &attrib[MESH_TANGENT].data[0];
   1.100 +		glBindBuffer(GL_ARRAY_BUFFER, attrib[MESH_TANGENT].vbo);
   1.101  		tang_loc = attrib[MESH_TANGENT].sdrloc;
   1.102  		if(tang_loc != -1) {
   1.103  			glEnableVertexAttribArray(tang_loc);
   1.104 -			glVertexAttribPointer(tang_loc, attrib[MESH_TANGENT].nelems, GL_FLOAT, 0, 0, ptr);
   1.105 +			glVertexAttribPointer(tang_loc, attrib[MESH_TANGENT].nelems, GL_FLOAT, 0, 0, 0);
   1.106  		}
   1.107  	}
   1.108  
   1.109 +	glBindBuffer(GL_ARRAY_BUFFER, 0);
   1.110 +
   1.111  	if(!index.empty()) {
   1.112 -		glDrawElements(prim, num_idx, GL_UNSIGNED_INT, &index[0]);
   1.113 +		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
   1.114 +		glDrawElements(prim, num_idx, GL_UNSIGNED_INT, 0);
   1.115 +		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
   1.116  	} else {
   1.117  		glDrawArrays(prim, 0, num_verts * 3);
   1.118  	}
     2.1 --- a/src/mesh.h	Sat Oct 04 03:41:59 2014 +0300
     2.2 +++ b/src/mesh.h	Thu Oct 09 01:29:28 2014 +0300
     2.3 @@ -46,6 +46,8 @@
     2.4  	Vector2 cur_tc;
     2.5  	bool cur_norm_valid, cur_tc_valid, cur_tang_valid;
     2.6  
     2.7 +	void update_buffers();
     2.8 +
     2.9  public:
    2.10  	Mesh();
    2.11  	~Mesh();