OpenGl(pyglet)如何在游戏中添加雾

2024-09-29 01:20:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我有生成3d场景的代码,我想向场景添加雾,但glFog不起作用。(本节):

        gl.glEnable(gl.GL_FOG)
        gl.glFogi(gl.GL_FOG_MODE, gl.GL_EXP)
        gl.glFogf(gl.GL_FOG_DENSITY, 0.5)
        gl.glFogf(gl.GL_FOG_START, 1.0)
        gl.glFogf(gl.GL_FOG_END, 5.0)
        gl.glFogfv(gl.GL_FOG_COLOR, (gl.GLfloat * 4)(0.5, 0.5, 0.5, 1.0))

我已经看到很多fog使用着色器,但我找不到任何与我的代码兼容的着色器。(如):

error in shader fog

这是我的着色器代码,没有添加任何雾(或尝试添加。基本上,这段代码工作正常)

垂直glsl:

#version 330

layout(location = 0) in vec3 vertex_position;
layout(location = 1) in vec3 tex_coords;
layout(location = 2) in float shading_value;

out vec3 global_position;
out vec4 local_position;
out vec3 interpolated_tex_coords;
out float interpolated_shading_value;

uniform mat4 matrix;

void main(void) {
    global_position = vertex_position;
    interpolated_tex_coords = tex_coords;
    interpolated_shading_value = shading_value;
    local_position = matrix * vec4(vertex_position, 1.0);
    gl_Position = local_position;
}

frag.glsl:

#version 330

out vec4 fragment_colour;

in vec3 local_position;
in vec3 interpolated_tex_coords;

in float interpolated_shading_value;

uniform sampler2DArray texture_array_sampler;

void main(void) {
    vec4 texture_colour = texture(texture_array_sampler, interpolated_tex_coords);

    if (texture_colour.a != 1) {
        discard;
    }

    fragment_colour  = (texture_colour * interpolated_shading_value);
}

主代码没有错误并且运行良好,当我尝试添加雾时,它只是中断


Tags: 代码invaluepositioncoordsouttexcolour