问题描述
好吧,仍然有一些问题,这就是我到目前为止的问题:
Okay still having a few problems with it, this is what I have so far:
Bitmap Display::m_HeightMap;
unsigned int Display:: textures;
我的初始化方法:
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);
上面的行给出了一个错误:.data的左侧必须具有class/struct/union
The above line gives an error: left of .data must have class/struct/ union
glDisable(GL_TEXTURE_2D);
绘制方法:
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.
希望有最后一件事!
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);}
image.data);}
当我尝试加载多个位图时,出现7个错误,
When I try and load multiple bitmaps I get 7 errors,
错误C2040:图像:位图[2]"在位图"的间接访问级别上有所不同
错误C2088:"["上课不合法(两次)
错误C2228:.BMP的左侧必须具有class/struct/union(两次)
没有运算符"[]"匹配这些操作数(两次)
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)
推荐答案
所以. glGenTextures
具有两个参数. int
和GLuint*
. int
告诉GL要生成多少纹理,而GLuint*
是GLuint
s的数组(在哪里生成纹理).您执行以下操作的原因...
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 GLuint
s (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);
这样,您告诉GL我想生成n个纹理,这是一个数组,至少为这么多纹理分配了空间.
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
}
请确保在程序结尾处按delete textures;
键,以便在分配该空间后正确进行清理.
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.
这几乎可以完成您想要做的事情.
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;
}
您指出的某些问题与OpenGL不完全相关,它们与C/C ++有关.我知道这不是您要寻找的答案,但这可能会帮助您学习C函数,指针,数组等,并在移动之前花一个月与函数/指针/数组紧密合作,这可能会帮到您很多像OpenGL之类的东西,它需要对C有相当适度的理解.
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.
这篇关于多种纹理OpenGL GLUT C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!