Maybe I'm just an Idiot

Started by
6 comments, last by steven katic 15 years, 9 months ago
I've been trying to get my world coordinates to match my screen coordinates to no success. I'm at a loss why I can't draw pixel for pixel like I would in MSPaint, etc. --Quick and dirty overview of the code
#define _STDCALL_SUPPORTED  //Required for
#define _M_IX86             //glut32 and mingw
#include <gl/glut.h>

void Reshape(int width, int height)
{
    if (height == 0)
        height = 1;

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 800, 600, 0, 0, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void Display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();

    glColor3f(0, 0, 0);
    glBegin(GL_POINTS);
        glVertex2i(0,0);
        glVertex2i(0,600);
        glVertex2i(800,600);
        glVertex2i(800,0);
    glEnd();
    glFlush();
}

int main (int argc, char* argv[])
{
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(800, 600);
    glutCreateWindow("OpenGL");
    glutReshapeFunc(Reshape);
    glutDisplayFunc(Display);

    glClearColor(1.0, 1.0, 1.0, 1.0);
    glutMainLoop();
    return(0);
}
So this should show single pixels in the four corners if I understand correctly. However, it doesn't show anything. I can make the top-left pixel show up by moving the vertex down one pixel. If I draw using GL_QUADS, the square is a pixel smaller than I expect. For example, a square drawn from (50,50)x(55,55) appears at (50,50)x(54,54), but that could be WAI. Something that's really odd is when I draw using GL_LINE_LOOP. I get a square that's missing its top-right corner and is a pixel too high. I'm using the CodeBlocks IDE, which uses the MinGW compiler. I realize 2D as simple as this can be a sore subject, so I'll write a nice guide for my UI system once I figure this out and everything is rendering pretty-like. ~Walking_Target
Musician, programmer, writer, aspiring artist - Tell me about your project! =D
Advertisement
You need to add 0.375 to the vertex coordinates so that it hits pixel centers, IIRC.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
ehh, maybe increase the point size to glPointSize(10); Maybe the glFlush isn't needed? Maybe the last parameter of glClearColor should be 0. I don't know otherwise, looks like the background is white and the dots should be there in black.
hmmm...have you given:

glOrtho(0, 800, 600, 0, -1, 1); a go. Then even:

gluOrtho2D(0, 800, 600, 0);

And then even something a little more conventional:

glOrtho(0, 800, 0, 600, -1, 1);
or
gluOrtho2D(0, 800, 0, 600);

If none of those make a difference, then....Maybe I'm just an idiot too.
Quote:Original post by steven katic
hmmm...have you given:

glOrtho(0, 800, 600, 0, -1, 1); a go. Then even:

gluOrtho2D(0, 800, 600, 0);

And then even something a little more conventional:

glOrtho(0, 800, 0, 600, -1, 1);
or
gluOrtho2D(0, 800, 0, 600);

If none of those make a difference, then....Maybe I'm just an idiot too.
Yea I've given all of those changes a shot. It is partially fixed by shifting the pixels' vertices by adding to it a little bit. The problem however is that this is not really versatile. If the user changes the window dimensions then it's messed up again.

I think that the problem is actually in mapping the pixels in the world coordinates to the physical pixels.
Musician, programmer, writer, aspiring artist - Tell me about your project! =D
AFAIK, that's an issue of the rasterizer implementation. The only solution I know is what V-man suggests: add 0.375 to the vertex positions. If the the user resizes the window you should resize your ortho projection matrix as well, since without doing so you'd most certainly NOT get a 1:1 world coord to pixel relation.
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
Quote:Original post by Walking_Target
I've been trying to get my world coordinates to match my screen coordinates to no success. I'm at a loss why I can't draw pixel for pixel like I would

glVertex2i(0,600);




Shouldn't it be 0,599? You are drawing out of screen.
or glVertex2i(0,600-0.375); ?

wish I could get a user rating of 0. it's a nice round number(if it is a number).How baaaad do I have to be.

This topic is closed to new replies.

Advertisement