fixed Mesh wireframe rendering for non-indexed meshes
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Thu, 28 Jul 2016 16:41:59 +0000 (19:41 +0300)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Thu, 28 Jul 2016 16:41:59 +0000 (19:41 +0300)
src/app.cc
src/fs.cc [new file with mode: 0644]
src/fs.h
src/icon.cc
src/mesh.cc

index c3f6b39..0c82972 100644 (file)
@@ -8,6 +8,9 @@
 #include "backdrop.h"
 #include "goatvr.h"
 #include "opt.h"
+#include "fs.h"
+
+static void draw_scene();
 
 int win_width, win_height;
 float win_aspect;
@@ -19,7 +22,6 @@ static bool should_swap;
 
 static float cam_theta, cam_phi;
 static float cam_height = 1.65;
-static Mesh *mesh_torus;
 
 static bool bnstate[16];
 static int prev_x, prev_y;
@@ -65,13 +67,14 @@ bool app_init(int argc, char **argv)
 
        Mesh::use_custom_sdr_attr = false;
 
-       mesh_torus = new Mesh;
-       gen_torus(mesh_torus, 1.0, 0.25, 32, 32);
-
        if(!init_backdrop()) {
                return false;
        }
 
+       if(!init_fs()) {
+               return false;
+       }
+
        return true;
 }
 
@@ -80,7 +83,6 @@ void app_cleanup()
        if(opt.vr) {
                goatvr_shutdown();
        }
-       delete mesh_torus;
        cleanup_backdrop();
 }
 
@@ -105,7 +107,7 @@ void app_draw()
                        glMatrixMode(GL_MODELVIEW);
                        glLoadMatrixf(view_matrix[0]);
 
-                       draw_backdrop();
+                       draw_scene();
                }
                goatvr_draw_done();
 
@@ -126,7 +128,7 @@ void app_draw()
                glMatrixMode(GL_MODELVIEW);
                glLoadMatrixf(view_matrix[0]);
 
-               draw_backdrop();
+               draw_scene();
 
                app_swap_buffers();
                app_redraw();   // since we added animation we need to redisplay even in non-VR mode
@@ -134,6 +136,12 @@ void app_draw()
        assert(glGetError() == GL_NO_ERROR);
 }
 
+static void draw_scene()
+{
+       draw_backdrop();
+       draw_fs();
+}
+
 void app_reshape(int x, int y)
 {
        glViewport(0, 0, x, y);
diff --git a/src/fs.cc b/src/fs.cc
new file mode 100644 (file)
index 0000000..c85040a
--- /dev/null
+++ b/src/fs.cc
@@ -0,0 +1,99 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "fs.h"
+#include "icon.h"
+#include "gmath/gmath.h"
+#include "opengl.h"
+#include "app.h"
+
+static IconRenderer *iconrend;
+
+bool init_fs()
+{
+       iconrend = new ShapesIcons;
+       if(!iconrend->init()) {
+               return false;
+       }
+
+       return true;
+}
+
+void cleanup_fs()
+{
+       delete iconrend;
+}
+
+void draw_fs()
+{
+       FSNode test;
+       test.type = FSNODE_DIR;
+
+       Mat4 xform;
+       xform.rotate(time_sec, 0, 0);
+       xform.rotate(0, 0, time_sec * 0.5);
+       xform.translate(0, 2, -5);
+
+       glPushMatrix();
+       glMultMatrixf(xform[0]);
+
+       glUseProgram(0);
+       glDisable(GL_TEXTURE_2D);
+       iconrend->draw(&test);
+
+       glPopMatrix();
+}
+
+// ---- FSNode implementation ----
+FSNode::FSNode()
+{
+       type = FSNODE_UNKNOWN;
+       abs_path = name = suffix = 0;
+}
+
+FSNode::~FSNode()
+{
+       if(abs_path) {
+               delete [] abs_path;
+       }
+}
+
+void FSNode::set_path(const char *s)
+{
+       int len = strlen(s);
+       if(!len) return;
+
+       delete [] abs_path;
+
+       const char *slash = s + len - 1;
+       while(slash > s && *slash != '/') {
+               --slash;
+       }
+       if(name == s) { // no slashes found
+               char buf[1024];
+               if(!getcwd(buf, sizeof buf)) {
+                       abort();
+               }
+
+               int dirlen = strlen(buf);
+               abs_path = new char[len + dirlen + 2];
+               sprintf(abs_path, "%s/%s", buf, s);
+
+               name = abs_path + dirlen + 1;
+               suffix = abs_path + len + dirlen - 1;
+       } else {
+               abs_path = new char[len + 1];
+               memcpy(abs_path, s, len + 1);
+
+               name = abs_path + (slash - s);
+               suffix = abs_path + len - 1;
+       }
+
+       while(suffix > name && *suffix != '.') {
+               --suffix;
+       }
+       if(suffix == name) {
+               suffix = 0;
+       }
+}
index d58c621..c6f95a7 100644 (file)
--- a/src/fs.h
+++ b/src/fs.h
@@ -14,7 +14,15 @@ public:
        char *abs_path;
        char *name, *suffix;
 
+       FSNode();
+       ~FSNode();
+
        void set_path(const char *s);
 };
 
