Shaders_练习三

使用out关键字把顶点位置输出到片段着色器,并将片段的颜色设置为与顶点位置相等(来看看连顶点位置值都在三角形中被插值的结果)。做完这些后,尝试回答下面的问题:为什么在三角形的左下角是黑的?

 1 // Vertex shader:
 2 // ==============
 3 #version 330 core
 4 layout (location = 0) in vec3 aPos;
 5 layout (location = 1) in vec3 aColor;
 6 
 7 // out vec3 ourColor;
 8 out vec3 ourPosition;
 9 
10 void main()
11 {
12     gl_Position = vec4(aPos, 1.0); 
13     // ourColor = aColor;
14     ourPosition = aPos;
15 }
16 
17 // Fragment shader:
18 // ================
19 #version 330 core
20 out vec4 FragColor;
21 // in vec3 ourColor;
22 in vec3 ourPosition;
23 
24 void main()
25 {
26     FragColor = vec4(ourPosition, 1.0);    // note how the position value is linearly interpolated to get all the different colors
27 }
28 
29 /* 
30 Answer to the question: Do you know why the bottom-left side is black?
31 -- --------------------------------------------------------------------
32 Think about this for a second: the output of our fragment's color is equal to the (interpolated) coordinate of 
33 the triangle. What is the coordinate of the bottom-left point of our triangle? This is (-0.5f, -0.5f, 0.0f). Since the
34 xy values are negative they are clamped to a value of 0.0f. This happens all the way to the center sides of the 
35 triangle since from that point on the values will be interpolated positively again. Values of 0.0f are of course black
36 and that explains the black side of the triangle.
37 */
View Code

答:颜色的范围是0.0f-1.0f,由于左下角的坐标都为负数,所以被置为0.0f,(0.0f,0.0f,0.0f)的颜色就为黑色。

2019/11/27

原文地址:https://www.cnblogs.com/ljy08163268/p/11943663.html