纹理显示绿色色调

时间:2013-10-23 22:54:02

标签: opengl textures

我正在学习如何在OpenGL中应用纹理。我有一个相当简单的立方体,我试图应用纹理使它看起来像一块木板。当我应用纹理时,它会显示绿色调。我可以应用一些看起来很好的其他纹理,所以我无法弄清楚这个有什么问题。我从我下载的jpg创建了纹理。当我在预览中查看时,bmp文件看起来很好(我在Mac上)。我将附上原始位图的截图以及OpenGL渲染时的外观截图。

enter image description here enter image description here

我正在使用的纹理加载代码here

unsigned int texture[2];  //  Texture names

// define the board
float square_edge       =    1;
float border            =  0.5;
float board_thickness   = 0.25;
float board_corner      = 4 * square_edge + border;
float board_width       = 2 * board_corner;

GLfloat board_vertices[8][3] = {
    {-board_corner,  board_corner, 0.0},
    {-board_corner, -board_corner, 0.0},
    { board_corner, -board_corner, 0.0},
    { board_corner,  board_corner, 0.0},
    {-board_corner,  board_corner, -board_thickness},
    {-board_corner, -board_corner, -board_thickness},
    { board_corner, -board_corner, -board_thickness},
    { board_corner,  board_corner, -board_thickness}
};

void polygon(int a, int b, int c, int d) {
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0, 0.0);  glVertex3fv(board_vertices[a]);
    glTexCoord2f(1.0, 0.0);  glVertex3fv(board_vertices[b]);
    glTexCoord2f(1.0, 1.0);  glVertex3fv(board_vertices[c]);
    glTexCoord2f(0.0, 1.0);  glVertex3fv(board_vertices[d]);
    glEnd();
}

void draw_board() {

    glPushMatrix();

    glRotatef(rotx, 1.0, 0.0, 0.0);
    glScalef(1/board_corner, 1/board_corner, 1/board_corner);

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, board_vertices);

    glBindTexture(GL_TEXTURE_2D, texture[1]);

    glColor3f(1.0, 1.0, 1.0); //color of the border, sides, bottom of board

    // draw the board
    polygon(0,3,2,1);
    polygon(2,3,7,6);
    polygon(0,4,7,3);
    polygon(1,2,6,5);
    polygon(4,5,6,7);
    polygon(0,1,5,4);

    glPopMatrix();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    glLoadIdentity();

    double Ex = -2*dim*Sin(th)*Cos(ph);
    double Ey = +2*dim        *Sin(ph);
    double Ez = +2*dim*Cos(th)*Cos(ph);
    gluLookAt(Ex,Ey,Ez , 0,0,0 , 0,Cos(ph),0);

    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

    draw_board();

    glFlush();
    glutSwapBuffers();    
}

2 个答案:

答案 0 :(得分:0)

问题在于bmp图像本身。正如我所提到的,我是通过Gimp将其转换为jpg文件创建的。不知怎的,这种转换出了问题。当我从命令行运行'file wood.bmp'时,我的响应是'wood.bmp:data'。它应该显示'wood.bmp:PC位图,Windows 3.x格式,128 x 128 x 24“或类似的东西。我使用好的'MS Paint进行了转换,问题就消失了。

我正试图在Gimp中找到如何做到这一点,但是现在这一切都有效。感谢所有的建议!

答案 1 :(得分:0)

使用gimp导出.bmp图像时,请确保在“兼容性选项”下拉列表下方选择“不写入颜色空间信息”选项。使用opengl渲染时,以这种方式导出的.bmp图像不会显示绿色色调。