glViewport大小不变

时间:2015-04-18 10:14:33

标签: opengl glut

我有两个视口,目前每个角都显示4个点。一个视口(左侧)有灰色角,另一个(右侧)有红色角。两个视口彼此相邻放置,红色视口有意变小。带红色边角的视口也在绘制GL_TRIANGLES。

你可以在这里看到它们: Viewports

三角形中的点应该是位于红色视口的左下角,左上角和右下角,但右下角非常短。

我认为第二个(红色)glViewport仍然使用来自前一个对象的测量值,可能是另一个视口。

我是否正确设置了第二个视口? 我希望第二个视口有100像素,而不是900像素,挤压成100像素。

这是我的代码。 我正在使用最新版本的过剩,目前仍在学习如何使用OpenGL /过剩。

// windows/pannels
int winWidth = 900; // window width
int winHeight = 600; // window height

int pannelToolsWidth = 100;
int pannelToolsHeight = winHeight;
int pannelToolsX = winWidth - pannelToolsWidth;
int pannelToolsY = 0;

int drawSpaceWidth = winWidth - pannelToolsWidth;
int drawSpaceHeight = winHeight;

// drawing
float red = 0.0f;
float green = 0.0f;
float blue = 0.0f;
float lineSize = 1.0f;
float pointSize = 1.0f;

// ...

// draws a pannel. Make sure to define a colour before calling.
void drawPannel(int x, int y, int width, int height)
{   
    glBegin(GL_TRIANGLES);
        glVertex2f(float(x), float(y));
        glVertex2f(float(x), float(height));
        glVertex2f(float(width), 0.0);
glEnd();
    //glBegin(GL_TRIANGLES);
    //  glVertex2f(, );
    //  glVertex2f(, );
    //  glVertex2f(, );
    //glEnd();
}

// Starts a new glViewport and visualises the bounderies with coloured points.
// scene: id of the viewport (viewports draw different things)
// x/y: viewports draw from the bottom left corner
void drawViewport(int x, int y, int width, int height, float red, float green, float blue, int scene)
{
    // draw viewport (colours used to visualise limits)
    glViewport(x, y, width, height);
    glColor3f(red, green, blue);
    glBegin(GL_POINTS);
        glVertex2f(0.0f, float(winHeight));
        glVertex2f(float(winWidth), float(winHeight));
        glVertex2f(0.0f, 0.0f);
        glVertex2f(float(winWidth), 0.0f);
    glEnd();

    // draw viewport content. text methods should be called last because the glRasterPos2i() effects the viewport
    switch (scene)
    {
        // add more cases for other viewports here.
    case 0:
        // colour
        // drawing
        // text
        break;
    case 1:
        glColor3f(0.8, 0.8, 0.7);
        drawPannel(0, 0, pannelToolsWidth, pannelToolsHeight);
        break;
    default:
        // do nothing 
        break;
    }
}

void init()
{
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f); //set clear background colour

    glPointSize(10.0);                     //set point size
    glColor3f(1.0f, 1.0f, 1.0f);          //set draw colour
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, winWidth, 0, winHeight);           //setup 2D projection
}

void collectScene()
{
    glClear(GL_COLOR_BUFFER_BIT);

    // viewport(s)
    drawViewport(0, 0, drawSpaceWidth, drawSpaceHeight, 0.5, 0.5, 0.5, 0); // paint area
    drawViewport(pannelToolsX, pannelToolsY, pannelToolsWidth, pannelToolsHeight, 1, 0, 0, 1); // GUI area

    glFlush();
    glutSwapBuffers();     //swap front and back buffers
}

//called when no event in queue
void idle()
{
    glutPostRedisplay();
}

int main()
{
    // create the window
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // double buffer with RGBA
    glutInitWindowSize(winWidth, winHeight);
    glutInitWindowPosition(100, 100); // from top left
    glutCreateWindow("Paint");
    init();
    glutDisplayFunc(collectScene);

    glutIdleFunc(idle);
    glutMainLoop();
    return 0;
}

谢谢

1 个答案:

答案 0 :(得分:1)

投影矩阵是相对于视口的,实际上它通常是在glViewport调用之后设置的。在你的情况下,它不会改变。