Create Wavefront .obj files in C++ (mesh 3D) - c++

I have a vector<vector<int>>, which contains my map (2D array created with my random generator):
(source: cjoint.com)
I want to display this map in 3D (with Irrlicht graphic 3D library). The big problem: my map is too big (1920x1080), so i can't display 2073600 little cube in my screen. (I want to be able to change my map and reload the screen with the good mesh)
So my solution is to create One cube, and write on it all the pixel I want
(here is my little paint to show you...)
(source: cjoint.com)
So... I know how to create/write/parse a file in c++, now my problem is: I don't know very well 3D perspective and .obj object...
I am learning OBJ format with wikipedia and other docs.
I wonder if there is more simplest solution than changing in live a .obj object... And if not... i required some help for the conception of my obj...

I think you are confusing issues here. Alias wavefront obj is a file format for storing 3d geometry, it is extremely easy to use to extract the geometry. The MTL (Material Template Library) is a bit more complex that just the geometry and is usually associated with .obj files for defining the visual representation of the geometry (in regards to its material appearance).
What you are asking is more along the lines of a geometric question (how to remove a hole from a surface) and depends entirely how your geometry is represented (I assume triangulated since you ask about obj, which represent triangulated data). More information is required about how you store your data.
Maybe try looking into Constructive Solid Geometry which use Boolean operations to construct geometry. If you have triangulated data, then unless you employ some form of BVH for processing which triangles/geometry to work on, you will ultimately be using brute force to see which triangles are valid and which need removing for your 'hole removal'.

Related

Joining two meshes into one

Suppose I have two meshes stored in any sane format (e.g. wavefront .obj or collada .dae), and I want to combine them into one mesh programmatically. More precise, I have a landscape and an object as two meshes. I want to put object into landscape after performing transformation to it, so it gets on the right place, and export this as result model.
As far as I understood, in assimp there is something similar named SceneCombiner, yet it seems that this is internal structure and has no interface (even though here https://github.com/assimp/assimp/issues/584 the ticket concerning it is closed, I couldn't find out how to use it).
Maybe I should use CGAL or something like that? I don't have very much experience in CG libraries, so any advice will be really useful!
You can do that with CGAL. You would read two meshes, and the call copy_face_graph(), and then write the mesh back.

Generating 3D models via primitive skinning

I am looking for a method by which to generate 3D models for use in video games. The idea is virtual primitives that are simply points with associated data for size, shape, material and rotation.
For instance an asteroid might start as two simple spheres that intersect. Material of dusty rock which would tell the skinning algorithm to provide smooth sandy curves and occasional jagged boulders. Probably end up with a sort of lumpy peanut shape.
After that add smaller spheres with material of void or crater, peppered around the object. These would produce crater like areas in the surface of the peanut and the skin would adjust to suit. In the end you would have a semi plausible representation of an asteroid.
Now with that in mind, my question is, are there any decent open source or public domain examples of skinning algorithms that can find the surface of a model and generate a smooth, evenly distributed quad-strip mesh that could be then textured?
Some more information; I'm looking at CSG methods for the underlying models (adding and subtracting volume) then looking at other methods for remeshing the whole thing.
Skinning is an art more than a scientific process (and so almost impossible to automate) because skinning is a visual approximation of movement. To get something fully automatic, you would either have to assume bone placement or simply assume there are none at all.
Here's an example. This is an open-source project that skins automatically based on the fact that the provided mesh is a humanoid.
http://igl.ethz.ch/projects/fast/
EDIT: Wait, you mean the other way around? Isn't that similar to marching cubes? http://en.wikipedia.org/wiki/Marching_cubes
This is an exciting question and no doubt there are many ways it could be done. Personally I'd probably start by getting basic shapes on .obj format, which is easy to both parse and create programmatically, and then do exactly that in my code: tweak or randomize the the vertices you export from a modelling program to create an infinite variety of similar but slightly different objects, like asteroids. Of course if you need more than asteroids, you'd go back to a different .obj file. It's hard to say the best technique for your case since I think some experimentation would be required no matter what you try.

List of verticies from OpenGL program to something importable

I'm working on making a new visualization of the type of binary stars I study, and I'm starting from an existing code that renders a nice view of them given some sensible physical parameters.
I would like a bit more freedom on the animation side of things, however, and my first thought was to output the models made by the program in a format that could be read in by something else (Blender?) I've read up on the (Wavefront?) .OBJ format, and while it seems straightforward, I can't seem to get it right; importing fails silently, and I suspect it's because I'm not understanding how the objects are actually stored.
The program I'm starting from is a C++ project called BinSim, and it already has a flag to output vertices to a log file for all the objects created. It seems pretty simple, just a list of indices, x, y, z, and R, G, B (sometimes A) values. An example output format I've been working with can be found here; Each object is divided up into a latitude/longitude grid of points, and this is a small snippet (full file is upwards of 180 MB for all the objects created).
I've been able to see that the objects are defined as triangle strips, but I'm confused enough by all of this that I can't see the clear path towards making this list of vertices into an .OBJ (or whatever) format. Sorry if this really belongs in another area (GameDev?), and thanks!
OpenGL is not a scene management system. It's a drawing API and starting off OpenGL data structures for model storage is tedious. As already said, OpenGL draws things. There are several drawing primitives, the triangle strip being one of them. You start with two vertices (forming a line) and each next incoming vertex extends the line of the last two specified vertices to a triangle. The Wavefront OBJ format doesn't know triangle strips, you'd have to break them down into individual triangles, emulating the way OpenGL does it.
Also don't forget that Blender is easily extensible using Python scripting and you can just write a import script for whatever data you already have without going through the hassle of using some ill suited format.

The best way to export 3d mesh animation in order to use it in OpenGL

Suppose that I have a 3d model with animation in, say, Blender. I need to export this model to some file and then use it in OpenGL app, i.e. without hardcoding animations, but reading them from file. What format is the best solution?
OpenGL doesn't support any format directly, but the classic OBJ file format lines up very well with drawing with vertex arrays. Basically, OBJ lists all vertices independently of the geometry. This way, several objects can share the same points. All kinds of groupings are also possible.
Also, it is one of the earliest formats to support a wide range of spline curves & surfaces, including Bezier, B-Splines & NURBS.
A basic decription can be found here:
http://en.wikipedia.org/wiki/Wavefront_.obj_file
The complete OBJ spec can be found here:
http://www.martinreddy.net/gfx/3d/OBJ.spec
It's not as modern as WebGL, but it's simple, human readable and widely supported.
What format is the best solution?
OpenGL doesn't care about file formats. So feel free to choose whatever suits your needs best. Due to the rise of WebGL I started dumping whole Blender scenes into collections of JSON formated files.

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.
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).
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.
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()
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.
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.
There's also:
Lib3DS - http://www.lib3ds.org/
LibOBJ (won't let me post link)
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