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

beginning opengl game programming 2004 phần 10 pps

36 403 0

Đ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

Định dạng
Số trang 36
Dung lượng 0,97 MB

Nội dung

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); Chapter 13 ■ The Endgame280 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 And finally, with stencil test- 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. 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. Chapter 13 ■ The Endgame282 Figure 13.4 A screenshot of the chess game. 13 BOGL_GP CH13 3/1/04 10:17 AM Page 282 TLFeBOOK Appendices Appendix A Answers to Review Questions and Exercises . . . . . . . . . . . . . . . . . . . .285 Appendix B Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .295 Appendix C What’s on the CD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .299 PART III 14 BOGL_GP AppA 3/1/04 10:17 AM Page 283 TLFeBOOK This page intentionally left blank TLFeBOOK 285 Answers to Review Questions and Exercises appendix A Chapter 1 Review Questions 1. 1992 2. At the time of writing (early 2004), OpenGL’s latest release is 1.5. 3. The OpenGL Architectural Review Board. On Your Own 1. glColor3f(1.0, 0.0, 0.0) should be used for all triangle vertices, and glColor3f(0.0, 0.0, 1.0) should be used for all polygon vertices. Chapter 2 Review Questions 1. A rendering context connects OpenGL to a window. 2. wglGetCurrentContext() 3. A PIXELFORMATDESCRIPTOR is a struct that defines the characteristics and behavior of the OpenGL rendering context. 4. glClearColor() clears the background color of the OpenGL window. 5. The DEVMODE struct 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 glColor3f(1.0, 0.0, 0.0) ;. 14 BOGL_GP AppA 3/1/04 10:17 AM Page 285 TLFeBOOK Chapter 3 Review Questions 1. Pass GL_LINE_SMOOTH to glIsEnabled() ; glIsEnabled(GL_LINE_SMOOTH) . 2. glEnable(GL_CULL_FACE) ; 3. OpenGL draws the first three vertices as a single triangle; after that, it takes every vertex specified and combines it with the previous two vertices to create another triangle. In general, every set of n triangles you can reduce to a triangle strip reduces the number of vertices from 3n to n + 2. 4. The first three vertices define a triangle, and each subsequent vertex defines a new triangle with the previous vertex and the first vertex. Fans allow you to draw n tri- 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); else glBegin(GL_POLYGON); // calculate each vertex on the circle for (int vertex = 0; vertex < numberOfSides; vertex++) { // calculate the angle of the current vertex // (vertex # * 2 * PI) / # of sides float angle = (float)vertex * 2.0 * 3.14159 / numberOfSides; // draw the current vertex at the correct radius glVertex3f(cosf(angle)*radius, 0.0, sinf(angle)*radius); } Appendix A ■ Answers to Review Questions and Exercises286 14 BOGL_GP AppA 3/1/04 10:17 AM Page 286 TLFeBOOK // if drawing edge only, then need to complete the loop with first vertex if (edgeOnly) glVertex3f(radius, 0.0, 0.0); glEnd(); } Chapter 4 Review Questions 1. glTranslatef(29.0, 3.0, 15.0) ; 2. glRotatef(45.0, 1.0, 0.0, 0.0) ; 3a. glScalef(3.0, 3.0, 3.0) ; 3b. glScalef(0.5, 0.5, 0.5) ; 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 cube glTranslatef(xPos, yPos, zPos); // perform the rotations glRotatef(xAngle, 1.0, 0.0, 0.0); glRotatef(yAngle, 0.0, 1.0, 0.0); glRotatef(zAngle, 0.0, 0.0, 1.0); // draw the cube DrawCube(); glPopMatrix(); } Answers to Review Questions and Exercises 287 14 BOGL_GP AppA 3/1/04 10:17 AM Page 287 TLFeBOOK Chapter 5 Review Questions 1. All OpenGL implementations are required to provide at least eight lights. You can find out how many are available by passing GL_MAX_LIGHTS to glGet() . 2. By passing an array holding the colors to glFog() with GL_FOG_COLOR . 3. glEnable(GL_COLOR_MATERIAL) ; glColorMaterial(GL_FRONT_AND_BACK, GL_SPECULAR) ; 4. False. It can be taken advantage of by using the secondary color. 5. b. GL_ALPHA_SATURATE On Your Own 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 glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 128.0); • Add a transparent sphere surrounding the cube, and set the material for it using color tracking. // after the cube has been drawn glEnable(GL_COLOR_MATERIAL); glColor4f(0.8f, 0.6f, 0.0f, 0.7f); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); gluSphere(m_pSphere, CUBE_SIZE, 64, 32); glDisable(GL_BLEND); glDisable(GL_COLOR_MATERIAL); Appendix A ■ Answers to Review Questions and Exercises288 14 BOGL_GP AppA 3/1/04 10:17 AM Page 288 TLFeBOOK [...]... related libraries, 9 10 lighting, 104 –121 See also materials; normals; positional lights; spotlights attenuation, 108 109 default color for, 108 demo for, 120 directional lights, 107 108 eight lights, availability of, 106 flashlights, 110 111 moving lights, 110 111 positional lights, 107 108 properties, assigning, 106 107 in real world, 104 106 rotating lights, 110 111 sources of light, 106 109 three lights,... $29.99 Game Programming for Teens ISBN: 1-59200-068-1 $29.99 Check out advanced books and the full Game Development series at WWW.COURSEPTR.COM/GAMEDEV Call 1.800.354.9706 to order Order online at www.courseptr.com TLFeBOOK Professional ■ Trade ■ Reference GOT GAME? COMING SPRING 2004! 3D Game Programming All in One Beginning OpenGL Game Programming The Dark Side of Game Texturing 3D Game Engine Programming. .. 198–201 color masking, 265 color matrix stack, 82 color-index mode, 100 colors, 99 102 See also blending; lighting; shading alpha value, 100 color-index mode, 100 interleaved arrays and, 237 material colors, 112–113 primary color, specifying, 100 101 rendering, 30 RGBA mode, 100 secondary color, tracking, 101 102 setting colors, 100 101 for specular highlight, 120 texture combiners and, 214 tracking... Layer), 10 SetupPixelFormat() function, 22–23 SetupProjection() function, 27 shaders, 9 shading, 102 104 example, 104 flat shading, 102 104 smooth shading, 102 , 103 shininess of materials, 113–114 ShowCursor() function, 32–33 shrinking objects, 80 shutdown in CGfxOpenGL class, 27 MainWindowProc() function in, 23–24 Shutdown() function, 27 Sikora, Michael, 4 Silicon Graphics, Inc (SGI), 7 simplistic game. .. Reference RISE TO THE TOP OF YOUR GAME WITH COURSE PTR! Check out more titles in the Beginning series from Course PTR—full of tips and techniques for the game developers of tomorrow! Perfect your programming skills and create eye-catching art for your games to keep players coming back for more Beginning C++ Game Programming ISBN: 1-59200-205-6 $29.99 Game Art for Teens Beginning DirectX 9 ISBN: 1-59200-307-9... of them using OpenGL TLFeBOOK Books 297 OpenGL OpenGL has an active and enthusiastic online community of game and graphics programmers 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... 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 TLFeBOOK INDEX A accumulation buffer, 271–275 example of, 272–275 jitter of light position, 275 operations, 271 active texture units, 209– 210 AGP bus, 141 alpha tests for color buffer, 263–264 alpha values, 100 ... Reading 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... 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... simplistic game architecture, 5 skyboxes, 179 smooth shading, 102 , 103 smoothing groups for normals, 117 sounds, 5 source code on CD, 299 source fragments, 122 specular highlight, 113–114 color, setting, 120 local viewpoint and, 119 specular light, 105 , 108 specular materials, 111 sphere mapping, 205 spotlights, 109 –111 direction of, 110 focus of, 110 squares, quadrilaterals for, 59 state machine, 35–39 . many advanced graphics and game programming topics, many of them using OpenGL. Appendix B ■ Further Reading296 15 BOGL_GP AppB 3/1/04 10: 18 AM Page 296 TLFeBOOK OpenGL OpenGL has an active and. 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 showcase your games. 15 BOGL_GP AppB 3/1/04 10: 18 AM Page 295 TLFeBOOK Garage Games http://www.garagegames.com Founded and operated by the developers of the Tribes franchise, Garage Games offers affordable

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

TỪ KHÓA LIÊN QUAN