Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So. <code>glGenTextures</code> takes two parameters. An <code>int</code>, and a <code>GLuint*</code>. The <code>int</code> tells GL how many textures to generate, and the <code>GLuint*</code> is an array of <code>GLuint</code>s (where to generate the textures). The reason you do the following...</p> <pre><code>GLuint m_TextureID glGenTextures(1, &amp;m_TextureID) </code></pre> <p>is because you only have one texture. If you have more than one, you would do this:</p> <pre><code>// Substitute 'n' for some const number GLuint *textures = new GLuint[n]; glGenTextures(n, textures); </code></pre> <p>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.</p> <p>Say you want to use both of them in your draw loop, you would implement that like this:</p> <pre><code>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 } </code></pre> <p>Make sure to <code>delete textures;</code> at the end of your program to properly clean up after allocating that space.</p> <p>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.</p> <p>To deal with multiple bitmaps, do this:</p> <pre><code>Bitmap image[2]; image[0].loadBMP("myTexture.bmp"); image[1].loadBMP("myOtherTexture.bmp"); </code></pre> <p>Then follow the process to generate one bitmap for both bitmaps.</p> <hr> <h2>Everything below this line is responding to your updated question.</h2> <p>This should pretty much do what you're trying to do.</p> <pre><code>// 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; } </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload