Partage
  • Partager sur Facebook
  • Partager sur Twitter

Problème lecture d'un fichier md2.

Il ne m'affiche rien et je ne trouve pas l'erreur.

Sujet résolu
    15 mai 2009 à 11:13:37

    Bonjour, j'ai un problème que je n'arrive pas à résoudre, je ne sais pas pourquoi, pourtant en c++ j'ai jamasi eu de problèmes, et là je n'arrive pas à afficher un modèle md2, j'ai du changer pas mal de choses car il n'y a pas de structures en Java ni d'opérateur sizeOf, j'ai du le lire donc octets par octets, et remplacer les structures par des classes, mais, ça ne marche pas car rien ne s'affiche, si je met une texture je me retrouves avec un ArrayIndexOutOfBoundException, pareil pour lorsque je veux tracer à l'aide de commandes openGL, aurai je mal lu une donnée du fichier ou me serai je trompé lors du dessin du modèle, je ne trouves pas le problème et ça fait depuis plusieurs jours que je cherche, si quelqu'un pourrait m'aider merci d'avance, c'est la seule choses qui me bloque avant la connexion d'un joueur sur la map.
    Alors voici mon code :
    Ha oui aussi j'ai du lire le fichier byte part byte et placer les bytes dans des entiers ou autre type de varaible, car readInt ne me renvoyaient pas les bonnes valeur et il n'y a pas d'unsigned en Java.
    Je vous préviens le code est long.
    package wotck.jeu;
    
    import java.io.*;
    import java.nio.FloatBuffer;
    import javax.media.opengl.*;
    
    import wotck.factory.TypeFactory;
    
    
    public class Model3D {
    	private static final int MD2_IDENT_WIN = 844121161;	
    	private static final int MD2_VERSION = 8;
    	private static final int NUMNORMALS = 162;
    	
    	private int ident; //Numéro magique.
    	private int version; //Vesion du fichier.
    	private int skinWidth; //Largeur texture.
    	private int skinHeight; //Hauteur texture.
    
    	private int frameSize; //Taille d'une frame en octets.
    
    	private int numSkins; //Nombre de textures.
    	private int numVertices; //Nombre de sommets par frame.
    	private int numCt; //nombre de coordonnées textures.
    	private int numTris; //Nombre de triangles.
    	private int glCommands; //Nombre de commandes openGL.
    	private int numFrames; // Nombre de frames;
    
    	private int offsetSkins; //Position des données sur les textures dans le fichier.
    	private int offsetCt; //Position des données sur les coordonnées des textures.
    	private int offsetTris; //Position des données sur les triangles.
    	private int offsetFrames; //Position des données sur les frames.
    	private int offsetCommands; //Position des données sur les commandes opengl.
    	
    	
    	
    	private static Vec3t[]  anorms = new Vec3t[NUMNORMALS]; //Vecteur nomrmaux pour le caclul des lumières.	private MD2TexCoord[] texCoords;
    	private MD2TexCoord[] texCoords;
    	private MD2Triangle[] triangles;
    	private MD2Frame[] frames;
    	private int[] glCmds;
    	private float scale;
    	private int textID;
    	
    	private String[] nameTextureFile; //Nom du fichier de la texture.
    	public Model3D () {
    		try {
    			FileInputStream fis = new FileInputStream ("normals.txt");
    			DataInputStream dis = new DataInputStream (fis);
    			for (int i = 0; i < NUMNORMALS; i++) {
    				anorms[i] = new Vec3t (dis.readFloat(), dis.readFloat(), dis.readFloat());				
    			}
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			System.out.println("Fichier normals.txt introuvable.");
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			System.out.println("Erreur lors de la lecture des données.");
    		}
    	}
    	public boolean loadMd2Model (String fileName) {
    		RandomAccessFile raf = null;		
    		FileWriter fw = null;
    		try {			
    			fw = new FileWriter (fileName+".txt");
    			raf = new RandomAccessFile (fileName, "r");			
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			System.out.println("Fichier "+fileName+" introuvable");
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		try {
    			raf.seek(0);
    			byte[] b = new byte[4];
    			byte[] bs = new byte[2];
    			byte[] bb = new byte[1];
    			raf.readFully(b);
    			ident = TypeFactory.makeInt(b);
    			fw.write("Numéro ID : "+ident+"\n");
    			raf.readFully(b);			
    			version = TypeFactory.makeInt(b); 
    			fw.write("Version : "+version+"\n");
    			
    			if (ident != MD2_IDENT_WIN || version != MD2_VERSION)
    				return false;
    			raf.readFully(b);
    			skinWidth = TypeFactory.makeInt(b);	
    			fw.write("Largeur textures : "+skinWidth+"\n");
    			raf.readFully(b);
    			skinHeight = TypeFactory.makeInt(b);
    			fw.write("Hauteur textures : "+skinHeight+"\n");
    			raf.readFully(b);
    			frameSize = TypeFactory.makeInt(b);	
    			fw.write("Taille des frames : "+frameSize+"\n");
    			raf.readFully(b);
    			numSkins = TypeFactory.makeInt(b);
    			fw.write("Nombre de textures : "+numSkins+"\n");
    			raf.readFully(b);
    			numVertices = TypeFactory.makeInt(b);
    			fw.write("Nombre de sommets : "+numVertices+"\n");
    			raf.readFully(b);			
    			numCt = TypeFactory.makeInt(b);
    			fw.write("Nombre de coord textures : "+numCt+"\n");
    			raf.readFully(b);
    			numTris = TypeFactory.makeInt(b);
    			fw.write("Nombre de triangles : "+numVertices+"\n");
    			raf.readFully(b);
    			glCommands =  TypeFactory.makeInt(b);
    			fw.write("Nombre de commandes openGL : "+glCommands+"\n");
    			raf.readFully(b);
    			numFrames = TypeFactory.makeInt(b);
    			fw.write("Nombre de frames : "+numFrames+"\n");
    			raf.readFully(b);
    			offsetSkins = TypeFactory.makeInt(b);
    			fw.write("Décalage textures : "+offsetSkins+"\n");
    			raf.readFully(b);
    			offsetCt = TypeFactory.makeInt(b);
    			fw.write("Décalage de coordonnées textures : "+offsetCt+"\n");
    			raf.readFully(b);
    			offsetTris = TypeFactory.makeInt(b);
    			fw.write("Déclage de triangles : "+offsetTris+"\n");
    			raf.readFully(b);
    			offsetFrames = TypeFactory.makeInt(b);
    			fw.write("Décalage de frames : "+offsetFrames+"\n");
    			raf.readFully(b);
    			offsetCommands = TypeFactory.makeInt(b);
    			fw.write("Décalage commandes openGL : "+offsetCommands+"\n");			
    			nameTextureFile = new String[numSkins];
    			texCoords = new MD2TexCoord[200];
    			triangles = new MD2Triangle[numTris];
    			frames = new MD2Frame [numFrames];
    			glCmds = new int[glCommands];
    			raf.seek(offsetSkins);
    			fw.write("Nom des fichiers pour les textures : \n");
    			for (int i = 0; i < numSkins; i++) {
    				byte[] caract = new byte[68];
    				raf.readFully(caract);
    				nameTextureFile[i] = new String (caract);	
    				fw.write("Nom texture fichier n° "+i+" : " + nameTextureFile[i]+"\n");
    			}
    			raf.seek(offsetCt);
    			fw.write("Coordonnées des textures : \n");
    			for (int i = 0; i < 200; i++) {				
    				texCoords[i] = new MD2TexCoord ();
    				raf.readFully(bs);
    				texCoords[i].setS(TypeFactory.makeShort(bs));
    				raf.readFully(bs);
    				texCoords[i].setT(TypeFactory.makeShort(bs));
    				fw.write(texCoords[i].getS()+" "+texCoords[i].getT()+"\n");
    			}
    			raf.seek(offsetTris);
    			fw.write("Triangles : \n");
    			for (int i =0; i < numTris; i++) {	
    				int x, y, z;
    				triangles[i] = new MD2Triangle ();				
    				raf.readFully(bs);
    				x = TypeFactory.makeUnsignedShort(bs);				
    				raf.readFully(bs);
    				y = TypeFactory.makeUnsignedShort(bs);
    				raf.readFully(bs);
    				z = TypeFactory.makeUnsignedShort(bs);				
    				triangles[i].setMd2TriangleVertex(x, y, z);	
    				fw.write(triangles[i].getVertex()[0]+" "+triangles[i].getVertex()[1]+" "+triangles[i].getVertex()[2]+"\n");
    				raf.readFully(bs);
    				x = TypeFactory.makeUnsignedShort(bs);
    				raf.readFully(bs);
    				y = TypeFactory.makeUnsignedShort(bs);
    				raf.readFully(bs);
    				z = TypeFactory.makeUnsignedShort(bs);				
    				triangles[i].setCt(x, y, z);
    				fw.write(triangles[i].getCt()[0]+" "+triangles[i].getCt()[1]+" "+triangles[i].getCt()[2]+"\n");				
    			}
    			raf.seek(offsetFrames);
    			fw.write("Frames : \n");
    			for (int i = 0; i < numFrames; i++) {
    				MD2Vertex[] verts = new MD2Vertex[numVertices];
    				frames[i] = new MD2Frame (numVertices);
    				float x, y, z;
    				raf.read(b);
    				x = TypeFactory.makeFloat(b);
    				raf.read(b);
    				y = TypeFactory.makeFloat(b);
    				raf.read(b);
    				z = TypeFactory.makeFloat(b);
    				frames[i].setScale(new Vec3t (x, y, z));
    				raf.read(b);
    				x = TypeFactory.makeFloat(b);
    				raf.read(b);
    				y = TypeFactory.makeFloat(b);
    				raf.read(b);
    				z = TypeFactory.makeFloat(b);
    				frames[i].setTranslate(new Vec3t (x, y, z));
    				byte[] nameFrame = new byte[16];
    				raf.readFully(nameFrame);
    				frames[i].setNameFrame(new String(nameFrame));
    				fw.write(frames[i].getNameFrame() + "\n");
    				for (int j = 0; j < numVertices; j++) {
    					verts[j] = new MD2Vertex ();
    					int dx, dy, dz, n;
    					raf.readFully(bb);
    					dx = TypeFactory.makeUnsignedByte(bb);					
    					raf.readFully(bb);
    					dy = TypeFactory.makeUnsignedByte(bb);
    					raf.readFully(bb);
    					dz = TypeFactory.makeUnsignedByte(bb);
    					raf.readFully(bb);
    					n = TypeFactory.makeUnsignedByte(bb);
    					verts[j].setV(new int[]{dx, dy, dz});
    					verts[j].setNormalIndex(n);	
    					fw.write (verts[j].getV()[0]+" "+verts[j].getV()[1]+" "+verts[j].getV()[2]+" "+verts[j].getNormalIndex()+"\n");
    				}
    				frames[i].setVerts(verts);				
    			}
    			raf.seek(offsetCommands);
    			fw.write("Commandes opengl : \n");
    			for (int i = 0; i < glCommands; i++) {
    				raf.readFully(b);
    				glCmds[i] = TypeFactory.makeInt(b);
    				fw.write(glCmds[i]+"\n");
    			}
    			raf.close();
    			fw.close();			
    			return true;			
    		} catch (IOException e) {
    			System.out.println("Erreur pendant la lecture du fichier md2.");
    			return false;
    		}
    	}
    	public void loadTexture (GL gl, int textID) {
    		this.textID = textID;
    	}
    	public void renderFrame (GL gl, int iFrame) {
    		int iMaxFrame = numFrames - 1;
    		if (iFrame < 0 || iFrame > iMaxFrame)
    			return;
    		gl.glBindTexture (GL.GL_TEXTURE_2D, textID);
    		gl.glBegin(GL.GL_TRIANGLES);
    		
    		for (int i = 0; i < numTris; i++) {
    			for (int k = 0; k < 3; k++) {
    				MD2Frame frame = frames[iFrame];
    				MD2Vertex vertex = frame.getVerts()[triangles[i].getVertex()[k]];
    				/*float s = texCoords[triangles[i].getCt()[k]].getS() / skinWidth;
    				float t = texCoords[triangles[i].getCt()[k]].getT() / skinHeight;
    				gl.glTexCoord2f(s, t);*/
    				gl.glNormal3fv(FloatBuffer.wrap(anorms[vertex.getNormalIndex()].getVec3t()));	
    				Vec3t v = new Vec3t (
    						(frame.getScale().getVec3t()[0] * vertex.getV()[0] + frame.getTranslate().getVec3t()[0]) * scale,
    						(frame.getScale().getVec3t()[1] * vertex.getV()[1] + frame.getTranslate().getVec3t()[1]) * scale,
    						(frame.getScale().getVec3t()[2] * vertex.getV()[2] + frame.getTranslate().getVec3t()[2]) * scale);	
    				gl.glVertex3fv(FloatBuffer.wrap(v.getVec3t()));
    				System.out.println(v.getVec3t()[0]+" "+v.getVec3t()[1]+" "+v.getVec3t()[2]);
    			}
    		}
    		gl.glEnd();
    	}
    	public void drawModelItp (GL gl, int iFrameA, int iFrameB, float fInterp) {
    		int iMaxFrame = numFrames - 1;
    		
    		if (iFrameA < 0 || iFrameB < 0)
    			return;
    		if (iFrameA > iMaxFrame || iFrameB > iMaxFrame)
    			return;
    		gl.glBindTexture(GL.GL_TEXTURE_2D, textID);		
    		gl.glBegin(GL.GL_TRIANGLES);		
    		for (int i = 0; i < numTris; i++) {
    			for (int k = 0; k < 3; k++) {
    				MD2Frame frameA = frames[iFrameA];
    				MD2Frame frameB = frames[iFrameB];				
    				
    				MD2Vertex vertA = frameA.getVerts()[triangles[i].getVertex()[k]];
    				MD2Vertex vertB = frameB.getVerts()[triangles[i].getVertex()[k]];
    				
    				/*float s = texCoords[triangles[i].getCt()[k]].getS() / skinWidth;
    				float t = texCoords[triangles[i].getCt()[k]].getT() / skinHeight;				
    				gl.glTexCoord2f(s, t);*/
    				
    				Vec3t normA, normB, n;
    				
    				normA = new Vec3t (anorms[vertA.getNormalIndex()].getVec3t()[0],
    						anorms[vertA.getNormalIndex()].getVec3t()[1],
    						anorms[vertA.getNormalIndex()].getVec3t()[2]);
    				normB = new Vec3t (anorms[vertB.getNormalIndex()].getVec3t()[0],
    						anorms[vertB.getNormalIndex()].getVec3t()[1],
    						anorms[vertB.getNormalIndex()].getVec3t()[2]);
    				n = new Vec3t (normA.getVec3t()[0]	+ fInterp * (normB.getVec3t()[0] - normA.getVec3t()[0]),
    						normA.getVec3t()[1]	+ fInterp * (normB.getVec3t()[1] - normA.getVec3t()[1]),
    								normA.getVec3t()[2]	+ fInterp * (normB.getVec3t()[2] - normA.getVec3t()[2]));
    				gl.glNormal3fv(FloatBuffer.wrap(n.getVec3t()));
    				Vec3t vecA, vecB, v;
    				vecA = new Vec3t (frameA.getScale().getVec3t()[0]  * vertA.getV()[0] + frameA.getTranslate().getVec3t()[0],
    						frameA.getScale().getVec3t()[1]  * vertA.getV()[1] + frameA.getTranslate().getVec3t()[1],
    						frameA.getScale().getVec3t()[2]  * vertA.getV()[2] + frameA.getTranslate().getVec3t()[2]);
    				
    				vecB = new Vec3t (frameB.getScale().getVec3t()[0]  * vertB.getV()[0] + frameB.getTranslate().getVec3t()[0],
    						frameB.getScale().getVec3t()[1]  * vertB.getV()[1] + frameB.getTranslate().getVec3t()[1],
    						frameB.getScale().getVec3t()[2]  * vertB.getV()[2] + frameB.getTranslate().getVec3t()[2]);
    				
    				v = new Vec3t ((vecA.getVec3t()[0] + fInterp * (vecB.getVec3t()[0] - vecA.getVec3t()[0])) * scale,
    						(vecA.getVec3t()[1] + fInterp * (vecB.getVec3t()[1] - vecA.getVec3t()[1])) * scale,
    						(vecA.getVec3t()[2] + fInterp * (vecB.getVec3t()[2] - vecA.getVec3t()[2])) * scale);
    				System.out.println(v.getVec3t()[0]+" "+ v.getVec3t()[1]+" "+v.getVec3t()[2]);
    				gl.glVertex3fv(FloatBuffer.wrap(v.getVec3t()));
    			}
    		}
    		gl.glEnd();		
    	}
    	public void renderFrameWithGLcmds (GL gl, int iFrame) {
    		int iMaxFrame = numFrames - 1;
    		if (iFrame < 0 || iFrame > iMaxFrame)
    			return;
    		gl.glBindTexture (GL.GL_TEXTURE_2D, textID);
    		int n = 0;
    		while (n < glCommands) {			
    			int j = glCmds[n];
    			if (j < 0) {
    				gl.glBegin(GL.GL_TRIANGLE_FAN);
    				j = -j;
    			} else {
    				gl.glBegin(GL.GL_TRIANGLE_STRIP);
    			}
    			n++;
    			for ( ;j>0; j--, n+=3) {
    				MD2GlCommand glCom = new MD2GlCommand ();
    				glCom.setS(glCmds[n]);
    				glCom.setT(glCmds[n+1]);
    				glCom.setIndex(glCmds[n+2]);
    				MD2Frame frame = frames[iFrame];
    				MD2Vertex vert = frame.getVerts()[glCom.getIndex()];
    				gl.glTexCoord2f(glCom.getS(), glCom.getT());
    				gl.glNormal3fv(FloatBuffer.wrap(anorms[vert.getNormalIndex()].getVec3t()));
    				
    				Vec3t v;
    				v = new Vec3t ((frame.getScale().getVec3t()[0] * vert.getV()[0] + frame.getTranslate().getVec3t()[0]) * scale,
    						(frame.getScale().getVec3t()[1] * vert.getV()[1] + frame.getTranslate().getVec3t()[1]) * scale,
    						(frame.getScale().getVec3t()[2] * vert.getV()[2] + frame.getTranslate().getVec3t()[2]) * scale);
    				
    				gl.glVertex3fv(FloatBuffer.wrap(v.getVec3t()));
    			}
    			gl.glEnd();
    		}		
    	}
    	public void drawModelItpWithGlCmds (GL gl, int iFrameA, int iFrameB, float fInterp) {
    		int iMaxFrame = numFrames - 1;
    		if (iFrameA < 0 || iFrameB < 0)
    			return;
    		if (iFrameA > iMaxFrame || iFrameB > iMaxFrame)
    			return;
    		gl.glBindTexture (GL.GL_TEXTURE_2D, textID);	
    		int n = 0;
    		while (n < glCommands) {			
    			int j = glCmds[n];
    			if (j < 0) {
    				gl.glBegin(GL.GL_TRIANGLE_FAN);
    				j = -j;
    			} else {
    				gl.glBegin(GL.GL_TRIANGLE_STRIP);
    			}
    			n++;
    			for ( ;j>0; j--, n+= 3) {
    				MD2GlCommand glCom = new MD2GlCommand ();
    				glCom.setS(glCmds[n]);
    				glCom.setT(glCmds[n+1]);
    				glCom.setIndex(glCmds[n+2]);
    				MD2Frame frameA = frames[iFrameA];
    				MD2Frame frameB = frames[iFrameB];
    				MD2Vertex vertA = frameA.getVerts()[glCom.getIndex()];
    				MD2Vertex vertB = frameB.getVerts()[glCom.getIndex()];
    				gl.glTexCoord2f(glCom.getS(), glCom.getT());
    				
    				Vec3t normA, normB, no;
    				normA = anorms[vertA.getNormalIndex()];
    				normB = anorms[vertB.getNormalIndex()];
    				no = new Vec3t (normA.getVec3t()[0] + fInterp * (normB.getVec3t()[0] - normA.getVec3t()[0]),
    						normA.getVec3t()[1] + fInterp * (normB.getVec3t()[1] - normA.getVec3t()[1]),
    						normA.getVec3t()[2] + fInterp * (normB.getVec3t()[2] - normA.getVec3t()[2]));
    				gl.glNormal3fv (FloatBuffer.wrap(no.getVec3t()));
    				Vec3t vecA, vecB, v;
    				vecA = new Vec3t (frameA.getScale().getVec3t()[0] * vertA.getV()[0] + frameA.getTranslate().getVec3t()[0],
    						frameA.getScale().getVec3t()[1] * vertA.getV()[1] + frameA.getTranslate().getVec3t()[1],
    						frameA.getScale().getVec3t()[2] * vertA.getV()[2] + frameA.getTranslate().getVec3t()[2]);
    				vecB = new Vec3t (frameB.getScale().getVec3t()[0] * vertB.getV()[0] + frameB.getTranslate().getVec3t()[0],
    						frameB.getScale().getVec3t()[1] * vertB.getV()[1] + frameB.getTranslate().getVec3t()[1],
    						frameB.getScale().getVec3t()[2] * vertB.getV()[2] + frameB.getTranslate().getVec3t()[2]);
    				v = new Vec3t (vecA.getVec3t()[0] + fInterp * (vecB.getVec3t()[0] - vecA.getVec3t()[0]) * scale,
    						vecA.getVec3t()[1] + fInterp * (vecB.getVec3t()[1] - vecA.getVec3t()[1]) * scale,
    						vecA.getVec3t()[2] + fInterp * (vecB.getVec3t()[2] - vecA.getVec3t()[2]) * scale);
    				gl.glVertex3fv(FloatBuffer.wrap(v.getVec3t()));
    			}
    			gl.glEnd();
    		}		
    	}
    	public static Vec3t[] getAnorms() {
    		return anorms;
    	}
    	public static void setAnorms(Vec3t[] anorms) {
    		Model3D.anorms = anorms;
    	}
    	public MD2Triangle[] getTirangles() {
    		return triangles;
    	}
    	public void setTirangles(MD2Triangle[] tirangles) {
    		this.triangles = tirangles;
    	}
    	public MD2Frame[] getFrames() {
    		return frames;
    	}
    	public void setFrames(MD2Frame[] frames) {
    		this.frames = frames;
    	}
    	public int[] getGlCmds() {
    		return glCmds;
    	}
    	public void setGlCmds(int[] glCmds) {
    		this.glCmds = glCmds;
    	}
    	public float getScale() {
    		return scale;
    	}
    	public void setScale(float scale) {
    		this.scale = scale;
    	}
    	public int getTextID() {
    		return textID;
    	}
    	public void setTextID(int textID) {
    		this.textID = textID;
    	}
    	public void setTexCoords (MD2TexCoord[] coord) {
    		this.texCoords = coord;
    	}
    	public MD2TexCoord[] getTexCoords () {
    		return texCoords;
    	}
    	private class Vec3t {
    		private float[] vec3t = new float[3];
    		public Vec3t (float x, float y, float z) {
    			vec3t[0] = x;
    			vec3t[1] = y;
    			vec3t[2] = z;
    		}		
    		public float[] getVec3t () {
    			return vec3t;
    		}
    	}
    	private class MD2Vertex {
    		private int[] v; //Position dans l'espace. (Relative au modèle)
    		private int normalIndex; //Index normal du sommet.
    		public  MD2Vertex () {
    			v = new int[3];
    		}
    		
    		public int[] getV() {
    			return v;
    		}
    		public void setV(int[] v) {
    			this.v = v;
    		}
    		public int getNormalIndex() {
    			return normalIndex;
    		}
    		public void setNormalIndex(int normalIndex) {
    			this.normalIndex = normalIndex;
    		}	
    		
    	}
    	private class MD2Triangle {
    		private int[] vertex; //Indice vertex du triangle.
    		private int[] ct; //Indice de coordonnée de la texture.
    		public MD2Triangle () {
    			vertex = new int[3];
    			ct = new int[3];
    		}
    		public void setMd2TriangleVertex (int x, int y, int z) {
    			vertex[0] = x;
    			vertex[1] = y;
    			vertex[2] = z;
    		}
    		public void setCt (int x, int y, int z) {
    			ct[0] = x;
    			ct[1] = y;
    			ct[2] = z;
    		}		
    		public int[] getVertex () {
    			return vertex;
    		}
    		public int[] getCt () {
    			return ct;
    		}
    	}
    	private class MD2TexCoord {
    		private int s;
    		private int t;
    		public int getS() {
    			return s;
    		}
    		public void setS(int s) {
    			this.s = s;
    		}
    		public int getT() {
    			return t;
    		}
    		public void setT(int t) {
    			this.t = t;
    		}		
    	}
    	private class MD2Frame {
    		private Vec3t scale; //Vecteur de redimentionnement.
    		private Vec3t translate; //Vecteur de translation.
    		private String nameFrame; //Nom de la frame.
    		private MD2Vertex[] verts; //liste des sommets.
    		public MD2Frame (int numVert) {
    			verts = new MD2Vertex[numVert];
    		}
    		public Vec3t getScale() {
    			return scale;
    		}
    		public void setScale(Vec3t scale) {
    			this.scale = scale;
    		}
    		public Vec3t getTranslate() {
    			return translate;
    		}
    		public void setTranslate(Vec3t translate) {
    			this.translate = translate;
    		}
    		public String getNameFrame() {
    			return nameFrame;
    		}
    		public void setNameFrame(String nameFrame) {
    			this.nameFrame = nameFrame;
    		}
    		public MD2Vertex[] getVerts() {
    			return verts;
    		}
    		public void setVerts(MD2Vertex[] verts) {
    			this.verts = verts;
    		}
    	}
    	private class MD2GlCommand {
    		private float s;
    		private float t;
    		private int index;
    		public float getS() {
    			return s;
    		}
    		public void setS(float s) {
    			this.s = s;
    		}
    		public float getT() {
    			return t;
    		}
    		public void setT(float t) {
    			this.t = t;
    		}
    		public int getIndex() {
    			return index;
    		}
    		public void setIndex(int index) {
    			this.index = index;
    		}		
    	}
    	
    }
    
    • Partager sur Facebook
    • Partager sur Twitter
      15 mai 2009 à 12:22:43

      Je n'y connais absolument rien à la 3D mais deux remarques à propos de la lecture de fichiers binaires :
      1 - Lire un fichier octet par octet, c'est lent, surtout si le flux n'est pas bufférisé (ce qui est le cas pour un RandomAccessFile, pas approprié ici, et pour un FileOutputStream brut). Ca ne réglera pas ton problème mais tu devrais songer à lire l'intégralité du fichier, le stocker dans un tableau de bytes et ensuite seulement traiter les données. Ca sera, sans mentir, 20 à 30 fois plus rapide. C'est d'autant plus vrai si tes fichiers sont gros.
      2 - readInt, readShort et konsor ne fonctionnent pas pour une raison très simple. Ces fonctions enregistrent/lisent les données dans l'ordonancement big-endian, ce qui est presque toujours le contraire de ce qu'on souhaite (little-endian) lorsqu'on veut lire des formats de fichier natifs.
      Pour une possible solution/alternative/contournement, regarder les classes ByteBuffer, IntBuffer, ShortBuffer, etc. du package java.nio.
      • Partager sur Facebook
      • Partager sur Twitter
        15 mai 2009 à 12:33:18

        Ok, je vais jeter un coup d'oeuil à ses classes là, et lire le fichier d'un coup pour ensuite placer correctement mes données, à mon avis ce doit être un problème lors de la lecture des données, ça ne peut être que ça.
        Si je comprends bien ma fonction readInt ne lis pas les octets dans le bonne ordre.
        • Partager sur Facebook
        • Partager sur Twitter
          15 mai 2009 à 15:20:29

          Exact. C'est java qui a été construit comme ça, malheureusement la plupart des fichiers binaires arrangent leurs octets dans l'ordre inverse. tu peux aussi chercher sur google des classes qui implémentent des équivalents de readInt, readShort, readUnsignedShort, etc. IL y en a plein, j'ai moi-même réécrit une classe du genre DataInput/OutputStream qui prend en charge les deux ordres little/big-endian.
          • Partager sur Facebook
          • Partager sur Twitter
            15 mai 2009 à 16:16:06

            Ok c'est bon j'ai fait une classe qui permet de faire des unsignedShort, et j'ai changer l'ordre j'ai mit la constante LITTLE_ENDIAN et ça marche, plus qu'à afficher la texture maitenant parce que un orgre tout bleu ça le fait pas.

            public boolean loadMd2Model (String fileName) {
            		RandomAccessFile raf = null;		
            		FileWriter fw = null;
            		ByteBuffer bBuff;
            		try {			
            			fw = new FileWriter (fileName+".txt");
            			raf = new RandomAccessFile (fileName, "r");	
            			fc = raf.getChannel();
            			int size = (int) fc.size();
            			bBuff = ByteBuffer.allocate(size);
            			bBuff.order(ByteOrder.LITTLE_ENDIAN);
            			fc.read(bBuff);
            			bBuff.flip();
            			//b = bBuff.array();			
            		} catch (FileNotFoundException e) {
            			// TODO Auto-generated catch block
            			System.out.println("Fichier "+fileName+" introuvable");
            			return false;
            		} catch (IOException e) {
            			System.out.println("Erreur lors de la lecture des données!");
            			return false;
            		}		
            		ident = bBuff.getInt();
            		version = bBuff.getInt();		
            		if (ident != MD2_IDENT_WIN && version != MD2_VERSION)
            			return false;
            		skinWidth = bBuff.getInt();
            		skinHeight = bBuff.getInt();
            		frameSize = bBuff.getInt();
            		
            		numSkins = bBuff.getInt();
            		numVertices = bBuff.getInt();
            		numCt = bBuff.getInt();
            		numTris = bBuff.getInt();
            		glCommands = bBuff.getInt();
            		numFrames = bBuff.getInt();
            		
            		offsetSkins = bBuff.getInt();
            		offsetCt = bBuff.getInt();
            		offsetTris = bBuff.getInt();
            		offsetFrames = bBuff.getInt();
            		offsetCommands = bBuff.getInt();
            		end = bBuff.getInt();
            		
            		skinNames = new String[numSkins];
            		texCoords = new MD2TexCoord[numCt];
            		triangles = new MD2Triangle[numTris];
            		frames = new MD2Frame[numFrames];
            		
            		glCmds = new int[glCommands];
            		bBuff.position(offsetSkins);
            		for (int i = 0; i < numSkins; i++) {
            			byte[] b = new byte[68];
            			bBuff.get(b);
            			skinNames[i] = new String(b);
            		}
            		bBuff.position(offsetCt);
            		for (int i = 0; i < numCt; i++){
            			texCoords[i] = new MD2TexCoord();
            			texCoords[i].setS(bBuff.getShort());
            			texCoords[i].setT(bBuff.getShort());
            		}
            		bBuff.position(offsetTris);
            		byte[] us = new byte[2];
            		for (int i = 0; i < numTris; i++) {
            			triangles[i] = new MD2Triangle ();
            			int x, y, z, ctx, cty, ctz;
            			bBuff.get(us);
            			x = UnsignedVarFactory.createUnsignedShort(us);	
            			bBuff.get(us);
            			y = UnsignedVarFactory.createUnsignedShort(us);	
            			bBuff.get(us);
            			z = UnsignedVarFactory.createUnsignedShort(us);	
            			bBuff.get(us);
            			ctx = UnsignedVarFactory.createUnsignedShort(us);	
            			bBuff.get(us);
            			cty = UnsignedVarFactory.createUnsignedShort(us);	
            			bBuff.get(us);
            			ctz = UnsignedVarFactory.createUnsignedShort(us);	
            			triangles[i].setMd2TriangleVertex(x, y, z);
            			triangles[i].setCt(ctx, cty, ctz);
            		}
            		bBuff.position(offsetFrames);
            		for (int i = 0; i < numFrames; i++) {
            			frames[i] = new MD2Frame (numVertices);
            			float sx, sy, sz, tx, ty, tz;
            			byte[] b = new byte[16];			
            			sx = bBuff.getFloat();
            			sy = bBuff.getFloat();
            			sz = bBuff.getFloat ();
            			tx = bBuff.getFloat();
            			ty = bBuff.getFloat();
            			tz = bBuff.getFloat ();
            			frames[i].setScale(new Vec3t(sx, sy, sz));
            			frames[i].setTranslate(new Vec3t(tx, ty, tz));
            			bBuff.get(b);
            			frames[i].setNameFrame(new String(b));
            			MD2Vertex[] verts = new MD2Vertex[numVertices];
            			byte[] ub = new byte[1];
            			for (int k = 0; k < numVertices; k++) {
            				verts[k]= new MD2Vertex();
            				int vx, vy, vz, n;
            				bBuff.get(ub);
            				vx = UnsignedVarFactory.createUnsignedByte(ub);
            				bBuff.get(ub);
            				vy = UnsignedVarFactory.createUnsignedByte(ub);
            				bBuff.get(ub);
            				vz = UnsignedVarFactory.createUnsignedByte(ub);
            				bBuff.get(ub);
            				n = UnsignedVarFactory.createUnsignedByte(ub);
            				verts[k].setV(new int[] {vx, vy, vz});
            				verts[k].setNormalIndex(n);				
            			}
            			frames[i].setVerts(verts);
            		}
            		bBuff.position(offsetCommands);
            		for (int i =0; i < glCommands; i++) {
            			glCmds[i] = bBuff.getInt();
            		}
            		return true;
            	}
            

            • Partager sur Facebook
            • Partager sur Twitter

            Problème lecture d'un fichier md2.

            × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
            × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
            • Editeur
            • Markdown