goat3dgfx

diff src/mesh.cc @ 18:6f82b9b6d6c3

added the ability to render in fixed function with the mesh class
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 08 Dec 2013 01:35:30 +0200
parents 7d6b667821cf
children 7aea3639c411
line diff
     1.1 --- a/src/mesh.cc	Sun Dec 01 02:25:29 2013 +0200
     1.2 +++ b/src/mesh.cc	Sun Dec 08 01:35:30 2013 +0200
     1.3 @@ -5,6 +5,7 @@
     1.4  #include "opengl.h"
     1.5  #include "mesh.h"
     1.6  #include "xform_node.h"
     1.7 +#include "shader.h"
     1.8  #include "logger.h"
     1.9  
    1.10  using namespace goatgfx;
    1.11 @@ -411,24 +412,59 @@
    1.12  
    1.13  void Mesh::draw() const
    1.14  {
    1.15 +	const ShaderProg *cur_sdr = get_current_shader();
    1.16 +#ifdef GL_ES_VERSION_2_0
    1.17 +	if(!cur_sdr) {
    1.18 +		error_log("%s: CrippledGL ES can't draw without a shader\n", __FUNCTION__);
    1.19 +		return;
    1.20 +	}
    1.21 +#endif
    1.22 +
    1.23  	((Mesh*)this)->update_buffers();
    1.24  
    1.25  	if(!vattr[MESH_ATTR_VERTEX].vbo_valid) {
    1.26  		error_log("%s: invalid vertex buffer\n", __FUNCTION__);
    1.27  		return;
    1.28  	}
    1.29 -	if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) {
    1.30 -		error_log("%s: shader attribute location for vertices unset\n", __FUNCTION__);
    1.31 -		return;
    1.32 -	}
    1.33  
    1.34 -	for(int i=0; i<NUM_MESH_ATTR; i++) {
    1.35 -		int loc = global_sdr_loc[i];
    1.36 -		if(loc >= 0 && vattr[i].vbo_valid) {
    1.37 -			glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo);
    1.38 -			glVertexAttribPointer(loc, vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0);
    1.39 -			glEnableVertexAttribArray(loc);
    1.40 +	if(cur_sdr) {
    1.41 +		// rendering with shaders
    1.42 +		if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) {
    1.43 +			error_log("%s: shader attribute location for vertices unset\n", __FUNCTION__);
    1.44 +			return;
    1.45  		}
    1.46 +
    1.47 +		for(int i=0; i<NUM_MESH_ATTR; i++) {
    1.48 +			int loc = global_sdr_loc[i];
    1.49 +			if(loc >= 0 && vattr[i].vbo_valid) {
    1.50 +				glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo);
    1.51 +				glVertexAttribPointer(loc, vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0);
    1.52 +				glEnableVertexAttribArray(loc);
    1.53 +			}
    1.54 +		}
    1.55 +	} else {
    1.56 +#ifndef GL_ES_VERSION_2_0
    1.57 +		// rendering with fixed-function (not available in GLES2)
    1.58 +		glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_VERTEX].vbo);
    1.59 +		glVertexPointer(vattr[MESH_ATTR_VERTEX].nelem, GL_FLOAT, 0, 0);
    1.60 +		glEnableClientState(GL_VERTEX_ARRAY);
    1.61 +
    1.62 +		if(vattr[MESH_ATTR_NORMAL].vbo_valid) {
    1.63 +			glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_NORMAL].vbo);
    1.64 +			glNormalPointer(GL_FLOAT, 0, 0);
    1.65 +			glEnableClientState(GL_NORMAL_ARRAY);
    1.66 +		}
    1.67 +		if(vattr[MESH_ATTR_TEXCOORD].vbo_valid) {
    1.68 +			glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_TEXCOORD].vbo);
    1.69 +			glTexCoordPointer(vattr[MESH_ATTR_TEXCOORD].nelem, GL_FLOAT, 0, 0);
    1.70 +			glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    1.71 +		}
    1.72 +		if(vattr[MESH_ATTR_COLOR].vbo_valid) {
    1.73 +			glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_COLOR].vbo);
    1.74 +			glColorPointer(vattr[MESH_ATTR_COLOR].nelem, GL_FLOAT, 0, 0);
    1.75 +			glEnableClientState(GL_COLOR_ARRAY);
    1.76 +		}
    1.77 +#endif
    1.78  	}
    1.79  	glBindBuffer(GL_ARRAY_BUFFER, 0);
    1.80  
    1.81 @@ -440,11 +476,28 @@
    1.82  		glDrawArrays(GL_TRIANGLES, 0, nverts);
    1.83  	}
    1.84  
    1.85 -	for(int i=0; i<NUM_MESH_ATTR; i++) {
    1.86 -		int loc = global_sdr_loc[i];
    1.87 -		if(loc >= 0 && vattr[i].vbo_valid) {
    1.88 -			glDisableVertexAttribArray(loc);
    1.89 +	if(cur_sdr) {
    1.90 +		// rendered with shaders
    1.91 +		for(int i=0; i<NUM_MESH_ATTR; i++) {
    1.92 +			int loc = global_sdr_loc[i];
    1.93 +			if(loc >= 0 && vattr[i].vbo_valid) {
    1.94 +				glDisableVertexAttribArray(loc);
    1.95 +			}
    1.96  		}
    1.97 +	} else {
    1.98 +#ifndef GL_ES_VERSION_2_0
    1.99 +		// rendered with fixed-function
   1.100 +		glDisableClientState(GL_VERTEX_ARRAY);
   1.101 +		if(vattr[MESH_ATTR_NORMAL].vbo_valid) {
   1.102 +			glDisableClientState(GL_NORMAL_ARRAY);
   1.103 +		}
   1.104 +		if(vattr[MESH_ATTR_TEXCOORD].vbo_valid) {
   1.105 +			glDisableClientState(GL_TEXTURE_COORD_ARRAY);
   1.106 +		}
   1.107 +		if(vattr[MESH_ATTR_COLOR].vbo_valid) {
   1.108 +			glDisableClientState(GL_COLOR_ARRAY);
   1.109 +		}
   1.110 +#endif
   1.111  	}
   1.112  }
   1.113