8
votes

Okay still having a few problems with it, this is what I have so far:

Bitmap Display::m_HeightMap;
unsigned int Display:: textures;

My initialise method:

 glEnable(GL_TEXTURE_2D);

Bitmap image[2];
GLuint *textures = new GLuint[2];
glGenTextures(1, textures);
glGenTextures(2, textures);
image[0].loadBMP("myTexture.bmp");
image[1].loadBMP("myOtherTexture.bmp");  

    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE , GL_MODULATE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image.width, image.height, GL_RGB, GL_UNSIGNED_BYTE, image.data);

The above line gives an error: left of .data must have class/struct/ union

glDisable(GL_TEXTURE_2D);

Draw Method:

void Draw()
{
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS); //TOP
glBindTexture(GL_TEXTURE_2D, textures[0]);
glNormal3f(0,1,0);
glColor4f(1,1,1,0);
//glColor3d(0.5,0.40,0.05);
glTexCoord2f(0.0f,0.0f);
glVertex3f(-4.5, 0.3, 2);//bottom left
glTexCoord2f(1.0f,0.0f);
glVertex3f(-4.5, 0.3, 2.5);//bottom right
glTexCoord2f(1.0f,1.0f);
glVertex3f(4.5, 0.3, 2.5);//top right
glTexCoord2f(0.0f,1.0f);
glVertex3f(4.5, 0.3, 2);//top left
glEnd();
glDisable(GL_TEXTURE_2D);
}

Only problem here is that texture is undefined.

One last thing hopefully!

void loadTexture(GLuint texture, const char* filename)
{
   Bitmap image;

   Bitmap image[2];

   image[0].loadBMP("myTexture.bmp");  <=== error 
   image[1].loadBMP("myTexture2.bmp"); <=== error


   glBindTexture(GL_TEXTURE_2D, texture);

   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE , GL_MODULATE);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);

   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image.width, image.height, GL_RGB, GL_UNSIGNED_BYTE, 

image.data); }

When I try and load multiple bitmaps I get 7 errors,

error C2040: image:'Bitmap [2]' differs in level of indirection of 'Bitmap'
error C2088: '[' illegal for class (twice)
error C2228: left of .BMP must have class/struct/union (twice)
no operator "[]" matches these operands (twice)

3

3 Answers

18
votes

So. glGenTextures takes two parameters. An int, and a GLuint*. The int tells GL how many textures to generate, and the GLuint* is an array of GLuints (where to generate the textures). The reason you do the following...

GLuint m_TextureID
glGenTextures(1, &m_TextureID)

is because you only have one texture. If you have more than one, you would do this:

// Substitute 'n' for some const number
GLuint *textures = new GLuint[n];
glGenTextures(n, textures);

This way, you are telling GL I want to generate n textures, and here's an array with allocated space for at least that many textures.

Say you want to use both of them in your draw loop, you would implement that like this:

void draw()
{
   glBindTexture(GL_TEXTURE_2D, textures[0]); // Tell GL to use the first texture
   // Any drawing here will use first texture

   glBindTexture(GL_TEXTURE_2D, textures[1]); // Tell GL to use the second textures
   // Any drawing here will use second texture

   glBindTexture(GL_TEXTURE_2D, 0); // Set the GL texture to NULL, standard cleanup
}

Make sure to delete textures; at the end of your program to properly clean up after allocating that space.

There are also ways to not have to bind separate textures. You can use what's called a "texture atlas". Basically, this is one bitmap that contains multiple sub-images. So you just generate and bind one bitmap, and use separate parts of it.

To deal with multiple bitmaps, do this:

Bitmap image[2];
image[0].loadBMP("myTexture.bmp");
image[1].loadBMP("myOtherTexture.bmp");

Then follow the process to generate one bitmap for both bitmaps.


Everything below this line is responding to your updated question.

This should pretty much do what you're trying to do.

// Global variable
GLuint textures[2];

// init function
void init()
{
   textures = new GLuint[2]; 
   glGenTextures(2, textures);

   loadTexture(textures[0], "texture1.bmp");
   loadTexture(textures[1], "texture2.bmp");

}

void loadTexture(GLuint texture, const char* filename)
{
   Bitmap image; 
   image.loadBMP(filename); 

   glBindTexture(GL_TEXTURE_2D, texture);

   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE , GL_MODULATE);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);

   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   gluBuild2DMipmaps(GL_TEXTURE_2D, 3, image.width, image.height, GL_RGB, GL_UNSIGNED_BYTE, image.data);
}

// draw function
void draw()
{
   glBegin(GL_QUADS); //TOP
   glBindTexture(GL_TEXTURE_2D, textures[0]);
   glNormal3f(0,1,0);
   glColor4f(1,1,1,0);
   glTexCoord2f(0.0f,0.0f); glVertex3f(-4.5, 0.3, 2); //bottom left
   glTexCoord2f(1.0f,0.0f); glVertex3f(-4.5, 0.3, 2.5); //bottom right
   glTexCoord2f(1.0f,1.0f); glVertex3f(4.5, 0.3, 2.5); //top right
   glTexCoord2f(0.0f,1.0f); glVertex3f(4.5, 0.3, 2); //top left
   glEnd();
}

// cleanup function
void cleanup()
{
   delete textures;
}

Some of the issues you are pointing out aren't exactly OpenGL related, they are more C/C++ related. I know this isn't the answer you're looking for, but it would probably help you out a lot to learn about C functions, pointers, arrays, etc, and spend a solid month working closely with functions/pointers/arrays before moving on to something like OpenGL, which requires a pretty moderate understanding of C.

0
votes

const GLsizei n = (number of textures here);

GLuint textureIDs = new GLuint[ n ];

glGenTextures( n, textureIDs );

0
votes

I assume you mean you want to generate multiple textures and store the id's in an array? You can do that like this:

GLsizei num_textures = 5;
GLuint textures[num_textures];

glGenTextures(num_textures, textures);

Once you've done that, just loop through your textures and for each one, bind it, set parameters, load in image data, etc.

You may want to create your texture array on the heap so you can access the texture IDs later:

GLuint* textures = new GLuint[num_textures];

Just make sure to delete the array later:

delete textures;