微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法在Java和OpenGL中使用平面照明

我一直在尝试使平面照明在openGL着色器中工作约两周,我已经尝试了很多教程,但仍然无法正常工作,这就是为什么我决定在这个我也搜索过的网站上发布东西的原因你可以帮助我.

顶点着色器:

#version 330 core

// vertex shader input \\
in vec3 position;
in vec2 textureCoords;
in vec3 normal;

// vertex shader output \\
out vec2 pass_textureCoords;
out vec3 surfacenormal;
out vec3 toLightVector;
out vec3 toCameraVector;
out float visibility;

// uniform variables \\
uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform vec3 lightPosition;

// fog settings \\
const float density = 0.0012;
const float gradient = 5.0;

void main(void){

    vec4 worldPosition = transformationMatrix * vec4(position,1.0);
    vec4 positionRelativetoCam = viewMatrix * worldPosition;
    gl_Position = projectionMatrix * positionRelativetoCam;
    pass_textureCoords = textureCoords * 40.0;

    surfacenormal = (transformationMatrix * vec4(normal,0.0)).xyz;
    toLightVector = lightPosition - worldPosition.xyz;
    toCameraVector = (inverse(viewMatrix) * vec4(0.0,0.0,1.0)).xyz - worldPosition.xyz;

    // fog stuff \\
    float distance = length(positionRelativetoCam.xyz);
    visibility = exp(-pow((distance * density),gradient));
    visibility = clamp(visibility,1.0);

}

片段着色器:

#version 330 core

// fragment shader input \\
in vec2 pass_textureCoords;
in vec3 surfacenormal;
in vec3 toLightVector;
in vec3 toCameraVector;
in float visibility;

// colour output \\
out vec4 out_Colour;

// uniform variables \\
uniform sampler2D textureSampler;
uniform vec3 lightColour;
uniform float shinedamper;
uniform float reflectivity;
uniform vec3 skyColour;

void main(void){

    vec3 unitnormal = normalize(surfacenormal);
    vec3 unitLightVector = normalize(toLightVector);

    float nDot1 = dot(unitnormal,unitLightVector);
    float brightness = max(nDot1,0.25) + 0.05;
    vec3 diffuse = brightness * lightColour;

    vec3 unitVectorToCamera = normalize(toCameraVector);
    vec3 lightDirection = -unitLightVector;
    vec3 reflectedLightDirection = reflect(lightDirection,unitnormal);

    float specularFactor = dot(reflectedLightDirection,unitVectorToCamera);
    specularFactor = max(specularFactor,0.0);
    float dampedFactor = pow(specularFactor,shinedamper);
    vec3 finalspecular = dampedFactor * lightColour;

    out_Colour =  vec4(diffuse,1.0) * texture(textureSampler,pass_textureCoords) + vec4(finalspecular,1.0);
    out_Colour = mix(vec4(skyColour,1.0),out_Colour,visibility);

}

如果您知道如何将平面着色应用于我的项目,请告诉我.

提前致谢

最佳答案

If you kNow how i Could apply flat shading to my project please let me kNow.

flat插值限定符用于顶点着色器输出,这些输出用于灯光的封闭:

顶点着色器:

flat out vec3 surfacenormal;
flat out vec3 toLightVector;
flat out vec3 toCameraVector;

片段着色器:

flat in vec3 surfacenormal;
flat in vec3 toLightVector;
flat in vec3 toCameraVector;

参见OpenGL Shading Language 3.30 Specification; 4.3.9 Interpolation; page 41

A variable qualified as flat will not be interpolated. Instead,it will have the same value for every fragment within a triangle. This value will come from a single provoking vertex,[…]

原文地址:https://www.jb51.cc/java/532826.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