Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added an example which prints out model info.

It loads a model in any supported file format (.obj and .ply at time of
writing) and prints out the number of polygons, vertices, textures, etc.
  • Loading branch information...
commit f22008a69c1acc4ebf07471969450d73cc598215 1 parent 37ecf1d
@vilya authored
Showing with 146 additions and 0 deletions.
  1. +1 −0  CMakeLists.txt
  2. +145 −0 example/modelinfo.cpp
View
1  CMakeLists.txt
@@ -90,6 +90,7 @@ example(arcball)
example(basic)
example(example)
example(imageview)
+example(modelinfo)
example(raymarch)
example(shadowmap)
example(stencil)
View
145 example/modelinfo.cpp
@@ -0,0 +1,145 @@
+// Example of using VGL's model loading functions to print out info about a
+// model to the terminal. This is a command line app, no gui involved.
+
+#include "vgl.h"
+
+#include <cstdio>
+#include <cstring>
+#include <vector>
+
+
+class ModelInfo : public vgl::ParserCallbacks
+{
+public:
+ ModelInfo() :
+ _filename(),
+ _totalFaces(0), _triangles(0), _quads(0), _polygons(0),
+ _totalVertices(0), _totalNormals(0), _totalTexCoords(0),
+ _totalMaterials(0), _totalTextures(0),
+ _low(1e20, 1e20, 1e20), _high(-1e20, -1e20, -1e20),
+ _vertsForCurrentFace(0), _maxVertsPerFace(0)
+ {
+ }
+
+ virtual void beginModel(const char* path)
+ {
+ _filename = path;
+ }
+
+ virtual void endModel()
+ {
+ printStats();
+ }
+
+ virtual void beginFace()
+ {
+ ++_totalFaces;
+ _vertsForCurrentFace = 0;
+ }
+
+ virtual void endFace()
+ {
+ if (_vertsForCurrentFace == 3)
+ ++_triangles;
+ else if (_vertsForCurrentFace == 4)
+ ++_quads;
+ else
+ ++_polygons;
+
+ if (_vertsForCurrentFace > _maxVertsPerFace)
+ _maxVertsPerFace = _vertsForCurrentFace;
+ }
+
+ virtual void beginMaterial(const char* name)
+ {
+ ++_totalMaterials;
+ }
+
+ virtual void indexAttributeParsed(int attr, size_t value)
+ {
+ if (attr == kCoordRef)
+ ++_vertsForCurrentFace;
+ }
+
+ virtual void vec3fAttributeParsed(int attr, const vgl::Vec3f& value)
+ {
+ if (attr == ParserCallbacks::kCoord) {
+ ++_totalVertices;
+ _low = pairwiseMin(_low, value);
+ _high = pairwiseMax(_low, value);
+ }
+ else if (attr == ParserCallbacks::kNormal) {
+ ++_totalNormals;
+ }
+ else if (attr == ParserCallbacks::kTexCoord) {
+ ++_totalTexCoords;
+ }
+ }
+
+ virtual void textureAttributeParsed(int attr, const char* path)
+ {
+ ++_totalTextures;
+ }
+
+private:
+ void printStats()
+ {
+ printf("%s\n", _filename.c_str());
+ printf("x = %f - %f\n", _low.x, _high.x);
+ printf("y = %f - %f\n", _low.x, _high.x);
+ printf("z = %f - %f\n", _low.x, _high.x);
+ printf("\n");
+ printf("%lu faces of up to %lu vertices\n", _totalFaces, _maxVertsPerFace);
+ printf("- %lu triangles\n", _triangles);
+ printf("- %lu quads\n", _quads);
+ printf("- %lu larger polygons\n", _polygons);
+ printf("\n");
+ printf("%lu vertices\n", _totalVertices);
+ printf("%lu texture coords\n", _totalTexCoords);
+ printf("%lu normals\n", _totalNormals);
+ printf("\n");
+ printf("%lu materials\n", _totalMaterials);
+ printf("%lu textures\n", _totalTextures);
+ }
+
+private:
+ std::string _filename;
+
+ size_t _totalFaces;
+ size_t _triangles;
+ size_t _quads;
+ size_t _polygons;
+
+ size_t _totalVertices;
+ size_t _totalNormals;
+ size_t _totalTexCoords;
+
+ size_t _totalMaterials;
+ size_t _totalTextures;
+
+ vgl::Vec3f _low;
+ vgl::Vec3f _high;
+
+ size_t _vertsForCurrentFace;
+ size_t _maxVertsPerFace;
+};
+
+
+int main(int argc, char** argv)
+{
+ if (argc <= 1) {
+ fprintf(stderr, "Usage: %s <model> [ <model> ... ]\n", argv[0]);
+ return 1;
+ }
+
+ for (int i = 1; i < argc; ++i) {
+ if (i > 1)
+ printf("---\n");
+
+ ModelInfo info;
+ vgl::loadModel(&info, argv[i]);
+ }
+
+ return 0;
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.