Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 have so far:

#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 (the window has just the title bar before that). Where do I go wrong?

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

like image 679
w00t Avatar asked Feb 19 '26 11:02

w00t


1 Answers

Try to make these changes in your code:

  • move the Main function at the end of the file
  • glPoinSize call belongs to the Display function
  • then you should provide a function to handle resizing of the window glutReshapeFunc(reshape), 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 glutSwapBuffers function so you don't need it there
  • insert this code (after glutCreateWindow call) to set the initial position for the projection
    glClearColor(0.2, 0.0, 0.0, 0.0);    
    glMatrixMode(GL_PROJECTION);  
    glLoadIdentity();  
    glOrtho(0.0, 10, 0.0, 10, -1.0, 1.0); 
like image 172
Machta Avatar answered Feb 21 '26 01:02

Machta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!