Sign in to follow this  

Making a model loader

This topic is 4302 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

Hey all, im using Java with OpenGL TO make a 3d shooter, up to loading models now. Im trying to read .3ds files and having some troubles.
import java.io.*;
import java.util.*;

public class ModelLoader{

	Set vertices;


	//For Testing
	
	public static void main(String args[]){

		new ModelLoader("M_SKEL1.3DS");

	}


	public ModelLoader(String filepath){

		try{

			File f = new File(filepath);
			DataInputStream input = new DataInputStream(new FileInputStream(f)); 

			readModel(input);

		}catch(Exception e){ System.out.println(e); }

	}

	public void readModel(DataInputStream in){

		try{

			//Read Header - 0x4D4D
			int header = in.readShort(); 
			int chunkLength = in.readInt();

			if(header == 0x4d4d){
				System.out.println("Found Main Chunk"); 

			}

			//Read Header - 0x3d3d - 3D EDITOR CHUNK
			header = in.readShort();
			chunkLength = in.readInt();
			
			if(header == 0x3d3d){
				System.out.println("Found 3D Editor Chunk"); 

			}



		}catch(Exception e){ System.out.println(e); }



		



	}

}








Now in the output i get that it found the main chunk, but it doesnt find the 3d editor chunk, im not sure why this is, in my understanding of the format of .3ds it should be reading the 3d editor chunk?? Any help would be greatly apprectiated. Thanks in advance, Nick.

Share this post


Link to post
Share on other sites
There can be tons of chunks before the 3d3d chunk. In most cases this will be the Version chunk. Anyway, there is no fixed order for chunks to appear. For example, the index, texcoord and vertex lists can appear in any order. Therefore your loader should be very flexible. You can't just make a standard Load() function for it.

Have fun!

Share this post


Link to post
Share on other sites
Ok continued working and yet another problem:



import java.io.*;
import java.util.*;

public class ModelLoader{

Set vertices;
int header;
int chunkLength;
String objectName;
int numVert;

float x,y,z;


//For Testing

public static void main(String args[]){

new ModelLoader("M_SKEL1.3DS");

}


public ModelLoader(String filepath){

try{

File f = new File(filepath);
DataInputStream input = new DataInputStream(new FileInputStream(f));

objectName = new String();
vertices = new HashSet();

readModel(input);

}catch(Exception e){ System.out.println(e); }

}

public void readModel(DataInputStream in){

try{

boolean finnished = false;


while(finnished == false){

//Read Header
header = in.readShort();
chunkLength = in.readInt();


if(header == 0x4d4d){
System.out.println("Found Main Chunk");

}

if(header == 0x3d3d){
System.out.println("Found 3D Editor Chunk");

}

if(header == 0x4000){
System.out.println("Found Object Block");

objectName = "";


char a = 's';
int i = [[0]];
while(a != '\[[0]]' && i < [[20]]){
a = in.readChar();
objectName += a;
i++;

}
System.out.println("Object Name:" + objectName);

}


if(header == 0x4100){
System.out.println("Found Triangular Mesh");



}

if(header == 0x4110){
System.out.println("Found Vertices List");

numVert = in.readUnsignedShort();
System.out.println("Num of vertices: " + numVert);

for(int i = [[0]]; i < numVert; i++){

x = in.readFloat();
y = in.readFloat();
z = in.readFloat();

vertices.add(new Vertex(x,y,z));

}

finnished = true;

}


if(header == 0x4120){
System.out.println("Found FACES DESCRIPTION");

}


if(header == 0x4130){
System.out.println("Found FACES MATERIAL");

}


if(header == 0x4140){
System.out.println("Found MAPPING COORDINATES LIST");
finnished = true;

}






}


}catch(Exception e){ System.out.println(e); }







}






Output =


Found Main Chunk
Found Triangular Mesh
Found 3D Editor Chunk
Found 3D Editor Chunk
Found 3D Editor Chunk
Found FACES DESCRIPTION
Found Object Block
Object Name:????????????????????
Found Triangular Mesh
Found Object Block
Object Name:☻
Found Object Block
Object Name:►
Found Object Block
Object Name
Found 3D Editor Chunk
Found 3D Editor Chunk
Found 3D Editor Chunk
Found Object Block
Object Name:
Found Object Block
Object Name:
Found Object Block
Object Name:
Found Object Block
Object Name:
Found Object Block
Object Name:
Found Object Block
Object Name:
Found Object Block
Object Name:
Found Object Block
Object Name:
Found Object Block
Object Name:
Found Object Block
Object Name:
Found Object Block
Object Name:?
Found Object Block
Object Name:?
Found Object Block
Object Name:
Found Object Block
Object Name:??
Found Object Block
Object Name:
Found Object Block
Object Name:
Found Object Block


And it goes on further??

It never finds the vertice list??

I really have no idea why this is happening, any help would be greatly appreciated!. Also i dont think im reading the object name corrrectly?

Thanks in advance, Nick

Share this post


Link to post
Share on other sites

This topic is 4302 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.

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