用不同的图像覆盖立方体的每一面 - OpenGL

时间:2013-05-05 16:18:59

标签: c++ opengl textures

我有这个立方体,我想用一个字母出现的不同图像覆盖它的每一面。

enter image description here

我的问题是多维数据集被同一图像覆盖,如下图所示。 但我希望字母A显示在顶部,B显示在左侧,C显示在右侧。

enter image description here

为了在纹理中转换/转换图像(字母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);
        }

我注意到,如果我用if(flagB==1){...}切换块if(flagC==1){...}(这样,带有C字母的图像是图像纹理转换过程中的最后一个),我的立方体只能被C字母覆盖。

为什么会出现这种奇怪的行为?

为了显示结果,我使用以下代码:

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上工作。

2 个答案:

答案 0 :(得分:0)

我们没有看到您的渲染代码。

你是否在某处编写了这个以生成纹理ID?

glGenTextures(1, &textureA);
glGenTextures(1, &textureB);
glGenTextures(1, &textureC);

你在渲染立方体时是否写过这个?

glBindTexture(GL_TEXTURE_2D, textureA);
// Draw the A face 

glBindTexture(GL_TEXTURE_2D, textureB);
// Draw the B face 

glBindTexture(GL_TEXTURE_2D, textureC);

//绘制C面

答案 1 :(得分:0)

你在做的是:

glGenTextures(1, textureA);
glGenTextures(1, textureB);
glGenTextures(1, textureC);

应该是:

glGenTextures(1, &textureA);
glGenTextures(1, &textureB);
glGenTextures(1, &textureC);

我认为问题发生是因为变量textureA,textureB和textureC没有得到有效值,因此glBindTexture调用什么都不做(虽然我认为你应该有分段错误)。