OpenGL 0(84) : error C7623: implicit narrowing of type from "vec4" to "float"

标签 opengl glsl minecraft

我正在为 Minecraft 着色器处理阴影,但我一直在尝试解决以下错误。

21:51:27.725   
[Shaders] Error compiling fragment shader: /shaders/composite.fsh  
21:51:27.726  
[Shaders] Shader info log: /shaders/composite.fsh  
0(84) : error C7623: implicit narrowing of type from "vec4" to "float"  
21:51:27.727  
[Shaders] Error linking program: 10 (composite)  

我知道该错误是由不兼容的类型引起的,但我仍然不确定如何解决它,因此感谢任何帮助。

#version 120


const int shadowMapResolution = 2048;
const float shadowDistance = 128;
const float shadowMapBias = 0.85;
const int noiseTextureResolution = 256;
#define SHADOWMAP_BIAS 0.85

uniform sampler2D colortex0;
uniform sampler2D shadowtex0;
uniform sampler2D shadowcolor0;
uniform sampler2D depthtex1;
uniform sampler2D noisetex;


uniform vec3 cameraPosition;
uniform mat4 gbufferModelViewInverse;
uniform mat4 gbufferModelView;
uniform mat4 shadowProjection;
uniform mat4 gbufferProjection;
uniform mat4 gbufferProjectionInverse;
uniform mat4 shadowModelView;

uniform float viewWidth;
uniform float viewHeight;

varying vec4 texcoord;

float depth = 0.5;

vec4 getCameraPosition(in vec2 coord)
{
    float getdepth = depth;
    vec4 positionNdcSpace = vec4(coord.s * 2.0 - 1.0, coord.t * 2.0 - 1.0, 2.0 * getdepth - 1.0, 1.0);
    vec4 positionCameraSpace = gbufferProjectionInverse * positionNdcSpace;

    return positionCameraSpace / positionCameraSpace.w;
}

vec4 getWorldSpacePosition(in vec2 coord)
{
    vec4 cameraPos = getCameraPosition(coord);
    vec4 worldPos = gbufferModelViewInverse * cameraPos;
    worldPos.xyz += cameraPosition;

    return worldPos;
}

vec3 getShadowSpacePosition(in vec2 coord)
{
    vec4 worldSpacePos = getWorldSpacePosition(coord);
    worldSpacePos.xyz -= cameraPosition;
    vec4 shadowSpacePos = shadowModelView * worldSpacePos;
    shadowSpacePos = shadowProjection * shadowSpacePos;

    return shadowSpacePos.xyz * 0.5 + 0.5;
}

mat2 getRotationMatrix(in vec2 coord)
{
    float rotationAmount = texture2D(
        noisetex,
        coord * vec2(
            viewWidth / noiseTextureResolution,
            viewHeight / noiseTextureResolution
        )
    ).r;
    return mat2(
        cos(rotationAmount), -sin(rotationAmount),
        sin(rotationAmount), cos(rotationAmount)
    );
}

vec3 getShadows(in vec2 coord)
{
    vec3 shadowCoord = getShadowSpacePosition(coord);
    mat2 rotationMatrix = getRotationMatrix(coord);
    vec3 shadowCol = vec3(0.0);
    for (int i = 0; i < 32; i++)
    {
        vec2 offset = vec2(32 / shadowMapResolution);
        offset = rotationMatrix * offset;
        float shadowMapSample = texture2D(shadowtex0, shadowCoord.st + offset);
        float visibility = step(shadowCoord.z - shadowMapSample, 0.001);
        vec3 dayCol = vec3(1.0);
        vec3 colorSample = texture2D(shadowcolor0, shadowCoord.st + offset).rgb;
        shadowCol += mix(colorSample, dayCol, visibility);
    }

    return vec3(shadowCol) / 32;
}

vec3 calculateLighting(in vec3 color)
{
    vec3 sunLight = getShadows(texcoord.st);
    vec3 ambientLight = vec3(0.5, 0.7, 1.0) * 0.5;

    return color * (sunLight + ambientLight);
}

void main()
{
    depth = texture2D(depthtex1, texcoord.st).r;
    vec3 color = texture2D(colortex0, texcoord.st).rbg;
    color = calculateLighting(color);

    gl_FragData[0] = vec4(color, 1.0);
    gl_FragData[1] = vec4(depth);
}

最佳答案

问题在于,texture2D 返回一个 vec4,但您将其视为 float 。改为读取红色部分

float shadowMapSample = texture2D(shadowtex0, shadowCoord.st + offset).r;

关于OpenGL 0(84) : error C7623: implicit narrowing of type from "vec4" to "float",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58585003/

相关文章:

OpenGL/GLSL - 纹理过滤的实现

javascript - 在 Canary minecraft mod 中使用 javascript scriptcraft 示例代码时出现问题

java - Ubuntu Minecraft 服务器 java.lang.ClassCastException : class jdk. internal.loader.ClassLoaders$AppClassLoader

Java "TRAP"断言错误

opengl - imageStore 是原子的吗?

opengl - 如何使用 ImageLoad() 访问深度纹理?

opengl - rviz 段错误(核心已转储)

linux - 如何从我的 Windows 机器上运行安装在 Linux 机器上的 OpenGL 应用程序?

user-interface - OpenGL在可滚动面板中绘制部分对象

opengl - OpenGL : Error moving object using keyboard