c++ - OpenGL 粒子,帮助控制方向

标签 c++ opengl particles

我正在尝试修改这个 Digiben 样本,以便获得从一个点(撞击点)产生并向上漂浮的粒子效果,有点像火的 Spark 。样本中的粒子在一个圆圈内旋转......我已经尝试删除余弦/正弦函数并将它们替换为具有增加 Y 值的普通 glTranslate 但我无法获得任何真实结果......任何人都可以指出我应该大致在哪里添加/修改此代码中的翻译以获得该结果?

void ParticleMgr::init(){
    tex.Load("part.bmp");
    GLfloat angle = 0; // A particle's angle
    GLfloat speed = 0; // A particle's speed
    // Create all the particles
    for(int i = 0; i < P_MAX; i++)
    {
        speed = float(rand()%50 + 450); // Make a random speed
        // Init the particle with a random speed
        InitParticle(particle[i],speed,angle);
        angle += 360 / (float)P_MAX; // Increment the angle so when all the particles are
                                     // initialized they will be equally positioned in a
                                     // circular fashion
    }
}
void ParticleMgr::InitParticle(PARTICLE &particle, GLfloat sss, GLfloat aaa)
{
    particle.speed = sss; // Set the particle's speed
    particle.angle = aaa; // Set the particle's current angle of rotation
    // Randomly set the particles color
    particle.red = rand()%255;
    particle.green = rand()%255;
    particle.blue = rand()%255;
}
void ParticleMgr::DrawParticle(const PARTICLE &particle)
{
    tex.Use();
    // Calculate the current x any y positions of the particle based on the particle's
    // current angle -- This will make the particles move in a "circular pattern"
    GLfloat xPos = sinf(particle.angle); 
    GLfloat yPos = cosf(particle.angle);
    // Translate to the x and y position and the #defined PDEPTH (particle depth)
    glTranslatef(xPos,yPos,PDEPTH);
    // Draw the first quad
    glBegin(GL_QUADS);
        glTexCoord2f(0,0);
        glVertex3f(-5, 5, 0);
        glTexCoord2f(1,0);
        glVertex3f(5, 5, 0);
        glTexCoord2f(1,1);
        glVertex3f(5, -5, 0);
        glTexCoord2f(0,1);
        glVertex3f(-5, -5, 0);
    glEnd(); // Done drawing quad
    // Draw the SECOND part of our particle
    tex.Use();
    glRotatef(particle.angle,0,0,1); // Rotate around the z-axis (depth axis)
    //glTranslatef(0, particle.angle, 0);
    // Draw the second quad
    glBegin(GL_QUADS);
        glTexCoord2f(0,0);
        glVertex3f(-4, 4, 0);
        glTexCoord2f(1,0);
        glVertex3f(4, 4, 0);
        glTexCoord2f(1,1);
        glVertex3f(4, -4, 0);
        glTexCoord2f(0,1);
        glVertex3f(-4, -4, 0);
    glEnd(); // Done drawing quad
    // Translate back to where we began
    glTranslatef(-xPos,-yPos,-PDEPTH);
}
void ParticleMgr::run(){
    for(int i = 0; i < P_MAX; i++)
    {
        DrawParticle(particle[i]);
        // Increment the particle's angle
        particle[i].angle += ANGLE_INC;
    }
}

现在我在上面的 run() 函数中添加了一个 glPushMatrix(), glTranslate(x, y, z),就在循环之前,x,y,z 作为敌人的位置,用于放置它们敌人的顶部......这是最好的地方吗?

感谢任何输入!

最佳答案

以这种方式使用 glTranslate 和 glRotate 实际上会降低程序的性能。 OpenGL 不是场景图,因此矩阵操作函数直接影响绘图过程,即它们不设置“对象状态”。您遇到的问题是,4×4 矩阵-矩阵乘法涉及 64 次乘法和 16 次加法。因此,与直接更新顶点位置相比,您花费 96 倍的计算能力来移动粒子。

现在解决您的问题:就像我已经告诉过您的那样,glTranslate 对 4 个可选矩阵之一的(全局)矩阵状态进行操作。并且效果会累积,即每个 glTranslate 将从前一个 glTranslate 留下的矩阵开始。 OpenGL 提供了一个矩阵堆栈,可以将当前矩阵的拷贝压入其中,然后弹出以恢复到之前的状态。

但是:矩阵操作已从 OpenGL-3 核心中移除,之后完全。 OpenGL 矩阵操作从未被加速(除了在 1996 年左右由 SGI 制造的一个特定图形工作站上)。今天它已经过时了,因为每个使用 3D 几何的受人尊敬的程序都通过自己的实现或第 3 方库使用更复杂的矩阵操作。 OpenGL 的矩阵堆栈只是多余的。所以我强烈建议您忘掉 OpenGL 的矩阵操作功能并自己动手做。

关于c++ - OpenGL 粒子,帮助控制方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8160310/

相关文章:

c++ - 无法从不是原点的其他地方旋转对象

java - LWJGL 的 Assimp 教程

java - 水平翻转 (X) ParticleEffect [Libgdx]

javascript - Three.js:如何为粒子沿线设置动画

c++ - 为什么从 Windows 启动时程序会运行而不是命令提示符?

c++ - 如何确保两个不同的 vector 在 C++ 中以相同的顺序混洗?

windows - 在没有硬件加速的情况下在 Windows 上部署 Qt5

c# - 实例数据管理

c++ - 如何为函数调用做 token 粘贴?

c++ - 从文件中读取 2 个字符串