Archived

This topic is now archived and is closed to further replies.

3DS File loading

This topic is 5003 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am trying to load 3DS file, it seems to load in everything. When I render it I get weird results. I think I am doing something wrong in code. Here is my code // 3ds.h
#ifndef _3DS_H
#define _3DS_H

#include <vector>
#include <iostream>
#include "mtxlib.h"

#define CHUNK_MAIN          0x4D4D
#define CHUNK_VERSION       0x0002

#define CHUNK_3DEDITOR      0x3D3D
#define OBJECT_BLOCK        0x4000
#define OBJECT_TRIMESH      0x4100

#define VERTICES_LIST       0x4110
#define FACE_LIST           0x4120
#define FACE_LIST2          0x4111



typedef struct {
    unsigned short usId;
    unsigned int uiLength;
} _3DSHeader;

typedef struct {
    std::vector <vector3> vVertices;
    std::vector <vector3> vTexCoords;
    std::vector <vector3> vNormals;
    std::vector <unsigned int> vFaces;
} _3DSCoordinates;

class C3DS {
    public :
        bool Load(const char *filename);
        void Destroy();
    
    private :
        _3DSHeader m_Header;
        _3DSCoordinates m_Coords;
};

#endif
//3ds.cpp
#include <iostream>
#include <cstdio>
#include "3ds.h"

using namespace std;

bool C3DS::Load(const char *filename)
{
    FILE *fp = fopen(filename, "rb");
    char name[20];
    unsigned short value;
    unsigned short flags;
    vector3 vertices;
    vector3 normals;
    vector3 textures;
    unsigned short face[3];
    
    if(!fp) return false;
    
    // read in the 1st 6 bytes and check if its a 3ds file

    fread(&m_Header, 1, sizeof(_3DSHeader), fp);
    
    if(m_Header.usId != CHUNK_MAIN) return false;
    
    // read in the next chunks in a while loop

    while(!feof(fp))
    {
        fread(&m_Header, 1, sizeof(_3DSHeader), fp);
        
        switch(m_Header.usId)
        {
            case CHUNK_3DEDITOR :
                // do nothing

                break;
            
            case OBJECT_BLOCK :
                // read in the name

                fread(name, 20, sizeof(char), fp);
                break;
            
            case OBJECT_TRIMESH :
                // do nothing        

                break;
            
            case VERTICES_LIST :
                fread(&value, 1, sizeof(unsigned short), fp);  
                for(int i = 0; i < value; i++)
                {
                    fread(&vertices.x, 1, sizeof(float), fp);
                    fread(&vertices.y, 1, sizeof(float), fp);
                    fread(&vertices.z, 1, sizeof(float), fp);
                    fread(&flags, 1, sizeof(unsigned short), fp);
                    
                    this->m_Coords.vVertices.push_back(vertices);
                }
                
                break;
            
            case FACE_LIST :
                fread(&value, 1, sizeof(unsigned short), fp);
                                
                for(int i = 0; i < value; i++)
                {
                    fread(face, 3, sizeof(unsigned short), fp);
                    fread(&flags, 1, sizeof(unsigned short), fp);
                    
                    cout << "Faces : " << face[0] << " " << face[1] << " " << face[2] << endl;
                                                            
                    this->m_Coords.vFaces.push_back(face[0]);
                    this->m_Coords.vFaces.push_back(face[1]);
                    this->m_Coords.vFaces.push_back(face[2]);
                }
                                
                break;
                                      
        }
    }   
                     
    fclose(fp);
    
    return true;
}


This is how I am rendering it
void C3DS::Render()
{
    vector3 *verts = &this->m_Coords.vVertices[0];
    unsigned int *faces = &this->m_Coords.vFaces[0];
    static int numSize = this->m_Coords.vFaces.size();
    
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    glBegin(GL_TRIANGLE_STRIP);
    for(int i = 0; i < numSize; i++)
    {
        glVertex3fv((float *)&verts[faces[i]]);
    }
    glEnd();
}
Hopefully someone can bring some light to what I may be doing wrong. Thanks in advance.

Share this post


Link to post
Share on other sites
dunno then, really. yo''ve got to check your input data. like if the vertices are way out of place, or if the triangle indices don''t actually eceed the number of vertices, or are negative. If it''s wrong, then eihter the file is corrupted, or you got the file format wrong.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Try the lib3ds library (http://lib3ds.sourceforge.net/)
There is some interesting examples

Share this post


Link to post
Share on other sites