gpt4 book ai didi

c++ - 多纹理 OpenGL GLUT C++

转载 作者:可可西里 更新时间:2023-11-01 17:17:55 24 4
gpt4 key购买 nike

好吧仍然有一些问题,这是我目前所拥有的:

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);

上面一行报错: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);
}

这里唯一的问题是纹理未定义。

希望最后一件事!

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,

图像数据);

当我尝试加载多个位图时出现 7 个错误,

错误 C2040:图像:“Bitmap [2]”在“Bitmap”的间接级别上不同
error C2088: '[' 类非法(两次)
错误 C2228:.BMP 的左边必须有类/结构/union (两次)
没有运算符“[]”匹配这些操作数(两次)

最佳答案

所以。 glGenTextures 有两个参数。一个 int 和一个 GLuint*int 告诉 GL 要生成多少纹理,而 GLuint* 是一个 GLuint 数组(在何处生成纹理)。您执行以下操作的原因...

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
}

确保在程序结束时删除纹理;,以便在分配该空间后正确清理。

还有一些方法可以不必绑定(bind)单独的纹理。您可以使用所谓的“纹理图集”。基本上,这是一个包含多个子图像的位图。因此,您只需生成并绑定(bind)一个位图,然后使用它的不同部分。

要处理多个位图,请执行以下操作:

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 有相当适度的理解。

关于c++ - 多纹理 OpenGL GLUT C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5681948/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com