+bool init_fs();
+void cleanup_fs();
+
+void draw_fs();
+
 #endif // FS_H_
index a72a6d0..502333c 100644 (file)
@@ -49,10 +49,15 @@ bool ShapesIcons::init()
                priv->shape[i] = new Mesh;
        }
 
-       gen_geosphere(priv->shape[SHAPE_SPHERE], 1.0, 2);
+       Mat4 xform;
+
+       gen_geosphere(priv->shape[SHAPE_SPHERE], 0.5, 0);
        gen_box(priv->shape[SHAPE_BOX], 1, 1, 1);
-       gen_torus(priv->shape[SHAPE_TORUS], 0.9, 0.2, 16, 8);
-       gen_cone(priv->shape[SHAPE_CONE], 0.8, 1.0, 8, 2);
+       gen_torus(priv->shape[SHAPE_TORUS], 0.4, 0.1, 12, 6);
+
+       gen_cone(priv->shape[SHAPE_CONE], 0.5, 1.0, 8, 2, 1);
+       xform.translation(0, -0.33, 0);
+       priv->shape[SHAPE_CONE]->apply_xform(xform, Mat4::identity);
 
        return true;
 }
@@ -64,6 +69,23 @@ void ShapesIcons::shutdown()
        }
 }
 
+static int fstype_shape(FSNodeType type)
+{
+       switch(type) {
+       case FSNODE_FILE:
+               return SHAPE_SPHERE;
+       case FSNODE_DIR:
+               return SHAPE_BOX;
+       case FSNODE_DEV:
+               return SHAPE_TORUS;
+       default:
+               break;
+       }
+       return SHAPE_CONE;
+}
+
 void ShapesIcons::draw(FSNode *node) const
 {
+       int s = fstype_shape(node->type);
+       priv->shape[s]->draw();
 }
index d3ccc8b..bf49be8 100644 (file)
@@ -263,7 +263,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 missmatch (%d instead of %d)\n", __FUNCTION__, num, nidx);
+               fprintf(stderr, "%s: index count mismatch (%d instead of %d)\n", __FUNCTION__, num, nidx);
                return 0;
        }
        nfaces = num / 3;
@@ -662,8 +662,9 @@ void Mesh::draw_wire() const
 
        ((Mesh*)this)->update_wire_ibo();
 
+       int num_faces = get_poly_count();
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, wire_ibo);
-       glDrawElements(GL_LINES, nfaces * 6, GL_UNSIGNED_INT, 0);
+       glDrawElements(GL_LINES, num_faces * 6, GL_UNSIGNED_INT, 0);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
 
        post_draw();
@@ -1196,14 +1197,16 @@ void Mesh::update_wire_ibo()
                glGenBuffers(1, &wire_ibo);
        }
 
-       unsigned int *wire_idxarr = new unsigned int[nfaces * 6];
+       int num_faces = get_poly_count();
+
+       unsigned int *wire_idxarr = new unsigned int[num_faces * 6];
        unsigned int *dest = wire_idxarr;
 
        if(ibo_valid) {
                // we're dealing with an indexed mesh
                const unsigned int *idxarr = ((const Mesh*)this)->get_index_data();
 
-               for(unsigned int i=0; i<nfaces; i++) {
+               for(int i=0; i<num_faces; i++) {
                        *dest++ = idxarr[0];
                        *dest++ = idxarr[1];
                        *dest++ = idxarr[1];
@@ -1214,7 +1217,7 @@ void Mesh::update_wire_ibo()
                }
        } else {
                // not an indexed mesh ...
-               for(unsigned int i=0; i<nfaces; i++) {
+               for(int i=0; i<num_faces; i++) {
                        int vidx = i * 3;
                        *dest++ = vidx;
                        *dest++ = vidx + 1;
@@ -1226,7 +1229,7 @@ void Mesh::update_wire_ibo()
        }
 
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, wire_ibo);
-       glBufferData(GL_ELEMENT_ARRAY_BUFFER, nfaces * 6 * sizeof(unsigned int), wire_idxarr, GL_STATIC_DRAW);
+       glBufferData(GL_ELEMENT_ARRAY_BUFFER, num_faces * 6 * sizeof(unsigned int), wire_idxarr, GL_STATIC_DRAW);
        delete [] wire_idxarr;
        wire_ibo_valid = true;
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);