Graphics Programming in c++ Springer London Berlin Heidelberg New York Barcelona Budapest Hong Kong Milan Paris Santa Clara Singapore Tokyo Mark Walmsley Graphics Programming in c++ Writing Graphics Applications for Windows 98 , Springer Mark Walmsley, BSc, PhD [email protected] ISBN-13:978-1-4471-1231-0 Springer-Verlag Berlin Heidelberg New York British Library Cataloguing in Publication Data Walmsley, Mark Graphics programming in c++ : writing graphics applications for Windows 98 l.C++ (Computer program language) 2.Computer graphics l.Title 006.6'633 ISBN-13:978-1-4471-1231-0 Library of Congress Cataloging-in-Publication Data Walmsley, Mark, 1964- Graphics programming in C++ : writing graphics applications for Windows 981 Mark Walmsley. p. em. Includes index ISBN-13:978-1-4471-1231-0 e-ISBN-13:978-1-4471-0905-1 DOI: 10.1007/978-1-4471-0905-1 1. Computer graphics. 2. C++ (Computer program language) 3. Microsoft Windows (Computer me) I. Title. T385.W353 1998 006.6'633--dc21 98-20520 Apart from any fair dealing for the purposes of research or private study, or criticism or review, as permitted under the Copyright, Designs and Patents Act 1988, this publication may only be reproduced, stored or transmitted, in any form or by any means, with the prior permission in writing of the publishers, or in the case of repro graphic reproduction in accordance with the terms of licences issued by the Copyright Licensing Agency. Enquiries concerning reproduction outside those terms should be sent to the publishers. © Springer-Verlag London Limited 1998 Softcover reprint of the hardcover 1st edition 1998 The use of registered names, trademarks etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant laws and regulations and therefore free for general use. The publisher makes no representation, express or implied, with regard to the accuracy of the information contained in this book and cannot accept any legal responsibility or liability for any errors or omissions that may be made. Typesetting: Camera Ready by author 34/3830-543210 Printed on acid-free paper Contents Preface ............................................................................................................... ix 1. Message-Based Programming ....................................................................... 1 1.1 Windows 98 Programming ......................................................................... 1 1.2 Window Components ................................................................................. 2 1.3 Message Queues ......................................................................................... 3 1.4 Window Callback Functions ....................................................................... 4 1.5 The WinMain () Function ........................................................................ 5 1.6 Windows 98 Messages ................................................................................ 6 1. 7 Some Important Messages .......................................................................... 7 1.8 Sununary .................................................................................................... 9 2. Window Management ..•••••.••.•.................•.........................••.........•....•...•..... 11 2.1 Window Classes ....................................................................................... 11 2.2 Creating a Window .................................................................................. 13 2.3 WinMain () Revisited ............................................................................. 14 2.4 Message Handlers .................................................................................... 16 2.5 The Hello Program ................................................................................ 17 2.6 Windows 98 Program Framework ............................................................ 18 2.7 Sununary .................................................................................................. 21 3. Object Orientation ••••..•..•..•......................................•.........................•.••.•... 23 3.1 Object Oriented Programming .................................................................. 23 3.2 The Manager Object ............................................................................... 24 3.3 Windows and Objects ............................................................................... 28 3.4 Message Boxes ......................................................................................... 32 3.5 Popup Menus ........................................................................................... 33 3.6 The VIEW Class ....................................................................................... 36 3.7 Summary .................................................................................................. 43 vi Graphics Programming in C++ 4. Pens and Bmshes ......................................................................................... 45 4.1 Graphics Device Interface ........................................................................ 45 4.2 GDI Objects ............................................................................................. 48 4.3 Windows 98 Coordinate Systems .............................................................. 49 4.4 GDI Drawing Functions ........................................................................... 50 4.5 Tracking the Mouse .................................................................................. 54 4.6 Summary .................................................................................................. 56 5. Bitmaps ........................................................................................................ 59 5.1 Pixels and Colours .................................................................................... 59 5.2 Windows 98 Resources ............................................................................. 60 5.3 Displaying a Bitmap Resource .................................................................. 61 5.4 The DIB Class ......................................................................................... 65 5.5 Storing Images in Files ............................................................................. 70 5.6 Summary .................................................................................................. 73 6, Palettes ......................................................................................................... 75 6.1 Logical and System Palettes ..................................................................... 75 6.2 Palette Messages ...................................................................................... 76 6.3 Creating a Palette ..................................................................................... 77 6.4 The PALETTE Class ................................................................................ 79 6.5 Updating the DIB Class ........................................................................... 84 6.6 Summary .................................................................................................. 91 7. Sprite Animation ......................................................................................... 93 7.1 The SPRITE Class ................................................................................... 93 7.2 Animation ................................................................................................. 96 7.3 Flicker .................................................................................................... 100 7.4 Transparency .......................................................................................... 105 7.5 Threads .................................................................................................. 109 7.6 Summary ................................................................................................ 110 Contents vii 8. Wire-Frame Graphics ............................................................................... 111 8.1 Polygon Worlds ...................................................................................... III 8.2 The WORLD and SCENE Objects ............................................................. US 8.3 Line Drawing ......................................................................................... 116 8.4 Line Clipping ......................................................................................... 124 8.5 Summary ................................................................................................ 130 9. Polygon-Fill Techniques ............................................................................ 131 9.1 The GDI Approach................................................................................. 131 9.2 Polygon Outlines .................................................................................... 132 9.3 Filling with a Solid Colour ..................................................................... 135 9.4 Drawing 3D Images ............................................................................... 138 9.5 Virtual World Chunks ............................................................................ 141 9.6 Summary ................................................................................................ 144 10. Assembly Language Programming ......................................................... 147 10.1 Computer Basics ............................................................................. ..... 147 10.2 Updating the Message Loop .................................................................. 148 10.3 The 80x86 Registers ............................................................................ 150 10.4 Data Transfer ....................................................................................... 151 10.5 Arithmetic and Logical Operations ....................................................... 154 10.6 Flow Control ........................................................................................ 155 10.7 Faster Polygon-Fill Routines ................................................................. 157 10.8 Program Stack ...................................................................................... 163 10.9 Summary .............................................................................................. 165 11. Mathematics of 3D Geometry ................................................................. 167 ILl Vectors and Matrices ............................................................................ 167 11.2 The VECTOR Class ............................................................................... 171 11. 3 Scalar Product ...................................................................................... 174 11.4 Vector Product ..................................................................................... 176 11.5 Triple Product ...................................................................................... 180 11.6 Lines and Planes .................................................................................. 181 11.7 Summary .............................................................................................. 182 viii Graphics Programming in C++ 12. Projection of tbe Viewing Volume ........................................................... 185 12.1 Perspective Projections .... .............. ....... ... .......... ......... ............ ..... ...... ... 185 12.2 The Viewing Volume ........................................................................... 186 12.3 Taking a Snapshot ................................................................................ 189 12.4 Projecting and Clipping ........................................................................ 191 12.5 A Vanishing Cube ................................................................................ 194 12.6 Summary .............................................................................................. 195 13. Hidden Pixel Removal ............................................................................. 197 13.1 Constructing a Scene Image ... ........ ........... .......... ........... ............. ......... 197 13.2 The Painter's Algorithm ....................................................................... 198 13.3 Separating Planes ................................................................................. 199 13.4 Binary Space Partitioning ..................................................................... 202 13.5 Drawing in Reverse .............................................................................. 206 13.6 Z-Buffers .............................................................................................. 209 13.7 Summary .............................................................................................. 210 14. Colour Sbading and Textures .................................................................. 213 14.1 Lights and Surfaces .............................................................................. 213 14.2 Shading Options ................................................................................... 214 14.3 Texture Mapping .................................................................................. 217 14.4 Implementation Optimizations ............................................................. 220 14.5 Depth Perception .................................................................................. 224 14.6 Summary .............................................................................................. 226 15. In Motion ... ............................................................................................. 227 15.1 The Graphics Pipeline .......................................................................... 227 15.2 Animating the Virtual World ............................................................... 228 15.3 Translations ......................................................................................... 229 15.4 Rotations .............................................................................................. 231 15.5 Transforming Coordinates .................................................................... 233 15.6 Summary .............................................................................................. 235 Postscript ........................................................................................................ 237 Index ............................................................................................................... 239 Preface This is a book about computer graphics. It is aimed primarily at anyone wishing to develop graphics applications for Windows 98 and attempts to provide a quick and clear introduction to the subject without encumbering the reader in a mass of extraneous details. The application of object oriented techniques to graphics programming is a principal theme throughout the text and most of the software is written in C++ so a familiarity with C/C++ will be helpful - for a fuller introduction to the C++ language than is possible here consult the book 'Programming in C++' (ISBN 0 85934 435 5). Beyond this only a general background in computing and/or mathematics is assumed and whenever new ideas are encountered they are fully explained with the aid of line-drawings and illustrative coding examples. For those brave enough chapter 10 also introduces the topic of 80 x86 assembly language programming - without dedicated graphics hardware this is still the best way to produce really snappy animation code. The material covered by the book may be divided into three main categories: - graphics facilities provided by Windows 98 - 2D drawing algorithms - simulation of a 3D virtual world These correspond roughly to chapters 1-6, chapters 7-10 and chapters 11-15 respectively although there is some overlap - more specifically the book is laid out according to the following plan: Chapters 1 and 2 An introduction to message-based programming under the Windows 98 operating system - the basic procedure for creating and managing a window is also detailed here. Chapter 3 Object oriented programming is a revolutionary approach to handling complexity in computer software design - this chapter discusses the application of object oriented techniques to graphics programming. Chapters 4 to 6 These chapters cover the graphics facilities provided by the Windows 98 Graphics Device Interface - the key graphical items available under Windows 98 include pens, brushes, bitmaps and palettes. x Graphics Programming in C++ Chapters 7 to 9 A selection of 2D drawing algorithms for generating bitmapped, wire-frame and polygon-fill images are described here - in particular, animated bitmap images are used to implement computer sprites. Chapters 10 and 11 A technical and mathematical interlude that introduces assembly language programming and 3D vector geometry - these are important topics for many graphics applications. Chapters 12 to 15 The last chapters of the book describe the process of projecting an image of a 3D object onto a 2D screen - hidden pixel removal, colour shading, texture mapping and 3D computer animation are all explained. The book is written in incremental fashion and it is recommended that earlier chapters are well digested before later ones are tasted - so to begin at the beginning ... MW March 1998