Vertices and Indices with SketchUp C API


#1

Hello,

I am working to implement some example code from the SketchUp C++ API (pasted below) and incorporate it into my OpenGL example but I am having problems distinguishing what are the indices in the code example below.

I get where the vertices come from/are; but what are the indices below.

And for that matter; is there an example of how to load the normals, texture coordinates, and associating material data with the with a specific face/edge/etc.?

I hope this question makes sense; thank you for your time.

#include <slapi/slapi.h>
#include <slapi/geometry.h>
#include <slapi/initialize.h>
#include <slapi/unicodestring.h>
#include <slapi/model/model.h>
#include <slapi/model/entities.h>
#include <slapi/model/face.h>
#include <slapi/model/edge.h>
#include <slapi/model/vertex.h>
#include <vector>
int main() {
  // Always initialize the API before using it
  SUInitialize();
  // Load the model from a file
  SUModelRef model = SU_INVALID;
  SUResult res = SUModelCreateFromFile(&model, "model.skp");
  // It's best to always check the return code from each SU function call.
  // Only showing this check once to keep this example short.
  if (res != SU_ERROR_NONE)
    return 1;
  // Get the entity container of the model.
  SUEntitiesRef entities = SU_INVALID;
  SUModelGetEntities(model, &entities);
  // Get all the faces from the entities object
  size_t faceCount = 0;
  SUEntitiesGetNumFaces(entities, &faceCount);
  if (faceCount > 0) {
    std::vector<SUFaceRef> faces(faceCount);
    SUEntitiesGetFaces(entities, faceCount, &faces[0], &faceCount);
    // Get all the edges in this face
    for (size_t i = 0; i < faceCount; i++) {
      size_t edgeCount = 0;
      SUFaceGetNumEdges(faces[i], &edgeCount);
      if (edgeCount > 0) {
        std::vector<SUEdgeRef> edges(edgeCount);
        SUFaceGetEdges(faces[i], edgeCount, &edges[0], &edgeCount);
        // Get the vertex positions for each edge
        for (size_t j = 0; j < edgeCount; j++) {
          SUVertexRef startVertex = SU_INVALID;
          SUVertexRef endVertex = SU_INVALID;
          SUEdgeGetStartVertex(edges[j], &startVertex);
          SUEdgeGetEndVertex(edges[j], &endVertex);
          SUPoint3D start;
          SUPoint3D end;
          SUVertexGetPosition(startVertex, &start);
          SUVertexGetPosition(endVertex, &end);
          // Now do something with the point data
        }
      }
    }
  }
  // Get model name
  SUStringRef name = SU_INVALID;
  SUStringCreate(&name);
  SUModelGetName(model, &name);
  size_t name_length = 0;
  SUStringGetUTF8Length(name, &name_length);
  char* name_utf8 = new char[name_length + 1];
  SUStringGetUTF8(name, name_length + 1, name_utf8, &name_length);
  // Now we have the name in a form we can use
  SUStringRelease(&name);
  delete []name_utf8;
  // Must release the model or there will be memory leaks
  SUModelRelease(&model);
  // Always terminate the API when done using it
  SUTerminate();
  return 0;
}

#2

I’m unclear to which indices you are referring to. Which line of that code does that relate to?

Face normals: SUFaceGetNormal
Texture data: SUFaceGetUVHelper

SUMaterialInput using SUGeometryInput.

(@bugra - can you confirm? Or is there better ways?)


#3

I’m unclear to which indices you are referring to. Which line of that code does that relate to?

See the code below.

With OpenGL the best performance for loading mesh data is when you have vertex/index data, I can see the vertices below but I am not sure what corresponds with indices for each vertex.

Or does the SketchUp API not support indices for the Vertexes?

Thank you.

// Get all the faces from the entities object
  size_t faceCount = 0;
  SUEntitiesGetNumFaces(entities, &faceCount);
  if (faceCount > 0) {
    std::vector<SUFaceRef> faces(faceCount);
    SUEntitiesGetFaces(entities, faceCount, &faces[0], &faceCount);
    // Get all the edges in this face
    for (size_t i = 0; i < faceCount; i++) {
      size_t edgeCount = 0;
      SUFaceGetNumEdges(faces[i], &edgeCount);
      if (edgeCount > 0) {
        std::vector<SUEdgeRef> edges(edgeCount);
        SUFaceGetEdges(faces[i], edgeCount, &edges[0], &edgeCount);
        // Get the vertex positions for each edge
        for (size_t j = 0; j < edgeCount; j++) {
          SUVertexRef startVertex = SU_INVALID;
          SUVertexRef endVertex = SU_INVALID;
          SUEdgeGetStartVertex(edges[j], &startVertex);
          SUEdgeGetEndVertex(edges[j], &endVertex);
          SUPoint3D start;
          SUPoint3D end;
          SUVertexGetPosition(startVertex, &start);
          SUVertexGetPosition(endVertex, &end);
          // Now do something with the point data
        }
      }
    }
  }

Question about material input
#4

I am not really sure I understand the question but SketchUp supports drawing of loose geometry, like disconnected edges and faces here and there. So not everything is necessarily a “mesh” per se. That code example just reads loose geometry from the root of the model, for instance. SketchUp doesn’t really assign an index for any given vertex. Instead, you may want to collect the data and use your own indexing scheme?

The SDK contains an exporter example (skp_to_xml) and an importer example (xml_to_skp). These do have code reading and writing most of what you want I think.

You can also look at the example I posted here. That shows mapping a texture on a face.
Do those help at all?


#5

Thank you; I’ll try this out.

I guess what I am trying to get at is to simply take the data and have OpenGL draw the data.

From the examples it is unclear whether which data I would create a buffer for with OpenGL and then draw it.

So, when you say loose geometry I get nervous and lost; does that mean I simply create a vertex buffer out of all of the geometry presented and draw it?

Hope this makes sense; I want to keep it simple for now and draw the geometry using OpenGL from the given data.

If there is an example of using OpenGL with this API that would be great.


#6

Sorry, I don’t know of an example showing how to get data from the C API and draw it with OpenGL. Whether you create a single large vertex buffer for the whole data or draw things in smaller chunks would be a design decision. I can’t really recommend an approach on that.

Good luck and let us know how it goes!