# Drawing in Space: Geometric Primitives and Buffers

• Print
This chapter is from the book

## Setting Up a 3D Canvas

Figure 3.1 shows a simple viewing volume that we use for the examples in this chapter. The area enclosed by this volume is a Cartesian coordinate space that ranges from –100 to +100 on all three axes—x, y, and z. (For a review of Cartesian coordinates, see Chapter 1, "Introduction to 3D Graphics and OpenGL.") Think of this viewing volume as your three-dimensional canvas on which you draw with OpenGL commands and functions.

We established this volume with a call to glOrtho, much as we did for others in the preceding chapter. Listing 3.1 shows the code for the ChangeSize function that is called when the window is sized (including when it is first created). This code looks a little different from that in preceding chapter, and you'll notice some unfamiliar functions (glMatrixMode, glLoadIdentity). We spend more time on these functions in Chapter 4, exploring their operation in more detail. Figure 3.1 Cartesian viewing volume measuring 100x100x100.

#### Listing 3.1 Code to Establish the Viewing Volume in Figure 3.1

```// Change viewing volume and viewport. Called when window is resized
void ChangeSize(GLsizei w, GLsizei h)
{
GLfloat nRange = 100.0f;

// Prevent a divide by zero
if(h == 0)
h = 1;

// Set Viewport to window dimensions
glViewport(0, 0, w, h);

// Reset projection matrix stack
glMatrixMode(GL_PROJECTION);

// Establish clipping volume (left, right, bottom, top, near, far)
if (w <= h)
glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
else
glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);

// Reset Model view matrix stack
glMatrixMode(GL_MODELVIEW);