英文:
Why can't I assign a value to an "in" value
问题
当我尝试为一个输入值赋值,就像这样:
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"in vec2 TexCoord;\n"
"in vec3 Normal;\n" // 这里是我想要赋值的地方
"in vec3 Tangent;\n"
"in vec3 biTangent;\n"
"uniform sampler2D ourTexture;\n"
"uniform sampler2D norTexture;\n"
"uniform vec3 lightDir;\n"
"uniform vec3 viewDir;\n"
"void main()\n"
"{\n"
" mat3 accNormalTrans = mat3(Tangent, biTangent, Normal);\n"
" Normal = texture(norTexture, TexCoord).xyz;\n" // 输入变量不可随意更改
" Normal = normalize(texNormal*2.0-1.0);\n"
" Normal = normalize(accNormalTrans*texNormal);\n"
" vec4 ambient = texture(ourTexture, TexCoord)*0.5;\n" // norTexture 错误 // vec4(texNormal, 1.0f)
" vec4 diffuse = ambient*max(0, -dot(lightDir, Normal));\n"
" vec4 specular = ambient*pow(max(0, -dot(normalize(lightDir+viewDir), Normal)), 32);\n"
" FragColor = ambient+diffuse+specular;\n"
"}const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"in vec2 TexCoord;\n"
"in vec3 Normal;\n" // 这里是我想要赋值的地方
"in vec3 Tangent;\n"
"in vec3 biTangent;\n"
"uniform sampler2D ourTexture;\n"
"uniform sampler2D norTexture;\n"
"uniform vec3 lightDir;\n"
"uniform vec3 viewDir;\n"
"void main()\n"
"{\n"
" mat3 accNormalTrans = mat3(Tangent, biTangent, Normal);\n"
" Normal = texture(norTexture, TexCoord).xyz;\n" // 输入变量不可随意更改
" Normal = normalize(texNormal*2.0-1.0);\n"
" Normal = normalize(accNormalTrans*texNormal);\n"
" vec4 ambient = texture(ourTexture, TexCoord)*0.5;\n" // norTexture 错误 // vec4(texNormal, 1.0f)
" vec4 diffuse = ambient*max(0, -dot(lightDir, Normal));\n"
" vec4 specular = ambient*pow(max(0, -dot(normalize(lightDir+viewDir), Normal)), 32);\n"
" FragColor = ambient+diffuse+specular;\n"
"}\0";
";
之前的Normal是模型平面的法向量,我想要将其分配一个使用法线贴图计算的法向量。但是我得到的模型是透明的,而且模型总是跟随我的摄像机。
然后我尝试为该法向量构建一个新的值,模型被正确渲染了。
我想知道为什么会发生这种情况。
我在谷歌上搜索了这个问题,但没有找到具体的解释。
如果您能解释一下或提供相关链接,我将不胜感激。
英文:
When I tried to assign a value to an in value like this:
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"in vec2 TexCoord;\n"
"in vec3 Normal;\n" // <------here is the value I want to assign
"in vec3 Tangent;\n"
"in vec3 biTangent;\n"
"uniform sampler2D ourTexture;\n"
"uniform sampler2D norTexture;\n"
"uniform vec3 lightDir;\n"
"uniform vec3 viewDir;\n"
"void main()\n"
"{\n"
" mat3 accNormalTrans = mat3(Tangent, biTangent, Normal);\n"
" Normal = texture(norTexture, TexCoord).xyz;\n"// in 变量不可随意更改
" Normal = normalize(texNormal*2.0-1.0);\n"
" Normal = normalize(accNormalTrans*texNormal);\n"
" vec4 ambient = texture(ourTexture, TexCoord)*0.5;\n"//norTexture error //vec4(texNormal, 1.0f)
" vec4 diffuse = ambient*max(0,-dot(lightDir,Normal));\n"
" vec4 specular = ambient*pow(max(0,-dot(normalize(lightDir+viewDir),Normal)),32);\n"
" FragColor = ambient+diffuse+specular;\n"
"}\nconst char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"in vec2 TexCoord;\n"
"in vec3 Normal;\n" // <------here is the value I want to assign
"in vec3 Tangent;\n"
"in vec3 biTangent;\n"
"uniform sampler2D ourTexture;\n"
"uniform sampler2D norTexture;\n"
"uniform vec3 lightDir;\n"
"uniform vec3 viewDir;\n"
"void main()\n"
"{\n"
" mat3 accNormalTrans = mat3(Tangent, biTangent, Normal);\n"
" Normal = texture(norTexture, TexCoord).xyz;\n"// in 变量不可随意更改
" Normal = normalize(texNormal*2.0-1.0);\n"
" Normal = normalize(accNormalTrans*texNormal);\n"
" vec4 ambient = texture(ourTexture, TexCoord)*0.5;\n"//norTexture error //vec4(texNormal, 1.0f)
" vec4 diffuse = ambient*max(0,-dot(lightDir,Normal));\n"
" vec4 specular = ambient*pow(max(0,-dot(normalize(lightDir+viewDir),Normal)),32);\n"
" FragColor = ambient+diffuse+specular;\n"
"}\n\0";
";
the previous Normal is a normal vector of model plain, and I want to assign it a normal vector calculated with normal map.
But I got my model transparent and the model will always follow my camera.
Then I tried to build a new value for that normal vector, and the model is rendered correctly.
I want to know why this situation happens.
I have searched this question on google, but no specific explanation.
I will appreciate if you could explain it or list a LINK for that.
答案1
得分: 2
这是不允许的。
OpenGL Shading Language 规范,版本 4.40:
将一个声明为输入的变量写入是在编译时出错的。
这可能是为了在硬件级别上简化和/或优化实现,因为这些变量通常使用固定位置来实现。
英文:
The language does not allow it.
The OpenGL Shading Language specification, Version 4.40:
> It is a compile-time error to write to a variable declared as an
> input.
This is probably to ease and/or optimize the implementation on the hardware level, because such variables are typically implemented using fixed locations.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论