0
\$\begingroup\$

I'm trying to set up an orthographic projection so I can draw a rectangle using screen coordinates, but even if I draw a rectangle using the width/height of the window, it only fills a quarter of the window. Am I doing something wrong with glOrtho?

Relevant code: (width/height refers to window dimensions)

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, width, height, 0.0f, -1.0f, 1.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glTranslatef(0.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glVertex2i(0, 0);
glVertex2i(width, 0);
glVertex2i(width, height);
glVertex2i(0, height);
glEnd();

Output looks like this:

Screenshot of OpenGL window

I am targeting OpenGL 2.1 if that matters.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I think I figured this out. I am calling glViewport with the window dimensions, but I believe it should be the framebuffer dimensions. In my case, the framebuffer was four times the size of the window.

I corrected the code as follows: (I'm using GLFW to manage windows)

glfwGetFramebufferSize(glfwWindow, &backingWidth, &backingHeight);
glViewport(0, 0, backingWidth, backingHeight);
\$\endgroup\$
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .