【问题标题】:Point Sprite Alpha Blending IssuePoint Sprite Alpha 混合问题
【发布时间】:2018-09-20 23:17:11
【问题描述】:

我正在尝试使用点精灵在 3D 空间中渲染大量星星。就 alpha 通道而言,我的纹理图像绝对是“好”的。它作为四边形渲染得非常好,但是当我渲染很多点精灵时,我可以看到图像的方形边框覆盖了一些图像。

上图显示了在我的立方体顶部很好地渲染了星星。在右下角,我希望看到连续的星形图像,它们被绘制成“花朵”图案。

我做错了什么?

片段着色器:

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

【问题讨论】:

  • 尝试添加 glDisable(GL_DEPTH_TEST)。

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


【解决方案1】:

也许深度剔除在欺骗你?在渲染精灵时尝试glDepthMask(false)

【讨论】:

  • 谢谢我接受你的回答。做到了。我尝试启用 GL_DEPTH_TEST 但没有尝试禁用它。 glDepthMask( false ) 和 glDisable( GL_DEPTH_TEST ) 都给了我想要的结果。
  • @SparkyNZ 太棒了!我建议使用 DepthMask,因为精灵可以放置在左上角(立方体)体积的后面,并具有正确的遮挡。如果同时禁用深度测试,则体积后面的任何精灵都将呈现在其顶部。
  • 好点。现在我还有其他事情要弄清楚和修复。我用点精灵创建了一个星域,果然它们现在都出现在立方体的顶部。
猜你喜欢
  • 1970-01-01
  • 2013-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-17
  • 1970-01-01
相关资源
最近更新 更多