openGL中的粒子渲染问题

openGL中的粒子渲染问题,opengl,glut,Opengl,Glut,我开始使用glut在OpenGL中编写一个小粒子喷泉。我已经让粒子出现并在屏幕上反弹,就像我想的那样。所以当我第一次运行这个程序时,我得到了这个 大约2秒钟后,它就会变成这样 我试图搞乱代码,看看是什么导致了这个问题,但我一直无法解决它 这就是到目前为止我在节目中看到的 Int main() Init_particles()//初始化所有内容,在main中调用一次 空洞激活_粒子()//激活粒子 void Adjust_particle()//设置粒子的速度和反弹 void Render_

我开始使用glut在OpenGL中编写一个小粒子喷泉。我已经让粒子出现并在屏幕上反弹,就像我想的那样。所以当我第一次运行这个程序时,我得到了这个

大约2秒钟后,它就会变成这样

我试图搞乱代码,看看是什么导致了这个问题,但我一直无法解决它

这就是到目前为止我在节目中看到的

  • Int main()
  • Init_particles()//初始化所有内容,在main中调用一次
  • 空洞激活_粒子()//激活粒子
  • void Adjust_particle()//设置粒子的速度和反弹
  • void Render_particle()//渲染粒子,此函数在DrawGLScene()函数中调用

  • void idle()//调用Adjust_particles和Activate_particles,后跟glutPostRedisplay()的idle函数

以下是完整的代码:

// particle_fountain.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<stdlib.h>
#include <stdio.h>
#include<Windows.h>
#include <time.h>
#include <GL\glut.h>
#include<GL\GLU.h>

#define MAX_PARTICLES 100
#define MAX_BOUNCE_COUNT 10
#define MAX_PARTICLE_AGE 50

//Colours
float R = 0.8f;
float G = 0.2f;
float B = 0.0f;
float cR = 0.001f;
float cG = 0.002f;
float cB = 0.003f;
float Size = 0.01f; //size for points
GLuint txParticle;
GLuint txPlane;

struct PARTICLE {
    float X,Y,Z; // Current position
    float sX,sY,sZ; // Current Speed/Movement
    float tX,tY,tZ; // Target Speed/Movement
    float R,B,G; // Particle Colour
    bool Active; // Is particle Active
    int Age; // Age of the particle
    int MaxAge; // Maximum Age before particle dies
    int BounceCount;
} Particles[MAX_PARTICLES];

void Init_Particles();
void Activate_Particles();
void Adjust_Particles();
void Render_Particles();
bool LoadBitmapTexture(char * FileName, GLuint &texid);
void idle();
void DrawGLscene();
void Reshape(GLsizei w, GLsizei h);

int main(int argc, char** argv){
    glutInit(&argc,argv);
    glutCreateWindow("Particle fountain");

    Init_Particles();
    glTranslatef(0.0f, -0.7f, 0.0f);
    glutDisplayFunc(Render_Particles);
    glutIdleFunc(idle);
    glutMainLoop();

}

void idle(){
    Activate_Particles();
    Adjust_Particles();
    glutPostRedisplay();
}

void Init_Particles(){

    int p;
    srand((int)time(NULL));
    for(p=0; p<MAX_PARTICLES; p++){
        Particles[p].Active = FALSE;
        Particles[p].tX = 0.0f;
        Particles[p].tY = -0.1f;
        Particles[p].tZ = 0.0f;
    }
}

void Activate_Particles(){
    int p;
    for(p=0; p<MAX_PARTICLES; p++){
        if(!Particles[p].Active){
            // Start the particle at 0,0,0 origin
            Particles[p].X = 0.0f;
            Particles[p].Y = 0.0f;
            Particles[p].Z = 0.0f;
            // The following lines set a random speed value
            Particles[p].sX = (((float)((rand() % 100) + 1)) /
                1000.0f) - 0.05f;
            Particles[p].sY = (((float)((rand() % 100) + 50)) /
                500.0f);
            Particles[p].sZ = (((float)((rand() % 100) + 1)) /
                1000.0f) - 0.05f;
            // We also activate the particle
            Particles[p].Active = true;
            // Set it's Age to zero
            Particles[p].Age = 0;
            // We also assign a max age to the particles
            Particles[p].MaxAge = MAX_PARTICLE_AGE;
            // We Also reset the bouncecount to zero
            Particles[p].BounceCount = 0;
            return;
        }
}
}

