OBJ Textures Loads With Missing Chunk

Started by stevenlin111, March 02, 2013, 06:14:19

Previous topic - Next topic

stevenlin111



I'm trying to load textures from an OBJ file with LWJGL, but there seems to be places where the textures aren't loading correctly. It's supposed to look like this without the lighting:



But it looks like this:



Anybody have an idea of what is wrong here?

ra4king

Right, because we all have magic telepathy that can instantly discover the problem by looking at the picture and seeing your thoughts :)

Post relevant code, there is no way to see what could have happened here.
-Roi

kappa

just a wild guess, could it be that you have back/front face culling on and your normals are the wrong way round?

quew8

Have you tried using textures you know work? (ie ones not loaded from obj files). My question really is are you sure it is the loading part going wrong or could it be the rendering? My advice is try to narrow the problem down before posting huge swathes of code for us to dredge through.

stevenlin111

Here's the code for drawing. It's messy right now since I was trying to fix the problem:
public static int renderModelDisplayList(Model model, int x, int y, int z, int scale) {
		int displayList = glGenLists(1);
		glNewList(displayList, GL_COMPILE);
		ArrayList<Face> faces = model.getFaces();
		HashMap<String, Texture> jpgs = new HashMap<String, Texture>();
		Set set = model.getMtls().keySet();
		Object[] keys = set.toArray();
		for(int i = 0; i < keys.length; i++) {
			MTL mtl = model.getMtls().get((String) keys[i]);
			try {
				Texture texture = TextureLoader.getTexture("JPG", new FileInputStream(new File("res/models/" + mtl.getTgaImage())));
				jpgs.put(mtl.getTgaImage(), texture);
			} catch(Exception e) {
				e.printStackTrace();
			}
		}
		Texture texture;
		String currentMtl = "";
		for(int i = 0; i < faces.size(); i++) {
			Face face = faces.get(i);
			if(face.getMTL() != null) {
				if(!currentMtl.equals(face.getMTL().getTgaImage())) {
					texture = jpgs.get(face.getMTL().getTgaImage());
					texture.bind();
					currentMtl = face.getMTL().getTgaImage();
				}
			}
			try {
				glBegin(GL_POLYGON);
				ArrayList<Vertex> points = face.getPoints();
				ArrayList<Integer> indices = face.getIndices();
				ArrayList<Integer> textureIndices = face.getIndicesTextures();
				for(int j = 0; j < indices.size(); j++) {
					
					Vertex vertex = model.getVertices().get(indices.get(j) - 1);
					if(textureIndices.size() != 0) {
						Vertex textureVertex = model.getVerticesTextures().get(textureIndices.get(j) - 1);
						if(face.getMTL() != null) {
							GL11.glTexCoord2f(textureVertex.getX(), textureVertex.getY());
						}
					}
					glVertex3f(vertex.getX() * scale, vertex.getY() * scale, vertex.getZ() * scale);
				}
				glEnd();
			} catch(Exception e) {
				e.printStackTrace();
			}
		}
		glEndList();
		return displayList;
	}

Also I tried with a really small obj file and it worked. Haven't calculated normals yet.

*edit*
Do the dimensions of the texture matter? I've heard a rumor about power of 2 or something. I have textures that are like 340x350. Could that be the cause of the problem?

Evil-Devil

As you load your textures with SLICK there is no problem with non power of two textures, because SLICK will take care of the padding.

Your rendering code looks fine except for that GL_POLYGON. I would recommend to use GL_TRIANGLES and or GL_QUADS and only GL_POLYGON for faces with more than 4 vertices.
It could be that using plain gl_polygons is causing problems with the objs u/v coordinates.

Maybe you could attach the model and we can load it in one of our own obj loaders to see if everthing is fine with the obj itself :)

stevenlin111

Fixed the problem. It was just because I was using NPOT texture.


Though I sometimes get a random obj model where the face tries to get a vertex not in the vertex list.
Ex:
f 412 92 221
I only have like 380 vertices in the list and it's trying to get vertex 412. I confirmed that "v " keyword was only used 380 times in the obj file. Any ideas?