Polygons Feel No Pain 2014
Total Page:16
File Type:pdf, Size:1020Kb
Polygons Feel No Pain Volume 1 Ingemar Ragnemalm Course book for TSBK07 Computer Graphics 1 Foreword Why would anyone name a computer graphics book “Polygons feel no pain”? In case you wonder, here are the reasons: • Computer graphics is very much about polygons. • Computer games tend to be violent and I am aiming for gaming! • A major point with it is to inflict less pain (in your back) than the others do. • It is a whole lot more fun than yet another book entitled “Computer Graphics with OpenGL”. So the message is that computer graphics is fun, computer games are fun too, and blasting some grunts in Quake doesn’t hurt anyone. Let’s start the game. INSERT COIN(S) TO CONTINUE... Cover image by Susanne Ragnemalm, based on the original Candide model. Related web pages: http://www.computer-graphics.se http://www.isy.liu.se http://www.ragnemalm.se All content of this book is © Ingemar Ragnemalm 2008-2014. Revised january 2014. 2 1. Introduction This book was written as course material for my course in Computer Graphics at Linköping University. There are many computer graphics books, including the Hearn&Baker book [9] that was course book for our course for many years. That book is excellent in many areas, low-level algorithms and splines in particular. However, the amount of material that was missing for my needs kept growing, and whatever book I con- sidered as replacement seemed to miss just as much. And they are all big books that don’t slip as easily into the backpack as they should. And that is why this book was created: I wanted to create a small, portable book that you can bring with you without any pain in the shoulders, and that fits the course well at the same time. 1.1 Who should read this book? Since this book is written for the course TSBK07 Computer Graphics at the University of Linköping, attendants of that course and similar courses are the target audience. That means students at 2nd to 4th grade with some experience in algebra and programming. The course does not demand any prior knowledge of computer graphics, so it will start from the bottom. The tempo will, however, be pretty high. 1.2 What should you expect to learn from this book, and its course? Computer graphics is the art of creating images from a description. You will learn how computer graphics works, including the math and programming behind it. There will be some 2D graphics algorithms, but most of the course is about 3D graphics, real-time graphics in particular. We will go reasonably far into the subject, including methods for working with very large worlds, but more advanced topics are left for volume 2, “So How Can We Make Them Scream?”. Computer graphics has many applications, and we focus on what I think is most challenging: Real-time graphics, including games. We focus on modern hardware (modern GPUs), and modern programming methods, which includes shader programming. Introduction 3 1.3 A few words about the past Most of the book is based on my lecture material for the computer graphics course, much of it first appearing in the ”Devil in a book” supplements from 2000-2007. The Computer Graphics course I am leading, currently with the name TSBK07 Computer Graphics, has a long history. It was started in the mid-80’s by professor Per-Erik Daniels- son and associate professor Björn Gudmundsson. I attended the course as a student, so it was my first formal training in computer graphics. I have been course leader and lecturer for it since 2000. As is natural for a fast-moving subject like this, the course is revised from year to year. This book is created as a part of that process. The changes for 2008 were larger than usual, important material moved down from the advanced course. This was a good time to write this book. I hope that I am offering a course and a book that are fairly up-to-date and that cover the subject fairly well, for being a first course on com- puter graphics. And with volume 2, I believe we have a neat book set that covers much of the essentials for graphics and game programmers. 1.4 A book using OpenGL, not an OpenGL book Note that this text is about computer graphics, not OpenGL or any other specific API. OpenGL will be used for examples, but you are expected to have some other book for learning OpenGL as such. Most books discuss OpenGL up to version 2.1 [4][5][2] How- ever, we will aim at the more modern OpenGL 3.2. The “OpenGL Programming Guide” by the OpenGL ARB. [1] is the “red book”, one of the three big OpenGL books that anyone who works seriously with OpenGL has. How- ever, the current version is rather outdated at this time. Pick up a copy when it is updated. Finally, this book tries to avoid any code that is heavily language- or operating system dependent. Code examples are in C, but that does not mean that you have to use C, with or without OO extensions (e.g. C++). There are other ways and I want to encourage you to explore them. Operating system dependency is avoided, so what you find in this book should work anywhere. There is no language or OS that is the only way. So what you have here is a computer graphics book with focus on OpenGL and game pro- gramming. Let us get started with chapter 3, a short chapter about some groundwork, sub- jects that I hope most readers know about already. We start to get the speed up in chapter 4 and chapter 5, with chapter 6 being the chapter where we take off, in full 3D. What you will find most interesting and enjoyable depends on you. Some last words before leaving the introduction. After the revisions, I hope most errors are gone. But if you still find any, please let me know when you find them. I wish to give spe- cial thanks to Richard Åklint, Arvid Kongstad, Kristofer Krus and Marcus Wallenberg, who did exactly that, and more than anyone else found and reported many of the errors in the earlier printings. 4 Introduction 2. Table of contents 1. Introduction..............................................................................................................3 1.1 Who should read this book?.................................................................................................3 1.2 What should you expect to learn from this book, and its course? .......................................3 1.3 A few words about the past..................................................................................................4 1.4 A book using OpenGL, not an OpenGL book .....................................................................4 2. Table of contents......................................................................................................5 3. Graphics systems ...................................................................................................11 3.1 Graphics hardware .............................................................................................................11 3.2 Image formats ....................................................................................................................12 3.3 Displaying color.................................................................................................................14 3.4 Accessing the image buffer................................................................................................15 3.5 The graphics pipeline and shader programs.......................................................................15 3.6 Graphics libraries...............................................................................................................16 3.7 OpenGL..............................................................................................................................17 3.7.1 Shader programming languages ..........................................................................18 3.7.2 A simple OpenGL example .................................................................................19 4. Linear algebra toolbox ...........................................................................................23 4.1 Vectors................................................................................................................................23 4.2 Vector products ..................................................................................................................24 4.3 Matrices and matrix multiplication....................................................................................24 4.4 Typographic conventions for vectors and matrices............................................................26 4.5 3D space.............................................................................................................................26 4.6 Orthonormal basis..............................................................................................................27 4.7 On what side of a plane is a point? ....................................................................................28 4.8 Intersection between a line segment and a plane ...............................................................29 4.9 Splitting a polygon.............................................................................................................29 4.10 Intersection ray-triangle.....................................................................................................30 4.11 Intersection line-sphere......................................................................................................32 4.12 Splitting and mirroring a vector.........................................................................................33 4.13 Decomposing