Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 365 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
365
Dung lượng
3,61 MB
Nội dung
OpenGLProgrammingonMacOSX ® ® This page intentionally left blank OpenGLProgrammingonMacOSX ® ® Architecture, Performance, andIntegration Robert P Kuehne J D Sullivan Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests For more information, please contact: U.S Corporate and Government Sales (800) 382-3419 corpsales@pearsontechgroup.com For sales outside of the United States, please contact: International Sales (317) 382-3419 international@pearsontechgroup.com Visit us on the Web: www.awprofessional.com Library of Congress Cataloging-in-Publication Data Kuehne, Robert P OpenGLprogrammingonMacOSX : architecture, performance, andintegration / Robert P Kuehne, J D Sullivan p cm Includes bibliographical references and index ISBN-13: 978-0-321-35652-9 (pbk : alk paper) ISBN-10: 0-321-35652-7 Computer graphics OpenGLMacOS I Sullivan, J D II Title T385.K82 2007 006.6’6 dc22 2007011974 Copyright c 2008 Robert P Kuehne and J D Sullivan All rights reserved Printed in the United States of America This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise For information regarding permissions, write to: Pearson Education, Inc Rights and Contracts Department 501 Boylston Street, Suite 900 Boston, MA 02116 Fax: (617) 671-3447 ISBN 13: 978-0-321-35652-9 ISBN 10: 0-321-35652-7 Text printed in the United States on recycled paper at Donnelley in Crawfordsville, Indiana First printing, December 2007 For my family —Bob For my family —John In memory of democracy This page intentionally left blank Contents List of Figures xv List of Tables xix List of Examples xxi Preface xxv Acknowledgments xxix About the Authors xxxi Chapter MacOpenGL Introduction Why the Mac? .2 Why OpenGL? The Book Chapter OpenGLArchitectureonOSX Overview About OpenGLMacOSX Implementation of the OpenGL Specification .11 OpenGL Feature Support .14 API Layers 15 The MacOSOpenGL Plug-In Architecture 17 Renderers 18 Drivers 21 Summary .21 Chapter Mac Hardware Architecture 23 Overview 23 Data Flow and Limitations 24 Problem Domains 27 Know Thine OS Requirements 27 CPU and Clock Rate 28 vii Bus 28 Video Memory: VRAM 30 RAM 31 Summary .32 Chapter Application ProgrammingonOSX 33 Overview 33 MacOSX Versions 33 System Configuration .34 Power Management .34 Filesystem .38 Finding, Verifying, and Filing Bugs 39 Threading 41 Data Parallel Computation: SIMD 42 PowerPC 42 Intel 43 Chapter OpenGL Configuration andIntegration 45 API Introductions and Overview 46 Mac-Only APIs 46 Cross-Platform APIs Supported on the Mac 48 API Introduction and Overview Wrap-Up 49 Configuration API Relationships .49 Sharing OpenGL Data Between Contexts 51 Framebuffers 53 Chapter The CGL API for OpenGL Configuration 55 Overview 55 Error Handling 57 Pixel Format Selection .57 CGLChoosePixelFormat 58 Policies and Buffer Sizing 59 Render Targets 60 Multisampling 61 Stereo 61 Selecting a Renderer 61 viii Contents Context Management 63 Renderer Information 68 Sharing OpenGL Objects Across CGL Contexts 76 Drawables 77 Pixel Buffers 78 Off-Screen Drawables 82 Full-Screen Drawables 82 Virtual Screen Management 83 Global CGL State Functions 84 Using CGL Macros 86 Summary .86 Chapter The AGL API for OpenGL Configuration 89 Overview 89 Software Layering 90 Pixel Format and Context 91 Full-Screen Application 92 Windowed Application 101 Summary 104 Additional Topics 104 Renderers .104 Context Sharing 107 Alternative Rendering Destinations 109 Off-Screen Surfaces 109 Pbuffers 110 Render-to/Copy-to-Texture 114 Framebuffer Objects 117 Summary 120 Chapter The Cocoa API for OpenGL Configuration 121 Overview 122 NSOpenGLView 122 NSView 133 Additional Topics 140 Manipulating Images and Pixels in OpenGL 140 Context Sharing 141 Full-Screen Surfaces .149 Contents ix You can a lot more with FBOs, including capturing other rendering results such as depth, stencil, and other render targets, but this kind of advanced usage is beyond the scope of this book We refer you to the OpenGL framebuffer object extension for complete details Before we leave the topic of FBOs, we’d like to point out a few more reasons why FBOs are superior to other forms of off-screen rendering First, FBOs consist of memory allocated on the graphics card itself that is directly usable in its target form—for example, as a texture As a consequence, you avoid any offcard copies to and from the host: You can even avoid on-card copies in good implementations of the extension Second, FBOs present a consistent, platformagnostic interface There just isn’t a simpler interface to intermediate rendering than FBO, largely due to the evolutionary process by which OpenGL is developed A variety of intermediate target rendering APIs and implementations were explored over the years, culminating in the design and implementation that exists today FBOs are the best choice for modern rendering on the Mac Third, FBOs avoid expensive context switching that can cost you a great deal of performance Copy-to-Texture In this section we describe a very common and widely available technique known as render-to-texture Render-to-texture is as simple as it sounds: You simply render your intermediate scene, copy it to a texture, and then use that texture in your final render Elegant, simple, and concise There are, of course, details to deal with concerning how you target the texture into which you want to render and, in some cases, how you move pixels around the system into your final texture Nevertheless, the process is largely as simple as described Render-to-texture is interesting because it’s a widely available technique and offers relatively high performance There are problems with it, too: It’s not as clean as the most modern OpenGL technique of FBOs, and there may be extra data copies Overall, however, it works pretty well Performance is pretty good, though not as consistently good as using FBOs Even so, you may sometimes run into problems when using cards from different vendors on which this technique is actually moderately expensive But if you can’t use FBOs, and this is the best alternative available, you gotta what you gotta The essence of the render-to-texture technique is actually a bit simpler than the FBO example presented earlier The code is virtually the same, but we omit the pieces of the rendering that relate to the FBO We begin by looking at the header for our custom view (Example C-14) 318 Appendix C: The Cocoa API for OpenGL Configuration in Leopard Example C-14 Custom View Header for Copy-to-Texture Example Code #import #import @interface MyOpenGLView : NSOpenGLView { GLuint textureID; float time; float angle; } - (void) angleUpdate: (NSTimer*) tt; - (void) reshape; @end Because we’re only going to render and copy into a texture, that’s the extent of the information we need to keep track of throughout our view class We then look at the initialization code, which is again very similar to the FBO example, but now without the FBO configuration (Example C-15) Example C-15 OpenGL Setup for Copy-to-Texture Rendering - (void) prepareOpenGL { glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glOrtho(-1,1,-1,1,-1,100); glMatrixMode( GL_MODELVIEW ); // enable, generate, and bind our texture objects glEnable( GL_TEXTURE_2D ); glGenTextures( (GLsizei) 1, &textureID ); glBindTexture( GL_TEXTURE_2D, textureID ); const unsigned int texdim = 64; const unsigned int nbytes = 3; unsigned char data[ texdim * texdim * nbytes ]; memset( data, 0, texdim * texdim * nbytes ); unsigned int ii; for( ii=0; ii