使用pow()时的GLSL问题

 小秋秋 发布于 2023-01-31 19:10

我正在实施一个着色器来做一些定向灯.我正在关注Lighthouse 3d的教程(http://www.lighthouse3d.com/tutorials/glsl-core-tutorial/directional-lights-per-pixel/)

我遇到的问题是以下几行:

vec3 h  = normalize(l_dir + e); 
float intSpec  = max(dot(h, n), 0.0);
float specularPower = pow(intSpec, shininess);

如果我留下"float specularPower"行 - 那么着色器不再"工作"....我在引号中说"工作",因为我没有从着色器日志中获得输出或错误,但是,现在统一的位置为我的全部返回-1,我无法设置我的纹理位置等.

如果我将其删除,则着色器的其余部分按预期工作(但由于缺少镜面反射功率而产生不正确的结果).

更奇怪的是,如果我附加了nVidia Nsight调试器,那么我在屏幕上得到输出它似乎"工作",但如果我只是在调试模式下使用VS2012我屏幕上没有显示任何内容,并出现以下错误信息:

First-chance exception at 0x000000005EB066E6 (nvoglv64.dll) in application.exe: 0xC0000005: Access violation reading location 0x0000000000000008.
First-chance exception at 0x000000005EB066E6 (nvoglv64.dll) in application.exe: 0xC0000005: Access violation reading location 0x0000000000000008.

在运行Windows 8 64位的PC上,GTX 480和GTX 560都可以看到这种现象.

我知道-1作为Uniform位置返回意味着名称错误,或者编译器已将其优化掉,但是添加一行是没有意义的,之后根本不会使用其结果.当连接或不连接NSight调试器时,行为是不同的

我可能做错了什么?

编辑:

下面是我可以创建的最简单的顶点/片段着色器,它可以复制问题,同时仍然是一个"真实世界"的着色器.

顶点着色器:

// Default Vertex Shader

#version 430 core

uniform mat4 projMatrix;
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;

in vec4 in_Position;
in vec3 in_Normal;
in vec2 in_TexCoords;

out vec2 pass_TexCoords;

out vec3 v;

out Data {
    vec3 normal;
    vec4 eye;
} DataOut;

void main(void)
{
    DataOut.normal = normalize(in_Normal);
    DataOut.eye = (viewMatrix * modelMatrix) * in_Position;

    pass_TexCoords = in_TexCoords;

    v = vec3(in_Position);

    gl_Position = projMatrix * viewMatrix * modelMatrix * in_Position;
}

片段着色器:

// Default Fragment Shader
#version 430 core

uniform sampler2D material[3];

in vec2 pass_TexCoords;


in Data {
    vec3 normal;
    vec4 eye;
} DataIn;

layout (location = 0) out vec4 out_Colour;

void main(void)
{
    float shininess         = 0.5;

    vec3 l_dir              = vec3(0.0, 0.0, 1.0);
    vec3 n                  = normalize(DataIn.normal);
    vec3 e                  = normalize(vec3(DataIn.eye));

    float intensity         = max(dot(n, l_dir), 0.0);
    float specularPower;

    if(intensity > 0.0) {
        vec3 h              = normalize(l_dir + e);     

        float dHN           = dot(h, n);
        float intSpec       = max(dHN, 0.0);

        float specularPower = pow(intSpec, shininess);
    } else {
        specularPower       = 1.0;
    }

    vec4 diffuse_Colour     = texture(material[0], pass_TexCoords);
    out_Colour              = diffuse_Colour * specularPower;
}

我还检查了程序信息日志,它没有返回任何错误.再次,使用这些着色器,在通过VS2012运行时失败(制服返回-1),但在连接nVidia Nsight调试器时"有效".

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有