1. Trang chủ
  2. » Công Nghệ Thông Tin

beginning opengl game programming 2004 phần 10 pps

36 410 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Beginning OpenGL Game Programming 2004 Part 10 Pps
Thể loại Sách hướng dẫn
Năm xuất bản 2004
Định dạng
Số trang 36
Dung lượng 0,97 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Since the depth and // color buffers are disabled,// only the stencil buffer will be modifiedRenderChessBoard; // turn color and depth buffers back onglColorMaskGL_TRUE, GL_TRUE, GL_TRUE

Trang 1

CChessBoardclass 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 CChessGameclass pointer to the CGfxOpenGLclass, which needs to know

about the data stored in CChessGamein 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

CGfxOpenGLclass is used as the entry point into the rest of the game software It is here that

we call the Update()method of the CChessGameclass, 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 ofCGfxOpenGL Let’s take a look at this method.

Figure 13.2 Initialize sequence diagram.

Trang 2

Using OpenGL in the Game

TheRender()method in the CGfxOpenGLclass is the entry point for all rendering ality in the game.

function-void CGfxOpenGL::Render(){

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

elsegluLookAt(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

// 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);

Trang 3

// render the chess board surface Since the depth and // color buffers are disabled,

// only the stencil buffer will be modifiedRenderChessBoard();

// turn color and depth buffers back onglColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

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 firstglPushMatrix();

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 normallyglPushMatrix();

glColor4f(1.0, 1.0, 1.0, 1.0);

RenderPieces();

Trang 4

And finally, with stencil ing disabled, we render the chess pieces normally The result is a set of chess pieces reflecting off a marble-look- ing chessboard sitting on a table, as shown in Figure 13.4.

test-Summary

The rest of the code for the chess game deals with piece movement algorithms, model rendering and loading, and the various state machines involved We invite you to browse the source code in detail, experiment with it, learn from it, and even make your own derivative!

More than anything, we hope you’ve learned a lot about OpenGL from this book and you enjoy using OpenGL as much as we have Just remember, in today’s world of 3D graphics, anything is possible!

What You Have Learned

■ The chess game is designed to be easily portable.

■ The chessboard is rendered to the stencil buffer to aid in rendering the chess piece reflections properly.

Review Questions

No review questions for this chapter.

On Your Own

1 Right now the chess game does not verify check or checkmate Add this

functional-ity to the chess game.

2 The chess game does not have a menu, nor does it display any statistics during the game Write code to display the current player, “White” or “Black,” on the screen, and add functionality for a basic menu.

3 In its current form, the game switches between only two views Add code to view closeups of piece capture moves, rotate views of the chessboard, and zoom the view.

Figure 13.4 A screenshot of the chess game.

Trang 7

2 At the time of writing (early 2004), OpenGL’s latest release is 1.5.

3 The OpenGL Architectural Review Board.

4. glClearColor()clears the background color of the OpenGL window.

5 The DEVMODEstruct is required to set up fullscreen mode.

On Your Own

1 The following should be changed:glClearColor(1.0, 1.0, 1.0, 1.0); and

Trang 8

angles while specifying only n + 2 vertices.

5 a Three coordinate vertex with float data type

b Two coordinate vertex with integer data type, passed as an array

c Four coordinate vertex with double data type

d Three coordinate vertex with float data type, passed as an array

e Two coordinate vertex with short data type

On Your Own

1 Answers may vary.

void DrawCircleApproximation(double radius, int numberOfSides){

// if edge only, use line strips; otherwise, use polygons

if (edgeOnly)glBegin(GL_LINE_STRIP);

elseglBegin(GL_POLYGON);

// calculate each vertex on the circlefor (int vertex = 0; vertex < numberOfSides; vertex++){

// calculate the angle of the current vertex

// (vertex # * 2 * PI) / # of sidesfloat angle = (float)vertex * 2.0 * 3.14159 / numberOfSides;

// draw the current vertex at the correct radiusglVertex3f(cosf(angle)*radius, 0.0, sinf(angle)*radius);

}

Trang 9

// if drawing edge only, then need to complete the loop with first vertex

if (edgeOnly)glVertex3f(radius, 0.0, 0.0);

4 Modelview matrix stack, projection matrix stack, texture matrix stack, color matrix stack

5. glLoadIdentity()

6 Save and restore the current matrix on the matrix stack.

On Your Own

1 Answers may vary.

void PositionAndRotate(float xPos, float yPos, float zPos, float xAngle, float yAngle, float zAngle)

{glPushMatrix();

// position the cubeglTranslatef(xPos, yPos, zPos);

// perform the rotationsglRotatef(xAngle, 1.0, 0.0, 0.0);

Trang 10

1 Answers may vary.

• Add an emissive property to the cube’s material.

// set up the cube’s material

GLfloat emmisive[] = { 0.2f, 0.2f, 0.2f, 1.0};

glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emmisive);

• Add attenuation to the red light.

// set up static red light

glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 0.5f);

glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.1f);

