Re: [cgkit-user] Using the object loaders in other code.
Brought to you by:
mbaas
From: Matthias B. <mat...@gm...> - 2008-04-08 21:31:36
|
René Dudfield wrote: > is there a way to use the obj loader, Yes. > and viewer in another pyopengl application? Well, the viewer is a simple visualization application that isn't meant to be embedded as a whole inside another application. But you might have noticed that it is a fairly tiny application and the bulk of the work is inside the Python package which is reusable. Now you could use the package at various levels, depending on what you want to do. Let's begin at the lowest level: - The module "cgkit.objmtl" allows you to parse an OBJ (and MTL) file and process the data that is in those files (see http://cgkit.sourceforge.net/doc2/module-cgkit.objmtl.html) . You can derive from the OBJReader class and implement the handler methods that process the data (vertices, faces, etc.). In this case, it is your responsibility to create an appropriate data structure that stores the model and that can display it. - If you want cgkit to handle the object loading, you could simply use the load() function inside the cgkit.cmds module (or import it via cgkit.all), see http://cgkit.sourceforge.net/doc2/commands.html. In this case, you just pass the file name of the OBJ file (or any other format that is supported by cgkit) as argument and the object will be loaded into memory. cgkit maintains its own scene which will then carry the loaded model (the scene is nothing fancy. Think of it as a DOM-like structure that you can use to get to the 3D data). The easiest way to get an idea of what has been loaded into a scene is by using the listWorld() command: >>> from cgkit.all import * >>> load("cube.obj") >>> listWorld() Root +---pCube1 (Polyhedron/PolyhedronGeom) In this case, an object called "pCube1" which is a Polyhedron has been loaded. Have a look at this page to get an idea about how to access the scene: http://cgkit.sourceforge.net/doc2/node102.html Each piece of geometry is represented by a "WorldObject" (which represents the world space transformation) and a "GeomObject" (which represents the actual geometry). Every GeomObject has a method drawGL() which just draws the corresponding geometry in its local object space using OpenGL commands (http://cgkit.sourceforge.net/doc2/node154.html). So in your own application you could use this to draw the model. Every other OpenGL-related aspect (opening a window, setting up viewport, camera, lights, applying materials and transformations, etc.) would then still be up to you. - There is a class GLRenderInstance which is used inside the viewer tool. I just noticed that this class isn't documented, sorry. Basically, what it does is it just draws the entire scene using the above drawGL() methods and does everything that you would have to do yourself in the above case (except from opening an OpenGL context). The class is fairly simple and it doesn't do any fancy optimizations or tricks. Have a look at the draw() method inside viewer.py to see how it is initialized and used. I hope one of those approaches suits your application. Cheers, - Matthias - |