• Advertisement
Sign in to follow this  

Swapping Textures during Program...

This topic is 4743 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i have 2 textures that i load into my program as such: load_bmp_texture("color_ramp_world1024x1psdbackup2.bmp", -1, color_ramp); its a simple bmp texture loader, the flag, in this case -1, indicates what type of texture it will be such as 1D..3D and what properties will be set and color_ramp is just of type GLuint. i also assign another texture like so: load_bmp_texture("color_ramp_world1024x1final_backup.bmp", -1, color_ramp2); --i have a GUI that does most stuff but i want to be able to color my terrain with 2 different textures, toggling between the textures. what is the best way to do this? one final note -- the load_bmp_texture function, does pretty much everything including the Binding of the texture.. thanks all in advance! appreciate the help! :)

Share this post


Link to post
Share on other sites
Advertisement
I'd just provide an additional parameter to the load_bmp_texture-function that denotes the texture-id (or have it returned by the function) and swap that during runtime:


GLuint tex1 = load_bmp_texture("color_ramp_world1024x1psdbackup2.bmp", -1, color_ramp);
GLuint tex2 = load_bmp_texture("color_ramp_world1024x1final_backup.bmp", -1, color_ramp2);

GLuint terrainTex = tex1;

// in your UI code
void toggle_textures(void) {
if (terrainTex == tex1)
terrainTex = tex2;
else
terrainTex = tex1;
}

// rendering code
glBindTexture(GL_TEXTURE_2D, terrainTex); // <- either tex1 or tex2
render_terrain();




[edit]
Is your ”color_ramp“ parameter the texture id? In this case, you'll just use

// in your UI code
void toggle_textures(void) {
if (terrainTex == color_ramp)
terrainTex = color_ramp2;
else
terrainTex = color_ramp;
}


[/edit]

Share this post


Link to post
Share on other sites
Just call glBindTexture in between each (of what I assume is a) tile:

while(tilesNotAllDrawn)
{
glBindTextures(GL_TEXTURE_*D, texture1);
//draw tile 1
glBindTextures(GL_TEXTURE_*D, texture2);
//draw tile 2
}

You can even store all of your textures in an array and enumerate the indices for greater ease:

glBindTextures(GL_TEXTURE_*D, texArray[grass]);
//draw tile 1
glBindTextures(GL_TEXTURE_*D, texArray[dirt]);
//draw tile 2

Hope this helps!

Share this post


Link to post
Share on other sites
in load_bmp_texture:


void load_bmp_texture (char *, int, GLuint &);

///////////////////////////////////////////////////////////
// function: load_bmp_texture
//
// Looks through the .bmp file skipping header information
// and reading in the RGB values. It then reverses the order
// of the colors.
///////////////////////////////////////////////////////////

void load_bmp_texture (char * filename, int num, GLuint &texture)
{
FILE * file;
short plane, bpp;
int temp;
image tree;

glGenTextures (1, &texture);

if ((file = fopen (filename, "rb")) == NULL)
{
cout << "problem opening file " << filename << endl;
exit (1);
}

fseek (file, 18, SEEK_CUR);

//tree.width = getint (file);
fread (&tree.width, 4, 1, file);
// cout << "width of texture: " << tree.width << endl;

//tree.height = getint (file);
fread (&tree.height, 4, 1, file);
//cout << "height of texture: " << tree.height << endl;

//plane = getshort (file);
fread (&plane, 2, 1, file);
fread (&bpp, 2, 1, file);
//bpp = getshort (file);

//cout << "It is a " << bpp << " formatted file\n" << endl;
cout << "Loading the " << tree.width << "x" << tree.height << " texture...";

fseek (file, 24, SEEK_CUR);

if ((tree.data = (char *) malloc (tree.height * tree.width * 3)) == NULL)
{
cout << "run out of memory\n";
exit (1);
}

fread (tree.data, tree.height * tree.width * 3, 1, file);

for (unsigned int i = 0; i < tree.height * tree.width * 3; i+=3)
{
temp = tree.data[i+2];
tree.data[i+2] = tree.data;
tree.data = temp;
}

cout << "done." << endl;

if(num == -1)
{
glBindTexture(GL_TEXTURE_1D, texture); //binding the texture
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); //wrap the s coordinate of the texture
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, tree.width, 0, GL_RGB, GL_UNSIGNED_BYTE, tree.data);
}
else if (num == 0)
{
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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tree.width, tree.height, 0, GL_RGB, GL_UNSIGNED_BYTE, tree.data);
}
else if (num == 1)
{
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, 3, tree.width, tree.height, 0, GL_RGB, GL_UNSIGNED_BYTE, tree.data);
}
else if (num == 2)
{
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_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, tree.width, tree.height, GL_RGB, GL_UNSIGNED_BYTE, tree.data);
}

fclose (file);
}



so yes -- color_ramp is the texture ID. but i load all these in at the instantiation of my class and its not my rendering code. so therefore all the binding and stuff is done at initialization. what should i move to the render code to make this work the most efficiently and easily?? -- that is if i have to move anything to the rendering code...? thanks again! one final note.. this is just a header file that my class includes, the class loads the texture.. the main.cpp knows absolutely nothing about textures or anyting else, so thought that little info my help.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement