3

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)

7
  • Could you post your entire code? We don't know what the particle[] array is. I can guess that the particles aren't within the default projection. Commented Mar 10, 2011 at 4:25
  • it doesn't matter, the rest of the code is tested and works ok, the code is a few thousand lines of code and I can't see how this would help. Focus o the part that X(particle[i]) is the x-coordinate of the particle and Y(particle[i]) the y-coordinate, this is the only thing relevant to the opengl part. Commented Mar 10, 2011 at 4:28
  • 1
    Are you calling glOrtho anywhere? Commented Mar 10, 2011 at 7:04
  • 2
    I would change glClearColor to see if it does anything, then try to render one point in the center to see if my modelview-projection matrices are correct. Commented Mar 10, 2011 at 7:09
  • @Jim Buck, no I have posted every OpenGL function I'm calling. Commented Mar 10, 2011 at 9:36

1 Answer 1

2

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); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.