I'm having problems with the GL_LINES block... the lines in the sample below do not connect on the ends (although sometimes it randomly decides to connect a corner or two). Instead, the endpoints come within 1 pixel of one another (leaving a corner that is not fully squared; if that makes sense). It is a simple block to draw a solid 1-pixel rectangle.
glBegin(GL_LINES);
glColor3b(cr, cg, cb);
glVertex3i(pRect->left, pRect->top, 0);
glVertex3i(pRect->right, pRect->top, 0);
glVertex3i(pRect->right, pRect->top, 0);
glVertex3i(pRect->right, pRect->bottom, 0);
glVertex3i(pRect->right, pRect->bottom, 0);
glVertex3i(pRect->left, pRect->bottom, 0);
glVertex3i(pRect->left, pRect->bottom, 0);
glVertex3i(pRect->left, pRect->top, 0);
glEnd();
The sample below seems to correct the problem, giving me sharp, square corners; but I can't accept it because I don't know why it's acting this way...
glBegin(GL_LINES);
glColor3b(cr, cg, cb);
glVertex3i(pRect->left, pRect->top, 0);
glVertex3i(pRect->right + 1, pRect->top, 0);
glVertex3i(pRect->right, pRect->top, 0);
glVertex3i(pRect->right, pRect->bottom + 1, 0);
glVertex3i(pRect->right, pRect->bottom, 0);
glVertex3i(pRect->left - 1, pRect->bottom, 0);
glVertex3i(pRect->left, pRect->bottom, 0);
glVertex3i(pRect->left, pRect->top - 1, 0);
glEnd();
Any OpenGL programmers out there that can help, I would appreciate it :)
The picture is a zoomed-in view of a screenshot. As you can see, the top left corner is not connected. The top right corner is. Not see are the bottom left and right, which are not connected.
The viewport is setup to a 1 to 1 pixel per coordinate.
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, __nRendererWidth, __nRendererHeight, 0, -1, 100);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glEnable (GL_TEXTURE_2D);
Turns out pixel centers are on half-integer boundaries per these docs. You can change this by adding
layout(pixel_center_integer) in vec4 gl_FragCoord;
to your fragment shader but it's not advised. It didn't work for me anyway (just moved the missing pixel to a different corner).
What worked instead was just to add 0.5 in my vertex shader, after the model transform but before the projection transform. i.e.
#version 460 core
layout (location = 0) in vec2 inPos;
uniform mat4 model;
uniform mat4 projection;
void main() {
vec4 modelPos = model * vec4(inPos, 0.0, 1.0);
// Pixel centers are on half-integer boundaries. Add 0.5 for pixel-perfect corners.
modelPos.x += 0.5;
modelPos.y += 0.5;
gl_Position = projection * modelPos;
}
Credit to Yakov Galka for figuring this out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With