goat3dgfx

changeset 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 f2da87b02fcd
children 7aea3639c411
files examples/viewscn/src/main.cc src/mesh.cc src/scene.cc src/shader.cc src/shader.h
diffstat 5 files changed, 114 insertions(+), 17 deletions(-) [+]
line diff
     1.1 --- a/examples/viewscn/src/main.cc	Sun Dec 01 02:25:29 2013 +0200
     1.2 +++ b/examples/viewscn/src/main.cc	Sun Dec 08 01:35:30 2013 +0200
     1.3 @@ -15,10 +15,12 @@
     1.4  static void motion(int x, int y);
     1.5  static bool parse_args(int argc, char **argv);
     1.6  
     1.7 -static float cam_theta, cam_phi;
     1.8 +static float cam_theta, cam_phi, cam_dist = 8;
     1.9  static const char *scene_filename;
    1.10  static Scene *scn;
    1.11  
    1.12 +static ShaderProg *sdr;
    1.13 +
    1.14  int main(int argc, char **argv)
    1.15  {
    1.16  	glutInit(&argc, argv);
    1.17 @@ -49,9 +51,15 @@
    1.18  {
    1.19  	glewInit();
    1.20  
    1.21 +	glClearColor(0.1, 0.1, 0.1, 1);
    1.22 +
    1.23  	glEnable(GL_DEPTH_TEST);
    1.24  	glEnable(GL_CULL_FACE);
    1.25  
    1.26 +	if(!(sdr = get_sdrprog("sdr/foo.v.glsl", "sdr/foo.p.glsl"))) {
    1.27 +		return false;
    1.28 +	}
    1.29 +
    1.30  	scn = new Scene;
    1.31  	if(!scn->load(scene_filename)) {
    1.32  		fatal_log("failed to load scene: %s\n", scene_filename);
    1.33 @@ -68,16 +76,28 @@
    1.34  
    1.35  static void display()
    1.36  {
    1.37 +	glClearColor(0.25, 0.25, 0.25, 1);
    1.38  	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.39  
    1.40  	Matrix4x4 view_matrix;
    1.41 +	view_matrix.translate(Vector3(0, 0, -cam_dist));
    1.42  	view_matrix.rotate(Vector3(1, 0, 0), M_PI * cam_phi / 180.0);
    1.43  	view_matrix.rotate(Vector3(0, 1, 0), M_PI * cam_theta / 180.0);
    1.44  	set_view_matrix(view_matrix);
    1.45  
    1.46 -	setup_gl_matrices();
    1.47 +	//setup_gl_matrices();
    1.48  
    1.49 +	bind_shader(sdr);
    1.50  	scn->draw();
    1.51 +	bind_shader(0);
    1.52 +
    1.53 +	/*glBegin(GL_QUADS);
    1.54 +	glColor3f(1, 1, 1);
    1.55 +	glVertex3f(-1, -1, 0);
    1.56 +	glVertex3f(1, -1, 0);
    1.57 +	glVertex3f(1, 1, 0);
    1.58 +	glVertex3f(-1, 1, 0);
    1.59 +	glEnd();*/
    1.60  
    1.61  	glutSwapBuffers();
    1.62  	CHECKGLERR;
     2.1 --- a/src/mesh.cc	Sun Dec 01 02:25:29 2013 +0200
     2.2 +++ b/src/mesh.cc	Sun Dec 08 01:35:30 2013 +0200
     2.3 @@ -5,6 +5,7 @@
     2.4  #include "opengl.h"
     2.5  #include "mesh.h"
     2.6  #include "xform_node.h"
     2.7 +#include "shader.h"
     2.8  #include "logger.h"
     2.9  
    2.10  using namespace goatgfx;
    2.11 @@ -411,24 +412,59 @@
    2.12  
    2.13  void Mesh::draw() const
    2.14  {
    2.15 +	const ShaderProg *cur_sdr = get_current_shader();
    2.16 +#ifdef GL_ES_VERSION_2_0
    2.17 +	if(!cur_sdr) {
    2.18 +		error_log("%s: CrippledGL ES can't draw without a shader\n", __FUNCTION__);
    2.19 +		return;
    2.20 +	}
    2.21 +#endif
    2.22 +
    2.23  	((Mesh*)this)->update_buffers();
    2.24  
    2.25  	if(!vattr[MESH_ATTR_VERTEX].vbo_valid) {
    2.26  		error_log("%s: invalid vertex buffer\n", __FUNCTION__);
    2.27  		return;
    2.28  	}
    2.29 -	if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) {
    2.30 -		error_log("%s: shader attribute location for vertices unset\n", __FUNCTION__);
    2.31 -		return;
    2.32 -	}
    2.33  
    2.34 -	for(int i=0; i<NUM_MESH_ATTR; i++) {
    2.35 -		int loc = global_sdr_loc[i];
    2.36 -		if(loc >= 0 && vattr[i].vbo_valid) {
    2.37 -			glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo);
    2.38 -			glVertexAttribPointer(loc, vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0);
    2.39 -			glEnableVertexAttribArray(loc);
    2.40 +	if(cur_sdr) {
    2.41 +		// rendering with shaders
    2.42 +		if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) {
    2.43 +			error_log("%s: shader attribute location for vertices unset\n", __FUNCTION__);
    2.44 +			return;
    2.45  		}
    2.46 +
    2.47 +		for(int i=0; i<NUM_MESH_ATTR; i++) {
    2.48 +			int loc = global_sdr_loc[i];
    2.49 +			if(loc >= 0 && vattr[i].vbo_valid) {
    2.50 +				glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo);
    2.51 +				glVertexAttribPointer(loc, vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0);
    2.52 +				glEnableVertexAttribArray(loc);
    2.53 +			}
    2.54 +		}
    2.55 +	} else {
    2.56 +#ifndef GL_ES_VERSION_2_0
    2.57 +		// rendering with fixed-function (not available in GLES2)
    2.58 +		glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_VERTEX].vbo);
    2.59 +		glVertexPointer(vattr[MESH_ATTR_VERTEX].nelem, GL_FLOAT, 0, 0);
    2.60 +		glEnableClientState(GL_VERTEX_ARRAY);
    2.61 +
    2.62 +		if(vattr[MESH_ATTR_NORMAL].vbo_valid) {
    2.63 +			glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_NORMAL].vbo);
    2.64 +			glNormalPointer(GL_FLOAT, 0, 0);
    2.65 +			glEnableClientState(GL_NORMAL_ARRAY);
    2.66 +		}
    2.67 +		if(vattr[MESH_ATTR_TEXCOORD].vbo_valid) {
    2.68 +			glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_TEXCOORD].vbo);
    2.69 +			glTexCoordPointer(vattr[MESH_ATTR_TEXCOORD].nelem, GL_FLOAT, 0, 0);
    2.70 +			glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    2.71 +		}
    2.72 +		if(vattr[MESH_ATTR_COLOR].vbo_valid) {
    2.73 +			glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_COLOR].vbo);
    2.74 +			glColorPointer(vattr[MESH_ATTR_COLOR].nelem, GL_FLOAT, 0, 0);
    2.75 +			glEnableClientState(GL_COLOR_ARRAY);
    2.76 +		}
    2.77 +#endif
    2.78  	}
    2.79  	glBindBuffer(GL_ARRAY_BUFFER, 0);
    2.80  
    2.81 @@ -440,11 +476,28 @@
    2.82  		glDrawArrays(GL_TRIANGLES, 0, nverts);
    2.83  	}
    2.84  
    2.85 -	for(int i=0; i<NUM_MESH_ATTR; i++) {
    2.86 -		int loc = global_sdr_loc[i];
    2.87 -		if(loc >= 0 && vattr[i].vbo_valid) {
    2.88 -			glDisableVertexAttribArray(loc);
    2.89 +	if(cur_sdr) {
    2.90 +		// rendered with shaders
    2.91 +		for(int i=0; i<NUM_MESH_ATTR; i++) {
    2.92 +			int loc = global_sdr_loc[i];
    2.93 +			if(loc >= 0 && vattr[i].vbo_valid) {
    2.94 +				glDisableVertexAttribArray(loc);
    2.95 +			}
    2.96  		}
    2.97 +	} else {
    2.98 +#ifndef GL_ES_VERSION_2_0
    2.99 +		// rendered with fixed-function
   2.100 +		glDisableClientState(GL_VERTEX_ARRAY);
   2.101 +		if(vattr[MESH_ATTR_NORMAL].vbo_valid) {
   2.102 +			glDisableClientState(GL_NORMAL_ARRAY);
   2.103 +		}
   2.104 +		if(vattr[MESH_ATTR_TEXCOORD].vbo_valid) {
   2.105 +			glDisableClientState(GL_TEXTURE_COORD_ARRAY);
   2.106 +		}
   2.107 +		if(vattr[MESH_ATTR_COLOR].vbo_valid) {
   2.108 +			glDisableClientState(GL_COLOR_ARRAY);
   2.109 +		}
   2.110 +#endif
   2.111  	}
   2.112  }
   2.113  
     3.1 --- a/src/scene.cc	Sun Dec 01 02:25:29 2013 +0200
     3.2 +++ b/src/scene.cc	Sun Dec 08 01:35:30 2013 +0200
     3.3 @@ -64,7 +64,6 @@
     3.4  	}
     3.5  
     3.6  	if(objects.empty()) {
     3.7 -		printf("rendering meshes directly\n");
     3.8  		for(size_t i=0; i<meshes.size(); i++) {
     3.9  			meshes[i]->draw();
    3.10  		}
     4.1 --- a/src/shader.cc	Sun Dec 01 02:25:29 2013 +0200
     4.2 +++ b/src/shader.cc	Sun Dec 08 01:35:30 2013 +0200
     4.3 @@ -26,6 +26,24 @@
     4.4  
     4.5  ShaderProg *ShaderProg::current;
     4.6  
     4.7 +void goatgfx::bind_shader(const ShaderProg *sdr)
     4.8 +{
     4.9 +	if(sdr) {
    4.10 +		sdr->bind();
    4.11 +	} else {
    4.12 +#ifndef GL_ES_VERSION_2_0
    4.13 +		glUseProgram(0);
    4.14 +		ShaderProg::current = 0;
    4.15 +#endif
    4.16 +	}
    4.17 +}
    4.18 +
    4.19 +const ShaderProg *goatgfx::get_current_shader()
    4.20 +{
    4.21 +	return ShaderProg::current;
    4.22 +}
    4.23 +
    4.24 +
    4.25  Shader::Shader()
    4.26  {
    4.27  	sdr = type = 0;
     5.1 --- a/src/shader.h	Sun Dec 01 02:25:29 2013 +0200
     5.2 +++ b/src/shader.h	Sun Dec 08 01:35:30 2013 +0200
     5.3 @@ -8,6 +8,13 @@
     5.4  
     5.5  namespace goatgfx {
     5.6  
     5.7 +class ShaderProg;
     5.8 +
     5.9 +
    5.10 +void bind_shader(const ShaderProg *sdr);
    5.11 +const ShaderProg *get_current_shader();
    5.12 +
    5.13 +
    5.14  class Shader {
    5.15  private:
    5.16  	unsigned int sdr;