如何正确地将GLSL 1.2代码转换为GLSL 1.5

时间:2013-10-30 20:26:19

标签: c++ opengl glsl shader

我一直在尝试更新一些代码以符合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着色器时没有任何错误,但没有任何绘制。我错过了什么?

0 个答案:

没有答案