• Make the beam of the flashlight more focused.

// set up the flashlight

Trang 12

2 Presently, Windows headers and libraries for OpenGL only support OpenGL 1.1,

so OpenGL 1.2, 1.3, 1.4, and 1.5 features have to be accessed via extensions.

3 By checking the extensions string for the name of the extension Alternatively, you can try obtaining a pointer from wglGetProcAddress(), with a null pointer indicating failure.

4 Any time you are accessing core functions using function names that do not include the extension suffix.

On Your Own

1 Answers will vary The program should make use ofglGetString(GL_EXTENSIONS)and

wglGetExtensionsString().

Trang 13

Chapter 9

Review Questions

1 Creating a new texture requires memory to be allocated, which can be slow.

2 There is one for every supported texture unit.

3. GL_OBJECT_LINEARandGL_EYE_LINEARboth make use of planes defined through

GL_OBJECT_PLANEandGL_EYE_PLANE, respectively.

4 You enable a texture unit by assigning a complete, valid texture image to it and enabling the corresponding texture target You disable the unit by disabling all tex- ture targets for it.

5. GL_COMBINE

On Your Own

1 Answers may vary GL_ADD adds the incoming fragment RGB with the texture RGB and multiplies the incoming fragment alpha by the texture alpha This can be done via combiners as follows:

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);

glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_ADD);

glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_MODULATE);

glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);

glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PREVIOUS);

glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE);

glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_TEXTURE);

glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR);

glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA);

glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR);

glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA, GL_SRC_ALPHA);

Chapter 10

Review Questions

1 By passing it to glIsList().

2 It is executed immediately and is not stored as part of the list.

3 By passing the appropriate flags to glEnableClientState().

4 You must first change the currently active texture unit by calling Texture() You can then use glTexCoordPointer()normally to assign an array of tex- ture coordinates.

Trang 14

glClientActive-5 The cost of doing per-triangle frustum culling on the CPU generally exceeds the cost of letting the graphics hardware do it for you.

On Your Own

1 Answers will vary.

// returns 1 if the sphere is in the frustum, -1 if it’s partially within thefrustum, of 0 if it’s completely outside

int SphereInFrustum(sphere_t sphere, frustum_t frustum){

if (dist <= -sphere.radius)return 0;

if (dist > radius) // sphere is doesn’t intersect this planenumOutside++;

}

if (6 == numOutside)return 1;

elsereturn -1;

Trang 15

Chapter 12

Review Questions

1 The depth buffer is 1.0, everything else is 0.

2 Alpha testing is used to accept or reject fragments based on the alpha value Alpha blending combines the incoming fragment with the pixel in the color buffer using the alpha value, but it does not typically reject fragments.

3 Blending is disabled internally.

4 False See the Tip in the section titled “Depth-Comparison Functions.”

5 It is logically ANDed with both the stencil reference value and the value in the stencil buffer before the two values are combined.

1 Answers will vary.

2 Answers will vary.

3 Answers will vary.

Trang 17

Further Reading

appendix B

T his book covered the core topics you need to understand to begin developing

games and graphics applications with OpenGL, but your journey has just begun.

This appendix lists Web sites and books that you may find useful in expanding your knowledge.

Online Resources

There’s no better way to keep up to date on the rapidly changing world of technology than

the Internet We’ve collected a list of the most important Web sites covering game

devel-opment and OpenGL.

Game Development

There are dozens — if not hundreds — of Web sites dedicated to game development The

ones listed here are the cream of the crop.

GameDev.net

http://www.gamedev.net

Co-founded and operated by the authors of this book, GameDev.net is the leading online

resource for game developers Features include thousands of articles, active and helpful

community forums, books and software reviews, daily news, and a place to upload and

showcase your games.

Trang 18

Game Tutorials contains dozens of short tutorial programs covering game development and OpenGL topics The tutorials tend to be light on explanation, but if you want to see how something is done in code it’s a fantastic resource.

Flipcode http://www.flipcode.com

More graphics oriented than GameDev.net and not as frequently updated, but still tains many good articles and a community of knowledgeable developers.

con-Gamasutra

http://www.gamasutra.com

Produced by the people who run the Game Developers Conference and publish Game

Developer Magazine, this site offers articles and other content taken from both the

maga-zine and the conference.

Developer Pages

http://developer.nvidia.com

http://www.ati.com/developer Both NVIDIA and ATI maintain sites containing a vast array of white papers, presenta- tions, and demos covering many advanced graphics and game programming topics, many

