我有这个立方体,我想用不同的图像覆盖它的每一面,其中出现一个字母。
我的问题是立方体被相同的图像覆盖,如下图所示。但我希望字母 A 出现在顶部,B 出现在左侧,C 出现在右侧。
为了在纹理中转换/转换图像(字母 A、B、C),我使用以下代码:
glGenTextures(1, textureA);
glGenTextures(1, textureB);
glGenTextures(1, textureC);
glClearColor (0.0, 0.0, 0.0, 0.0);
if (flagA ==1){
cvtColor(imageA, imageA, CV_BGR2RGB);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glBindTexture(GL_TEXTURE_2D, textureA);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
//glTexImage2D(GL_TEXTURE_2D, 0, 4,image.cols, image.rows, 0, GL_RGB,GL_UNSIGNED_BYTE, image.data);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, imageA.cols, imageA.rows, GL_RGB, GL_UNSIGNED_BYTE, imageA.data);
}
if (flagC ==1){
cvtColor(imageC, imageC, CV_BGR2RGB);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glBindTexture(GL_TEXTURE_2D, textureC);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
//glTexImage2D(GL_TEXTURE_2D, 0, 4,image.cols, image.rows, 0, GL_RGB,GL_UNSIGNED_BYTE, image.data);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, imageC.cols, imageC.rows, GL_RGB, GL_UNSIGNED_BYTE, imageC.data);
}
if (flagB ==1){
cvtColor(imageB, imageB, CV_BGR2RGB);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glBindTexture(GL_TEXTURE_2D, textureB);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
//glTexImage2D(GL_TEXTURE_2D, 0, 4,image.cols, image.rows, 0, GL_RGB,GL_UNSIGNED_BYTE, image.data);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, imageB.cols, imageB.rows, GL_RGB, GL_UNSIGNED_BYTE, imageB.data);
}
我注意到如果我切换块(这样带有 C 字母的图像是图像纹理转换过程中的最后一个),我的立方体只被 C 字母覆盖if(flagB==1){...}
。if(flagC==1){...}
为什么会出现这种奇怪的行为?
为了显示结果,我使用以下代码:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, WIDTH, HEIGHT, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if(flagA==1){
glBindTexture(GL_TEXTURE_2D, textureA);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f((GLfloat)((A[3].x)),(GLfloat)(A[3].y));
glTexCoord2f(1.0f, 0.0f); glVertex2f((GLfloat)((A[0].x)),(GLfloat)(A[0].y));
glTexCoord2f(1.0f, 1.0f); glVertex2f((GLfloat)((A[1].x)),(GLfloat)(A[1].y));
glTexCoord2f(0.0f, 1.0f); glVertex2f((GLfloat)((A[2].x)),(GLfloat)(A[2].y));
glEnd();
}
if(flagB==1){
glBindTexture(GL_TEXTURE_2D, textureB);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f((GLfloat)((B[3].x)),(GLfloat)(B[3].y));
glTexCoord2f(1.0f, 0.0f); glVertex2f((GLfloat)((B[0].x)),(GLfloat)(B[0].y));
glTexCoord2f(1.0f, 1.0f); glVertex2f((GLfloat)((B[1].x)),(GLfloat)(B[1].y));
glTexCoord2f(0.0f, 1.0f); glVertex2f((GLfloat)((B[2].x)),(GLfloat)(B[2].y));
glEnd();
}
if(flagC==1){
glBindTexture(GL_TEXTURE_2D, textureC);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f((GLfloat)((C[3].x)),(GLfloat)(C[3].y));
glTexCoord2f(1.0f, 0.0f); glVertex2f((GLfloat)((C[0].x)),(GLfloat)(C[0].y));
glTexCoord2f(1.0f, 1.0f); glVertex2f((GLfloat)((C[1].x)),(GLfloat)(C[1].y));
glTexCoord2f(0.0f, 1.0f); glVertex2f((GLfloat)((C[2].x)),(GLfloat)(C[2].y));
glEnd();
}
我在 windows7 x64 和 OpenGL 上工作。