c++ - 点 Sprite Alpha 混合问题

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

我正在尝试使用点 Sprite 在 3D 空间中渲染大量星星。就 alpha channel 而言,我的纹理图像绝对是“好”的。它作为四边形渲染得非常好,但是当我渲染很多点 Sprite 时,我可以看到图像的方形边框覆盖了一些图像。 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 );
}

最佳答案

也许深度剔除在捉弄你?在渲染 Sprite 时尝试 glDepthMask(false)

关于c++ - 点 Sprite Alpha 混合问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49768828/

相关文章:

c++ - 以重复模式绑定(bind)纹理

c++ - 在 GLSL 中计算点后绘制三角形

opengl - glsl算术运算符

c++ - STL VS13 C++ : Error C4996 not disabling

c++ - 重载 * 乘法运算符两次作为成员函数?

c++ - cygwin 不包含 mysql.h .. 我怎样才能在 cygwin 中得到它?

c++ - 我怎样才能在cython中调用这个函数?

c++ - 帮我评价一下这个选角

java - 为什么我的文字不显示?

c++ - imageAtomicExchange 无法编译