本文介绍了问题理解 OpenGL 中的光照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解光照在 OpenGL 中的工作方式时遇到了一些问题(尤其是理解预定义的变量.是否有它们的列表?).我一直在我的 Vertex Shader 文件中乱七八糟地为我的 shpere 着色.所以我设法遮蔽它,但我不知道我做了什么.代码如下:

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:

顶点着色器:

#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 矩阵相乘,然后与环境变量相乘.

I am multiplying each point on the sphere with a MVP matrix, and then with an ambient variable.

片段着色器:

#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 如何知道在哪里停止插值?

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 ?

推荐答案

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

precision highp float"是什么意思;是吗?

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

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.

好的插值是如何工作的?

在这里回答太宽泛了.这是这里有更好的回答,这是我的一部分更大的教程系列.

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

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

三角形的边缘.

这篇关于问题理解 OpenGL 中的光照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 13:04