理解 OpenGL 中的光照问题

发布于 2024-12-15 01:16:36 字数 1099 浏览 0 评论 0原文

我在理解 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痞味浪人 2024-12-22 01:16:36
ambient = ambient * in_Position ; // HERE IS WHAT I DONT GET!

这就是我们两个人。我不知道你想用这个来实现什么目的。那肯定会产生一种颜色。但这不会有任何实际意义。它当然不符合“照明”的资格。

“ precision highp float;”是什么意思是吗?

没有什么。不在桌面 OpenGL 中。我真的不知道为什么有人把它放在那里; precision highp 内容是为了 GL ES 兼容性,但 3.30 版着色器从根本上与 GL ES 2.0 不兼容,因为 ES 2.0 不使用 in/out 限定符和桌面 GLSL 3.30 确实如此。

好的,插值是如何工作的?

太宽泛了,无法在这里回答。这是这里更好的答案,这是我的大部分内容的一部分更大的教程系列。

Opengl 如何知道在哪里停止插值?

三角形的边。

ambient = ambient * in_Position ; // HERE IS WHAT I DONT GET!

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".

What does "precision highp float;" does?

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 use in/out qualifiers and desktop GLSL 3.30 does.

Ok how does interpolation works?

Way too broad to answer here. This is better answered here, which is part of my much larger tutorial series.

How does Opengl know where to stop the interpolation ?

The edge of your triangle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文