Jump to content
  • Advertisement
Sign in to follow this  
Walking_Target

OpenGL Maybe I'm just an Idiot

This topic is 3734 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
You need to add 0.375 to the vertex coordinates so that it hits pixel centers, IIRC.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!