🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Having Troubles with multiple textures

Started by
11 comments, last by Icewater11 16 years, 8 months ago
Ive been working with NeHe's lesson 10 and created a pretty cool world with the txt file, but ive been having trouble with loading multiple textures once i change the intLoadGlTextures() here's what i have now: int LoadGLTextures() // Load Bitmaps And Convert To Textures { int Status=FALSE; // Status Indicator AUX_RGBImageRec *TextureImage[5]; // Create Storage Space For The Texture Data memset(TextureImage,0,sizeof(void *)*5); // Set The Pointer To NULL if ((TextureImage[0]=LoadBMP("Data/morestone.bmp")) && // Logo Texture (TextureImage[1]=LoadBMP("Data/Crate.bmp")) && // First Mask (TextureImage[2]=LoadBMP("Data/stone.bmp")) && // First Image (TextureImage[3]=LoadBMP("Data/wall.bmp")) && // Second Mask (TextureImage[4]=LoadBMP("Data/mud.bmp"))) // Second Image { Status=TRUE; // Set The Status To TRUE glGenTextures(5, &texture[0]); // Create Five Textures for (loop=0; loop<5; loop++) // Loop Through All 5 Textures { glBindTexture(GL_TEXTURE_2D, texture[loop]); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[loop]->sizeX, TextureImage[loop]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[loop]->data); } } for (loop=0; loop<5; loop++) // Loop Through All 5 Textures { if (TextureImage[loop]) // If Texture Exists { if (TextureImage[loop]->data) // If Texture Image Exists { free(TextureImage[loop]->data); // Free The Texture Image Memory } free(TextureImage[loop]); // Free The Image Structure } } return Status; // Return The Status } Id also like to know what i have to do on the txt file; if i have to change the upload / what do i put to specify which texture is going to be on which polly. From what ive seen i might need to put a glBindTexture tag before each one, and this would take a long time seeing as i already have 360 pollies.
Advertisement
I would probably do something similar to this

unsigned int LoadGLTexture(char filename[120]) // Load Bitmap And Convert To Texture
{
unsigned int texture;
AUX_RGBImageRec *TextureImage=NULL; // Create Storage Space For The Texture Data

if (TextureImage=LoadBMP(filename)
{
glGenTextures(1, &texture[0]); // Create One Texture
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage->sizeX, TextureImage->sizeY,
0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);
}

if (TextureImage) // If Texture Exists
{
if (TextureImage->data) // If Texture Image Exists
{
free(TextureImage->data); // Free The Texture Image Memory
}
free(TextureImage); // Free The Image Structure
}

return texture; // Return The Status
}

load the textures with
texture1=LoadGLTexture("Data/morestone.bmp");
texture2=LoadGLTexture("Data/Crate.bmp");
texture3=LoadGLTexture("Data/stone.bmp");
texture4=LoadGLTexture("Data/wall.bmp");
texture5=LoadGLTexture("Data/mud.bmp");

Regarding the other problem, group them into texture groups, take all the polygons that uses one texture and then place them in the same group, that way you don't have to change texture for every polygon.
I would advise you to look into how Wavefront obj (.obj) files work, since it has already solved a lot of the problems related to this, it's pretty easy to parse and a lot of 3d modeling softwares support it in one way or another.

Get it from wotsit.org near the bottom of the page.
I tried your setup but im recieving 6 errors and 5 appear to be the same one

1>c:\documents and settings\dmcguire\desktop\tutorials 6-10\lesson10\lesson10\lesson10.cpp(158) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\dmcguire\desktop\tutorials 6-10\lesson10\lesson10\lesson10.cpp(159) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\dmcguire\desktop\tutorials 6-10\lesson10\lesson10\lesson10.cpp(160) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\dmcguire\desktop\tutorials 6-10\lesson10\lesson10\lesson10.cpp(161) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\dmcguire\desktop\tutorials 6-10\lesson10\lesson10\lesson10.cpp(162) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

