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 10 pps
MIỄN PHÍ
Số trang
36
Kích thước
996.7 KB
Định dạng
PDF
Lượt xem
1123

beginning opengl game programming 2004 phần 10 pps

Nội dung xem thử

Mô tả chi tiết

CChessBoard class is initialized, allowing the chessboard to be set up and all of the pieces to

be positioned correctly and their models loaded.

Finally, we “attach” the CChessGame class pointer to the CGfxOpenGL class, which needs to know

about the data stored in CChessGame in order to render the chessboard and pieces correctly.

Next we have the main game loop, whose sequence diagram is shown in Figure 13.3. The

CGfxOpenGL class is used as the entry point into the rest of the game software. It is here that

we call the Update() method of the CChessGame class, where we then proceed to update chess

piece model animations, piece movements, captures, the game board, and the overall

game state.

After performing the data update for the current frame, we then render it with the

Render() method of CGfxOpenGL. Let’s take a look at this method.

Using OpenGL in the Game 279

Figure 13.3 Update sequence diagram.

Figure 13.2 Initialize sequence diagram.

13 BOGL_GP CH13 3/1/04 10:17 AM Page 279

TLFeBOOK

Using OpenGL in the Game

The Render() method in the CGfxOpenGL class is the entry point for all rendering function￾ality in the game.

void CGfxOpenGL::Render()

{

glClearColor(0.0f, 0.0, 0.0, 0.0);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT |

GL_STENCIL_BUFFER_BIT);

glLoadIdentity();

if (m_currentView == WHITE)

gluLookAt(m_whiteViewPos.x, m_whiteViewPos.y,

m_whiteViewPos.z, 4.0, 0.0, 4.0, 0.0, 1.0, 0.0);

else

gluLookAt(m_blackViewPos.x, m_blackViewPos.y,

m_blackViewPos.z, 4.0, 0.0, 4.0, 0.0, 1.0, 0.0);

In this first block of code, you can see that we clear the color, depth, and stencil buffer bits,

load the identity matrix, and set the camera position based on the current player. We clear

the stencil buffer bit because we use the stencil buffer to properly render piece reflections

on the chessboard.

// render the wood table

glDisable(GL_DEPTH_TEST);

RenderTable();

glEnable(GL_DEPTH_TEST);

In this section we draw the background wood table with the RenderTable() method. The

background table is drawn primarily for aesthetic purposes, but since we are drawing

piece reflections on the chessboard, we need to disable depth testing while drawing it; oth￾erwise, the piece reflections will not look correct as the background table will mix into the

reflected piece rendering.

// prepare to write to the stencil buffer by turning off

// writes to the color and depth buffer

glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);

glDepthMask(GL_FALSE);

// setup the stencil func and op to place a 1 in the stencil buffer

// everywhere we’re about to draw

glEnable(GL_STENCIL_TEST);

glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);

glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);

280 Chapter 13 ■ The Endgame

13 BOGL_GP CH13 3/1/04 10:17 AM Page 280

TLFeBOOK

// render the chess board surface. Since the depth and

// color buffers are disabled,

// only the stencil buffer will be modified

RenderChessBoard();

// turn color and depth buffers back on

glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

glDepthMask(GL_TRUE);

// from this point on, only draw where stencil buffer is set to 1

glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);

// don’t modify the contents of the stencil buffer

glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

The preceding section of code is responsible for setting up and rendering the chessboard

to the stencil buffer. The stencil buffer is used as a cutout for determining what is actually

rendered to the screen.

// draw reflected chess pieces first

glPushMatrix();

glScalef(1.0, -1.0, 1.0);

RenderPieces();

glPopMatrix();

// draw chessboard and selection square with blending

glEnable(GL_BLEND);

RenderSelections();

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_DST_ALPHA);

RenderChessBoard();

glDisable(GL_BLEND);

// turn off stencil testing

glDisable(GL_STENCIL_TEST);

With stencil testing enabled, we draw the reflected pieces and the chessboard. The stencil

testing prevents anything rendered at this step from being drawn outside the bounds of

the chessboard that we rendered onto the stencil buffer.

// draw pieces normally

glPushMatrix();

glColor4f(1.0, 1.0, 1.0, 1.0);

RenderPieces();

glPopMatrix();

}

Using OpenGL in the Game 281

13 BOGL_GP CH13 3/1/04 10:17 AM Page 281

TLFeBOOK

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