Jump to content
  • Advertisement
Sign in to follow this  
VanillaGorilla

OpenGL Loading and showing .obj files in OpenGL

This topic is 4223 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I realize that there is an entry in the FAQ for this but it doesn't completely answer my question. The problem I am having is actually displaying it in an OpenGL window. I got the code from here: (to load the .obj) http://www.gamedev.net/community/forums/topic.asp?topic_id=312335 Here is the main.cpp (the actual .obj loader class can be downloaded from the link above) main.cpp:
#include <iostream>
#include "ObjLoader.cpp"
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#define ESCAPE 27
int window; 

using namespace std;

void InitGL(int Width, int Height)	       
{
  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);		
  glClearDepth(1.0);				
  glDepthFunc(GL_LESS);			       
  glEnable(GL_DEPTH_TEST);		       
  glShadeModel(GL_SMOOTH);			

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();				

  gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);	

  glMatrixMode(GL_MODELVIEW);
}

void DrawGLScene()
{

  ObjModel data;
  ObjLoader LoaderClass;

  LoaderClass.LoadObj("cube.obj");
  data = LoaderClass.ReturnObj();

  glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);	
  glLoadIdentity();				

  glTranslatef(-1.5f,0.0f,-6.0f);		

  glBegin(GL_TRIANGLES);				

  for(int i = 0; i < data.NumVertex; i++)  {
	glVertex3f(data.VertexArray.X, data.VertexArray.Y,data.VertexArray.Z);
  }

  glEnd();

  glBegin(GL_QUADS);				

  for(int i = 0; i < data.NumVertex; i++)  {
	glVertex3f(data.VertexArray.X, data.VertexArray.Y,data.VertexArray.Z);
  }

  glEnd();					

  glutSwapBuffers();
}

void keyPressed(unsigned char key, int x, int y) 
{
    usleep(100);

    if (key == ESCAPE) 
    { 
      
      glutDestroyWindow(window); 
      
      
      exit(0);                   
    }
}

int main(int argc, char **argv)  {
  glutInit(&argc, argv);  
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);  
  glutInitWindowSize(640, 480);  
  glutInitWindowPosition(0, 0);  
  window = glutCreateWindow("OBJ to OpenGL");  
  glutDisplayFunc(&DrawGLScene);  
  glutFullScreen();
  glutIdleFunc(&DrawGLScene);
  glutKeyboardFunc(&keyPressed);
  InitGL(640, 480);
  glutMainLoop();  
  return 1;
}



I am just using a simple cube which I exported in blender, I was able to set up the original program which just outputted information in the console. When I run the main.cpp you see above, here is the output: http://img341.imageshack.us/img341/5765/opengloutputhb8.jpg [Edited by - VanillaGorilla on March 22, 2007 6:35:45 AM]

Share this post


Link to post
Share on other sites
Advertisement
Sorry I did not get to finish my post earlier, I was a bit rushed.

Basically what I am asking is how can I fix it to render the entire cube, given this information:

Quote:
Object Model has: 6 faces!
1 1 -1
1 -1 -1
-1 -1 -1
-1 1 -1
1 0.999999 1
0.999999 -1 1
-1 -1 1
-1 1 1
Indacies into faces:
0 1 2
Indacies into faces:
4 7 6
Indacies into faces:
0 4 5
Indacies into faces:
1 5 6
Indacies into faces:
2 6 7
Indacies into faces:
4 0 3
Vertex from triangle info]:
1 0.999999 1
-1 1 1
-1 -1 1


As you can see from the post above, the image from OpenGL only shows the front and back faces. So how do I make those extra vertices. (Note: in the main.cpp you see above, I do not use the "Indacies into faces" so I think that might be the problem) How do I plot the points for the "Indacies"

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Instead of quoting and making a 50 page post. Put your code between [ source] and [ /source] just remove the spaces.

Please edit your posts and fix it.

Share this post


Link to post
Share on other sites
The problem is how the vertex data is stored.

In an .obj file all the vertex data comes first, then the face descriptions that says how all this vertex data should be ordered to make the model.

The .obj loader reads in all the vertex info, and stores it so you can access it like this:



glBegin(GL_TRIANGLES);

for(int i = 0; i < data.NumTriangle; i++)
{

glVertex3f(data.VertexArray[data.TriangleArray.Vertex[0]].X,
data.VertexArray[data.TriangleArray.Vertex[0]].Y,
data.VertexArray[data.TriangleArray.Vertex[0]].Z);


glVertex3f(data.VertexArray[data.TriangleArray.Vertex[1]].X,
data.VertexArray[data.TriangleArray.Vertex[1]].Y,
data.VertexArray[data.TriangleArray.Vertex[1]].Z);


glVertex3f(data.VertexArray[data.TriangleArray.Vertex[2]].X,
data.VertexArray[data.TriangleArray.Vertex[2]].Y,
data.VertexArray[data.TriangleArray.Vertex[2]].Z);
}

