Reading Textures from a SketchUp Model Using the Reader SDK
September 29th, 2008 | Published in Google SketchUp
In case you haven't looked yet, we have an SDK that allows you to read SketchUp files using a C++ interface. This allows you to write your own exporter for Google SketchUp or an importer for another application to read SketchUp files. Oftentimes you want all the polygons on your exported model to be triangles. Since most faces in a SketchUp model are quadrilateral polygons, you will need to triangulate the faces. Also, if a face has a texture you will want the uvq coordinates of the triangulated faces as well. Besides the face and the mesh that represents the face after it is triangulated, there are several other objects in the SketchUp Reader SDK that are used to read the uvq texture coordinates of a face. For example, there is the texture writer object. This is the object used to write out the face textures to disk. In order to retrieve the uvq texture coordinates of the mesh you also need a UV helper object and also a perspective object.
First, using the SkechUp document object, create the texture writer object:
First, using the SkechUp document object, create the texture writer object:
ISkpApplication* sketchup_application = NULL;
sketchup_document->get_Application(&sketchup_application);
ISkpTextureWriter* texture_writer = NULL;
sketchup_application->CreateTextureWriter(&texture_writer);
Next, retrieve the ISkpCorrectPerspective interface of the texture writer:
ISkpCorrectPerspective* perspective = NULL;
texture_writer->QueryInterface(IID_ISkpCorrectivePerspective,
&perspective);
For each face retrieve its UV helper object using the perspective object:
ISkpUVHelper* uv_helper = NULL;
face->GetUVHelper(face,
true, // front face
perspective,
&uv_helper);
Finally, create the triangulated mesh:
ISkpPolygonMesh* mesh = NULL;
face->CreateMesh(
PolygonMeshPoints |
PolygonMeshUVQFront |
PolygonMeshUVQBack,
perspective, &mesh);
Index through the vertices of the mesh and retrieve the position value of each vertex. If the face object belongs to a group or a component instance, the position needs to be transformed. The homogeneous transformation used to transform the position is the aggregate of the transformation of the parent group or component instance. If the parent is nested in other group(s) or component instance(s) then you the need to aggregate all the transformations.
double position[] = {0, 0, 0};
mesh->_GetPoint(index, position);
Finally, retrieve the uvq value that corresponds to the vertex position. The uvq value does not need to be transformed like the position value:
double uvq[] = {0, 0, 0};
uv_helper->GetFrontUVQ(
position[0],
position[1],
position[2],
&uvq[0],
&uvq[1],
&uvq[1]);
Stay tuned for further tips and tricks.