可以在绘制粒子之前或之后绘制任何其他对象

时间:2014-06-14 16:42:22

标签: opengl drawing buffer system particles

我正在开发一款游戏,并尝试在http://www.opengl-tutorial.org/intermediate-tutorials/billboards-particles/particles-instancing/上实现编程的实时化CPU粒子系统 我设法让它在我的代码结构中工作,但我试图在同一个窗口中绘制其他对象,我无法测试它,它只允许我绘制一个,要么绘制粒子系统或绘制我想要的对象。

问题特别发生在这个代码部分:

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    // Use our shader
    glUseProgram(particleprogramID->programHandle);

    unit2 +=1;

    glActiveTexture(GL_TEXTURE0 + unit2);
    glBindTexture(GL_TEXTURE_2D, texture);
    glUniform1i(TextureID, unit2);

    glm::mat4 ViewMatrix = camera->getViewMatrix();
    // Same as the billboards tutorial
    glUniform3f(CameraRight_worldspace_ID, ViewMatrix[0][0], ViewMatrix[1][0], ViewMatrix[2][0]);
    glUniform3f(CameraUp_worldspace_ID   , ViewMatrix[0][1], ViewMatrix[1][1], ViewMatrix[2][1]);

    glUniformMatrix4fv(ViewProjMatrixID, 1, GL_FALSE, &mvp[0][0]);
    //glUniformMatrix4fv(modviewprojID, 1, GL_FALSE, &mvp[0][0]);

    //1rst attribute buffer : vertices
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, billboard_vertex_buffer);
    glVertexAttribPointer(
        0,                  
        3,                  
        GL_FLOAT,           
        GL_FALSE,           
        0,                  
        (void*)0            
    );

    // 2nd attribute buffer : positions of particles' centers
    glEnableVertexAttribArray(1);
    glBindBuffer(GL_ARRAY_BUFFER, particles_position_buffer);
    glVertexAttribPointer(
        1,                                
        4,                               
        GL_FLOAT,                         
        GL_FALSE,                        
        0,                               
        (void*)0                         
    );

    // 3rd attribute buffer : particles' colors
    glEnableVertexAttribArray(2);
    glBindBuffer(GL_ARRAY_BUFFER, particles_color_buffer);
    glVertexAttribPointer(
        2,                                
        4,                                
        GL_UNSIGNED_BYTE,                 
        GL_TRUE,                          
        0,                                
        (void*)0                          
    );


    glVertexAttribDivisor(0, 0); 
    glVertexAttribDivisor(1, 1);
    glVertexAttribDivisor(2, 1); 

    glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, ParticlesCount);

    glDisableVertexAttribArray(0);
    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(2);

然后我试着画出我的明星:

unit2 += 1;
    starTexture->Bind(unit2);
    shaderObject ->useShader();
    glUniform1i(glGetUniformLocation(shaderObject->programHandle, "colorTexture"), unit2);
    glUniformMatrix4fv(glGetUniformLocation(shaderObject->programHandle, "modelMatrix"), 1, GL_FALSE, glm::value_ptr(star1->getModelMatrix()));
    glUniformMatrix4fv(glGetUniformLocation(shaderObject->programHandle, "projectionMatrix"), 1, GL_FALSE, glm::value_ptr(projectionViewMatrix));

    star1->draw();

粒子系统的顶点和片段着色器:

    #version 330 core

// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 squareVertices;
layout(location = 1) in vec4 xyzs; // Position of the center of the particule and size of the square
layout(location = 2) in vec4 color; // Position of the center of the particule and size of the square

// Output data ; will be interpolated for each fragment.
out vec2 UV;
out vec4 particlecolor;

// Values that stay constant for the whole mesh.
uniform vec3 CameraRight_worldspace;
uniform vec3 CameraUp_worldspace;
uniform mat4 VP; // Model-View-Projection matrix, but without the Model (the position is in BillboardPos; the orientation depends on the camera)

void main()
{
    float particleSize = xyzs.w; // because we encoded it this way.
    vec3 particleCenter_wordspace = xyzs.xyz;

    vec3 vertexPosition_worldspace = 
        particleCenter_wordspace
        + CameraRight_worldspace * squareVertices.x * particleSize
        + CameraUp_worldspace * squareVertices.y * particleSize;

    // Output position of the vertex
    gl_Position = VP * vec4(vertexPosition_worldspace, 1.0f);

    // UV of the vertex. No special space for this one.
    UV = squareVertices.xy + vec2(0.5, 0.5);
    particlecolor = color;
}

frragment shader:

    #version 330 core

// Interpolated values from the vertex shaders
in vec2 UV;
in vec4 particlecolor;

// Ouput data
out vec4 color;

uniform sampler2D myTexture;

void main(){
    // Output color = color of the texture at the specified UV
    color = texture2D( myTexture, UV ) * particlecolor;

}

它只显示粒子系统:

值得一提的是: 我想要绘制的对象是在混合器中建模的星形,并且在单独绘制时或与粒子系统以外的其他对象一起绘制时正确显示。并且它有自己的类,有psions,UVs,index和normals的缓冲区......

似乎星形数据被缓冲区吞噬......

我感谢所有的帮助...

0 个答案:

没有答案
相关问题