好的,仍然有一些问题,这是我到目前为止所得到的:
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的左边必须有类/结构/联合
glDisable(GL_TEXTURE_2D);Draw方法:
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);
}唯一的问题是纹理是未定义的。
希望还有最后一件事!
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);}
当我尝试加载多个位图时,我得到了7个错误,
错误C2040:图像:‘Bitmap 2’在'Bitmap‘的间接级别不同
错误C2088:'[‘对于类非法(两次)
错误C2228:.BMP的左侧必须有类/结构/联合(两次)
没有运算符"[]“与这些操作数匹配(两次)
发布于 2011-04-16 04:33:30
所以。glGenTextures有两个参数。一个int和一个GLuint*。int告诉GL要生成多少纹理,而GLuint*是一个GLuints数组(生成纹理的位置)。你这样做的原因是...
GLuint m_TextureID
glGenTextures(1, &m_TextureID)是因为你只有一种纹理。如果你有多个,你可以这样做:
// Substitute 'n' for some const number
GLuint *textures = new GLuint[n];
glGenTextures(n, textures);这样,你告诉GL我想生成n个纹理,这是一个分配了至少那么多纹理空间的数组。
假设你想在你的绘图循环中使用这两个元素,你可以这样实现:
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;命令,以便在分配该空间后正确清理。
还有一些方法可以不必绑定单独的纹理。你可以使用所谓的“纹理地图集”。基本上,这是一个包含多个子图像的位图。因此,您只需生成并绑定一个位图,并使用它的不同部分。
要处理多个位图,请执行以下操作:
Bitmap image[2];
image[0].loadBMP("myTexture.bmp");
image[1].loadBMP("myOtherTexture.bmp");然后按照这个过程为两个位图生成一个位图。
这条线下面的所有内容都是对您更新的问题的回应。
这应该能完成你想要做的事情。
// 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有一定了解的东西之前,花一个月的时间密切地使用函数/指针/数组。
发布于 2011-04-16 04:29:44
const GLsizei n=(此处的纹理数);
GLuint textureIDs =新的GLuint n;
glGenTextures( n,textureIDs );
发布于 2011-04-16 04:30:09
我想您的意思是想要生成多个纹理并将id存储在一个数组中?你可以这样做:
GLsizei num_textures = 5;
GLuint textures[num_textures];
glGenTextures(num_textures, textures);一旦你这样做了,只需循环你的纹理,对于每个纹理,绑定它,设置参数,加载图像数据,等等。
您可能希望在堆上创建纹理数组,以便以后可以访问纹理ID:
GLuint* textures = new GLuint[num_textures];只需确保稍后删除该数组:
delete textures;https://stackoverflow.com/questions/5681948
复制相似问题