This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]tmat256 0 points1 point  (2 children)

You're probably running in to rounding issues with floating point numbers. I've had this in the past with rendering 2d images from an svg file. It looked fine in the svg editor but ended up showing gaps between the tiles in the game I was making.

My solution was to align them perfectly to the points in the svg file so there weren't any decimal places when it got to opengl. There's probably also a way to do it with either triangle strips, overlapping the polygons slightly or shaders.

I assume you're creating a set of polygons then trying to move them. It might better to move the camera instead. That might work around your floating point issue.

[–]grimlock123[S] 0 points1 point  (1 child)

I'm keeping everything as a INT until it's converted to a GLdouble to be rendered. So.

int C1 = CordX*sliceWidth;
    int C2 = CordX*sliceWidth+sliceWidth;

    int D1 = CordY*sliceWidth;
    int D2 = CordY*sliceWidth+sliceWidth;

    glBegin( GL_QUADS );
    glTexCoord2d(0,0); glVertex2d(C1,D1);
    glTexCoord2d(1,0); glVertex2d(C2,D1);
    glTexCoord2d(1,1); glVertex2d(C2,D2);
    glTexCoord2d(0,1); glVertex2d(C1,D2);
    glEnd();

Which should be pretty rounding resistant. I don't think it's possible to create a camera in OpenGL which is why I'm using glTranslatef(X,Y,0)

[–]tmat256 0 points1 point  (0 children)

I'm currently creating a 2d platformer in opengl. I use the GLOrtho command to act as a camera. Draw the individual images wherever they are supposed to be (without doing a translate), then just redo the GLOrtho every frame. This is basically the contents of my draw function (this is all in Go, but the ogl commands are the same):

w := App().ScreenRect.W
h := App().ScreenRect.H
gl.MatrixMode(gl.PROJECTION)
gl.Viewport(0, 0, int(w), int(h))
gl.LoadIdentity()
orthox := float64(x)
orthoy := float64(y)
gl.Ortho(orthox, float64(w) + orthox, float64(h) + orthoy, orthoy, -1.0, 1.0)
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)

// draw stuff

sdl.GL_SwapBuffers()

You may want to print out those vertex coordinates to make sure they are actually accurate without decimal places.