This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

| 3DS ASCII Loader Submitted by

The code loads and draws an ASC(3DS ASCII) file in OpenGL. It does not load textures or colors. This code is totally free for use, no strings attached.

Download Associated File: asc.cpp (2,615 bytes)

    #include <vector>
    #include <iostream>
    #include <fstream>
    #include <string.h>
    #include <math.h>
    using namespace std;

    struct Triangle{
    	int data[3];
    };

    struct Vertex{
    	float x, y, z;
    };

    //strips the prefix from the faces and UV coords
    //and returns the index
    int strip(string b){
    	int num=0;

    	int x;
    	int colon;

    	int size=b.size();
    	colon=b.find(":");		//find the :
    	colon++;
    	for(int i=colon;i<size;i++){
    		num=((num*10)+(b[i]-'0'));

    	}
    	return num;
    }

    class ASC{
    	private:
    	float pos[3];
    	float rot[3];
    	float scale[3];

    	int numVerts;
    	int numFaces;
    	vector <Vertex> vertices;
    	vector <Triangle> tris;
    //	vector <UVTex>

    	public:
    	void Load(char* filename);
    	void Draw();
    	void setPos(float x, float y, float z){};
    	void setRot(float x, float y, float z){};
    	void setScale(float x, float y, float z){};
    };

    void ASC::Load(char* filename){
    	ifstream fin(filename);
    	char b[256];

    	string buffer;
    	Vertex t;
    	Triangle tri;

    	fin>>buffer>>buffer>>buffer;	//pass the named object line
    	fin>>buffer>>buffer;		//reads past "tri-mesh" and "vertices"
    	fin>>(int)numVerts;		//number of vertices
    	fin>>buffer;			//reads past "buffer"
    	fin>>(int)numFaces;		//number of faces

    	fin>>buffer>>buffer;		//pass the vertex list line

    	//reads in vertices
    	for(int i=0;i<numVerts;i++){
    		fin>>buffer>>buffer>>buffer;	//pass "vertex", "0:" and "X:"
    		fin>>(float)t.x;
    		fin>>buffer;
    		fin>>(float)t.y;
    		fin>>buffer;
    		fin>>(float)t.z;
    		vertices.push_back(t);
    	}

    	fin>>buffer>>buffer;			//passes "Face list:"

    	//reads in the vertex indices
    	for(int i=0;i<numFaces;i++){
    	fin>>buffer>>buffer;
    	fin>>buffer;				//get A:-
    	tri.data[0]=strip(buffer);
    	fin>>buffer;				//get B:-
    	tri.data[1]=strip(buffer);
    	fin>>buffer;				//get C:-
    	tri.data[2]=strip(buffer);
    	tris.push_back(tri);

    	fin>>buffer;				//get AB:-
    	//tri.data[0]=strip(buffer);
    	fin>>buffer;				//get BC:-
    	//tri.data[1]=strip(buffer);
    	fin>>buffer;				//get CA:-
    	//tri.data[2]=strip(buffer);
    	//tris.push_back(tri);

    	memset(b,256,' ');
    	fin.getline(b,256);			//reads material line
    	}

    	fin.close();
    }

    void ASC::Draw(){
    	glPushMatrix();
    	glColor3f(1.0,0.0,0.0);
    	glBegin(GL_TRIANGLES);
    	for(int i=0; i<numFaces; i++){
    		for(int j=0; j<3; j++){
    			glVertex3f(vertices[tris[i].data[j]].x,vertices[tris[i].data[j]].y,vertices[tris[i].data[j]].z);
    		}
    	}
    	glEnd();
    	glPopMatrix();
    }

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.