LWJGL Forum

Programming => OpenGL => Topic started by: Rekenaar on April 19, 2012, 21:15:11

Title: [SOLVED]Need help rendering data parsed from a .obj file.
Post by: Rekenaar on April 19, 2012, 21:15:11
Hi everyone, sorry if this has been asked already (couldn't find it).

Anyway, I've parsed all of the data from a .obj file into float arrays (for verts, normals, texture-coords, and the face data aswell)
I've checked all the data against the actual file, so I don't think my parsing is a problem, but when I try to render the object (which is a cube) I just get 2 triangles. (Below, viewed from an angle)

(http://i.imgur.com/EheYk.png)

I'm guessing that my problem is that I'm not doing anything with the face data and am just drawing the vertices.
If that is the case, could someone show me how to draw the faces aswell?

Here's my code:

The parse function of my OBJModel class
Code: [Select]
try {
while(((currentLine = file.readLine()) != null)) {
lineCount++;
currentLine = currentLine.trim();

if(currentLine.length() > 0) {
// Vertices
if(currentLine.charAt(0) == 'v' & currentLine.charAt(1) == ' ') {
String[] vertexCoordinatesString = currentLine.split("\\s+");
for(int i = 1; i < vertexCoordinatesString.length; i++) {
vertexArrayList.add(Float.valueOf(vertexCoordinatesString[i]).floatValue());
}
}
// Normals
if(currentLine.charAt(0) == 'v' & currentLine.charAt(1) == 'n') {
String[] normalCoordinatesString = currentLine.split("\\s+");
for(int i = 1; i < normalCoordinatesString.length; i++) {
vertexNormalsArrayList.add(Float.valueOf(normalCoordinatesString[i]).floatValue());
}
}
// Texture Coordinates
if(currentLine.charAt(0) == 'v' & currentLine.charAt(1) == 't') {
String[] textureCoordinatesString = currentLine.split("\\s+");
for(int i = 1; i < textureCoordinatesString.length; i++) {
vertexTextureCoordinatesArrayList.add(Float.valueOf(textureCoordinatesString[i]).floatValue());
}
}
if(currentLine.charAt(0) == 'f' & currentLine.charAt(1) == ' ') {
String[] faceCoordinatesString = currentLine.split("\\s+");

for(int i = 1; i < faceCoordinatesString.length; i++) {
String fixstring = faceCoordinatesString[i].replaceAll("//","/0/");
String[] tempstring = fixstring.split("/");

faceArrayList.add(Float.valueOf(tempstring[0]).floatValue());
if(tempstring.length > 1) {
faceTextureCoordinatesArrayList.add(Float.valueOf(tempstring[1]).floatValue());
}
else {
faceTextureCoordinatesArrayList.add(0.0f);
}
if(tempstring.length > 2) {
faceNormalsArrayList.add(Float.valueOf(tempstring[2]).floatValue());
}
else {
faceNormalsArrayList.add(0.0f);
}
}
}
}
}
// Convert ArrayLists to float[] arrays
// Vertices
vertices = new float[vertexArrayList.size()];
for (int i = 0; i < vertexArrayList.size(); i++) {
vertices[i] = vertexArrayList.get(i);
}
vertexNormals = new float[vertexNormalsArrayList.size()];
for (int i = 0; i < vertexNormalsArrayList.size(); i++) {
vertexNormals[i] = vertexNormalsArrayList.get(i);
}
vertexTextureCoordinates = new float[vertexTextureCoordinatesArrayList.size()];
for (int i = 0; i < vertexTextureCoordinatesArrayList.size(); i++) {
vertexTextureCoordinates[i] = vertexTextureCoordinatesArrayList.get(i);
}
// Faces
faces = new float[faceArrayList.size()];
for (int i = 0; i < faceArrayList.size(); i++) {
faces[i] = faceArrayList.get(i);
}
faceNormals = new float[faceNormalsArrayList.size()];
for (int i = 0; i < faceNormalsArrayList.size(); i++) {
faceNormals[i] = faceNormalsArrayList.get(i);
}
faceTextureCoordinates = new float[faceTextureCoordinatesArrayList.size()];
for (int i = 0; i < faceTextureCoordinatesArrayList.size(); i++) {
faceTextureCoordinates[i] = faceTextureCoordinatesArrayList.get(i);
}
}

The code I use to draw the object
Code: [Select]
public void Draw(Vector3f position, Vector3f rotation, Color colour) {
GL11.glTranslatef(position.x,position.y,position.z);
GL11.glRotatef(rotation.x, 1, 0, 0);
GL11.glRotatef(rotation.y, 0, 1, 0);
GL11.glRotatef(rotation.z, 0, 0, 1);
GL11.glColor3f(colour.r, colour.g, colour.b);

GL11.glBegin(GL11.GL_TRIANGLES);
for (int i = 0; i < model.vertices.length / 3; i++) {
GL11.glVertex3f(model.vertices[0 + 3 * i],model.vertices[1 + 3 * i],model.vertices[2 + 3 * i]);
}
GL11.glEnd();
}

Thanks  :)
--
Daniel
Title: Re: Need help rendering data parsed from a .obj file.
Post by: kappa on April 19, 2012, 21:30:32
haven't had a look at your code but there is a working obj loader in this thread (http://lwjgl.org/forum/index.php/topic,917.0.html) which might help.
Title: Re: Need help rendering data parsed from a .obj file.
Post by: Rekenaar on April 19, 2012, 21:42:23
Yeah, I actually based mine off that  ::)
Anyway, if nothing comes of this thread I'll just use that one.

