1

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

这是顶点着色器:

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     
}
4

1 回答 1

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 个)。这可能就是当您取消注释该行时它“不起作用”的原因,因为您的顶点着色器将忽略您实际上没有写入的任何变化。

You should not be passing those matrices at all. They're uniforms; by definition, they don't change from vertex to vertex. And fragment shaders are perfectly capable of accessing those same uniforms too. So just declare them in your fragment shader if you need to use them.

于 2012-11-02T05:42:02.037 回答