Graphics Reference
In-Depth Information
• Arguments that are constant over all triangles are passed as global (“uni-
form”) variables.
• Points, vectors, and colors are all stored in
vec3
type.
•
const
has different semantics (compile-time constant).
•
in
,
out
, and
inout
are used in place of C++ reference syntax.
•
length
,
dot
, etc. are functions instead of methods on vector classes.
Listing 15.32: Vertex shader for projecting vertices. The output is in
homogeneous space before the division operation. This corresponds to the
perspectiveProject
function from Listing 15.24.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#version 130
// Triangle vertices
in vec3 vertex;
in vec3 normal;
// Camera and screen parameters
uniform
float
fieldOfViewX;
uniform
float
zNear;
uniform
float
zFar;
uniform
float
width;
uniform
float
height;
// Position to be interpolated
out vec3 Pinterp;
// Normal to be interpolated
out vec3 ninterp;
vec4 perspectiveProject(in vec3 P) {
// Compute the side of a square at z = -1 based on our
// horizontal left-edge-to-right-edge field of view .
float
s = -2.0f
*
tan(fieldOfViewX
*
0.5f);
float
aspect = height / width;
// Project onto z = -1
vec4 Q;
Q.x = 2.0
*
-Q.x / s;
Q.y = 2.0
*
-Q.y / (s
*
aspect);
Q.z = 1.0;
Q.w = -P.z;
return
Q;
}
void
main() {
Pinterp = vertex;
ninterp = normal;
gl_Position = perspectiveProject(Pinterp);
}
None of these affect the expressiveness or performance of the basic language.
The specifics of shading-language syntax change frequently as new versions are
released, so don't focus too much on the details. The point of this example is
how the overall form of our original program is preserved but adjusted to the
conventions of the hardware API.