model load problem

This is a discussion on model load problem within the Game Programming forums, part of the General Programming Boards category; I've started my game engine (in OpenGL + SDL), but when I call render::draw after loading a 3ds file, all ...

  1. #1
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735

    model load problem

    I've started my game engine (in OpenGL + SDL), but when I call render::draw after loading a 3ds file, all I get is a black screen. (Everything else is set up right, because I can do simple stuff like drawing primitives) Here's the code, Thanks a bunch for any help.

    Code:
    char render::Load3DS (mesh * Mesh, char *p_filename)
    {
    	int i;
    	FILE *l_file;
    	int temp;
    	unsigned short l_chunk_id;
    	unsigned int l_chunk_lenght;
    	unsigned char l_char;
    	unsigned short l_qty;
    	unsigned short l_face_flags;
    	if ((l_file=fopen (p_filename, "rb"))== NULL) return 0;
    	while (ftell (l_file) < filelength (fileno (l_file)))
    	{
    		fread (&l_chunk_id, 2, 1, l_file);
    		fread (&l_chunk_lenght, 4, 1, l_file);
    		switch (l_chunk_id)
            {
    			case 0x4d4d: 
    			break;    
    			case 0x3d3d:
    			break;
    			case 0x4000: 
    				i=0;
    				do
    				{
    					fread (&l_char, 1, 1, l_file);
    //                  p_object->name[i]=l_char;
    					i++;
                    }while(l_char != '\0' && i<20);
    			break;
    			case 0x4100:
    			break;
    			case 0x4110: 
    				fread (&l_qty, sizeof (unsigned short), 1, l_file);
                    Mesh->NVertices = l_qty;
    				Mesh->pVerts = new float * [l_qty];
    				for (temp = 0; temp < l_qty; temp++) {Mesh->pVerts[temp] = new float[3];}
                    for (i=0; i<l_qty; i++)
                    {
    					fread (&Mesh->pVerts[i][0], sizeof(float), 1, l_file);
                        fread (&Mesh->pVerts[i][1], sizeof(float), 1, l_file);
    					fread (&Mesh->pVerts[i][2], sizeof(float), 1, l_file);
    				}
    				break;
    			case 0x4120:
    				fread (&l_qty, sizeof (unsigned short), 1, l_file);
                    Mesh->NPolys = l_qty;
    				Mesh->pPolys = new unsigned short * [l_qty];
    				for (temp = 0; temp < l_qty; temp++) {Mesh->pPolys[temp] = new unsigned short[3];}
                    for (i=0; i<l_qty; i++)
                    {
    					fread (&Mesh->pPolys[i][0], sizeof (unsigned short), 1, l_file);
    					fread (&Mesh->pPolys[i][1], sizeof (unsigned short), 1, l_file);
    					fread (&Mesh->pPolys[i][2], sizeof (unsigned short), 1, l_file);
    					fread (&l_face_flags, sizeof (unsigned short), 1, l_file);
    				}
                    break;
    			default:
    				 fseek(l_file, l_chunk_lenght-6, SEEK_CUR);
            } 
    	}
    	fclose (l_file);
    	return (1);
    }
    Code:
    void render::Draw()
    {
    	glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    	glLoadIdentity();
    	glTranslatef(0.0,0.0,-300);
        glBegin(GL_TRIANGLES);
        for (int l_index=0;l_index<Engine.jug.NPolys;l_index++)
        {
    		glColor3b(127, 0, 127);
            glVertex3f( Engine.jug.pVerts[ Engine.jug.pPolys[l_index][0] ][0],
                        Engine.jug.pVerts[ Engine.jug.pPolys[l_index][0] ][1],
                        Engine.jug.pVerts[ Engine.jug.pPolys[l_index][0] ][2]);
    		glColor3b(127, 0, 127);
            glVertex3f( Engine.jug.pVerts[ Engine.jug.pPolys[l_index][1] ][0],
                        Engine.jug.pVerts[ Engine.jug.pPolys[l_index][1] ][1],
                        Engine.jug.pVerts[ Engine.jug.pPolys[l_index][1] ][2]);
    		glColor3b(127, 0, 127);
            glVertex3f( Engine.jug.pVerts[ Engine.jug.pPolys[l_index][2] ][0],
                        Engine.jug.pVerts[ Engine.jug.pPolys[l_index][2] ][1],
                        Engine.jug.pVerts[ Engine.jug.pPolys[l_index][2] ][2]);
        }
        glEnd();
    	SDL_GL_SwapBuffers();	
    }
    oh, and the mesh decleration
    Code:
    struct mesh
    {
    	float ** pVerts;
    	unsigned short ** pPolys;
    	unsigned short NVertices;
    	unsigned short NPolys;
    };

  2. #2
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070
    Try starting the rendering loop outside the glBegin and glEnd functions.

    For example you have:
    Code:
    glBegin(GL_TRIANGLES);
    for(stuff)
       do stuff;
    glEnd();
    change it to:
    Code:
    for(stuff)
      glBegin(GL_TRIANGLES);
      stuff;
      glEnd();
    If that doesn't work, try running the debugger to see if the mesh data is being loaded properly.

    -psychopath

    [EDIT]
    Actually, my first suggestion probably won't make a difference, but you can try it anyway [/EDIT]
    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Model not showing up on screen...
    By Shamino in forum Game Programming
    Replies: 14
    Last Post: 03-09-2006, 07:00 PM
  3. Just finished assembly assignment
    By xddxogm3 in forum Tech Board
    Replies: 6
    Last Post: 10-08-2005, 04:01 PM
  4. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  5. Help with file reading/dynamic memory allocation
    By Quasar in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2004, 03:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21