In my application, I have the shape and dimensions of a complex 3D solid (say a Cylinder Block) taken from user input. I need to construct vertex and index buffers for it.开发者_开发技巧
Since the dimensions are taken from user input, I cannot user Blender or 3D Max to manually create my model. What is the textbook method to dynamically generate such a mesh?
I am looking for something that will generate the triangles given the vertices, edges and holes. Something like TetGen, though TetGen has no way of excluding the triangles which fall on the interior of the solid/mesh.
Sounds like you need to create an array of verticies, and a list of triangles each of which contains a list of 3 indicies into the vertex array. There is no easy way to do this. To draw a box, you need 8 veticies and 12 triangles (2 per side). Some representations will use explicit edge representations too. I suspect this is way more work than you want to do so.....
What you need is a mesh library that can do CSG (composite solid geometry). This way you should be able to specify the dimensions of the block, and then the dimensions of the cylinders and tell it to cut them out for you (CSG difference). All the vertex and triangle management should be done for you. In the end, such a library should be able to export the mesh to some common formats. Only problem here is that I don't know the name of such a library. Something tells me that Blender can actually do all of this if you know how to script it. I also suspect there are 1 or 2 fairly good libraries out there.
Google actually brought me back to StackOverflow with this:
A Good 3D mesh library
You may ultimately need to generate simple meshes programatically and manipulate them with a library if they don't provide functions for creating meshes (they all talk about manipulating a mesh or doing CSG).
It depends a bit on your requirements.
If you don't need to access the mesh after generating, but only need to render it, the fastest option is to create a vertex buffer with glGenBuffers
, map it into memory with glMapBuffer
, write your data into the buffer, then unmap it with glUnmapBuffer
. Drawing will be very fast because all data can be uploaded to video card memory.
If you do need to access the mesh data after generating it, or if you expect to modify it regularly, you might be better off building your vertex data in a regular array and using vertex arrays with glVertexPointer
and friends.
You can also use a combination: generate the mesh data in main memory, then memcpy()
it into a mapped vertex buffer.
Finally, if by "dimensions" you mean just scaling the entire thing, you can create it offline in any 3D modelling program and use the OpenGL transformations, for example glScale
, to apply the dimensions to the mesh while rendering.
I'm not sure if the Marching Cube algorithm would be any help?.
精彩评论