问题描述
我正在编写一个用于纹理映射的GLSL程序.我有一个奇怪的问题.从我的顶点着色器,我试图将纹理坐标vec2作为变体传递给碎片着色器.在同一个编的差异着色器中,我执行了相同的操作,并且有效.但是对于这种纹理,它不起作用.如果我评论那条线,那么一切正常.我不知道为什么会这样.
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.
这是顶点着色器:
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
}
推荐答案
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个)而言,这太多了.这就是为什么当您取消注释该行时它不起作用"的原因,因为您的顶点着色器将忽略您实际上未写入的任何变化.
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.
这篇关于将属性分配给顶点着色器中的变化时,GLSL中出现奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!