How can I create a 3D model file from geometric sh

2019-08-04 12:03发布

问题:

I am writing a program that will output 3D model files based on simple geometric shapes (e. g. rectangular prisms & cylinders) with known coordinates in 3-dimensional space. As an example, imagine creating a 3D model of stonehenge. this question suggests that OBJ files are the easiest to generate, but I'm struggling to find a good tutorial or easy-to-use library for doing so.

Can anyone either

(1) describe step-by-step how to create a simple file OR (2) point me to a tutorial that describes how to do so

Notes: * Using a GUI-based program to draw such files is not an option for me * I have no prior experience with 3D modeling * Other formats such as WRL or DAE would work for me as well

EDIT:

I do not need to use textures, just combinations of simple geometric shapes positioned in 3D space.

回答1:

I strongly recommend to use some ASCII exchange format there are many out there I usually use these:

  1. *.x DirectX object (it is a C++ source code)

    this one is easiest to implement !!! But there are not many tools that can handle them. If you do not want to spend too much time coding then this is the right choice. Just copy the templates (at the start) from any *.x file to get started.

    here some specs

  2. *.iges common and importable on most CAD/CAM platform (Catia included)

    this one is a bit complicated but for export purposes it is not that bad. It supports Volume operation like +,-,&,^ which are VERY HARD to implement properly but you do not have to use them :)

  3. *.dxf AutoCAD exchange format

    this one is even more complicated then IGES. I do not recommend to use it

  4. *.ac AC3D

    I first saw this one in flight gear.

    here some specs

    at first look it is quite easy but the sub-object implementation is really tricky. Unless you use it you should be fine.

This approach is easily verifiable in note pad or by loading to some 3D model viewer. Chose one that is most suitable for your needs and code save/load function to your Apps internal model class/struct. This way you will be compatible with other software and eliminate incompatibility problems which are native to creating 'almost known' binary formats like 3ds,...

In your case I would use IGES (Initial Graphics Exchange Specification)

For export you do not need to implement all just few basic shapes so it would not be too difficult. I code importers which are much much more complicated. Mine IGES loader class is about 30KB of C++ source code look here for more info

You did not provide any info about your 3D mesh model structure and capabilities

like what primitives you use, are your object simple or in skeleton hierarchy, are you using textures, and more ... so it is impossible to answer

Anyway export often looks like this:

  1. create header and structure of target file format
  2. if the format has any directory structure fill it and write it (IGES)

    for sub-objects do not forget to add transformation matrices ...

  3. write the chunks you need (points list, faces list, normals, ...)

With ASCII formats you can do this inside String variable so you can easily insert into or modify. Do all thing in memory and write the whole thing to file at the end which is fast and also add capability to work with memory instead of files. This is handy if you want to pack many files to single package file like *.pak or send/receive files through IPC or LAN ...

[Edit1] more about IGES

fileformat specs

I learned IGES from this pdf ... Have no clue where from I got it but this was first valid link I found in google today. I am sure there is some non registration link out there too. It is about 13.7 MB and original name IGES5-3_forDownload.pdf.

win32 viewer

this is free IGES viewer. I do not like the interface and handling but it works. It is necessary to have functional viewer for testing yours ...

examples

here are many tutorial files for many entities there are 3 sub-links (igs,peek,gif) where you can see example file in more ways for better understanding.

exporting to IGES

you did not provide any info about your 3D mesh internal structure so I can not help with export. There are many ways to export the same way so pick one that is closest to your App 3D mesh representation. For example you can use:

  • point cloud
  • rotation surfaces
  • rectangle (QUAD) surfaces
  • border lines representation (non solid)
  • trim surface and many more ...