Importing 3D files into C++

Started by
8 comments, last by AverageJoeSSU 14 years, 2 months ago
Hey, I just started learning C++ a month ago. So far, I know the basics; I can make simple programs and I know how to import text files. But how do I import 3D files? I want to make a simple game which will display a model on the screen. Do I need some kind of a file converter? or do I have to use a game engine? *I mostly use Wings3D and Blender to make my models
Advertisement
Try using: http://assimp.sourceforge.net/
I had never heard of that one, it looks pretty cool.

Thanks.

------------------------------

redwoodpixel.com

Thanks for the reply but how do I exactly install assimp? It says I have to install boost first. I downloaded both of them but I'm having trouble installing them
Moved to the For Beginners forum.
You'll need a 3D graphics API like DirectX or OpenGL to properly display the 3d objects. I think what you're missing is a good sense of what your 3d objects actually are.

A 3d mesh is just a huge collection of vertices, which are themselves just triples or 4-tuples of float/double values indicating x,y,z coordinates of each vertex.

Part of the "simple game" you're thinking of writing will have to accept a model file of some arbitrary format, read it, and know how to draw all the verts or the polygons they represent to the screen. This gets much trickier than you might originally think, it's not as simple as importing a text file (which is raw text, something your console knows how to work with quite easily).

There are a host of beginner DirectX/OpenGL tutorials out there, I think once you get the hang of rendering a single triangle to the screen, you'll understand more of what you're up against. For only having been working with C++ for a month, this is a bigger step up than what you may think.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

Ok thanks for the advice
A typical mesh file contains:
- vertices (array of xyz points)
- triangles (refers to 3 vertices, a material, 3 normals, 3 texture coordinates)
- materials (name, texture filepath, diffuse/specular/emmissive setting, transparency settings)
- maybe 1 or more groups. Each group is a collection of triangles.
- maybe a skeleton for animation data + the animation keyframes
- maybe scene settings (camera, lights, fog, ...)

In code it could look like:
struct TVertex { float x, float y, float z };struct TVector3 { float x, float y, float z };struct TTexcoord { float s, float t };struct TTriangle {    *TMaterial material;   int[2] vertexIndex;   TVector[2] normal;   TTexcoord[2] texcoord;};struct TMaterial{   char[127] imageFilename;   TVector3 diffuseColor, specularColor, emmissiveColor, ambientcolor;   float transparency;   int blendingModus;};struct TGroup{   *TTriangle tris; // Collection of triangles};struct TMesh{   // here it all comes together   *TVertex vertices; // all points   *TGroup groups; // 1 or more triangle groups   *TMaterial materials;   void loadFromFile( filename ... ) {}   void render( ... ){}};


This is pretty much how meshes are made, in particular the Milkshape 3D fileformat here. Other file formats ussually contain the same stuff, although maybe with more or less info, and easier/harder to read.

Good luck
Based on how recently you started and how you asked your question, I would advise backing waaaay up from the last couple answers. Start with Binary File I/O. Basically reading/writing to/from binary files (instead of text files).

And I assume you're already familiar with dynamic memory allocation? If not, you'll want to pick that up too before starting in on 3D


-me
I agree,

i didnt realize the newness of the OP.

Definitely want to back it up and start with the basics, it will save you time and headache.

and as for an importer, this comes after you make/have a renderer, and that is IF you even want to make one.

------------------------------

redwoodpixel.com

This topic is closed to new replies.

Advertisement