Strange error in GLSL while assigning attribute to

2019-08-03 05:43发布

I am writing a GLSL program for texture mapping. I am having a weird problem. From my vertex shader I am trying to pass the texture coordinate vec2 as a varying to frag shader. In a diff shader in the same prog I did the same and it worked. But for this texture its not working. If I comment that line, then everything works. I have no clue why this is happening.

This is the vertex shader:

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条回答
仙女界的扛把子
2楼-- · 2019-08-03 06:14
varying vec4 pcolor; 
varying vec3 fNormal;
varying vec3 v;
varying mat4 modelMat;
varying mat4 viewMat;
varying mat4 projectionMat;
varying vec2 texCoordinate;
varying vec3 reflector;

This is 17 total varying vectors. That's too many for most 2.1-class hardware (they generally only support 16). That's probably why it is "not working" when you uncomment that line, because your vertex shader will ignore any varying that you don't actually write to.

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.

查看更多
登录 后发表回答