# OpenGL flickerinng near the edges

I am trying to simulate particles moving around the scene with OpenCL for computation and OpenGL for rendering with GLUT. There is no OpenCL-OpenGL interop yet, so the drawing is done in the older fixed pipeline way. Whenever circles get close to the edges, they start to flicker. The drawing should draw a part of the circle on the top of the scene and a part on the bottom.

The effect is the following:

The balls you see on the bottom should be one part on the bottom and one part on the top. Wrapping around the scene, so to say, but they constantly flicker.

The code for drawing them is:

void
Scene::drawCircle(GLuint index){
glMatrixMode(GL_MODELVIEW);

glTranslatef(pos.at(2*index),pos.at(2*index+1),  0.0f);

glBegin(GL_TRIANGLE_FAN);
GLfloat incr = (2.0 * M_PI) / (GLfloat) slices;
glColor3f(0.8f, 0.255f, 0.26f);
glVertex2f(0.0f, 0.0f);
glColor3f(1.0f, 0.0f, 0.0f);
for(GLint i = 0; i <=slices; ++i){
GLfloat x = radius * sin((GLfloat) i * incr);
GLfloat y = radius * cos((GLfloat) i * incr);

glVertex2f(x, y);
}
glEnd();
}


If it helps, this is the reshape method:

 void
Scene::reshape(GLint width, GLint height){
if(0 == height) height = 1; //Prevent division by zero

glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);

gluOrtho2D(xmin, xmax, ymin, ymax);
std::cout << xmin << " " << xmax << " " << ymin << " " << ymax << std::endl;
}

• Perhaps a video representing the problem will help. – concept3d Jun 11 '14 at 10:48
• I can't seem to be able to get that flickering recorded..will still try though – Daniel Jun 11 '14 at 11:22
• Are you using double buffering? – concept3d Jun 11 '14 at 11:29
• Yes, it is used. Investigating some other possibility right now and will get back to you. I may use a different particle size in calculation and rendering, which would explain things. Not why the flickering occurs, but then the particle will just get teleported and not transition smoothly. – Daniel Jun 11 '14 at 11:54
• I think we need more info. – concept3d Jun 11 '14 at 12:21

Regardless of other glitches you have, in order to draw a shape in a wrapping world, you need to duplicate it on both sides of a wrapping edge if it's intersecting that edge. You should perform bounding shape tests to determine if such an overlap exists.

This means that for a particular shape, you may need to draw it up to four times if it straddles a corner.

There is no base knowledge of wrapping worlds in OpenGL but you may be able to use geometry shaders to synthesize additional geometry.

• Yes, it makes sense. I will try that out and give feedback – Daniel Jun 11 '14 at 17:06
• You were right, I was missing some OpenGL knowledge – Daniel Jun 25 '14 at 8:28

What makes you think that GL should draw part of your object at the top and part at the bottom if its position spans the edge? What will actually happen is that GL will clip the vertices.

For some primitive types (e.g. points) this by itself will produce flickering since a single infinitely small point is used to clip a primitive that actually has an area (e.g. if the center of the point is offscreen, the entire thing disappears). You are not using point primitives here; this was just a simple example of clipping in action.

The only way to produce the wrapping behavior you want is if you determine which of your objects straddles the edges of the viewport. This can be done generally if you transform them into clip-space (projection x view x model x vertex) and test for objects with coordinates > w or < -w. Any objects with offending coordinates must be drawn twice, the second time you draw them you need to add/subtract 2 * w from the clip-space X or Y coordinates (you can do this very easily with a vertex shader and a second pass). You probably do not want to wrap objects that are behind your near plane or beyond your far plane, but you could do that the same way.