backported minor changes to Mesh::dump_obj from meshgen project
[laserbrain_demo] / src / mesh.cc
index beb5b49..04d25c2 100644 (file)
@@ -4,20 +4,13 @@
 #include <assert.h>
 #include "opengl.h"
 #include "mesh.h"
+#include "logger.h"
 //#include "xform_node.h"
 
 #define USE_OLDGL
 
 bool Mesh::use_custom_sdr_attr = true;
-int Mesh::global_sdr_loc[NUM_MESH_ATTR] = { 0, 1, 2, 3, 4, 5, 6 };
-/*
-       (int)SDR_ATTR_VERTEX,
-       (int)SDR_ATTR_NORMAL,
-       (int)SDR_ATTR_TANGENT,
-       (int)SDR_ATTR_TEXCOORD,
-       (int)SDR_ATTR_COLOR,
-       -1, -1};
-*/
+int Mesh::global_sdr_loc[NUM_MESH_ATTR] = { 0, 1, 2, 3, 4, 5, 6, 7 };
 unsigned int Mesh::intersect_mode = ISECT_DEFAULT;
 float Mesh::vertex_sel_dist = 0.01;
 float Mesh::vis_vecsize = 1.0;
@@ -162,12 +155,12 @@ void Mesh::clear()
 float *Mesh::set_attrib_data(int attrib, int nelem, unsigned int num, const float *data)
 {
        if(attrib < 0 || attrib >= NUM_MESH_ATTR) {
-               fprintf(stderr, "%s: invalid attrib: %d\n", __FUNCTION__, attrib);
+               error_log("%s: invalid attrib: %d\n", __FUNCTION__, attrib);
                return 0;
        }
 
        if(nverts && num != nverts) {
-               fprintf(stderr, "%s: attribute count missmatch (%d instead of %d)\n", __FUNCTION__, num, nverts);
+               error_log("%s: attribute count missmatch (%d instead of %d)\n", __FUNCTION__, num, nverts);
                return 0;
        }
        nverts = num;
@@ -188,7 +181,7 @@ float *Mesh::set_attrib_data(int attrib, int nelem, unsigned int num, const floa
 float *Mesh::get_attrib_data(int attrib)
 {
        if(attrib < 0 || attrib >= NUM_MESH_ATTR) {
-               fprintf(stderr, "%s: invalid attrib: %d\n", __FUNCTION__, attrib);
+               error_log("%s: invalid attrib: %d\n", __FUNCTION__, attrib);
                return 0;
        }
 
@@ -199,17 +192,17 @@ float *Mesh::get_attrib_data(int attrib)
 const float *Mesh::get_attrib_data(int attrib) const
 {
        if(attrib < 0 || attrib >= NUM_MESH_ATTR) {
-               fprintf(stderr, "%s: invalid attrib: %d\n", __FUNCTION__, attrib);
+               error_log("%s: invalid attrib: %d\n", __FUNCTION__, attrib);
                return 0;
        }
 
        if(!vattr[attrib].data_valid) {
 #if GL_ES_VERSION_2_0
-               fprintf(stderr, "%s: can't read back attrib data on CrippledGL ES\n", __FUNCTION__);
+               error_log("%s: can't read back attrib data on CrippledGL ES\n", __FUNCTION__);
                return 0;
 #else
                if(!vattr[attrib].vbo_valid) {
-                       fprintf(stderr, "%s: unavailable attrib: %d\n", __FUNCTION__, attrib);
+                       error_log("%s: unavailable attrib: %d\n", __FUNCTION__, attrib);
                        return 0;
                }
 
@@ -263,7 +256,7 @@ unsigned int *Mesh::set_index_data(int num, const unsigned int *indices)
 {
        int nidx = nfaces * 3;
        if(nidx && num != nidx) {
-               fprintf(stderr, "%s: index count mismatch (%d instead of %d)\n", __FUNCTION__, num, nidx);
+               error_log("%s: index count mismatch (%d instead of %d)\n", __FUNCTION__, num, nidx);
                return 0;
        }
        nfaces = num / 3;
@@ -291,11 +284,11 @@ const unsigned int *Mesh::get_index_data() const
 {
        if(!idata_valid) {
 #if GL_ES_VERSION_2_0
-               fprintf(stderr, "%s: can't read back index data in CrippledGL ES\n", __FUNCTION__);
+               error_log("%s: can't read back index data in CrippledGL ES\n", __FUNCTION__);
                return 0;
 #else
                if(!ibo_valid) {
-                       fprintf(stderr, "%s: indices unavailable\n", __FUNCTION__);
+                       error_log("%s: indices unavailable\n", __FUNCTION__);
                        return 0;
                }
 
@@ -639,14 +632,14 @@ bool Mesh::pre_draw() const
        ((Mesh*)this)->update_buffers();
 
        if(!vattr[MESH_ATTR_VERTEX].vbo_valid) {
-               fprintf(stderr, "%s: invalid vertex buffer\n", __FUNCTION__);
+               error_log("%s: invalid vertex buffer\n", __FUNCTION__);
                return false;
        }
 
        if(cur_sdr && use_custom_sdr_attr) {
                // rendering with shaders
                if(global_sdr_loc[MESH_ATTR_VERTEX] == -1) {
-                       fprintf(stderr, "%s: shader attribute location for vertices unset\n", __FUNCTION__);
+                       error_log("%s: shader attribute location for vertices unset\n", __FUNCTION__);
                        return false;
                }
 
@@ -680,6 +673,13 @@ bool Mesh::pre_draw() const
                        glColorPointer(vattr[MESH_ATTR_COLOR].nelem, GL_FLOAT, 0, 0);
                        glEnableClientState(GL_COLOR_ARRAY);
                }
+               if(vattr[MESH_ATTR_TEXCOORD2].vbo_valid) {
+                       glClientActiveTexture(GL_TEXTURE1);
+                       glBindBuffer(GL_ARRAY_BUFFER, vattr[MESH_ATTR_TEXCOORD2].vbo);
+                       glTexCoordPointer(vattr[MESH_ATTR_TEXCOORD2].nelem, GL_FLOAT, 0, 0);
+                       glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+                       glClientActiveTexture(GL_TEXTURE0);
+               }
 #endif
        }
        glBindBuffer(GL_ARRAY_BUFFER, 0);
@@ -725,6 +725,11 @@ void Mesh::post_draw() const
                if(vattr[MESH_ATTR_COLOR].vbo_valid) {
                        glDisableClientState(GL_COLOR_ARRAY);
                }
+               if(vattr[MESH_ATTR_TEXCOORD2].vbo_valid) {
+                       glClientActiveTexture(GL_TEXTURE1);
+                       glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+                       glClientActiveTexture(GL_TEXTURE0);
+               }
 #endif
        }
 }
@@ -1087,7 +1092,7 @@ bool Mesh::dump(FILE *fp) const
        }
 
        fprintf(fp, "VERTEX ATTRIBUTES\n");
-       static const char *label[] = { "pos", "nor", "tan", "tex", "col", "bw", "bid" };
+       static const char *label[] = { "pos", "nor", "tan", "tex", "col", "bw", "bid", "tex2" };
        static const char *elemfmt[] = { 0, " %s(%g)", " %s(%g, %g)", " %s(%g, %g, %g)", " %s(%g, %g, %g, %g)", 0 };
 
        for(int i=0; i<(int)nverts; i++) {
@@ -1129,7 +1134,7 @@ bool Mesh::dump_obj(const char *fname) const
        return false;
 }
 
-bool Mesh::dump_obj(FILE *fp) const
+bool Mesh::dump_obj(FILE *fp, int voffs) const
 {
        if(!has_attrib(MESH_ATTR_VERTEX)) {
                return false;
@@ -1137,20 +1142,20 @@ bool Mesh::dump_obj(FILE *fp) const
 
        for(int i=0; i<(int)nverts; i++) {
                Vec4 v = get_attrib(MESH_ATTR_VERTEX, i);
-               fprintf(fp, "v %g %g %g\n", v.x, v.y, v.z);
+               fprintf(fp, "v %f %f %f\n", v.x, v.y, v.z);
        }
 
        if(has_attrib(MESH_ATTR_NORMAL)) {
                for(int i=0; i<(int)nverts; i++) {
                        Vec4 v = get_attrib(MESH_ATTR_NORMAL, i);
-                       fprintf(fp, "vn %g %g %g\n", v.x, v.y, v.z);
+                       fprintf(fp, "vn %f %f %f\n", v.x, v.y, v.z);
                }
        }
 
        if(has_attrib(MESH_ATTR_TEXCOORD)) {
                for(int i=0; i<(int)nverts; i++) {
                        Vec4 v = get_attrib(MESH_ATTR_TEXCOORD, i);
-                       fprintf(fp, "vt %g %g\n", v.x, v.y);
+                       fprintf(fp, "vt %f %f\n", v.x, v.y);
                }
        }
 
@@ -1163,14 +1168,14 @@ bool Mesh::dump_obj(FILE *fp) const
                for(int i=0; i<numtri; i++) {
                        fputc('f', fp);
                        for(int j=0; j<3; j++) {
-                               unsigned int idx = *idxptr++ + 1;
+                               unsigned int idx = *idxptr++ + 1 + voffs;
                                fprintf(fp, " %u/%u/%u", idx, idx, idx);
                        }
                        fputc('\n', fp);
                }
        } else {
                int numtri = nverts / 3;
-               unsigned int idx = 1;
+               unsigned int idx = 1 + voffs;
                for(int i=0; i<numtri; i++) {
                        fputc('f', fp);
                        for(int j=0; j<3; j++) {