LWJGL
June 20, 2013, 03:31:36 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: LWJGL is now using GitHub
 
   Home   Help Search Login Register  



Pages: [1]
  Print  
Author Topic: [SOLVED]Need help rendering data parsed from a .obj file.  (Read 1547 times)
Rekenaar
Newbie
*
Posts: 6


The Sasquatch


WWW
« on: April 19, 2012, 14: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)



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:
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:
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  Smiley
--
Daniel
Logged
kappa
Administrator
Nerdus Imperius
*****
Posts: 1117



« Reply #1 on: April 19, 2012, 14:30:32 »

haven't had a look at your code but there is a working obj loader in this thread which might help.
Logged
Rekenaar
Newbie
*
Posts: 6


The Sasquatch


WWW
« Reply #2 on: April 19, 2012, 14:42:23 »

Yeah, I actually based mine off that  Roll Eyes
Anyway, if nothing comes of this thread I'll just use that one.

Thanks
--
Daniel
Logged
seanrowens
Newbie
*
Posts: 3


« Reply #3 on: April 19, 2012, 20:51:55 »

Or you could try this one;

http://darksleep.com/oobjloader/
Logged
Rekenaar
Newbie
*
Posts: 6


The Sasquatch


WWW
« Reply #4 on: April 20, 2012, 00:18:53 »

Oh wow, that one looks great!
I'll check that one out later today and report progress.
Logged
Epicbo
Newbie
*
Posts: 12


« Reply #5 on: April 20, 2012, 01:35:56 »

Take a look at this youtube tutorial: http://www.youtube.com/watch?v=izKAvSV3qk0
Logged
seanrowens
Newbie
*
Posts: 3


« Reply #6 on: April 20, 2012, 16: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.
Logged
Rekenaar
Newbie
*
Posts: 6


The Sasquatch


WWW
« Reply #7 on: April 21, 2012, 12: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.
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:
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 Smiley
--
Daniel
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines
SMFAds for Free Forums
Valid XHTML 1.0! Valid CSS!