void Adjust_Particles(){
    int p;
    for(p=0; p<MAX_PARTICLES; p++){
        // We move the speed towards the target speed by 1/20 (5%)
        Particles[p].sX+= (Particles[p].tX - Particles[p].sX) / 20.0f;
        Particles[p].sY+= (Particles[p].tY - Particles[p].sY) / 20.0f;
        Particles[p].sZ+= (Particles[p].tZ - Particles[p].sZ) / 20.0f;
        // Then we adjust the position of
        // the particle by the new speed
        Particles[p].X+= Particles[p].sX;
        Particles[p].Y+= Particles[p].sY;
        Particles[p].Z+= Particles[p].sZ;
        // Now for the bounce code.
        if(Particles[p].Y < 0.0f){
            Particles[p].Y = 0.0f;
            Particles[p].sY = -Particles[p].sY;
            Particles[p].BounceCount++;
            if(Particles[p].BounceCount > MAX_BOUNCE_COUNT){
                Particles[p].Active = FALSE;
            }
        }
        // And finally the age check
        Particles[p].Age++;
        if(Particles[p].Age > Particles[p].MaxAge){
            Particles[p].Active = FALSE;
        }
    }

}

void Render_Particles(){
    int p;
    glBegin(GL_POINTS);
    for(p=0; p<MAX_PARTICLES; p++){
        if(Particles[p].Active){
            glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
            glVertex3f(Particles[p].X,
                Particles[p].Y,
                Particles[p].Z);
        }
    }
    glEnd();
    glFlush();

}
//particle_fountain.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括
#包括
#包括
#包括
#包括
#包括
#定义最大粒子数100
#定义最大反弹次数10
#定义最大粒子年龄50
//颜色
浮子R=0.8f;
浮球G=0.2f;
浮球B=0.0f;
浮子cR=0.001f;
浮子重心=0.002f;
浮子cB=0.003f;
浮子尺寸=0.01f//点数大小
胶合物;
胶合txPlane;
结构粒子{
浮动X,Y,Z;//当前位置
浮动sX,sY,sZ;//当前速度/移动
浮动tX,tY,tZ;//目标速度/移动
浮点数R,B,G;//粒子颜色
bool-Active;//粒子是否处于活动状态
int Age;//粒子的年龄
int MaxAge;//粒子死亡前的最大年龄
整数弹跳计数;
}粒子[最大粒子数];
void Init_粒子();
空洞激活_粒子();
空隙调整_粒子();
void Render_粒子();
bool LoadBitmapTexture(char*文件名、GLuint和texid);
无效空闲();
void DrawGLscene();
空洞重塑(GLsizei w,GLsizei h);
int main(int argc,字符**argv){
glutInit(&argc,argv);
玻璃窗(“粒子喷泉”);
Init_粒子();
glTranslatef(0.0f,-0.7f,0.0f);
glutDisplayFunc(渲染粒子);
glutIdleFunc(空闲);
glutMainLoop();
}
无效空闲(){
激活_粒子();
调整_粒子();
再发现();
}
void Init_粒子(){
INTP;
srand((int)time(NULL));

对于(p=0;p在绘图前添加了16ms刷新和
glClear()

// g++ main.cpp -o main -lglut -lGL
#include <GL/glut.h>

#define MAX_PARTICLES 100
#define MAX_BOUNCE_COUNT 10
#define MAX_PARTICLE_AGE 50

struct PARTICLE 
{
    float X,Y,Z; // Current position
    float sX,sY,sZ; // Current Speed/Movement
    float tX,tY,tZ; // Target Speed/Movement
    float R,B,G; // Particle Colour
    bool Active; // Is particle Active
    int Age; // Age of the particle
    int MaxAge; // Maximum Age before particle dies
    int BounceCount;
} Particles[MAX_PARTICLES];

void Init_Particles()
{
    srand(0);
    for(unsigned int p=0; p<MAX_PARTICLES; p++)
    {
        Particles[p].Active = false;
        Particles[p].tX = 0.0f;
        Particles[p].tY = -0.1f;
        Particles[p].tZ = 0.0f;
    }
}

void Activate_Particles()
{
    for(unsigned int p=0; p<MAX_PARTICLES; p++)
    {
        if(!Particles[p].Active)
        {
            // Start the particle at 0,0,0 origin
            Particles[p].X = 0.0f;
            Particles[p].Y = 0.0f;
            Particles[p].Z = 0.0f;
            // The following lines set a random speed value
            Particles[p].sX = (((float)((rand() % 100) + 1)) / 1000.0f) - 0.05f;
            Particles[p].sY = (((float)((rand() % 100) + 50)) / 500.0f);
            Particles[p].sZ = (((float)((rand() % 100) + 1)) / 1000.0f) - 0.05f;
            // We also activate the particle
            Particles[p].Active = true;
            // Set it's Age to zero
            Particles[p].Age = 0;
            // We also assign a max age to the particles
            Particles[p].MaxAge = MAX_PARTICLE_AGE;
            // We Also reset the bouncecount to zero
            Particles[p].BounceCount = 0;
            return;
        }
    }
}

void Adjust_Particles()
{
    for(unsigned int p=0; p<MAX_PARTICLES; p++)
    {
        // We move the speed towards the target speed by 1/20 (5%)
        Particles[p].sX+= (Particles[p].tX - Particles[p].sX) / 20.0f;
        Particles[p].sY+= (Particles[p].tY - Particles[p].sY) / 20.0f;
        Particles[p].sZ+= (Particles[p].tZ - Particles[p].sZ) / 20.0f;
        // Then we adjust the position of
        // the particle by the new speed
        Particles[p].X+= Particles[p].sX;
        Particles[p].Y+= Particles[p].sY;
        Particles[p].Z+= Particles[p].sZ;
        // Now for the bounce code.
        if(Particles[p].Y < 0.0f)
        {
            Particles[p].Y = 0.0f;
            Particles[p].sY = -Particles[p].sY;
            Particles[p].BounceCount++;
            if(Particles[p].BounceCount > MAX_BOUNCE_COUNT)
            {
                Particles[p].Active = false;
            }
        }
        // And finally the age check
        Particles[p].Age++;
        if(Particles[p].Age > Particles[p].MaxAge)
        {
            Particles[p].Active = false;
        }
    }

}

void Render_Particles()
{
    Activate_Particles();
    Adjust_Particles();

    glClear( GL_COLOR_BUFFER_BIT );

    glBegin(GL_POINTS);
    for(unsigned int p=0; p<MAX_PARTICLES; p++)
    {
        if(Particles[p].Active)
        {
            glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
            glVertex3f(Particles[p].X, Particles[p].Y, Particles[p].Z);
        }
    }
    glEnd();

    glutSwapBuffers();
}

void timer(int extra)
{
    glutPostRedisplay();
    glutTimerFunc(16, timer, 0);
}

int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode( GLUT_RGBA| GLUT_DOUBLE );
    glutInitWindowSize( 640, 480 );    
    glutCreateWindow("Particle fountain");

    Init_Particles();
    glTranslatef(0.0f, -0.7f, 0.0f);
    glutDisplayFunc(Render_Particles);
    glutTimerFunc(0, timer, 0);
    glutMainLoop();
}
//g++main.cpp-o main-lglut-lGL
#包括
#定义最大粒子数100
#定义最大反弹次数10
#定义最大粒子年龄50
结构粒子
{
浮动X,Y,Z;//当前位置
浮动sX,sY,sZ;//当前速度/移动
浮动tX,tY,tZ;//目标速度/移动
浮点数R,B,G;//粒子颜色
bool-Active;//粒子是否处于活动状态
int Age;//粒子的年龄
int MaxAge;//粒子死亡前的最大年龄
整数弹跳计数;
}粒子[最大粒子数];
void Init_粒子()
{
srand(0);
对于(unsigned int p=0;pThank you:),我以前尝试过使用
glClear()
,但是粒子移动得太快了,我什么都看不见!现在一切都好了!