Point Sprite Alpha混合问题

时间:2018-04-11 07:18:24

标签: c++ opengl glsl opengl-3 glm-math

我试图使用点精灵在3D空间中渲染一大堆星星。我的纹理图像绝对是“好”的。就alpha通道而言。它呈现完美的四边形,但当我渲染很多点精灵时,我可以看到图像的方形边框覆盖了一些图像。 enter image description here

上图显示了星星在我的立方体顶部很好地渲染。在右下方,我希望看到“花朵”中连续出现的星形图像。他们被吸引进来的模式。

我做错了什么?

片段着色器:

#version 140

#extension GL_ARB_explicit_attrib_location : enable

precision mediump float;

uniform sampler2D uTexture;

void main( void )
{    
  gl_FragColor = texture2D( uTexture, gl_PointCoord );
}

顶点着色器:

#version 140

#extension GL_ARB_explicit_attrib_location : enable

precision mediump float;

layout (location = 0) in float aTheta; 

uniform float uK;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main( void )
{
  float x = cos( uK * aTheta ) * sin( aTheta );
  float y = cos( uK * aTheta ) * cos( aTheta );

  gl_Position  = projection * view * model * vec4( x, y, 0.0, 1.0 );
  gl_PointSize = 64.0;
}

代码:

void CPoint::Render( void )
{
  g_PointProgram.UseProgram();

  glEnable( GL_PROGRAM_POINT_SIZE );
  glEnable( GL_POINT_SPRITE );

  // Set the alpha blending function
  glEnable( GL_BLEND );
  glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  glBlendEquation( GL_FUNC_ADD );

  glBindBuffer( GL_ARRAY_BUFFER, m_VBO );

  glm::mat4 Model = glm::mat4( 1.0 );

  Model = glm::translate( Model, glm::vec3( 2.0f, 0.0f, 4.0f ) );

  glm::mat4 Projection = g_Camera.GetProjection();
  glm::mat4 View       = g_Camera.GetView();

  g_PointProgram.SetUint( "uTexture", g_ImgStar.m_Texture );

  g_PointProgram.SetMatrix4fv( "model", Model );
  g_PointProgram.SetMatrix4fv( "view", View );
  g_PointProgram.SetMatrix4fv( "projection", Projection );

  g_PointProgram.SetFloat( "uK", m_Emitter.k );

  glActiveTexture( GL_TEXTURE0 );
  glBindTexture( GL_TEXTURE_2D, g_ImgStar.m_Texture );
  glEnable( GL_TEXTURE_2D );

  // Attributes
  glVertexAttribPointer( 0,                // 1st attribute array (only have 1)
                         1,                                        // One theta angle per particle
                         GL_FLOAT,                                 // Data is floating point type
                         GL_FALSE,                                 // No fixed point scaling
                         sizeof( Particle ),                       // No gaps in data
                         (void*) 0 );      // Start from "theta" offset within bound buffer


  glEnableVertexAttribArray( 0 );

  glDrawArrays( GL_POINTS, 0, NUM_PARTICLES );

  glDisableVertexAttribArray( 0 );
  glBindBuffer( GL_ARRAY_BUFFER, 0 );

  glDisable( GL_TEXTURE_2D );
}

1 个答案:

答案 0 :(得分:1)

也许深度剔除会对你起伎俩?渲染精灵时尝试glDepthMask(false)