Table of Contents
Introduction
Basics
Transformation
Lighting and Texture Maps
Tips and Tricks
Mesh Files
More Advanced Lighting and Texture Effects
New Shader Stages
2d Rendering
Animation
Multi-Pass Rendering
Discussion
Anton's OpenGL 4 Tutorials
Preface
The Point of This Book
This book is a practical guide to starting 3d programming with OpenGL, usingthe most recent version. I started building this material as lab worksheetsfor the practical part of Stefan Petersson's most excellent 3d Programmingcourse at Blekinge Tekniska Hgskola in Sweden, and continued it as anon-line seriesafterwards. It's now extended into a book format, for off-line viewing, and withadditional material. It would suit anyone learning 3dprogramming that needs a practical guide with some help for common problems. Theoriginal material is often used in this way by university courses and hobbyists.This book is a collection of worked-through examples of common real-timerendering techniques as used in video games or student projects. There are alsosome chapters or short articles for Tips and Tricks - not-so-obvious techniquesthat can add a lot of value to projects or make it easier to find problems.
This is not a guide for graphics experts, nor is it intended to describe indetail the latest features of OpenGL. It is also not intended to be aninstruction to 3d programming theory. A light introduction is given to thetopics covered, but there are much more comprehensive and capable sources forthat. The idea is to be something like a lab manual - to get you going andover the trickier and more confusing hurdles presented by the API.
In computer lab support for graphics courses I had a couple of key jobs that Ifound I was repeating over and over for students, and these are mymain motivations in the style of writing for this material:
- To explain convoluted problems in a clear, practical,rational way, that appeals to common sense - not to programming or mathematicalexperience, and without using a lot of jargon.
- "I only get a black screen!" - To provide a rational,step-by-step process of elimination to debug and diagnose 3d programmingproblems so that they don't look so utterly hopeless.
- And to make a list of the most common problems. I originally started my lab worksheetsbecause I found that I was answering the same questions hundreds of times!
After the first hundred or so hours of that I was able to spot a problem fromthe other side of the lab, and the students would think I had some sort ofsuper-power. In reality it's just that the same mistakes pop up all the timefor people learning. I feel like this is the kind of help that's missing fromother OpenGL references - and it is so easy to make mistakes with the OpenGLinterface - so I've put all the advice that I can here for your benefit too.
An additional motivation for writing this content was the state of existingreference texts. When I first started teaching there were no OpenGL 4 bookspublished. The larger tomes were not yet updated. At the time of writing some ofthemajor texts have updated to OpenGL 4. You can assemble a collection of texts toeducate yourself, which I will list, but these are very large, and veryexpensive volumes. None of them are satisfactory as a sole reference for auniversity course, which means you'll need to refer to several of them.This is an absolutely ridiculous expectation for a student budget, and I thinkvery disappointing for education in this area, which has so much potential tograb attention and to motivate. I try to fill this gap here with something easy toget into, with human-digestible instruction steps, and not expensive or tooheavy to travel with. It's still worth getting access to acollection of the larger volumes, which will give you precise technical detailand more advanced technical information. Perhaps you can get your library tobuy them, or pool together with other enthusiasts.
What Should I Know Before Starting?
Programming Knowledge
Graphics programming is not easy. You will need to have strong C programmingskills. You don't need to know C++ for OpenGL. The articles in this book assumethat you are familiar with the following concepts:
- Built-in data-types; int , float , unsigned int ,etc. and what sort of numbers that they can represent
- Boolean logic
- Functions and parameters
- Arrays, loops, and strings
- Compiling and linking C programmes
- File reading and writing
- Linking external libraries
- Pointers, addresses
- Casting between data types
- Dynamic memory allocation and deallocation
- Identifying different types of errors, and debugging
In other words, about the equivalent of the first 2 courses in a universityComputer Science programme. If you're not a C programmer, but have some otherprogramming background then I can suggest Herbert Schildt's "C/C++ Programmer'sReference". It has an excellent break-down of the basic functionality of C andC++. The pocket edition is great. The official "The C Programming Language" byBrian Kerninghan and Dennis Ritchie is in a 101 tutorial format, but it'sshort, boring, and expensive, although is the definitive guide. One to borrow,perhaps!
It's possible to learn the entirety of C in a short time and keep itall in your working knowledge. This is absolutely not the case with C++. Isuggest learning C first.I write my code in C, but occasionally use a feature from C++, where I feel itwill save me some time. This means that I use a C++ compiler. You can quiteeasily replace my C++ bits with the equivalent C code and use a C compiler.
Mathematical Knowledge
I feel that people over-emphasise mathematics as a pre-requisite forprogramming. You barely need any maths to be a good programmer - what you needis logic. We do use some very specific mathematical concepts for 3d graphics,but actually learning mathematical concepts when you need to use them is quitepossible, and in fact, probably the most effective way to learn. Some conceptsthat you might want to brush up on before starting:
- Basic trigonometric functions; sine, cosine, tangent.
- 3d vectors. Dot and cross product functions for vectors
- Matrices for affine transformations of vectors
We will be using a lot of the above techniques, and you will probably find ituseful to put together a little summary sheet of the techniques that you need areference for. You can get my cheat sheet athttp://antongerdelan.net/teaching/3dprog1/maths_cheat_sheet.pdf.
Hardware
You're going to need a fairly modern graphics adapter to codein modern OpenGL. Find out what graphics device your computerhas, and look up on Wikipedia if it will support OpenGL 3 or 4.Most integrated graphics chips will not be good enough. Somelaptops have a dedicated graphics adapter that will work, butmost won't. Fairly modern AMD Radeon, Nvidia, and some newIntel adapters will work.
Although this book is aimed at using OpenGL 4, almost all of itwill also run on OpenGL 3.2 too - I'll point out any smallchanges that need to be made. Earlier versions of OpenGL use adifferent interface, and will not be covered.
You'll also need a calculator and a pencil and paper. Most of the problems thatyou need to solve will be geometric, and drawing your problem is always a goodfirst step. Solving the first few equations by hand on a calculator, andcomparing it to your drawing is far better than doing what most new studentsdo - jumping in to an IDE right away, and getting frustrated when the debuggerwon't tell them what to do!