johnbakers
johnbakers

Reputation: 24750

simple call to glViewport causes blank screen

This has no glViewport and draws correctly:

glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0f, 0,0);
glBegin(GL_TRIANGLES);
{
    glVertex3f(  0.0,  1, 0.0);
    glVertex3f( -0.2, -0.3, 0.0);
    glVertex3f(  0.2, -0.3 ,0.0);

}

The coordinates of the vertices are all within -1 and 1, so I added a viewport which should contain them:

glViewport(-1,-1,2.0,2.0);

glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0f, 0,0);
glBegin(GL_TRIANGLES);
{
    glVertex3f(  0.0,  1, 0.0);
    glVertex3f( -0.2, -0.3, 0.0);
    glVertex3f(  0.2, -0.3 ,0.0);

}

As soon as I do this, the screen is blank. Without the viewport, visually the screen appears to be drawing in units such that the screen width and height are 2, as demonstrated by the demo triangle. I can also place something at -1,-1 and it is in the lower left corner, which is why my glViewport is also set that lower left coordinate. What is the obvious reason it is not displaying anything after the viewport?

Upvotes: 0

Views: 206

Answers (1)

BDL
BDL

Reputation: 22167

Seems you missunderstood what glViewport does: It defines to which pixel coordinates the visible NDC range ([-1, 1]) should be mapped. If you use [-1, -1, 2, 2], then the whole image is drawn from pixel -1 to pixel 1 in the window.

If you want to specify which area of your world-coordinate system is mapped to the visible NDC range, you should use an appropriate projection matrix.

Upvotes: 4

Related Questions