this is with each line of the LoadGlTexture:

texture1=LoadGLTexture("Data/morestone.bmp");
texture2=LoadGLTexture("Data/Crate.bmp");
texture3=LoadGLTexture("Data/stone.bmp");
texture4=LoadGLTexture("Data/wall.bmp");
texture5=LoadGLTexture("Data/mud.bmp");


The last error is being shown on a line where only a "{" appears:


1>c:\documents and settings\dmcguire\desktop\tutorials 6-10\lesson10\lesson10\lesson10.cpp(131) : error C2447: '{' : missing function header (old-style formal list?)
naturally texture1-5 should be defined as unsigned int beforehand.
I found the solution to the LoadGlTexture part. The stupid error i made was missing the Gluint loop; so it wasnt recognizing it. It now builds without errors, but initialization fails every time. know of any reason this might happen?
Ok, so now i got rid of that error, the filenames were incorrect... more stupid mistakes, but now im really stumped. When i run the program, it builds fine, runs fine, but the triangles im using are EVERYWHERE. they dont go to any of the verticies at all, but they only do this if i put glBindTexture(GL_TEXTURE_2D, texture[1]); anywhere in the txt file. if i put the glBindTexture(GL_TEXTURE_2D, texture[1]); into the glDrawScene, it works fine, but i only have one texture still. Where exactly do i put these tags, and do i need to change the loops. I pasted my whole glDrawScene below incase i do.



int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View

GLfloat x_m, y_m, z_m, u_m, v_m;
GLfloat xtrans = -xpos;
GLfloat ztrans = -zpos;
GLfloat ytrans = -walkbias-0.25f;
GLfloat sceneroty = 360.0f - yrot;
glClearColor(0.5f,0.5f,0.5f,1.0f); // We'll Clear To The Color Of The Fog ( Modified )
glFogi(GL_FOG_MODE, fogMode[fogfilter]); // Fog Mode
glFogfv(GL_FOG_COLOR, fogColor); // Set Fog Color
glFogf(GL_FOG_DENSITY, 0.35f); // How Dense Will The Fog Be
glHint(GL_FOG_HINT, GL_DONT_CARE); // Fog Hint Value
glFogf(GL_FOG_START, 1.0f); // Fog Start Depth
glFogf(GL_FOG_END, 5.0f); // Fog End Depth
glEnable(GL_FOG); // Enables GL_FOG


int numtriangles;

glRotatef(lookupdown,1.0f,0,0);
glRotatef(sceneroty,0,1.0f,0);
glTranslatef(xtrans, ytrans, ztrans);
numtriangles = sector1.numtriangles;

// Process Each Triangle
for (int loop_m = 0; loop_m < numtriangles; loop_m++)
{
glBegin(GL_TRIANGLES);
glNormal3f( 0.0f, 0.0f, 1.0f);
x_m = sector1.triangle[loop_m].vertex[0].x;
y_m = sector1.triangle[loop_m].vertex[0].y;
z_m = sector1.triangle[loop_m].vertex[0].z;
u_m = sector1.triangle[loop_m].vertex[0].u;
v_m = sector1.triangle[loop_m].vertex[0].v;
glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);

x_m = sector1.triangle[loop_m].vertex[1].x;
y_m = sector1.triangle[loop_m].vertex[1].y;
z_m = sector1.triangle[loop_m].vertex[1].z;
u_m = sector1.triangle[loop_m].vertex[1].u;
v_m = sector1.triangle[loop_m].vertex[1].v;
glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);

x_m = sector1.triangle[loop_m].vertex[2].x;
y_m = sector1.triangle[loop_m].vertex[2].y;
z_m = sector1.triangle[loop_m].vertex[2].z;
u_m = sector1.triangle[loop_m].vertex[2].u;
v_m = sector1.triangle[loop_m].vertex[2].v;
glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);
glEnd();
}
return TRUE; // Everything Went OK
}
Quote: Original post by Icewater11
When i run the program, it builds fine, runs fine, but the triangles im using are EVERYWHERE. they dont go to any of the verticies at all, but they only do this if i put glBindTexture(GL_TEXTURE_2D, texture[1]); anywhere in the txt file. if i put the glBindTexture(GL_TEXTURE_2D, texture[1]); into the glDrawScene, it works fine, but i only have one texture still. Where exactly do i put these tags, and do i need to change the loops.

