0

我一直在尝试更新一些代码以兼容 OpenGL 3.2,但我的着色器遇到了问题。我一直在在线阅读差异,并且我的着色器编译,但我的屏幕仍然是我的 glClearColor。我有一个顶点和片段着色器(进行 phong 着色)。在 OpenGL 2.1 / GLSL 1.2 下一切正常,问题仅在 OpenGL 3.2 / GLSL 1.5 下出现

这是我的 1.2 版着色器:

顶点:

#version 120

uniform mat4 uProjMatrix;
uniform mat4 uViewMatrix;
uniform mat4 uModelMatrix;
uniform mat4 uNormalMatrix;

attribute vec3 aPosition;
attribute vec3 aNormal;

varying vec3 vWorldPosition;
varying vec3 vNormal;

void main() {
  // Transforms
  vec4 lPosition = uModelMatrix * vec4(aPosition.x, aPosition.y, aPosition.z, 1);
  vWorldPosition = vec3(lPosition.x, lPosition.y, lPosition.z);
  gl_Position = uProjMatrix * uViewMatrix * lPosition;

  // Calculate the relative normal
  vec4 lNormal = vec4(aNormal.x, aNormal.y, aNormal.z, 0);
  lNormal = uNormalMatrix * lNormal;
  vNormal = vec3(lNormal.x, lNormal.y, lNormal.z);
}

分段:

#version 120

struct Light {
  vec3 position, color;
  float constFalloff, linearFalloff, squareFalloff;
};

struct Material {
  vec3 ambient, diffuse, specular, emission;
  float shininess;
};

uniform Light uLights[10];
uniform int uNumLights;
uniform Material uMaterial;
uniform vec3 uCameraPos;

varying vec3 vWorldPosition;
varying vec3 vNormal;

void main() {
  vec3 lNormal = normalize(vNormal);

  vec3 finalColor = vec3(0.0, 0.0, 0.0);
  for(int i = 0; i < uNumLights; ++i) {
    // Diffuse light
    vec3 toLight = uLights[i].position - vWorldPosition;
    float lightDistance = length(toLight);
    toLight = normalize(toLight);
    float diffuseAmount = max(dot(lNormal, toLight), 0.0);

    // Specular light
    vec3 toCamera = normalize(uCameraPos - vWorldPosition);
    vec3 reflection = normalize(2.0 * dot(toLight, lNormal) * lNormal - toLight);
    float specularAmount = pow(max(dot(toCamera, reflection), 0.0), uMaterial.shininess);

    // Falloff
    float falloff = 1.0 / (uLights[i].constFalloff
                          + uLights[i].linearFalloff * lightDistance
                          + uLights[i].squareFalloff * lightDistance * lightDistance);

    finalColor += ((uMaterial.diffuse * diffuseAmount
                  + uMaterial.specular * specularAmount) * falloff
                  + uMaterial.ambient) * uLights[i].color;
  }

  finalColor += uMaterial.emission;

  gl_FragColor = vec4(finalColor.r, finalColor.g, finalColor.b, 1.0);
}

我的 1.5 版着色器:

顶点:

#version 150 core

uniform mat4 uProjMatrix;
uniform mat4 uViewMatrix;
uniform mat4 uModelMatrix;
uniform mat4 uNormalMatrix;

in vec3 aPosition;
in vec3 aNormal;

out vec3 vWorldPosition;
out vec3 vNormal;

void main() {
  // Transforms
  vec4 lPosition = uModelMatrix * vec4(aPosition.x, aPosition.y, aPosition.z, 1);
  vWorldPosition = vec3(lPosition.x, lPosition.y, lPosition.z);
  gl_Position = uProjMatrix * uViewMatrix * lPosition;

  // Calculate the relative normal
  vec4 lNormal = vec4(aNormal.x, aNormal.y, aNormal.z, 0);
  lNormal = uNormalMatrix * lNormal;
  vNormal = vec3(lNormal.x, lNormal.y, lNormal.z);
}

分段:

#version 150 core

struct Light {
  vec3 position, color;
  float constFalloff, linearFalloff, squareFalloff;
};

struct Material {
  vec3 ambient, diffuse, specular, emission;
  float shininess;
};

uniform Light uLights[10];
uniform int uNumLights;
uniform Material uMaterial;
uniform vec3 uCameraPos;

in vec3 vWorldPosition;
in vec3 vNormal;

out vec4 outColor;

void main() {
  vec3 lNormal = normalize(vNormal);

  vec3 finalColor = vec3(0.0, 0.0, 0.0);
  for(int i = 0; i < uNumLights; ++i) {
    // Diffuse light
    vec3 toLight = uLights[i].position - vWorldPosition;
    float lightDistance = length(toLight);
    toLight = normalize(toLight);
    float diffuseAmount = max(dot(lNormal, toLight), 0.0);

    // Specular light
    vec3 toCamera = normalize(uCameraPos - vWorldPosition);
    vec3 reflection = normalize(2.0 * dot(toLight, lNormal) * lNormal - toLight);
    float specularAmount = pow(max(dot(toCamera, reflection), 0.0), uMaterial.shininess);

    // Falloff
    float falloff = 1.0 / (uLights[i].constFalloff
                          + uLights[i].linearFalloff * lightDistance
                          + uLights[i].squareFalloff * lightDistance * lightDistance);

    finalColor += ((uMaterial.diffuse * diffuseAmount
                  + uMaterial.specular * specularAmount) * falloff
                  + uMaterial.ambient) * uLights[i].color;
  }

  finalColor += uMaterial.emission;

  outColor = vec4(finalColor.r, finalColor.g, finalColor.b, 1.0);
}

我没有从编译/链接 1.5 着色器中得到任何错误,但没有任何结果。我错过了什么?

4

0 回答 0