Sign in to follow this  
Jaspar

openGL imported object mess (help please)

Recommended Posts

First off, I'd like to say a big 'HELLO!', this is my first post here. I'm generally quite a newbie to programming, I have a little experience in various programming languages, but nothing in-depth. I'm currently trying to learn C and OpenGL. I have the OpenGL red book, and am slowly going through it. I am currently trying to implement the import of custom objects (possibly a bit premature, as I'm only on chapter 2). I have a background in 3D modelling (as a hobby), and am fairly strong at vbscript, so I wrote an 'Alias Wavefront' obj translater. I've got it so it exports the verts, the tri/vert refs and the quad/vert refs to separate tables, for me to paste into the program. I have set up the quads as a test, but they appear as a total mess (it's meant to be a simple 'J'). I'm pretty sure the vert pointers are ordered correctly (CCW). It also doesn't seem to display GL_LINE polygon mode (without seeing the lines, I can't tell how it's drawing it). It could even be a problem with my laptop's OpenGL support, had a hell of a problem making the test model in Wings3d. Any help appreciated. A pointer as to how to display these quads as wireframes might well get me on the right tracks. (thanks in advance)...
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>



void setupPointers(void)
{
   static GLdouble vertices[] = {
     -0.448,-0.196,0.448,
    -0.356,-0.288,0.448,
    -0.448,0.46,0.448,
    -0.448,0.552,0.356,
    0.448,0.46,0.448,
    0.448,0.552,0.356,
    0.448,-0.196,0.448,
    0.356,-0.288,0.448,
    -0.448,-0.196,-0.448,
    -0.356,-0.288,-0.448,
    -0.448,0.46,-0.448,
    -0.448,0.552,-0.356,
    0.448,0.552,-0.356,
    0.448,0.46,-0.448,
    0.448,-0.196,-0.448,
    0.356,-0.288,-0.448,
    1.42464,-0.196,-0.448,
    1.42464,0.46,-0.448,
    0.356,-1.808,0.448,
    0.448,-1.808,0.356,
    -0.356,-1.808,0.448,
    -0.448,-1.9,0.448,
    -0.448,-1.9,-0.448,
    -0.356,-1.808,-0.448,
    0.448,-1.808,-0.356,
    0.356,-1.808,-0.448,
    -0.448,-2.518,0.448,
    -0.448,-2.61,0.356,
    -0.448,-2.518,-0.448,
    -0.448,-2.61,-0.356,
    -1.28,-2.518,-0.448,
    -1.28,-1.9,-0.448,
    0.448,-2.518,0.356,
    0.448,-2.518,-0.356,
    1.42464,0.46,0.356,
    1.42464,-0.196,0.356,
    0.356,-2.61,-0.356,
    0.356,-2.61,0.356,
    0.356,-2.518,-0.448,
    -1.33264,0.46,0.448,
    -1.33264,-0.196,0.448,
    1.33264,0.552,0.356,
    1.33264,0.552,-0.356,
    -1.28,-1.9,0.448,
    -1.28,-2.518,0.448,
    1.33264,-0.196,0.448,
    1.33264,0.46,0.448,
    -1.28,-2.61,0.356,
    -1.28,-2.61,-0.356,
    -1.33264,0.552,-0.356,
    -1.33264,0.552,0.356,
    -1.33264,-0.196,-0.448,
    -1.33264,0.46,-0.448,
    0.356,-2.518,0.448,
    0.448,-0.38,-0.356,
    0.448,-0.38,0.356,
    -0.54,-1.808,0.356,
    -0.54,-1.808,-0.356,
    -1.28,-1.808,-0.356,
    -1.28,-1.808,0.356,
    -0.54,-0.288,-0.356,
    -0.54,-0.288,0.356,
    -1.33264,-0.288,0.356,
    -1.33264,-0.288,-0.356,
    -0.448,-0.38,0.356,
    -0.448,-0.38,-0.356,
    -0.448,-1.716,-0.356,
    -0.448,-1.716,0.356,
    -1.372,-2.518,-0.356,
    -1.372,-2.518,0.356,
    -1.372,-1.9,0.356,
    -1.372,-1.9,-0.356,
    0.54,-0.288,0.356,
    0.54,-0.288,-0.356,
    1.33264,-0.288,-0.356,
    1.33264,-0.288,0.356,
    -1.42464,-0.196,0.356,
    -1.42464,0.46,0.356,
    -1.42464,0.46,-0.356,
    -1.42464,-0.196,-0.356


};

   glEnableClientState (GL_VERTEX_ARRAY);

   glVertexPointer (3, GL_DOUBLE, 0, vertices);

}



void init(void)
{
   glClearColor (0.0, 0.0, 0.0, 0.0);
   glShadeModel (GL_FLAT);
   setupPointers ();

}

