gpt4 book ai didi

opengl - 使用 VBO 的纹理绑定(bind)无法正常工作

转载 作者:行者123 更新时间:2023-12-02 11:47:02 25 4
gpt4 key购买 nike

我的程序的目的是显示 2 个具有 2 个不同纹理的四边形。问题是加载的最旧的纹理设置在两个四边形上(仅 texQuadB)。我尝试用 texQuadA 和 texQuadB 替换 buf[3] 的两次调用,但它不起作用。我找不到将纹理与特定 VBO 绑定(bind)的正确方法。

#define OFFSET_BUFFER(bytes) ((GLfloat *)NULL + bytes)

GLfloat verticesQuadA[12] =
{
-1.0f, 0.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
0.0f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f,
};

GLfloat verticesQuadB[12] =
{
0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f,
};

GLfloat colors[12] =
{
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f,
};

GLfloat texture[8] =
{
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f
};

int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("Texture Mapping",NULL);
SDL_SetVideoMode(500, 500, 32, SDL_OPENGL);

bool continuer = true;
SDL_Event event;
GLuint buf[4];
GLuint texQuadA, texQuadB;

glClearDepth(1.0f);
glClearColor(0.1f, 0.1f, 0.1f, 0.1f);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70.0f, (float)500.0f / (float)500.0f, 1.0f, 3000.0f);
glewInit();

texQuadA = loadTexture("caisse.jpg");
texQuadB = loadTexture("metal.jpg");

//glBindTexture(GL_TEXTURE_2D, texQuadA);
//glBindTexture(GL_TEXTURE_2D, texQuadB);

glGenBuffers(4, buf);

glBindBuffer(GL_ARRAY_BUFFER, buf[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(verticesQuadA), verticesQuadA, GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, buf[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(verticesQuadB), verticesQuadB, GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, buf[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, buf[3]);
glBufferData(GL_ARRAY_BUFFER, sizeof(texture), texture, GL_STATIC_DRAW);

while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = false;
}

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_TEXTURE_2D);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

//Draw Quad A ---------------------------------------

glBindBuffer(GL_ARRAY_BUFFER, buf[0]);
glVertexPointer(3, GL_FLOAT, 0, OFFSET_BUFFER(0));

glBindBuffer(GL_ARRAY_BUFFER, buf[2]);
glColorPointer(3, GL_FLOAT, 0, OFFSET_BUFFER(0));

glBindBuffer(GL_ARRAY_BUFFER, buf[3]);
glTexCoordPointer(2, GL_FLOAT, 0, OFFSET_BUFFER(0));

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glDrawArrays(GL_QUADS, 0, 4);

glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

//Draw Quad B ---------------------------------------

glBindBuffer(GL_ARRAY_BUFFER, buf[1]);
glVertexPointer(3, GL_FLOAT, 0, OFFSET_BUFFER(0));

glBindBuffer(GL_ARRAY_BUFFER, buf[2]);
glColorPointer(3, GL_FLOAT, 0, OFFSET_BUFFER(0));

glBindBuffer(GL_ARRAY_BUFFER, buf[3]);
glTexCoordPointer(2, GL_FLOAT, 0, OFFSET_BUFFER(0));

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glDrawArrays(GL_QUADS, 0, 4);

glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

//----------------------------------------------------

glFlush();
SDL_GL_SwapBuffers();
}
glDeleteBuffers(3, buf);
glDisable(GL_TEXTURE_2D);
SDL_Quit();

return 0;
}

最佳答案

OpenGL 是一个状态机。绑定(bind)纹理是设置该状态的一部分。启用纹理后,OpenGL 将在绘制几何体时使用当前纹理状态(您绑定(bind)的最后一个纹理)。在绘制 Quad A 之前,请绑定(bind) texQuadA。在绘制Quad B之前,绑定(bind)texQuadB:

glBindTexture(GL_TEXTURE_2D, texQuadA);
// Draw Quad A

glBindTexture(GL_TEXTURE_2D, texQuadB);
// Draw Quad B

在这种情况下,VBO 是不相关的。

关于opengl - 使用 VBO 的纹理绑定(bind)无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10385739/

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