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: Flickering image

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:

Scene::drawCircle(GLuint index){

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

    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);

If it helps, this is the reshape method:

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

     glViewport(0, 0, width, height);


     gluOrtho2D(xmin, xmax, ymin, ymax);
     std::cout << xmin << " " << xmax << " " << ymin << " " << ymax << std::endl;
  • \$\begingroup\$ Perhaps a video representing the problem will help. \$\endgroup\$ – concept3d Jun 11 '14 at 10:48
  • \$\begingroup\$ I can't seem to be able to get that flickering recorded..will still try though \$\endgroup\$ – Daniel Jun 11 '14 at 11:22
  • 1
    \$\begingroup\$ Are you using double buffering? \$\endgroup\$ – concept3d Jun 11 '14 at 11:29
  • \$\begingroup\$ 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. \$\endgroup\$ – Daniel Jun 11 '14 at 11:54
  • 1
    \$\begingroup\$ I think we need more info. \$\endgroup\$ – 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.

  • \$\begingroup\$ Yes, it makes sense. I will try that out and give feedback \$\endgroup\$ – Daniel Jun 11 '14 at 17:06
  • \$\begingroup\$ You were right, I was missing some OpenGL knowledge \$\endgroup\$ – 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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.