【问题标题】:Can't see lights working openGL ES 2.0看不到灯光工作 openGL ES 2.0
【发布时间】:2013-04-26 16:46:07
【问题描述】:

我已经在我的代码和着色器中设置了我的照明,但除了我目前拥有的四边形之外什么都看不到。灯光不工作,我真的看不出哪里出错了。

我有一个带有纹理的四边形,其中四边形旋转了 90 度,因此它是平放的,在我的代码中,我已经完成了这样的照明......

    // Set light direction in model space
PVRTVec4 vLightDirModel;
vLightDirModel =  modelView.inverse() * PVRTVec4(0.57735f, 0.57735f, 0.57735f, 0);

glUniform3fv(m_ShaderProgram.auiLoc[eLight], 1, &vLightDirModel.x);

// Set eye position in model space
PVRTVec4 vEyePosModel;
vEyePosModel = modelView.inverse() * PVRTVec4(0, 0, 0, 1);

glUniform3fv(m_ShaderProgram.auiLoc[eEyePos], 1, &vEyePosModel.x);

这是我的着色器

垂直着色器:

attribute highp   vec3  inVertex;
attribute mediump vec2  inTexCoord;


uniform highp   mat4  MVPMatrix;
uniform mediump vec3  LightDir;
uniform mediump vec3  EyePos;

varying mediump vec3  EyeDir;
varying lowp    float  specIntensity;
varying mediump vec2  TexCoord;

const mediump float  cShininess = 10.0;

void main()
{
    // Transform position
    gl_Position = MVPMatrix * vec4(inVertex,1.0);

    // Calculate direction from eye position in model space
    mediump vec3 EyeDir = normalize(EyePos - inVertex);

        // Specular lighting
    // We ignore that N dot L could be negative (light coming 
    // from behind the surface)
    mediump vec3 halfVector = normalize(LightDir + EyeDir);
    lowp float NdotH = max(dot(inNormal, halfVector), 0.0);     
    specIntensity = pow(NdotH, cShininess);

    TexCoord = inTexCoord;
}

这是我的碎片着色器

uniform sampler2D reflectionTex;

varying mediump vec2  TexCoord;
varying lowp    float specIntensity;



//This gets updated within the main code
uniform highp float Time;

void main()
{   
    lowp vec3 refColor = texture2D(reflectionTex, TexCoord).rgb;
    gl_FragColor =  vec4(refColor + specIntensity, 1.0);
}

【问题讨论】:

    标签: opengl-es opengl-es-2.0


    【解决方案1】:

    您正在混淆您的照明计算。在您的 cmets 中,您说您正在计算镜面反射照明项。因此,对于您的平面,您希望在代码中看到的最多的是四边形某处的一个小的白色区域,如果光线正好成角度。即使这样计算也是错误的,因为您没有计算反射光矢量。

    基本的 phong 光照模型包括另外两个术语,漫反射(根据法线和光照 vec 之间的角度改变颜色,即角度越大,颜色越深)和环境光(向每个片段添加均匀的光模拟环境光)。

    我建议你阅读这个优秀的教程并将其改编为 ES: http://www.ozone3d.net/tutorials/glsl_lighting_phong.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      • 2012-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多