As you noticed, putting the call to glBindTexture inside your text file doesn't work all that well; it disrupts the ordering of vertices for your triangles, and doesn't have the intended effect of texturing things. Also as you noticed, putting the call inside of your code does not change your geometry, which is a good thing and what we should explore. The answer will likely depend on how the texture information for each triangle is stored. In your text file, what tells you that one triangle will use one texture, while the next triangle will use another?

Looking at your drawing code, I'll make some comments:
int DrawGLScene(GLvoid)									// Here's Where We Do All The Drawing{	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear The Screen And The Depth Buffer	glLoadIdentity();									// Reset The View	GLfloat x_m, y_m, z_m, u_m, v_m;  // You should initialize these to have some value (0.0f works fine)	GLfloat xtrans = -xpos;	GLfloat ztrans = -zpos;	GLfloat ytrans = -walkbias-0.25f;	GLfloat sceneroty = 360.0f - yrot;  // You don't need that "360.0f" there, but if it helps to visualize, then keep it, I suppose	glClearColor(0.5f,0.5f,0.5f,1.0f);			// We'll Clear To The Color Of The Fog ( Modified )        //  All this fog setup should go into your initialization code, unless these parameters are actually changed from draw to draw	glFogi(GL_FOG_MODE, fogMode[fogfilter]);		// Fog Mode	glFogfv(GL_FOG_COLOR, fogColor);			// Set Fog Color	glFogf(GL_FOG_DENSITY, 0.35f);				// How Dense Will The Fog Be	glHint(GL_FOG_HINT, GL_DONT_CARE);			// Fog Hint Value	glFogf(GL_FOG_START, 1.0f);				// Fog Start Depth	glFogf(GL_FOG_END, 5.0f);				// Fog End Depth	glEnable(GL_FOG);					// Enables GL_FOG        //  go ahead and initialize this here, instead of four lines later	int numtriangles;	glRotatef(lookupdown,1.0f,0,0);	glRotatef(sceneroty,0,1.0f,0);	glTranslatef(xtrans, ytrans, ztrans);	numtriangles = sector1.numtriangles;		// Process Each Triangle	for (int loop_m = 0; loop_m &lt; numtriangles; loop_m++)	{                //  Moving this glBegin/glEnd pair to outside of the for loop                //  will speed things up and have absolutely no effect on                //  appearance.  With the primitive mode set to GL_TRIANGLES,                //  GL will automatically take groups of three vertices and                //  make a triangle, no need to stop and start again.                //  However, this is where you'll want to bind the needed texture.                //  This information is stored somewhere with the triangle in                //  your text file, and should be bound here.                //  Also, since you're performing the same operation on three                //  vertices stored in an array, why not make this a loop, as                //  well?  0-2 for the vertex array.		glBegin(GL_TRIANGLES);			glNormal3f( 0.0f, 0.0f, 1.0f);			x_m = sector1.triangle[loop_m].vertex[0].x;			y_m = sector1.triangle[loop_m].vertex[0].y;			z_m = sector1.triangle[loop_m].vertex[0].z;			u_m = sector1.triangle[loop_m].vertex[0].u;			v_m = sector1.triangle[loop_m].vertex[0].v;			glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);						x_m = sector1.triangle[loop_m].vertex[1].x;			y_m = sector1.triangle[loop_m].vertex[1].y;			z_m = sector1.triangle[loop_m].vertex[1].z;			u_m = sector1.triangle[loop_m].vertex[1].u;			v_m = sector1.triangle[loop_m].vertex[1].v;			glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);						x_m = sector1.triangle[loop_m].vertex[2].x;			y_m = sector1.triangle[loop_m].vertex[2].y;			z_m = sector1.triangle[loop_m].vertex[2].z;			u_m = sector1.triangle[loop_m].vertex[2].u;			v_m = sector1.triangle[loop_m].vertex[2].v;			glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);		glEnd();	}	return TRUE;										// Everything Went OK}

