# HG changeset patch # User John Tsiombikas # Date 1386459330 -7200 # Node ID 6f82b9b6d6c351641b8db4b24e1a2d5bcd3957a4 # Parent f2da87b02fcd529cf527af69cb64a1e2edaa2b74 added the ability to render in fixed function with the mesh class diff -r f2da87b02fcd -r 6f82b9b6d6c3 examples/viewscn/src/main.cc --- a/examples/viewscn/src/main.cc Sun Dec 01 02:25:29 2013 +0200 +++ b/examples/viewscn/src/main.cc Sun Dec 08 01:35:30 2013 +0200 @@ -15,10 +15,12 @@ static void motion(int x, int y); static bool parse_args(int argc, char **argv); -static float cam_theta, cam_phi; +static float cam_theta, cam_phi, cam_dist = 8; static const char *scene_filename; static Scene *scn; +static ShaderProg *sdr; + int main(int argc, char **argv) { glutInit(&argc, argv); @@ -49,9 +51,15 @@ { glewInit(); + glClearColor(0.1, 0.1, 0.1, 1); + glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); + if(!(sdr = get_sdrprog("sdr/foo.v.glsl", "sdr/foo.p.glsl"))) { + return false; + } + scn = new Scene; if(!scn->load(scene_filename)) { fatal_log("failed to load scene: %s\n", scene_filename); @@ -68,16 +76,28 @@ static void display() { + glClearColor(0.25, 0.25, 0.25, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); Matrix4x4 view_matrix; + view_matrix.translate(Vector3(0, 0, -cam_dist)); view_matrix.rotate(Vector3(1, 0, 0), M_PI * cam_phi / 180.0); view_matrix.rotate(Vector3(0, 1, 0), M_PI * cam_theta / 180.0); set_view_matrix(view_matrix); - setup_gl_matrices(); + //setup_gl_matrices(); + bind_shader(sdr); scn->draw(); + bind_shader(0); + + /*glBegin(GL_QUADS); + glColor3f(1, 1, 1); + glVertex3f(-1, -1, 0); + glVertex3f(1, -1, 0); + glVertex3f(1, 1, 0); + glVertex3f(-1, 1, 0); + glEnd();*/ glutSwapBuffers(); CHECKGLERR; diff -r f2da87b02fcd -r 6f82b9b6d6c3 src/mesh.cc --- a/src/mesh.cc Sun Dec 01 02:25:29 2013 +0200 +++ b/src/mesh.cc Sun Dec 08 01:35:30 2013 +0200 @@ -5,6 +5,7 @@ #include "opengl.h" #include "mesh.h" #include "xform_node.h" +#include "shader.h" #include "logger.h" using namespace goatgfx; @@ -411,24 +412,59 @@ void Mesh::draw() const { + const ShaderProg *cur_sdr = get_current_shader(); +#ifdef GL_ES_VERSION_2_0 + if(!cur_sdr) { + error_log("%s: CrippledGL ES can't draw without a shader\n", __FUNCTION__); + return; + } +#endif + ((Mesh*)this)->update_buffers(); if(!vattr[MESH_ATTR_VERTEX].vbo_valid) { error_log("%s: invalid vertex buffer\n", __FUNCTION__); return; } - if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) { - error_log("%s: shader attribute location for vertices unset\n", __FUNCTION__); - return; - } - for(int i=0; i= 0 && vattr[i].vbo_valid) { - glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo); - glVertexAttribPointer(loc, vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0); - glEnableVertexAttribArray(loc); + if(cur_sdr) { + // rendering with shaders + if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) { + error_log("%s: shader attribute location for vertices unset\n", __FUNCTION__); + return; } + + for(int i=0; i= 0 && vattr[i].vbo_valid) { + glBindBuffer(GL_ARRAY_BUFFER, vattr[i].vbo); + glVertexAttribPointer(loc, vattr[i].nelem, GL_FLOAT, GL_FALSE, 0, 0); + glEnableVertexAttribArray(loc); + } + } + } else { +#ifndef GL_ES_VERSION_2_0 + // rendering with fixed-function (not available in GLES2) + glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_VERTEX].vbo); + glVertexPointer(vattr[MESH_ATTR_VERTEX].nelem, GL_FLOAT, 0, 0); + glEnableClientState(GL_VERTEX_ARRAY); + + if(vattr[MESH_ATTR_NORMAL].vbo_valid) { + glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_NORMAL].vbo); + glNormalPointer(GL_FLOAT, 0, 0); + glEnableClientState(GL_NORMAL_ARRAY); + } + if(vattr[MESH_ATTR_TEXCOORD].vbo_valid) { + glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_TEXCOORD].vbo); + glTexCoordPointer(vattr[MESH_ATTR_TEXCOORD].nelem, GL_FLOAT, 0, 0); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + } + if(vattr[MESH_ATTR_COLOR].vbo_valid) { + glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_COLOR].vbo); + glColorPointer(vattr[MESH_ATTR_COLOR].nelem, GL_FLOAT, 0, 0); + glEnableClientState(GL_COLOR_ARRAY); + } +#endif } glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -440,11 +476,28 @@ glDrawArrays(GL_TRIANGLES, 0, nverts); } - for(int i=0; i= 0 && vattr[i].vbo_valid) { - glDisableVertexAttribArray(loc); + if(cur_sdr) { + // rendered with shaders + for(int i=0; i= 0 && vattr[i].vbo_valid) { + glDisableVertexAttribArray(loc); + } } + } else { +#ifndef GL_ES_VERSION_2_0 + // rendered with fixed-function + glDisableClientState(GL_VERTEX_ARRAY); + if(vattr[MESH_ATTR_NORMAL].vbo_valid) { + glDisableClientState(GL_NORMAL_ARRAY); + } + if(vattr[MESH_ATTR_TEXCOORD].vbo_valid) { + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + } + if(vattr[MESH_ATTR_COLOR].vbo_valid) { + glDisableClientState(GL_COLOR_ARRAY); + } +#endif } } diff -r f2da87b02fcd -r 6f82b9b6d6c3 src/scene.cc --- a/src/scene.cc Sun Dec 01 02:25:29 2013 +0200 +++ b/src/scene.cc Sun Dec 08 01:35:30 2013 +0200 @@ -64,7 +64,6 @@ } if(objects.empty()) { - printf("rendering meshes directly\n"); for(size_t i=0; idraw(); } diff -r f2da87b02fcd -r 6f82b9b6d6c3 src/shader.cc --- a/src/shader.cc Sun Dec 01 02:25:29 2013 +0200 +++ b/src/shader.cc Sun Dec 08 01:35:30 2013 +0200 @@ -26,6 +26,24 @@ ShaderProg *ShaderProg::current; +void goatgfx::bind_shader(const ShaderProg *sdr) +{ + if(sdr) { + sdr->bind(); + } else { +#ifndef GL_ES_VERSION_2_0 + glUseProgram(0); + ShaderProg::current = 0; +#endif + } +} + +const ShaderProg *goatgfx::get_current_shader() +{ + return ShaderProg::current; +} + + Shader::Shader() { sdr = type = 0; diff -r f2da87b02fcd -r 6f82b9b6d6c3 src/shader.h --- a/src/shader.h Sun Dec 01 02:25:29 2013 +0200 +++ b/src/shader.h Sun Dec 08 01:35:30 2013 +0200 @@ -8,6 +8,13 @@ namespace goatgfx { +class ShaderProg; + + +void bind_shader(const ShaderProg *sdr); +const ShaderProg *get_current_shader(); + + class Shader { private: unsigned int sdr;