Many OpenGL GLUT C ++ textures

I still have a few problems with this, this is what I still have:

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

My initialization 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: the class / struct / union should be to the left of .data

 glDisable(GL_TEXTURE_2D); 

Drawing 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 is that the texture is undefined.

I hope for a long time!

 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 to load multiple bitmaps, I get 7 errors,

Error C2040: Image: "Bitmap [2]" differs in level of indirection "Bitmap"
error C2088: '[' is prohibited for the class (twice)
error C2228: to the left of .BMP there should be a class / struct / union (twice)
no operator "[]" matches these operands (twice)

+7
source share
3 answers

So. glGenTextures takes two parameters. A int and a GLuint* . int tells GL how many textures to generate, and GLuint* is the GLuint array (for generating textures). The reason you do the following ...

 GLuint m_TextureID glGenTextures(1, &m_TextureID) 

- this is because you have only 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); 

So you say GL, I want to generate n textures, but here is an array with allocated space, at least in order to have a lot of textures.

Suppose you want to use both of them in your drawing cycle, you would do it 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 delete textures; at the end of your program it will clean up correctly after allocating this space.

There are also ways not to snap individual textures. You can use the so-called texture atlas. Basically, this is a single bitmap containing multiple sub-images. Thus, you simply create and link one raster image and use its separate parts.

To handle multiple bitmaps, do the following:

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

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


Everything below this line answers your updated question.

This will pretty much do what you are 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 problems you point out are not entirely related to OpenGL, they are more related to C / C ++. I know this is not the answer you are looking for, but it will probably help you learn a lot about C functions, pointers, arrays, etc. And spend a whole month working closely with functions / pointers / arrays before moving on to something like OpenGL, which requires a fairly moderate understanding of C.

+16
source

const GLsizei n = (number of textures here);

GLuint textureIDs = new GLuint [n];

glGenTextures (n, textureID);

0
source

I assume you mean that you want to create several textures and store the identifier in an array? You can do it like this:

 GLsizei num_textures = 5; GLuint textures[num_textures]; glGenTextures(num_textures, textures); 

Once you do this, just scroll through your textures and for each of them, bind them, set parameters, load image data, etc.

You might want to create your array of textures on the heap so that you can later access the texture identifiers:

 GLuint* textures = new GLuint[num_textures]; 

Just delete the array later:

 delete textures; 
0
source

All Articles