So, you'll bind each texture as it is needed inside of the existing for loop, and you'll determine which texture is needed based on the information within the text file.
That would all work well, but adding the tag inside the code would only let me add one texture. To get two wouldnt i have to load another txt file and draw another set of triangles?
Quote: Original post by Icewater11
That would all work well, but adding the tag inside the code would only let me add one texture. To get two wouldnt i have to load another txt file and draw another set of triangles?

It depends on how the texture information is stored in the text file. Can you post it, or a snippet, at least, to get an idea of how it's organized? The basics would look like this:
// Process Each TriangleglBegin(GL_TRIANGLES);	for (int loop_m = 0; loop_m < numtriangles; loop_m++)	{                unsigned int uiTextureNeededByThisTriangle = sector1.triangle[loop_m].textureID;                glBindTexture(GL_TEXTURE_2D, uiTextureNeededByThisTriangle);                //  As was mentioned before, a handy thing to do would be to                //  collect all the geometry that uses each texture, and render                //  in that order, because binding textures is a costly                //  operation. 		glNormal3f( 0.0f, 0.0f, 1.0f);                for (unsigned int ui = 0; ui < 3; ++ui)                {			x_m = sector1.triangle[loop_m].vertex[ui].x;			y_m = sector1.triangle[loop_m].vertex[ui].y;			z_m = sector1.triangle[loop_m].vertex[ui].z;			u_m = sector1.triangle[loop_m].vertex[ui].u;			v_m = sector1.triangle[loop_m].vertex[ui].v;			glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);					}	}glEnd( );
Again, though, I can't tell you any more without seeing how texture information is stored in your data file.
its 372 pollies long, but after the beginning its just repetetive:

NUMPOLLIES 372

// Floor 1

-3.0 0.0 -3.0 0.0 6.0
3.0 0.0 -3.0 6.0 6.0
3.0 0.0 3.0 6.0 0.0

// Floor 2
-0.5 0.0 3.0 0.0 1.0
-0.5 0.0 4.0 1.5 1.0
0.5 0.0 3.0 1.5 0.0
-0.5 0.0 4.0 1.5 0.0
0.5 0.0 3.0 0.0 1.0
0.5 0.0 4.0 0.0 0.0

// Floor 3
-0.5 0.0 4.0 0.0 1.0
-0.5 0.0 5.0 1.5 1.0
0.5 0.0 4.0 1.5 0.0
-0.5 0.0 5.0 1.5 0.0
0.5 0.0 4.0 0.0 1.0
0.5 0.0 5.0 0.0 0.0


// Floor 4
-0.5 0.0 5.0 0.0 1.0
-0.5 0.0 6.0 1.5 1.0
0.5 0.0 5.0 1.5 0.0
-0.5 0.0 6.0 1.5 0.0
0.5 0.0 5.0 0.0 1.0
0.5 0.0 6.0 0.0 0.0


// Floor 5
-0.5 0.0 6.0 0.0 1.0
-0.5 0.0 7.0 1.5 1.0
0.5 0.0 6.0 1.5 0.0
-0.5 0.0 7.0 1.5 0.0
0.5 0.0 6.0 0.0 1.0
0.5 0.0 7.0 0.0 0.0


// Floor 6
-0.5 0.0 7.0 0.0 1.0
-0.5 0.0 8.0 1.5 1.0
0.5 0.0 7.0 1.5 0.0
-0.5 0.0 8.0 1.5 0.0
0.5 0.0 7.0 0.0 1.0
0.5 0.0 8.0 0.0 0.0

and so on from there

This topic is closed to new replies.

Advertisement