Sign in to follow this  
zeltrax5

Importing 3D files into C++

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this