OpenGL粒子系统

发布于 2024-10-21 11:12:54 字数 1066 浏览 2 评论 0原文

我正在尝试使用 OpenGl 模拟粒子系统,但我无法让它工作,这就是我到目前为止所拥有的:

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

}

几秒钟后我得到一个黑色窗口(该窗口之前只有标题栏) 。我哪里出错了?

LE:每个粒子的x和y坐标都在区间(0,size)内

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软糖 2024-10-28 11:12:54

尝试在代码中进行这些更改:

  • 将 Main 函数移到文件末尾
  • glPoinSize 调用属于 Display 函数
  • ,然后您应该提供一个函数来处理窗口大小的调整 glutReshapeFunc(reshape),类似这样的
    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 是从调用的glutSwapBuffers 函数,因此您不需要它
  • 插入此代码(在 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); 

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); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文