Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Blender/SketchUp Models in OpenGL

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).

like image 547
mkenyon Avatar asked Dec 01 '09 07:12

mkenyon


2 Answers

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.

like image 109
Andreas Brinck Avatar answered Sep 21 '22 15:09

Andreas Brinck


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()
like image 40
pestilence669 Avatar answered Sep 18 '22 15:09

pestilence669