使用gluOrtho2D在OpenGL中绘制纹理

时间:2012-05-20 07:00:34

标签: opengl graphics

我正在使用以下代码来显示原始图像文件,但它没有显示任何内容:

GLuint texture;
GLuint LoadTexture( const char * filename, int width, int height);
void FreeTexture(GLuint texture);

void display()
{
    glClear (GL_COLOR_BUFFER_BIT);
    glBindTexture( GL_TEXTURE_2D, texture ); 
    glBegin (GL_QUADS); //begin drawing our quads
    glTexCoord2d(0.0, 0.0);
    glVertex3f(0.0, 0.0, 0.0); //with our vertices we have to assign a texcoord

    glTexCoord2d(1.0, 0.0);
    glVertex3f(1.0, 0.0, 0.0); //so that our texture has some points to draw to

    glTexCoord2d(1.0, 1.0);
    glVertex3f(1.0, 1.0, 0.0);

    glTexCoord2d(0.0, 1.0);
    glVertex3f(0.0, 1.0, 0.0);
    glEnd();
    glPopMatrix();
    glFlush();
}

void init()
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, 640, 0, 480);
    glEnable( GL_TEXTURE_2D );
}

int main(int argc, char **argv)
{
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE);
    glutInitWindowSize (640, 480);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("A basic OpenGL Window");
    glutDisplayFunc (display);
    init();
    texture = LoadTexture("texture.raw", 256, 256);
    glutMainLoop ();
    FreeTexture(texture);

    return 0;
}

我得到的所有输出都是空屏幕。可能有什么不对?

1 个答案:

答案 0 :(得分:2)

在你的gluOrtho2d调用之后,你的视图将从0,0映射到640,480。您正在绘制一个四边形,其值从0到1,1,这将是左下角屏幕上单个像素的大小。

使用全屏(glVertex(640,480);),或使用gluOrtho2d指定1x1视口(gluOrtho2d(0,1,0,1);