理解 OpenGL 中的光照问题
我在理解 OpenGL 中光照的工作原理时遇到一些问题(特别是理解预定义变量。是否有它们的列表?)。我一直在弄乱我的顶点着色器文件来为我的球体着色。所以我设法遮蔽它,但我不知道我做了什么。代码如下:
顶点着色器:
#version 330
precision highp float;
in vec3 in_Position; //declare position
in vec3 in_Color; //color passed from the cpp file
//mycode
in vec3 in_Normal;
// mvpmatrix is the result of multiplying the model, view, and projection matrices */
uniform mat4 MVP_matrix;
vec3 ambient;
out vec3 ex_Color;
void main(void) {
// Multiply the MVP_ matrix by the vertex to obtain our final vertex position (mvp was created in *.cpp)
gl_Position = MVP_matrix * vec4(in_Position, 1.0);
//mycode
ambient = vec3(0.0f,0.1f,1.0f); // just a test vector
ambient = ambient * in_Position ; // HERE IS WHAT I DONT GET!
ex_Color = ambient;
}
我将球体上的每个点与 MVP 矩阵相乘,然后与环境变量相乘。
片段着色器:
#version 330
precision highp float;
in vec3 ex_Color;
out vec4 gl_FragColor;
void main(void) {
gl_FragColor = vec4(ex_Color,1.0);
}
“Precision Highp float;”是什么意思?做?我知道顶点文件将颜色传递给片段着色器,然后该片段被“插值”。好的,插值是如何工作的? Opengl 如何知道在哪里停止插值?
I am having some problems understanding how lighting works in OpenGL (especially understanding the pre-defined variables. is there a list of them somewhere?). I have been messing around in my Vertex Shader file to shade my shpere. So I managed to shade it but I don't know what I did. Here is the code:
Vertex Shader:
#version 330
precision highp float;
in vec3 in_Position; //declare position
in vec3 in_Color; //color passed from the cpp file
//mycode
in vec3 in_Normal;
// mvpmatrix is the result of multiplying the model, view, and projection matrices */
uniform mat4 MVP_matrix;
vec3 ambient;
out vec3 ex_Color;
void main(void) {
// Multiply the MVP_ matrix by the vertex to obtain our final vertex position (mvp was created in *.cpp)
gl_Position = MVP_matrix * vec4(in_Position, 1.0);
//mycode
ambient = vec3(0.0f,0.1f,1.0f); // just a test vector
ambient = ambient * in_Position ; // HERE IS WHAT I DONT GET!
ex_Color = ambient;
}
I am multiplying each point on the sphere with a MVP matrix, and then with an ambient variable.
Fragment Shader:
#version 330
precision highp float;
in vec3 ex_Color;
out vec4 gl_FragColor;
void main(void) {
gl_FragColor = vec4(ex_Color,1.0);
}
What does "precision highp float;" does? I know that the vertex file passes the color to the fragment shader and then that fragment gets "interpolated". Ok how does interpolation works? How does Opengl know where to stop the interpolation ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是我们两个人。我不知道你想用这个来实现什么目的。那肯定会产生一种颜色。但这不会有任何实际意义。它当然不符合“照明”的资格。
没有什么。不在桌面 OpenGL 中。我真的不知道为什么有人把它放在那里;
precision highp
内容是为了 GL ES 兼容性,但 3.30 版着色器从根本上与 GL ES 2.0 不兼容,因为 ES 2.0 不使用in/out
限定符和桌面 GLSL 3.30 确实如此。太宽泛了,无法在这里回答。这是这里更好的答案,这是我的大部分内容的一部分更大的教程系列。
三角形的边。
That makes two of us. I have no idea what it is that you intend to accomplish with this. That will certainly produce a color. But it won't be meaningful in any real way. It certainly doesn't qualify as "lighting".
Nothing. Not in desktop OpenGL. I really have no idea why someone put that there; the
precision highp
stuff is for GL ES compatibility, but version 3.30 shaders are fundamentally incompatible with GL ES 2.0, since ES 2.0 doesn't usein/out
qualifiers and desktop GLSL 3.30 does.Way too broad to answer here. This is better answered here, which is part of my much larger tutorial series.
The edge of your triangle.