OpenGL Particle System

I'm trying to simulate a particle system using OpenGl, but I can't get it to work, this is what I still have:

#include <GL/glut.h>
int main (int argc, char **argv){

  // data allocation, various non opengl stuff
  ............
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
  glutInitWindowPosition(100,100);
  glutInitWindowSize(size, size);
  glPointSize (4);
  glutCreateWindow("test gl");
  ............
  // initial state, not opengl
  ............
  glViewport(0,0,size,size);
  glutDisplayFunc(display);
  glutIdleFunc(compute);
  glutMainLoop();


}

void compute (void) {

 // change state not opengl

  glutPostRedisplay();

}

void display (void) {

  glClear(GL_COLOR_BUFFER_BIT);
  glBegin(GL_POINTS);

  for(i = 0; i<nparticles; i++) {

    // two types of particles
    if (TYPE(particle[i]) == 1) glColor3f(1,0,0);
      else glColor3f(0,0,1);

    glVertex2f(X(particle[i]),Y(particle[i]));

  }

  glEnd();
  glFlush();
  glutSwapBuffers();

}

I get a black window after a couple of seconds (before that, the window has only a title). Where am I mistaken?

LE: the x and y coordinates of each particle are in the range (0, size)

+3
source share
1 answer

Try making these changes to your code:

  • move the main function at the end of the file
  • glPoinSize call refers to the Display function
  • then you should provide a function to handle window resizing glutReshapeFunc (resize), something like this
    void reshape(int w, int h)
    {
        glViewport(0, 0, (GLsizei) w, (GLsizei) h);  
        glMatrixMode(GL_PROJECTION);  
        glLoadIdentity();  
        gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);  
    }
  • glFlush is called from the glutSwapBuffers function, so you don't need it
  • ( glutCreateWindow),
    glClearColor(0.2, 0.0, 0.0, 0.0);    
    glMatrixMode(GL_PROJECTION);  
    glLoadIdentity();  
    glOrtho(0.0, 10, 0.0, 10, -1.0, 1.0); 
+2

All Articles