Using Blender/SketchUp Models in OpenGL
Asked Answered
S

6

13

I'm making a renderer using OpenGL. I have textured models in Blender / Sketchup (I can exchange between the two easily), and I'd like to be able to export those files into my renderer. My initial idea was to simply export the raw faces and render those triangles, but I'd like to easily slice my texture files into texture coordinates as well.

By that, I mean that my model faces get carved into triangles. You can see in this image (reproduced below) that my curve becomes 24 triangles. I would like to know what texture coordinates to use for each triangle.

Polygonized curved 3D object

Would a DAE file be the easiest way to do that? I've been reading the specs for the format and it looks easy enough. I think I could parse the XML and faithfully recreate the models in OpenGL. I'm wondering if there is an easier way (i.e. one that doesn't reinvent the wheel).

Stabile answered 1/12, 2009 at 7:44 Comment(3)
Can you elaborate on what you mean by "I'd like to easily slice my texture files into texture coordinates as well"Stymie
Sure thing! Edited my original post.Stabile
Questions with images rule, so I took the liberty of adding yours.Josiejosler
S
12

If you're comfortable with parsing the .dae-format, sure use it. However, if you're only interested in exporting textured triangle meshes I would consider using the .obj format which is much more simple to parse. From what I can tell both Sketchup and Blender can export this format.

Stymie answered 1/12, 2009 at 10:14 Comment(2)
That looks like a simpler way of representing the same data. I'll look into the OBJ format tonight.Stabile
I want you to know that the OBJ (and corresponding MTL files) worked beautifully. Thank you so much!Stabile
O
5

If binary formats don't scare you, I'd suggest writing a Blender & Sketchup plug-in and exporting the geometry pre-baked into packed vertex arrays.

The beautiful thing about this method is that there's no parsing or type conversion in your app. Everything is ready to be sent to the GPU in simple contiguous memory copies. Great for static geometry.

A stripped down blender exporter looks something like this:


#!BPY
import bpy, struct
from Blender import *

self.fh = open("MyFileName", "w")
m = bpy.data.meshes["MyMeshName"]
faces = m.faces
for face in faces:
    for (vertex, uv) in zip(face.verts, face.uv):
        self.fh.write(struct.pack('<fff', *vertex.co)) # coords
        self.fh.write(struct.pack('<fff', *vertex.no)) # normals
        self.fh.write(struct.pack('<ff',  uv.x, uv.y)) # uvs

self.fh.close()
Objectivism answered 9/12, 2009 at 23:2 Comment(0)
O
2

If you wish to parse .dae files, i would suggest to look into Collada parsers. .dae is actually the extension for Collada files, the latest effort from the Khronos group (maintainers of OpenGL) to have a single unified file format for 3D data exchange.

As for the existing parsers, here is what I've come across:

  • collada-dom, the reference implementation. As the name suggests, it is just an abstraction of the XML tree.
  • FCollada, a nicer abstraction. However, this project has been dead for almost two years, and, from what I've gathered, it is unlikely that we'll see any update in the future.
  • OpenCollada, a recent new effort. Haven't tried it, but there is an active community behind it.

That being said, if your only goal is loading a simple piece of geometry with vertices, normals and texture coordinates, going with the .obj file format might be a quicker way.

Ottavia answered 1/12, 2009 at 22:33 Comment(1)
I want you to know that OBJ files worked beautifully. Thank you so much!Stabile
P
1

Any decent file format used by modeling programs (such as Blender or Sketchup) will include all information necessary to recreate the geometry you see. This should include the geometry type (e.g. triangle strips, individual triangles, etc), the vertices and normals for the geometry, the material properties used, and also the textures used along with the appropriate texture coordinates. If anything is lacking from a potential file format, choose another one.

If you think that parsing XML is simple, then I hope you're planning on using existing libraries to do this, such as expat, Xerces, or some other language specific implementation.

When considering import/export, first try to find an open source library that can handle the import for you and make the data available to your program in a reasonable format. If that's not available, and you must write your own importer, then try to find a simple ASCII (not XML-based) or binary format that fits your needs. PLY might be suitable. Only as a a last resort, would I recommend trying to implement an importer for an XML-based format.

Porras answered 1/12, 2009 at 10:11 Comment(1)
I've worked on web apps before; I know enough to not do my own XML parsing. It would be through a library.Stabile
B
1

There's also: Lib3DS - http://www.lib3ds.org/ LibOBJ (won't let me post link)

Bambara answered 11/12, 2009 at 0:29 Comment(2)
That's interesting! I'll check it out.Stabile
@Stabile There's also probably plenty of open source libraries all over Github.Astrahan
F
0

You should take a look at:

http://sketchup.google.com/community/developers.html

The sketchup team provides a C++ COM server for free. Using this, you can get access to lots of information about a .skp file.

You should be able to use this COM server to write a .skp importer for your application.

You should try to get the .tlb file if you can. This will give you access to smart pointers, which will simplify your importer (COM client)

I used the version 6 SDK (which included the tlb file). The current version of the SDK does not appear to include this file.

Someone posted a solution (for the missing tlb file) on the developer forums: http://groups.google.com/group/su-sdk-fileshare/topics

Figured answered 7/12, 2009 at 22:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.