Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Multiple Textures...

  1. #1
    Member Contributor
    Join Date
    Dec 2001
    Location
    Raleigh, NC, USA
    Posts
    72

    Multiple Textures...

    I posted this on the beginners board since I originally thought it was a beginners question, but I never got a reply, so it obviously went over their heads, so I'll post here.
    I am trying to load multiple textures when the level loads, but for some reason, once the bitmaps are loaded, one of the two is invisible, like the polygons with that texture aren't even drawn! Here is the source, maybe you'll spot something i am just over-looking...
    Code :
    //MAX_TEXTURES is 256 right now
     
    //Global vars
    char texlist[MAX_TEXTURES][32];
    GLuint texture[MAX_TEXTURES];
     
    //This is called when drawing the screen
    unsigned long FindTexture(char *texname)
    	{
    	char texbuf[32];
     
    	sprintf(texbuf, "%s", texname);
    	for(unsigned long loop = 0; loop < MAX_TEXTURES; loop++)
    		if(strstr(texlist[loop], texbuf) != NULL)
    			return (loop + 1);
     
    	return 1;
    	}
     
    //This is my loading routine
    BOOL LoadBMP()
    	{
    	char texbuf[256];
    	int texnum = 0;
    	BOOL status;
    	GLubyte fixer, image[32][32][3];
    	FILE *texfile;
    	BITMAPFILEHEADER bmfh;
    	BITMAPINFOHEADER bmih;
     
    	for(unsigned long loop = 0; loop < area.numObjects; loop++)
    		{
    		status = TRUE;
    		for(unsigned long t_loop = 0; t_loop < MAX_TEXTURES; t_loop++)
    			{
    			if(strstr(texlist[t_loop], area.object[loop].Texture) != NULL)
    				{
    				status = FALSE;
    				}
    			}
    		if(status)
    			{
    			sprintf(texlist[texnum], "%s", area.object[loop].Texture);
    			texnum++;
    			}
    		}
     
    	glGenTextures(texnum, &amp;texture[0]);
     
    	for(unsigned long loop = 0; loop < texnum; loop++)
    		{
    		MessageBox(glwnd, texlist[loop], "LoadBMP(unsigned long)", MB_OK);
    		sprintf(texbuf, "Textures\\%s", texlist[loop]);
    		if((texfile = fopen(texbuf, "rb")) == NULL)
    			return FALSE;
     
    		fread(&amp;bmfh, sizeof(BITMAPFILEHEADER), 1, texfile);
    		fread(&amp;bmih, sizeof(BITMAPINFOHEADER), 1, texfile);
    		fread(&amp;image, sizeof(image), 1, texfile);
    		fclose(texfile);
     
    		for(short int x_loop = 0; x_loop < 32; x_loop++)
    			{
    			for(short int y_loop = 0; y_loop < 32; y_loop++)
    				{
    				fixer = image[y_loop][x_loop][0];
    				image[y_loop][x_loop][0] = image[y_loop][x_loop][2];
    				image[y_loop][x_loop][2] = fixer;
    				}
    			}
     
    		glBindTexture(GL_TEXTURE_2D, texture[loop]);
    		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    		gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, 32, 32, GL_RGB, GL_UNSIGNED_BYTE, image);
    		}
     
    	return TRUE;
    	}
     
    //And here's the drawing routine
    	glNormal3f(0.0f, 0.0f, 1.0f);
    	for(unsigned long o_loop = 0; o_loop < area.numObjects; o_loop++)
    		{
    		glBindTexture(GL_TEXTURE_2D, FindTexture(area.object[o_loop].Texture));
    		glBegin(GL_TRIANGLES);
    		for(int t_loop = 0; t_loop < area.object[o_loop].numTriangles; t_loop++)
    			{
    			glTexCoord2f(area.object[o_loop].triangle[t_loop].vertex[0].u, area.object[o_loop].triangle[t_loop].vertex[0].v);
    			glVertex3f(area.object[o_loop].triangle[t_loop].vertex[0].x, area.object[o_loop].triangle[t_loop].vertex[0].y, area.object[o_loop].triangle[t_loop].vertex[0].z);
     
    			glTexCoord2f(area.object[o_loop].triangle[t_loop].vertex[1].u, area.object[o_loop].triangle[t_loop].vertex[1].v);
    			glVertex3f(area.object[o_loop].triangle[t_loop].vertex[1].x, area.object[o_loop].triangle[t_loop].vertex[1].y, area.object[o_loop].triangle[t_loop].vertex[1].z);
     
    			glTexCoord2f(area.object[o_loop].triangle[t_loop].vertex[2].u, area.object[o_loop].triangle[t_loop].vertex[2].v);
    			glVertex3f(area.object[o_loop].triangle[t_loop].vertex[2].x, area.object[o_loop].triangle[t_loop].vertex[2].y, area.object[o_loop].triangle[t_loop].vertex[2].z);
    			}
    		glEnd();
    		}
    Now my small 512w*128h*512l room only loads two textures, one for the walls and one for the ceiling/floor. The ceiling/floor texture works perfectly, while it acts as though my wall polygons aren't even drawn! Any ideas?
    -Sephiroth

  2. #2
    Senior Member Frequent Contributor
    Join Date
    Feb 2000
    Location
    San Diego, CA, USA
    Posts
    604

    Re: Multiple Textures...

    First off, I just wanna say that I love this data structure:

    area.object[o_loop].triangle[t_loop].vertex[0].x

    That's gotta be cache-friendly

    Now, about that problem. Start by making sure that your walls are actually not being drawn. What I mean is check to be sure that you're not just drawing white walls on a white background, for example. Also make sure that you glEnable(GL_TEXTURE_2D).

    If the walls truly aren't being drawn, make sure that face culling, alpha testing, and blending are all off.

    I'm guessing it's one of the latter problems.

    -- Zeno

  3. #3
    Member Contributor
    Join Date
    Dec 2001
    Location
    Raleigh, NC, USA
    Posts
    72

    Re: Multiple Textures...

    Already tested all of them. See when I started the engine, I only loaded two textures, so I'd load them and easily reference them with loops since the ceiling and floor are drawn last. Culling works because when I would go outside the map (no collision-detection yet), the wall I was looking at from the outside wouldn't be drawn at all and I could see into the level, to the opposing wall. I have GL_TEXTURE_2D on, and I've disabled culling to rule it out. Any other ideas?
    Oh and that mega-structure isn't static. It is dynamically allocated using new and freed up between levels using delete. It also frees everything before exiting, just to be safe :P.
    -Sephiroth

  4. #4
    Senior Member OpenGL Guru knackered's Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    2,833

    Re: Multiple Textures...

    "unsigned long FindTexture(char *texname)
    { char texbuf[32]; sprintf(texbuf, "%s", texname); for(unsigned long loop = 0; loop < MAX_TEXTURES; loop++) if(strstr(texlist[loop], texbuf) != NULL) return (loop + 1); return 1; }"

    shouldn't your return be:-
    return texture[loop];

    ???
    Knackered

  5. #5
    Senior Member OpenGL Guru knackered's Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    2,833

    Re: Multiple Textures...

    BTW, what Zeno was hinting at about cache friendliness was that your way of drawing your triangles may be really nice and object orientated, but will run extremely slowly...no matter what machine you're running on.
    Aside from the fact that you should be using VAR, or any kind of vertex array, if you were going to do your drawing loop using immediate mode (glVertex3f, and you're not even using glVertex3fv(&area.object[o_loop].triangle[t_loop].vertex[0].x)) you should at least keep all your vertices in a linear array, and just increment a pointer to step through that array.
    Knackered

  6. #6
    Senior Member OpenGL Pro
    Join Date
    Jun 2000
    Location
    Shreveport, LA, USA
    Posts
    1,502

    Re: Multiple Textures...

    Wandering OT, but that FindTexture code given above is rather horrendous in itself. Much better to use a binary search on a sorted array. That is, as long as the array only has to be sorted one time, or can be kept sorted any time a texture is added or removed from the array. Personally I've come to depend rather heavily on the STL map container for this kind of thing.

  7. #7
    Senior Member Frequent Contributor
    Join Date
    Feb 2000
    Location
    San Diego, CA, USA
    Posts
    604

    Re: Multiple Textures...

    Originally posted by DFrey:
    Much better to use a binary search on a sorted array.
    Hehe, much better to just store the texture object number instead of the string inside the area: bject::Texture member.

    -- Zeno



    [This message has been edited by Zeno (edited 01-22-2002).]

  8. #8
    Senior Member OpenGL Guru knackered's Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    2,833

    Re: Multiple Textures...

    Poor sod. He's in for a rough ride.
    Knackered

  9. #9
    Senior Member OpenGL Pro
    Join Date
    Jun 2000
    Location
    Shreveport, LA, USA
    Posts
    1,502

    Re: Multiple Textures...

    much better to just store the texture object number instead of the string inside the area:bject::Texture member
    True, unless you are trying to render a scenegraph as described by a human readable script that can change at any time.

    [This message has been edited by DFrey (edited 01-22-2002).]

  10. #10
    Member Contributor
    Join Date
    Dec 2001
    Location
    Raleigh, NC, USA
    Posts
    72

    Re: Multiple Textures...

    Thanks, bt I already got it working, and on my P2/233 (the programming machine), with dynamic lighting and fog on, I get around 50-84fps all the time. I fixed the texture problem like this:
    No more funding textures
    The map no longer has a string for a texture name on each object, it now has a short int identifying the texture id.
    The map stores each texture used in the level, but only once! It does this in a char TexList[number of tex in level][32] format.
    When the level loads it reads this, no deciding what is already loaded!
    Finally, the drawing loop simply uses the ID to choose what texture to bind to. IE: TexList[0] is ID 1, TexList[1] is 2, etc. Only limit is 32,766 textures per map, but MAN you'd take time just putting 32k textures into a level!
    On a side note, the AMD K6-2 300 with a 32mb AGP card (the P2/233 has a 16mb pci voodoo) only gets 23-62 fps. Uh, wow AMD, way to kill my frames...
    -Sephiroth

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 9
    Last Post: 11-02-2018, 08:16 AM
  2. multiple textures
    By tnt1674 in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 06-18-2016, 08:29 AM
  3. Multiple Viewports, Multiple Textures - 3D
    By Cyndy in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 04-08-2010, 04:47 PM
  4. multiple lights and multiple textures
    By jide in forum OpenGL: GLSL
    Replies: 4
    Last Post: 10-26-2005, 06:47 AM
  5. multiple textures
    By Thr33d in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 04-27-2000, 04:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean