OpenGL Programming Guide

1K 1.6K 0
OpenGL Programming Guide

Đ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

OpenGL ® Programming Guide Seventh Edition ® OpenGL Series Visit informit.com /opengl for a complete list of available products T he OpenGL graphics system is a software interface to graphics hardware (“GL” stands for “Graphics Library.”) It allows you to create interactive programs that produce color images of moving, threedimensional objects With OpenGL, you can control computer-graphics technology to produce realistic pictures, or ones that depart from reality in imaginative ways The OpenGL Series from Addison-Wesley Professional comprises tutorial and reference books that help programmers gain a practical understanding of OpenGL standards, along with the insight needed to unlock OpenGL’s full potential OpenGL ® Programming Guide Seventh Edition The Official Guide to Learning OpenGL®, Versions 3.0 and 3.1 Dave Shreiner The Khronos OpenGL ARB Working Group 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 U.S., please contact: International Sales international@pearsoned.com Visit us on the Web: informit.com/aw Library of Congress Cataloging-in-Publication Data Shreiner, Dave OpenGL programming guide : the official guide to learning OpenGL, versions 3.0 and 3.1 / Dave Shreiner; the Khronos OpenGL ARB Working Group — 7th ed p cm Includes index ISBN 978-0-321-55262-4 (pbk : alk paper) Computer graphics OpenGL I Title T385.O635 2009 006.6'6—dc22 2009018793 Copyright © 2010 Pearson Education, Inc 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-55262-4 ISBN 10: 0-321-55262-8 Text printed in the United States on recycled paper at Edwards Brothers in Ann Arbor, Michigan First printing, July 2009 For my family—Felicity, Max, Sarah, and Scout —JLN For my family—Ellyn, Ricky, and Lucy —TRD To Tom Doeppner and Andy van Dam, who started me along this path —MW For my family—Vicki, Bonnie, Bob, Phantom, Squiggles, Tuxedo, and Toby —DRS In memory of Phil Karlton, Celeste Fowler, and Ben Cheatham This page intentionally left blank Contents Figures xxi Tables xxv Examples xxix About This Guide xxxv What This Guide Contains xxxv What’s New in This Edition xxxviii What You Should Know Before Reading This Guide xl How to Obtain the Sample Code xli Errata xlii Style Conventions .xlii Distinguishing Deprecated Features xliii Acknowledgments xlv Introduction to OpenGL What Is OpenGL? A Smidgen of OpenGL Code OpenGL Command Syntax OpenGL as a State Machine OpenGL Rendering Pipeline 10 Display Lists 11 Evaluators 11 Per-Vertex Operations 12 Primitive Assembly 12 vii Pixel Operations 13 Texture Assembly 13 Rasterization 14 Fragment Operations 14 OpenGL-Related Libraries 14 Include Files 15 GLUT, the OpenGL Utility Toolkit 17 Animation 22 The Refresh That Pauses 23 Motion = Redraw + Swap 24 OpenGL and Its Deprecation Mechanism 27 OpenGL Contexts 27 Accessing OpenGL Functions 29 State Management and Drawing Geometric Objects 31 A Drawing Survival Kit 34 Clearing the Window 34 Specifying a Color 37 Forcing Completion of Drawing 38 Coordinate System Survival Kit 40 Describing Points, Lines, and Polygons 42 What Are Points, Lines, and Polygons? 42 Specifying Vertices 46 OpenGL Geometric Drawing Primitives 47 Basic State Management 53 Displaying Points, Lines, and Polygons 55 Point Details 55 Line Details 56 Polygon Details 60 Normal Vectors 68 Vertex Arrays 70 Step 1: Enabling Arrays 72 Step 2: Specifying Data for the Arrays 73 Step 3: Dereferencing and Rendering 77 Restarting Primitives 83 Instanced Drawing 86 Interleaved Arrays 88 viii Contents Buffer Objects .91 Creating Buffer Objects 92 Making a Buffer Object Active .93 Allocating and Initializing Buffer Objects with Data 93 Updating Data Values in Buffer Objects 96 Copying Data Between Buffer Objects 101 Cleaning Up Buffer Objects 102 Using Buffer Objects with Vertex-Array Data 102 Vertex-Array Objects 104 Attribute Groups .110 Some Hints for Building Polygonal Models of Surfaces .113 An Example: Building an Icosahedron 115 Viewing 123 Overview: The Camera Analogy 126 A Simple Example: Drawing a Cube 129 General-Purpose Transformation Commands .134 Viewing and Modeling Transformations 137 Thinking about Transformations 137 Modeling Transformations 140 Viewing Transformations .146 Projection Transformations 152 Perspective Projection 153 Orthographic Projection 156 Viewing Volume Clipping 158 Viewport Transformation 158 Defining the Viewport 159 The Transformed Depth Coordinate 161 Troubleshooting Transformations 162 Manipulating the Matrix Stacks 164 The Modelview Matrix Stack 167 The Projection Matrix Stack 168 Additional Clipping Planes .168 Examples of Composing Several Transformations 172 Building a Solar System 172 Building an Articulated Robot Arm 175 Reversing or Mimicking Transformations 179 Contents ix f32.f = 0.0; if (exponent == 0) { if (mantissa != 0) { const GLfloat scale = 1.0 / (1 15) { /* Overflow - flush to Infinity */ uf11 = UF11_MAX_EXPONENT; } else if (exponent > -15) { /* Representable value */ exponent += UF11_EXPONENT_BIAS; mantissa >>= UF11_MANTISSA_SHIFT; uf11 = exponent > UF11_EXPONENT_SHIFT; int mantissa = (val & 0x003f); Floating-Point Formats Used in OpenGL f32.f = 0.0; if (exponent == 0) { if (mantissa != 0) { const GLfloat scale = 1.0 / (1 23) & 0xff) - 127; int mantissa = f32 & 0x007fffff; if (sign) return 0; if (exponent == 128) { /* Infinity or NaN */ uf10 = UF10_MAX_EXPONENT; if (mantissa) uf10 |= (mantissa & UF10_MANTISSA_BITS); } else if (exponent > 15) { /* Overflow - flush to Infinity */ uf10 = UF10_MAX_EXPONENT; } else if (exponent > -15) { /* Representable value */ exponent += UF10_EXPONENT_BIAS; mantissa >>= UF10_MANTISSA_SHIFT; uf10 = exponent > UF10_EXPONENT_SHIFT; int mantissa = (val & 0x003f); f32.f = 0.0; if (exponent == 0) { if (mantissa != 0) { const GLfloat scale = 1.0 / (1 red_0 val ) = val; if ( max < val ) max = val; } } if ( == max ) { /* Constant color across the block - we'll use the ** first set of codes (red_0 > red_1), and set ** all bits to be code */ encoded[0] = min; encoded[1] = max; encoded[2] = 0x00; encoded[3] = 0x00; encoded[4] = 0x00; encoded[5] = 0x00; encoded[6] = 0x00; encoded[7] = 0x00; } RGTC Overview else { GLfloat d1 = (max - min) / 7.0; GLfloat d2 = (max - min) / 5.0; GLubyte *pptr = (GLubyte*) pixels; GLubyte *cptr = (GLubyte*) codes; for ( i = 0; i < 16; ++i, ++pptr, ++cptr ) { GLfloat v1 = (*pptr - min) / d1; GLfloat v2 = (*pptr - min) / d2; GLubyte ip1 = (GLubyte) v1; GLubyte ip2 = (GLubyte) v2; GLfloat fp1 = v1 - ip1; GLfloat fp2 = v2 - ip2; enum { MinCode = 0, MaxCode = }; if ( fp1 > 0.5 ) { fp1 -= 0.5; ip1 += 1; } if ( fp2 > 0.5 ) { fp2 -= 0.5; ip1 += 1; } if ( ip1 == ) { *cptr = MinCode; } else if ( ip1 == ) { *cptr = MaxCode; } else { *cptr = (ip2 >= Shift[group]; mask = code

Ngày đăng: 28/04/2014, 15:47

Từ khóa liên quan

Mục lục

  • Cover Page

  • Title Page

  • ISBN 978-0321552624

  • Contents

  • Figures

  • Tables

  • Examples

  • About This Guide

    • What This Guide Contains

    • What's New in This Edition

    • What You Should Know Before Reading This Guide

    • How to Obtain the Sample Code

    • Errata

    • Style Conventions

    • Distinguishing Deprecated Features

    • Acknowledgments

    • 1. Introduction to OpenGL

      • What Is OpenGL?

      • A Smidgen of OpenGL Code

      • OpenGL Command Syntax

      • OpenGL as a State Machine

      • OpenGL Rendering Pipeline

        • Display Lists

Tài liệu cùng người dùng

Tài liệu liên quan