0

I am trying to render an image by drawing it pixel for pixel (yes I know this isn't the most efficient but its more for learning purposes) and I got the weird problem that it draws white lines like this: the rendering

I use glut and visual studio 2013. This is my glutinit:

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_ALPHA); // Double buffering, RGB format
glutInitWindowSize(width, heigth);
glutInitWindowPosition(0, 0);
glutCreateWindow("Test Plaatje");
glClearColor(1.0, 1.0, 1.0, 1.0);

glColor3f(1.0, 1.0, 1.0);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 1024, 0.0, 500);
glMatrixMode(GL_MODELVIEW);

and this is my rendering code with some variables explained:

...
std::vector<std::vector<RGB>> _map = std::vector<std::vector<RGB>>();
...
struct RGB
{
    int R;
    int G;
    int B;
};
...

void Map::draw()
{
    glBegin(GL_POINTS);

    for (float x = 0; x < _w; x++)
    {
        for (float y = 0; y < _h; y++)
        {
            RGB pixel = _map[y][x];
            glColor3ub(pixel.R, pixel.G, pixel.B);
            glVertex2f(_p[0] + x, _p[1] + y);
        }
    }

    glEnd();
}

I already tried shifting the pixels with some decimal value but that didn't work. I verified that the _map is correct by dumping the pixel values to file. for reference this is what it should render:

the source

I have no idea how to fix this. anyone have any ideas?

WillieWonka
  • 115
  • 1
  • 11
  • 1
    Try this: `glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);` You probably want to enable blending beforehand: `glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_POINT_SMOOTH);` but I'm not sure which of those are necessary and which are not. Also try to increase point size `glPointSize(2.0)`. – n. m. could be an AI Dec 30 '14 at 11:53
  • @n.m. i used glEnable(GL_BLEND); glEnable(GL_POINT_SMOOTH); glHint(GL_POINT_SMOOTH_HINT, GL_NICEST); and it worked! thankyou! – WillieWonka Dec 30 '14 at 11:59
  • 1
    Assuming both your window and you image are exactly 1024x500 pixels (and the modelview matrix left at identity), your code should work if `_p[0]` and `_p[1]` are set to 0.5, without having to use `GL_POINT_SMOOTH`. – derhass Dec 30 '14 at 13:39
  • @derhass yes but that varies with the configuration. the p makes sure that there is a top and side bar in the window. – WillieWonka Dec 30 '14 at 14:29
  • @user2988415: can you elaborate on that? The thing is, you set up your projection matrix so that that the pixel centers lie at ".5", and the pixel borders are exactly at the integers. Ideally, you should always draw the points at pixel centers. But whatever you put in `_p` and in the modelview matrix, will also affect this. – derhass Dec 30 '14 at 14:32
  • Why not upload the map data to a texture, bind it, and render a textured quad? – Colonel Thirty Two Dec 30 '14 at 17:19

2 Answers2

0

at n.m. tip i added glEnable(GL_BLEND); glEnable(GL_POINT_SMOOTH); glHint(GL_POINT_SMOOTH_HINT, GL_NICEST); at the end of my init and it worked!

WillieWonka
  • 115
  • 1
  • 11
0

You can solve this floating point error in many ways:

  • use glPointSize(2.0)
  • use blend and point smooth
  • shift vertex coords to project in pixels centers +.5,+.5
  • use glRect instead of begin/vertex/end

Personalny i'd preffer vertex shift. But if you plan to zoom your image, better approach is glRect.

If you're looking for more serious solution, you should consider uploadind your pixels to texture and draw it with single quad. Also consider using glDrawPixels.

Anonymous
  • 2,122
  • 19
  • 26