Graphics Reference
In-Depth Information
interests of saving space and avoiding repetition, the details of the OpenGL im-
plementation are not listed. Readers are encouraged to read the source code and
explore similarities. The run-time screenshots from these tutorials are identical to
the previous two.
Tutorials 3.5 and 3.6. The Circle Class
Tutorial 3.5.
Project Name
D3D _ CircleClass
Goal. Remind ourselves of an important strength of object-based design,
where new types of objects with similar behaviors (public methods) can be
easily integrated into the application.
Tutorial 3.6.
Project Name
OGL _ CircleClass
Approach. The structure of Listing 3.5 suggests that additional and differ-
ent geometric types can be inserted in between BeginDeviceDraw() and
EndDeviceDraw() . A circle geometric shape can support public methods
similartothatofa CRectangle2D and thus can be integrated into Tuto-
rial 3.3 in a straightforward manner.
θ
Figure 3.8 shows how to approximate a circle with 12 triangles. From trigonom-
etry, we know that with a radius r ,thevertex
(
x
,
y
)
positions of the triangles can
x = r × cos(θ)
y = r × sin(θ)
be defined by
θ
, the angular offset from the x -axis, where:
x
=
r
×
cos
( θ ) ,
Figure 3.8. Approximat-
ing a circle with 12 trian-
gles.
y
=
r
×
sin
( θ ) .
This means that if we know how to draw a triangle, then we can approximate a
circle. Recall that the CRectangle2D class approximates a rectangle with two
triangles. Therefore, we know how to draw circles with both D3D and OpenGL.
Figure 3.9 is a screenshot of running Tutorial 3.5. Notice the circle on the
output window.
This circle is an instance of the CCircle2D class.
We have
defined the public methods of the CCircle2D class to be similar to that of the
CRectangle2D class. The significant difference is the Draw() function. In this
case, we represented the circle with 100 triangles.
Figure 3.9.
Tutorial 3.5.
class CCircle2D {
public :
. // constructor and destructor (not shown)
void SetCenter( float x, float y);
void SetRadius( float radius);
void Draw( LPDIRECT3DDEVICE9 pDevice ) const ;
Source file.
Circle2D.h/.cpp file in
the Primitives folders of the
D3D _ CircleClass project.
Listing 3.7. D3D Circle class (Tutorial 3.5).
Search WWH ::




Custom Search