glEnd();







Edit: Source code layout changed

Share this post


Link to post
Share on other sites
Thank you for the reply. I changed it and put yours in and I think the new output is an improvement:

http://img524.imageshack.us/img524/4866/colouropenglwe9.jpg (edit: added colour)

Any ideas why it is doing this?

Also, I have looked in some other topics about .obj files and I am noticing that for the lines that start with "f" for everyone else it looks different than mine: (this is mine)

f 1//1 2//2 3//3 4//4
f 5//5 8//8 7//7 6//6
f 1//1 5//5 6//6 2//2
f 2//2 6//6 7//7 3//3
f 3//3 7//7 8//8 4//4
f 5//5 1//1 4//4 8//8

where as everyone else has something like

f 1/1/1 2/2/2 3/3/3 4/4/4

Is mine messed up?

[Edited by - VanillaGorilla on March 22, 2007 3:35:30 PM]

Share this post


Link to post
Share on other sites
The numbers refer to vertex indices, normal indices, and texture coordinate indices. The latter two are optional, and if they aren't present they're just left blank.

Share this post


Link to post
Share on other sites
Alright, thanks. Any ideas on what it causing it to do this:

EDIT:

I decided to add colour to each of the faces so you could tell the difference and here is the problem:



It is only making the face with 3 vertices, so each face that should be square is coming out as a triangle.

[Edited by - VanillaGorilla on March 22, 2007 3:49:05 PM]

Share this post


Link to post
Share on other sites
Am I forgetting to draw something in the OpenGL drawing code? I am using the code that ChristmasCelery posted above to draw it.

Or is my .obj file messed:

# Wavefront OBJ (1.0) exported by lynx's OBJ import/export script

# Meshname: Cube
# Total number of Faces: 6
# Total number of Vertices: 8

o Cube

g Cube

v 1.0 0.999999940395 -1.0
v 1.0 -1.0 -1.0
v -1.00000011921 -0.999999821186 -1.0
v -0.999999642372 1.00000035763 -1.0
v 1.00000047684 0.999999463558 1.0
v 0.999999344349 -1.00000059605 1.0
v -1.00000035763 -0.999999642372 1.0
v -0.999999940395 1.0 1.0
vn 0.577349185944 0.577349185944 -0.577349185944
vn 0.577349185944 -0.577349185944 -0.577349185944
vn -0.577349185944 -0.577349185944 -0.577349185944
vn -0.577349185944 0.577349185944 -0.577349185944
vn 0.577349185944 0.577349185944 0.577349185944
vn 0.577349185944 -0.577349185944 0.577349185944
vn -0.577349185944 -0.577349185944 0.577349185944
vn -0.577349185944 0.577349185944 0.577349185944
f 1//1 2//2 3//3 4//4
f 5//5 8//8 7//7 6//6
f 1//1 5//5 6//6 2//2
f 2//2 6//6 7//7 3//3
f 3//3 7//7 8//8 4//4
f 5//5 1//1 4//4 8//8




Or maybe the ObjLoader.cpp doesn't work? (has anyone actually successfully used it and outputted models in OpenGL, if so please post the drawing code you used)

Share this post


Link to post
Share on other sites
I found the problem, the .obj file was not made properly. I was using a python script that I found by searching around (and exporting it in blender). So I updated my blender and it comes with a .obj exporter, which now produces a different format, which the Obj Loader doesn't work with since it wasn't set up to read this:

# Blender3D v243 OBJ File: cube.blend
# www.blender3d.org
mtllib cube.mtl
o Cube
v 1.000018 -1.000000 -1.003398
v 1.000018 -1.000000 0.996602
v -0.999982 -1.000000 0.996602
v -0.999981 -1.000000 -1.003398
v 1.000019 1.000000 -1.003397
v 1.000018 1.000000 0.996603
v -0.999982 1.000000 0.996602
v -0.999982 1.000000 -1.003398
usemtl (null)
usemtl (null)
s 1
f 5 1 8
f 1 4 8
f 3 7 8
f 3 8 4
f 2 6 3
f 6 7 3
f 1 5 2
f 5 6 2
f 5 8 7
f 5 7 6
f 1 2 3
f 1 3 4



Although this one produces the correct faces (since I can put on the "triangulate" option), I can't read it because it is doing "f x x x" instead of "f x/x/x". Are there any obj loaders which read this format?

Share this post


Link to post
Share on other sites
i only scanned the obj loader in that post for a second but i believe if you find where it says.


find_first_of("/");


and replace it with

//replace the / with a phisical space
find_first_of(" ");



it should work for the new format.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!