void display(void)
{

static GLubyte quads[] = {
  1,2,5,3,
 1,62,65,2,
 2,21,19,8,
 2,65,68,21,
 3,40,41,1,
 4,6,13,12,
 4,51,40,3,
 5,47,42,6,
 6,4,3,5,
 6,42,43,13,
 7,46,47,5,
 7,73,76,46,
 8,7,5,2,
 8,56,73,7,
 9,52,53,11,
 9,61,64,52,
 10,66,61,9,
 11,16,10,9,
 11,53,50,12,
 12,50,51,4,
 13,43,18,14,
 14,11,12,13,
 14,18,17,15,
 15,16,11,14,
 15,74,55,16,
 16,26,24,10,
 17,75,74,15,
 18,35,36,17,
 19,54,33,20,
 20,33,34,25,
 20,56,8,19,
 21,22,54,19,
 21,68,57,22,
 22,44,45,27,
 22,57,60,44,
 23,58,67,24,
 24,67,66,10,
 25,34,39,26,
 25,55,56,20,
 26,16,55,25,
 26,39,23,24,
 27,45,48,28,
 28,38,54,27,
 28,48,49,30,
 29,31,32,23,
 29,39,37,30,
 30,37,38,28,
 30,49,31,29,
 31,69,72,32,
 32,59,58,23,
 33,38,37,34,
 35,47,46,36,
 36,76,75,17,
 40,78,77,41,
 41,63,62,1,
 43,42,35,18,
 44,71,70,45,
 48,70,69,49,
 50,79,78,51,
 52,80,79,53,
 55,74,73,56,
 57,68,67,58,
 58,59,60,57,
 59,72,71,60,
 61,66,65,62,
 62,63,64,61,
 63,77,80,64,
 66,67,68,65,
 70,71,72,69,
 74,75,76,73,
 78,79,80,77



   };

   glClear (GL_COLOR_BUFFER_BIT);

glPolygonMode(GL_FRONT, GL_FILL);
/*glPolygonMode(GL_BACK, GL_LINE);*/

glColor3f (1.0, 0.0, 1.0);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

 glDrawElements(GL_QUADS, 284, GL_UNSIGNED_BYTE, quads);
   glFlush ();
}

void reshape (int w, int h)
{
   glViewport (0, 0, (GLsizei) w, (GLsizei) h);
   glMatrixMode (GL_PROJECTION);
   glLoadIdentity ();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode(GL_MODELVIEW);
}


void keyboard(unsigned char key, int x, int y)
{
   switch (key) {
      case 27:
         exit(0);
         break;
   }
}

int main(int argc, char** argv)
{
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
   glutInitWindowSize (400, 400);
   glutInitWindowPosition (100, 100);
   glutCreateWindow (argv[0]);
   init ();
   glutDisplayFunc(display);
   glutReshapeFunc(reshape);
   glutKeyboardFunc(keyboard);
   glutMainLoop();
   return 0;
}


[Edited by - Jaspar on December 6, 2004 10:47:23 AM]

Share this post


Link to post
Share on other sites
Thanks alot! Probably would have spent days wondering what the hell was going on, without considering the fact that array indexes start at zero! It's now looking considerably less messy!

Any advice on how to get wireframes up and running? Would I need to use glDrawArrays, with the array broken up into a multi-dimentional array?

On a side note, I notice you're from Plymouth! I went to Plymouth College school (hope you don't hold it against me).

Share this post


Link to post
Share on other sites
I remember having some troubles with that a while back on a GeForce2 but can't remember what was wrong or how to fix it. You could try fiddling with the back face culling, anti aliasing or line widths (start with the last one and see if increasing the line width helps).

If the LINES fill mode isn't working try just a simple loop over the polygons issuing each quad as a line loop. It won't be optimal but since it's only for debugging you'll probably find it reasonable.



for(int i=0;i<num_faces;i++)
{
glDrawElements(GL_LINE_LOOP,4,GL_UNSIGNED_BYTE, quads + (i * 4));
}



Quote:

On a side note, I notice you're from Plymouth! I went to Plymouth College school (hope you don't hold it against me).


Hey cool! I've never seen anyone around here whos been anywhere remotely near Plymouth :)

Are you still down here or did you cut and run at the first opportunity? ;P

Share this post


Link to post
Share on other sites
I reckon the lack of lines might well be down to my graphics card. Most of my problems with using this laptop with Wings3D stemmed from the fact that I couldn't visibly select edges. I guess I couldn't expect much 3d performance from an old laptop, with an ATI card. Thanks for all your help, I'll try out your suggestion, if it doen't work, I'll try it again on my desktop when I return.

As far as Plymouth is concerned, I'm afraid I cut and ran at the first! I've been living up in Manchester for the last 8 years. I go down to Devon a few times a year, mostly to Totnes/Dartmouth region. Haven't been to Plymouth in a while, though I've got a few friends who live there. Will def return to the region to live one day though, still where my heart is.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this