Graphics Reference
In-Depth Information
Most programmers wrap the underlying hardware API with their own layer
that is easier to use and provides type safety and memory management. This also
has the advantage of abstracting the renderer from the specific hardware API. Most
console, OS, and mobile device vendors intentionally use equivalent but incom-
patible hardware rendering APIs. Abstracting the specific hardware API into a
generic one makes it easier for a single code base to support multiple platforms,
albeit at the cost of one additional level of function invocation.
For Listing 15.30, we wrote to one such platform abstraction instead of
directly to a hardware API. In this code, the VertexBuffer class is a managed
memory array in device RAM and AttributeArray and IndexArray are subsets
of a VertexBuffer . The “vertex” in the name means that these classes store per-
vertex data. It does not mean that they store only vertex positions—for example,
the m_normal array is stored in an AttributeArray . This naming convention is
a bit confusing, but it is inherited from OpenGL and DirectX. You can either trans-
late this code to the hardware API of your choice, implement the VertexBuffer
and AttributeArray classes yourself, or use a higher-level API such as G3D
that provides these abstractions.
Listing 15.30: Host code for an indexed triangle mesh (equivalent to a set of
Triangle instances that share a BSDF ).
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
class Mesh {
private :
AttributeArray
m_vertex;
AttributeArray
m_normal;
IndexStream
m_index;
shared_ptr< BSDF >
m_bsdf;
public :
Mesh() {}
Mesh( const std::vector< Point3 >& vertex,
const std::vector< Vector3 >& normal,
const std::vector< int >& index, const shared_ptr< BSDF >& bsdf) : m_bsdf(bsdf) {
shared_ptr<VertexBuffer> dataBuffer =
VertexBuffer::create((vertex.size() + normal.size()) *
sizeof ( Vector3 )+ sizeof (int) * index.size());
m_vertex = AttributeArray(&vertex[0], vertex.size(), dataBuffer);
m_normal = AttributeArray(&normal[0], normal.size(), dataBuffer);
m_index = IndexStream(&index[0], index.size(), dataBuffer);
}
...
};
/ ** The rendering API pushes us towards a mesh representation
because it would be inefficient to make per-triangle calls. * /
class MeshScene {
public :
std::vector< Light >
lightArray;
std::vector<Mesh>
meshArray;
};
 
Search WWH ::




Custom Search