Advertisement

lesson 10 question about textures

Started by October 17, 2009 12:43 AM
2 comments, last by Haladria 15 years, 1 month ago
Is there a way to pick the texture you want to use inside the world.txt that the lesson code uses to draw the world? And what if I have more than one texture? To help clarify, here is a piece of the world.txt file from which opengl gets its triangle data. With added to it what I would like to know:


// Is this possible?

texture1 = "data/wall.bmp"

// A1

-2.0  1.0  -2.0 0.0 1.0
-2.0  0.0  -2.0 0.0 0.0
-0.5  0.0  -2.0 1.5 0.0
-2.0  1.0  -2.0 0.0 1.0
-0.5  1.0  -2.0 1.5 1.0
-0.5  0.0  -2.0 1.5 0.0

// A2

 2.0  1.0  -2.0 2.0 1.0
 2.0  0.0  -2.0 2.0 0.0
 0.5  0.0  -2.0 0.5 0.0
 2.0  1.0  -2.0 2.0 1.0
 0.5  1.0  -2.0 0.5 1.0
 0.5  0.0  -2.0 0.5 0.0

//possible to declare a different texture for the next triangles?
texture2 = "data/wall2.bmp"

// B1

-2.0  1.0  2.0 2.0  1.0
-2.0  0.0   2.0 2.0 0.0
-0.5  0.0   2.0 0.5 0.0
-2.0  1.0  2.0 2.0  1.0
-0.5  1.0  2.0 0.5  1.0
-0.5  0.0   2.0 0.5 0.0

// B2

 2.0  1.0  2.0 2.0  1.0
 2.0  0.0   2.0 2.0 0.0
 0.5  0.0   2.0 0.5 0.0
 2.0  1.0  2.0 2.0  1.0
 0.5  1.0  2.0 0.5  1.0
 0.5  0.0   2.0 0.5 0.0



So what I would like to know is, How can I make the opengl code read the texture file location and draw the right texture for the right triangles Any answer is appreciated, lesson 10 link: Lesson 10 I hope to have been clear enough, thanks =]
Yes it's possible. You can divide the file into groups, where each group stores a texture name, number of triangles, and the vertices.

Something like:

// Number of groups2"data/wall.bmp"// Number of triangles that use this texture4-2.0  1.0  -2.0 0.0 1.0-2.0  0.0  -2.0 0.0 0.0-0.5  0.0  -2.0 1.5 0.0-2.0  1.0  -2.0 0.0 1.0-0.5  1.0  -2.0 1.5 1.0-0.5  0.0  -2.0 1.5 0.0 2.0  1.0  -2.0 2.0 1.0 2.0  0.0  -2.0 2.0 0.0 0.5  0.0  -2.0 0.5 0.0 2.0  1.0  -2.0 2.0 1.0 0.5  1.0  -2.0 0.5 1.0 0.5  0.0  -2.0 0.5 0.0"data/wall2.bmp"// Number of triangles that use this texture4-2.0  1.0  2.0 2.0  1.0-2.0  0.0   2.0 2.0 0.0-0.5  0.0   2.0 0.5 0.0-2.0  1.0  2.0 2.0  1.0-0.5  1.0  2.0 0.5  1.0-0.5  0.0   2.0 0.5 0.0 2.0  1.0  2.0 2.0  1.0 2.0  0.0   2.0 2.0 0.0 0.5  0.0   2.0 0.5 0.0 2.0  1.0  2.0 2.0  1.0 0.5  1.0  2.0 0.5  1.0 0.5  0.0   2.0 0.5 0.0


To load the data, you basically do this:

read num groupsfor each group    read texture name    read num triangles    for each triangle        read 3 vertices


To draw a group, you bind its associated texture and draw the triangles it contains.
Advertisement
Okay.. after trying this and that here and there..

It does compile but for some reason it fails to initialize, I have no idea whats wrong..

This is what Ive gotten so far:

Ive added the variable char* texturelink and Ive added the GROUPS structure:
char* texturelink;typedef struct tagVERTEX{	float x, y, z;	float u, v;} VERTEX;typedef struct tagTRIANGLE{	VERTEX vertex[3];} TRIANGLE;typedef struct tagGROUPS{    int numtriangles;    TRIANGLE* triangle;} GROUPS;      typedef struct tagSECTOR{	int numgroups;	GROUPS* groups;} SECTOR;SECTOR sector1;				

