在OpenGL中设置顶点的不透明度,可以通过以下几个步骤实现:
in vec4 a_vertexColor;
in float a_opacity;
out vec4 outColor;
outColor = vec4(a_vertexColor.rgb, a_vertexColor.a * a_opacity);
glVertexAttribPointer(a_vertexColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, color));
glVertexAttribPointer(a_opacity, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, opacity));
完整的顶点着色器和片段着色器代码如下:
#version 330 core
layout (location = 0) in vec3 a_position;
layout (location = 1) in vec4 a_vertexColor;
layout (location = 2) in float a_opacity;
out vec4 v_vertexColor;
out float v_opacity;
uniform mat4 u_modelViewProjection;
void main()
{
gl_Position = u_modelViewProjection * vec4(a_position, 1.0);
v_vertexColor = a_vertexColor;
v_opacity = a_opacity;
}
#version 330 core
in vec4 v_vertexColor;
in float v_opacity;
out vec4 outColor;
void main()
{
outColor = vec4(v_vertexColor.rgb, v_vertexColor.a * v_opacity);
}
通过以上步骤,可以在OpenGL中设置顶点的不透明度。
领取专属 10元无门槛券
手把手带您无忧上云