将属性分配给变化的顶点着色器时GLSL中的奇怪错误

时间:2012-11-02 04:40:48

标签: opengl glsl shader vertex

我正在为纹理映射编写一个GLSL程序。我有一个奇怪的问题。从我的顶点着色器,我试图将纹理坐标vec2作为变化传递给frag着色器。在同一个编程中的差异着色器中,我做了同样的工作。但对于这种纹理它不起作用。如果我评论那条线,那么一切正常。我不知道为什么会这样。

这是顶点着色器:

attribute vec4 position; 
attribute vec4 color1; 
attribute vec4 normal; 
attribute vec2 texCoord;

uniform mat4 model; //passed to shader 
uniform mat4 projection; //passed to shader
uniform mat4 view; // passed to shader
uniform mat4 normalMatrix; //passed to shader
uniform mat4 worldNormalMatrix;
uniform vec3 eyePos;

varying vec4 pcolor; 
varying vec3 fNormal;
varying vec3 v;
varying mat4 modelMat;
varying mat4 viewMat;
varying mat4 projectionMat;
varying vec2 texCoordinate;
varying vec3 reflector;

void main()
{     
      //texCoordinate = texCoord;  // If I uncomment this, then I get wrong output. but the same thing works in a diff shader!! 
      mat4 projectionModelView;
      vec4 N;
      vec3 WorldCameraPosition = vec3(model*vec4(eyePos,1.0));
      vec3 worldPos = vec3(model*position);
      vec3 worldNorm = normalize(vec3(worldNormalMatrix*normal));
      vec3 worldView = normalize(vec3(WorldCameraPosition-worldPos));
      reflector = reflect(-worldView, worldNorm);      
      projectionMat = projection;
      modelMat=model;
      viewMat=view;
      N=normalMatrix*normal;      
      fNormal = vec3(N); //need to multiply this with normal matrix
      projectionModelView=projection*view*model;
      v=vec3(view*model*position); // v is the position at eye space for each vertex passed to frag shader
      gl_Position =  projectionModelView * position; // calculate clip space position     
}

1 个答案:

答案 0 :(得分:7)

varying vec4 pcolor; 
varying vec3 fNormal;
varying vec3 v;
varying mat4 modelMat;
varying mat4 viewMat;
varying mat4 projectionMat;
varying vec2 texCoordinate;
varying vec3 reflector;

这是17个总变化向量。这对于大多数2.1级硬件来说太多了(它们通常只支持16个)。这可能就是为什么当你取消注释该行时它“不工作”,因为你的顶点着色器会忽略你实际上没有写入的任何变化。

你不应该完全传递这些矩阵。他们是制服;根据定义,它们不会从顶点变为顶点。片段着色器也完全能够访问这些相同的制服。因此,如果您需要使用它们,请在片段着色器中声明它们。