This is where the .txt file gets scanned, Ive added a loop for the groups, and made it scan for TX (the textures location) and NUMTU (number of triangles in each group) along with some other changes:
void SetupWorld(){	float x, y, z, u, v;	int numtriangles;	int numgroups;	FILE *filein;	char oneline[255];	filein = fopen("data/test.txt", "rt");				// File To Load World Data From	readstr(filein,oneline);    sscanf(oneline, "NUMGROUPS %d\n", &numgroups);	sector1.groups = new GROUPS[numgroups];  for (int lus = 0; lus < numgroups; lus++)  {      readstr(filein,oneline);      sscanf(oneline, "TX", &texturelink);      sscanf(oneline, "NUMTU %d\n", &numtriangles);	      sector1.groups->triangle = new TRIANGLE[numtriangles];      sector1.numgroups = numgroups;	  sector1.groups->numtriangles = numtriangles;	for (int loop = 0; loop < numtriangles; loop++)	{		for (int vert = 0; vert < 3; vert++)		{			readstr(filein,oneline);			sscanf(oneline, "%f %f %f %f %f", &x, &y, &z, &u, &v);			sector1.groups->triangle[loop].vertex[vert].x = x;			sector1.groups->triangle[loop].vertex[vert].y = y;			sector1.groups->triangle[loop].vertex[vert].z = z;			sector1.groups->triangle[loop].vertex[vert].u = u;			sector1.groups->triangle[loop].vertex[vert].v = v;		}     }	  }	fclose(filein);	return;}

In the section where the texture gets loaded I have swapped the direct location with the variable in which the location is stored:
        if (TextureImage[0]=LoadBMP(texturelink))

The world gets drawn here, some changes have been made to include the groups structure:
	numtriangles = sector1.groups->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.groups->triangle[loop_m].vertex[0].x;			y_m = sector1.groups->triangle[loop_m].vertex[0].y;			z_m = sector1.groups->triangle[loop_m].vertex[0].z;			u_m = sector1.groups->triangle[loop_m].vertex[0].u;			v_m = sector1.groups->triangle[loop_m].vertex[0].v;			glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);						x_m = sector1.groups->triangle[loop_m].vertex[1].x;			y_m = sector1.groups->triangle[loop_m].vertex[1].y;			z_m = sector1.groups->triangle[loop_m].vertex[1].z;			u_m = sector1.groups->triangle[loop_m].vertex[1].u;			v_m = sector1.groups->triangle[loop_m].vertex[1].v;			glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);						x_m = sector1.groups->triangle[loop_m].vertex[2].x;			y_m = sector1.groups->triangle[loop_m].vertex[2].y;			z_m = sector1.groups->triangle[loop_m].vertex[2].z;			u_m = sector1.groups->triangle[loop_m].vertex[2].u;			v_m = sector1.groups->triangle[loop_m].vertex[2].v;			glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);		glEnd();    }	

And lastly, this is what the .txt file from which the world data gets loaded looks like now:
NUMGROUPS 2TX "data/Mud.bmp"NUMTU 2// left wall-2.0 1.0 1.0 0.0 1.0-2.0 0.0 1.0 0.0 0.0-2.0 0.0 -2.0 1.0 0.0-2.0 1.0 1.0 0.0 1.0-2.0 1.0 -2.0 1.0 1.0-2.0 0.0 -2.0 1.0 0.0TX "data/wall.bmp"NUMTU 2// right wall-2.0 1.0 -2.0 0.0 1.0-2.0 0.0 -2.0 0.0 0.0-6.0 0.0 -2.0 1.0 0.0-2.0 1.0 -2.0 0.0 1.0-6.0 1.0 -2.0 1.0 1.0-6.0 0.0 -2.0 1.0 0.0


Anyone know what im doing wrong?
Or maybe someone has an example of an easier way to reach what Im trying here?
Save yourself the trouble and use XML instead for loading (even for testing these kinds of small stuff). For example use this library: http://www.grinninglizard.com/tinyxml/

This topic is closed to new replies.

Advertisement