OpenGL glRectf()没有显示

时间:2015-11-02 00:10:18

标签: c++ opengl glut

我的窗口中没有显示glRectf():

#include <GL/gl.h>
#include <GL/glut.h>

void renderScene(){
    glClearColor(0.0f,1.0f,1.0f,1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0f,0.0f,0.0f);
    glRectf(225.0f,150.0f,150.0f,100.0f);
    glFlush();
}

int main(int argc, char*argv[]){
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
    glutInitWindowSize(450, 350);
    glutCreateWindow("My first OpenGL Program...took forever");
    glutDisplayFunc(renderScene);
    glutMainLoop();
    return 0;
}

1 个答案:

答案 0 :(得分:2)

(225.0f,150.0f)远不及身份平凡。

提供一些合适的变换矩阵,这样你的矩形就不会被剪裁:

#include <GL/glut.h>

void renderScene()
{
    glClearColor(0.0f,1.0f,1.0f,1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho( -300, 300, -300, 300, -1, 1 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    glColor3f(1.0f,0.0f,0.0f);
    glRectf(225.0f,150.0f,150.0f,100.0f);

    glFlush();
}

int main(int argc, char*argv[])
{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
    glutInitWindowSize(450, 350);
    glutCreateWindow("My first OpenGL Program...took forever");
    glutDisplayFunc(renderScene);
    glutMainLoop();
    return 0;
}