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.
[/code]
Look at the file in a hex viewer. 3D3D is not necessarily going to be immediately after 4D4D. I opened one and here is what I have found.
4D 4D 1A 4A 00 00 02 00 0A 00 00 00 03 00 00 00
3D 3D 30 45 00 00 3E 3D 0A 00 00 00 03 00 00 00
Also don't forget that the .3ds file may be written in little endian, but Java reads files in big endian format by default. To change the way Java reads in from a file, you have 2 choices.
1. Read in byte by byte and convert each type manually.
2. Use java.nio.ByteBuffer and java.nio.channels.FileChannel
With the second method you can set the endian type in the ByteBuffer so when you call getShort or getInt it will return what you expect.
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
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 till eof??
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
Don't forget that a char is 16 bits in Java. You should be reading in a byte at a time.