Thanks
--
Daniel
Title: Re: Need help rendering data parsed from a .obj file.
Post by: seanrowens on April 20, 2012, 03:51:55
Or you could try this one;

http://darksleep.com/oobjloader/
Title: Re: Need help rendering data parsed from a .obj file.
Post by: Rekenaar on April 20, 2012, 07:18:53
Oh wow, that one looks great!
I'll check that one out later today and report progress.
Title: Re: Need help rendering data parsed from a .obj file.
Post by: Epicbo on April 20, 2012, 08:35:56
Take a look at this youtube tutorial: http://www.youtube.com/watch?v=izKAvSV3qk0
Title: Re: Need help rendering data parsed from a .obj file.
Post by: seanrowens on April 20, 2012, 23:11:43
Cool.  I wrote it.  I'm going to work on making materials work better (currently it parses everything in materials and then ignores everything unless it's a mapKd in which cases it uses it as a texture, but very simply.)   I'm also thinking of changing the sample Builder implementation to use org.lwjgl.util.vector.Vector3f instead of the various vertex classes.  Let me know what you think.
Title: Re: Need help rendering data parsed from a .obj file.
Post by: Rekenaar on April 21, 2012, 19:41:56
Yay! I managed to figure it out!
What I wasn't doing, was drawing the faces correctly (or at all for that matter).

This posted by Rraawwrr really helped me out. (http://www.gamedev.net/topic/620179-loading-obj-file-question/)
Quote
The rows that start with f are position/UV index pairs.

So, the 1/1 line refers to position at index 1 (v 0.084387 1.041998 1.380985) and texture coordinate (vt 0.000000 0.000000).

Note also that obj files are indexed starting from 1.

Here is the updated rendering code:
Code: [Select]
GL11.glBegin(GL11.GL_TRIANGLES);
for(int i = 0; i < model.faces.length; i++) {
GL11.glVertex3f(model.vertices[((model.faces[i] - 1) * 3)], model.vertices[((model.faces[i] - 1) * 3) + 1], model.vertices[((model.faces[i] - 1) * 3) + 2]);
}
GL11.glEnd();

Thanks for all the help everyone :)
--
Daniel