0

I've trying to load different textures to the GL_TEXTUREX variables and then assign them to different spheres. but so far I've having problems. I tried some of the suggestions in post like this and this but couldnt solve it.

This is part of my code:

GLuint textures[2];
void LoadTextures(std::string const& dirname)
{
glGenTextures(2, textures);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures[0]);
Image_t sun = loadPNG(std::string(dirname + "/sun.png"));
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1024, 512, 0, GL_RGB, GL_UNSIGNED_BYTE, &(sun.data[0]));  
glBindTexture(GL_TEXTURE_2D, textures[0]);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, textures[1]);
Image_t mercury = loadPNG(std::string(dirname + "/mercury.png"));
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 1024, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, &(mercury.data[0]));  
}

and

void draw(void)
{

glEnable (GL_TEXTURE_2D);
glBindTexture (GL_TEXTURE_2D, textures[0]);
glPushMatrix();
glTranslatef(0.4,0,0);
gluSphere(sun, 0.5, 36, 36); //I want this sphere to use the texture GL_TEXTURE0
glPopMatrix();
glDisable(GL_TEXTURE_2D);
}
Community
  • 1
  • 1
user3276768
  • 1,416
  • 3
  • 18
  • 28
  • Are you using [gluQuadricTexture](http://www.talisman.org/opengl-1.1/Reference/gluQuadricTexture.html)? What results you want to achieve and what it have to do with multitexturing? – keltar May 13 '14 at 17:16
  • Yes, I am using it this way: gluQuadricTexture(sun, GL_TRUE); And what I want to do with multitexturing is just to texture all my spheres with a different texture (Sun, Earth, Mars, etc.). I'm not sure if I am on the correct track. Thanks for the reply. – user3276768 May 13 '14 at 18:29

1 Answers1

0

You need to call glBindTexture just before performing a draw call:

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures[0]);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, textures[1]);

draw_things();

In the above example, whatever you draw with draw_things will have access to both textures 0 and 1 (GL_TEXTURE0 and GL_TEXTURE1).

Now, if what you want is to draw many things, but each with a different texture, then you need to:

glActiveTexture(GL_TEXTURE0); // activate any texture unit
glEnable(GL_TEXTURE_2D);      // make sure texturing is enabled

glBindTexture(GL_TEXTURE_2D, thing1_texture);
draw_thing1();

glBindTexture(GL_TEXTURE_2D, thing2_texture);
draw_thing2();

// etecetera
glampert
  • 4,371
  • 2
  • 23
  • 49
  • I see some improvements. This is how my model is looking: [link](http://imgur.com/KVUj4wq) Thanks! Now I'm getting closer. – user3276768 May 13 '14 at 19:25
  • It looks like the color format of the texture is wrong. I see that you've hardcoded `GL_RGB/GL_RGBA` in the calls to `glTexImage2D`. Are you sure those are the correct formats? Furthermore, the dimensions are also fixed. Do they match the image sizes? The ideal would be to query this kind of data from the `Image_t` object. It should have methods like `getWidth()`, `getFormat(),` etc. – glampert May 13 '14 at 21:44
  • 1
    The problem was with the color format. Thank you very much for your time! :D – user3276768 May 14 '14 at 08:55