Graphics Reference
In-Depth Information
derived from a generic graphics application (
GApp
) class: The
App
contains a ref-
erence to an indexed-face-set model and a single directional light (specified by its
direction and radiance), values for the diffuse color of the surface and the diffuse
reflection coefficient, and a reference to a shader object.
When the
run()
method on a
GApp
is invoked, it first invokes
GInit
, and
then repeatedly invokes
onGraphics()
, whose job is to describe what should be
rendered.
As you can see, the initialization of the application instance is fairly straight-
forward: In lines 20 and 21, we assign a diffuse reflectance and color to be used
for a surface, and create a representation of a directional light (a direction and
radiance value).
During initialization, G3D's
Shader
class is used to read the vertex and pixel
shaders (as text) from their text files (line 25), and we load a model of an icosahe-
dron from a file (line 26), and set the camera's position and view (lines 28 and 29).
At each frame, the
onGraphics
method is called (see Listing 33.2). The
setProjectionAndCameraMatrix
method (line 2) invokes several GL operations
to establish values for predefined variables like
gl_ModelViewProjectionMatrix
.
The next two lines clear the image on the GPU to a constant color.
Listing 33.2: The graphics-drawing procedure and
main
.
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
void App::onGraphics(RenderDevice
*
rd, Array<SurfaceRef>& posed3D){
rd->setProjectionAndCameraMatrix(defaultCamera);
rd->setColorClearValue(Color3(0.1f, 0.2f, 0.4f));
rd->clear(true, true, true);
rd->pushState(); {
Surface::Ref surface = model->pose(G3D::CoordinateFrame());
// Enable the shader
configureShaderArgs(light);
rd->setShader(myShader);
// Send model geometry to the graphics card
rd->setObjectToWorldMatrix(surface->coordinateFrame());
surface->sendGeometry(rd);
} rd->popState();
}
void App::configureShaderArgs() {
myShader->args.set("wsLight",light.position.xyz().direction());
myShader->args.set("lightColor", light.color);
myShader->args.set("wsEyePosition",
defaultCamera.coordinateFrame().translation);
myShader->args.set("diffuseColor", diffuseColor);
myShader->args.set("diffuse", diffuse);
}
G3D_START_AT_MAIN();
int main(int argc, char
**
argv) {
return App().run();
}