Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

beginning opengl game programming 2004 phần 5 ppt
MIỄN PHÍ
Số trang
34
Kích thước
745.4 KB
Định dạng
PDF
Lượt xem
1391

beginning opengl game programming 2004 phần 5 ppt

Nội dung xem thử

Mô tả chi tiết

133

Bitmaps and Images

with OpenGL

chapter 6

Now it’s time to break off from the world of 3D graphics and take a look at the

world of raster graphics, which are graphics in which an image is composed of

an array of pixels arranged in rows and columns. In this chapter you’ll be look￾ing specifically at how you can use OpenGL to perform various functions on bitmaps and

images. We’ll also be discussing how to load and save the Targa (.tga) image file format.

In this chapter you will discover:

■ How to use OpenGL bitmaps

■ OpenGL pixel functions

■ How to load and save the Targa image format

The OpenGL Bitmap

The term bitmap in the context of OpenGL is defined as a rectangular array of pixels,

where one bit of information (a 0 or 1) is stored about each pixel. Bitmaps are composed

of a mask encapsulated in a two-dimensional array representing a rectangular area of the

window. You can use them for rendering font characters, 2D objects in a game, or as ele￾ments in a GUI. For instance, take a 16 × 16 bitmap and divide it into a 16 × 16 grid as

shown in Figure 6.1. When you draw this bitmap, every bit in the two-dimensional array

that is set to 1 corresponds to a pixel in the current raster color on the screen. If the bit is

06 BOGL_GP CH06 3/1/04 10:01 AM Page 133

TLFeBOOK

not set, then nothing is drawn. Given this behavior, the

16 × 16 bitmap shown in Figure 6.1 will be drawn by

OpenGL as the letter X. You will see an example in this

chapter that does something similar.

Actually specifying the bitmap data is slightly different

than that shown in Figure 6.1. Bitmap data is always

stored in 8-bit multiple chunks, although the width of

the actual bitmap does not need to be a multiple of 8.

OpenGL draws the bitmap by starting at the lower-left

corner and working its way up, row by row. Therefore,

you need to specify your bitmap’s data in this order, so

that the bottom of the bitmap is the first set of data and

the top of the bitmap is the last set of data.

An example of bitmap data for Figure 6.1 in code looks like:

unsigned char bitmapX[] = {

0x80, 0x01, // 1000 0000 0000 0001

0x40, 0x02, // 0100 0000 0000 0010

0x20, 0x04, // 0010 0000 0000 0100

0x10, 0x08, // 0001 0000 0000 1000

0x08, 0x10, // 0000 1000 0001 0000

0x04, 0x20, // 0000 0100 0010 0000

0x02, 0x40, // 0000 0010 0100 0000

0x01, 0x80, // 0000 0001 1000 0000

0x01, 0x80, // 0000 0001 1000 0000

0x02, 0x40, // 0000 0010 0100 0000

0x04, 0x20, // 0000 0100 0010 0000

0x08, 0x10, // 0000 1000 0001 0000

0x10, 0x08, // 0001 0000 0000 1000

0x20, 0x04, // 0010 0000 0000 0100

0x40, 0x02, // 0100 0000 0000 0010

0x80, 0x01, // 1000 0000 0000 0001

};

Positioning the Bitmap

The glRasterPos() function specifies the current raster coordinates for drawing bitmaps

in the OpenGL window. The coordinates sent to the function define the bottom-left

corner of the bitmap’s rectangle. For example, passing the coordinates (30, 10) to the

134 Chapter 6 ■ Bitmaps and Images with OpenGL

Figure 6.1 A 16 × 16 bitmap

divided into a grid of zeroes and ones.

06 BOGL_GP CH06 3/1/04 10:01 AM Page 134

TLFeBOOK

glRasterPos() function draws the next bitmap with its bottom-left corner at (30, 10). The

function is defined as:

void glRasterPos{234}{sifd}(TYPE x, TYPE y, TYPE z, TYPE w);

void glRasterPos{234}{sifd}v(TYPE *coords);

To set the current raster coordinates to (30, 10), you would call the function like this:

glRasterPos2i(30, 10);

When setting the raster coordinates in a 3D viewport and projection matrix, the coordi￾nates sent to glRasterPos() are converted to 2D screen coordinates, in much the same way

as when you use the glVertex() function. If you want to specify the raster coordinates in

screen coordinates, then you need to set up a 2D viewport and projection matrix with the

width and height of the viewport equal to the width and height of the OpenGL window.

You can use the glOrtho() or gluOrtho2D() function to define a 2D viewport with ortho￾graphic projection, as described in Chapter 4, “Transformations and Matrices.”

For error checking, you can find out if the raster position you passed to the function is a

valid raster position by passing the GL_CURRENT_RASTER_POSITION_VALID parameter to the

glGetBooleanv() function. If the function returns GL_FALSE, the position is invalid.

If you would like to obtain the current raster position, you can simply pass

GL_CURRENT_RASTER_POSITION as the first parameter to glGetFloatv(). The second parameter

should be a pointer to an array of floats to hold the (x, y, z, w) values that are returned by

glGetFloatv().

Drawing the Bitmap

After you set the current raster position, you can draw your bitmap with the glBitmap()

function, which is defined as:

void glBitmap(GLsizei width, GLsizei height, GLfloat xOrigin, GLfloat yOrigin,

GLfloat xIncrement, GLfloat yIncrement, const GLubyte *bitmap);

This function draws a bitmap with the specified width and height in pixels at the coordi￾nates (xOrigin, yOrigin) relative to the current raster position. The values xIncrement and

yIncrement specify step increments that are added to the current raster position after the

bitmap is drawn. Figure 6.2 shows how a bitmap is affected by these parameters.

Note

One drawback to OpenGL bitmaps is that you can neither rotate nor zoom them, but you can do

these operations with pixel maps, or images, as you will soon see.

The OpenGL Bitmap 135

06 BOGL_GP CH06 3/1/04 10:01 AM Page 135

TLFeBOOK

Tải ngay đi em, còn do dự, trời tối mất!