of them using OpenGL.

Trang 19

OpenGL has an active and enthusiastic online community of game and graphics

pro-grammers A quick search of the Internet will turn up hundreds — if not thousands — of

pages containing OpenGL information, but we’ve distilled the best of them here.

OpenGL.org

http://www.opengl.org

OpenGL.org is the official site of the OpenGL ARB Besides regular OpenGL-related news,

they maintain several FAQs full of helpful information.

NeHe Productions

http://nehe.gamedev.net

Jeff Molofee has worked hard to make NeHe one of the top OpenGL resources on the

Web In addition to more than 40 original OpenGL tutorials, he’s collected an impressive

suite of demo programs displaying the capabilities of OpenGL He also routinely posts

links and descriptions of new OpenGL online resources.

Delphi3D

http://www.delphi3d.net

Although all the demos are written in Delphi, this site has some of the most useful

arti-cles and demos covering advanced OpenGL topics.

Books

Whether you’re looking for resources to meet the prerequisites for reading this book or

ready to expand your knowledge, the books listed here should prove valuable.

C++

Accelerated C++: Practical Programming by Example

Andrew Koenig, Barbara E Moo, Addison-Wesley, 2000

Trang 20

3D Math

Mathematics for 3D Game Programming & Computer Graphics 2nd Ed.

Eric Lengyel, Charles River Media, 2003

3D Math Primer for Graphics and Game Development

Fletcher Dunn, Ian Parberry, Wordware, 2002

OpenGL

OpenGL Programming Guide 4th Ed.

Woo, Neider, Davis, Shreiner, Addison-Wesley, 2003

OpenGL Extensions Guide

Eric Lengyel, Charles River Media, 2003

Graphics Programming

Real-Time Rendering

Tomas Akenine-Möller, Eric Haines, A.K Peters, 2002

Game Development

Game Programming Gems 1, 2, 3

Marc DeLoura (editor), Dante Treglia (editor), Charles River Media, 2000, 2001, 2002

Core Techniques and Algorithms in Game Programming

Daniel Sanchez-Crespo Dalmau, New Riders, 2003

Game Scripting Mastery

Alex Varanese, Premier Press, 2002

AI Techniques for Game Programming

Mat Buckland, Premier Press, 2002

Trang 21

What’s on the CD

appendix C

T he CD included with this book contains resources intended to be used in

con-junction with the text It includes an auto-installer, so all you have to do is insert the CD into your CD player and the installer will launch itself.

The contents of the CD are detailed in this appendix.

Source Code

Most importantly, the CD includes the full source code for all of the demos used

through-out the book These are arranged by chapter, with each project having its own directory

within the chapter directory.

GLee

Many of the example programs starting in Chapter 8 use the GLee library by Ben

Wood-house for managing extensions We’ve included the latest version of it on the CD.

Trang 22

Bonus Chapters

There were many chapters from OpenGL Game Programming covering topics that are not

covered in this volume and that we don’t intend to cover in future volumes These ters have been included on the CD in Adobe Acrobat format to supplement the material presented here The chapters included are

chap-Using Windows with OpenGL

An Overview of 3D Graphics Theory OpenGL Quadrics

Curves and Surfaces Using DirectX: DirectInput Using DirectX Audio Working with 3D Models Making a Game: A Time to Kill

Bonus Game

In addition to the game included in Chapter 13, “The Endame,” we’re including the source

code for the game from OpenGL Game Programming entitled A Time to Kill.

Trang 23

A

accumulation buffer, 271–275

example of, 272–275jitter of light position, 275operations, 271

active texture units, 209–210

AGP bus, 141

alpha tests for color buffer, 263–264

alpha values, 100 See also blending; Targa files

for color buffer, 263–264

for Disk Blender, 127–128

ambient light, 105, 108

global ambient light, 119

ambient materials, 111

American McGee’s Alice, 201

answers to review questions and exercises, 285–293

antialiasing

lines, 48points, 44polygons, 55

API, working with, 8

blend equations, 125–126 blend factor, 122–123

for fog, 128–129

blending, 121–128 See also blend factor

blend equations, 125–126constant blend color, 127depth buffer and, 124Disk Blender, 127–128separate blend functions, 125transparency and, 123–124

book resources, 297–298 borders of texture map, 156 bound texture objects, 153–154

buffers See also color buffer; depth buffer

accumulation buffer, 271–275clearing, 262

defined, 261–262scissor boxes, 262–263stencil buffer, 268–271

Bungie Software’s Halo, 4

C

C++ resources, 297 Carmack, John, 4

CD information, 299–300

Ngày đăng: 05/08/2014, 10:20

TỪ KHÓA LIÊN QUAN