COMPUTER GRAPHICS FROM SCRATCH
A Programmers Introduction to 3D Rendering
by Gabriel Gambetta
San Francisco
COMPUTER GRAPHICS FROM SCRATCH. Copyright 2021 by Gabriel Gambetta
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
ISBN-13: 978-1-7185-0076-1 (print)
ISBN-13: 978-1-7185-0077-8 (ebook)
Publisher: William Pollock
Executive Editor: Barbara Yien
Production Manager: Rachel Monaghan
Production Editor: Kassie Andreadis
Developmental Editor: Alex Freed
Cover Illustrator: Rob Gale
Interior Design: Octopod Studios
Technical Reviewer: Alejandro Segovia Azapian
Copyeditor: Gary Smith
Proofreader: Elizabeth Littrell
Indexer: Elise Hess
For information on book distributors or translations, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
245 8th Street, San Francisco, CA 94103
phone: 1-415-863-9900;
www.nostarch.com
Library of Congress Cataloging-in-Publication Data
Names: Gambetta, Gabriel, 1980- author.
Title: Computer graphics from scratch : a programmers introduction to 3D rendering / Gabriel Gambetta.
Description: San Francisco : No Starch Press, [2021] | Includes index.
Identifiers: LCCN 2020056364 (print) | LCCN 2020056365 (ebook) | ISBN
9781718500761 (print) | ISBN 9781718500778 (ebook)
Subjects: LCSH: Computer graphics.
Classification: LCC T385 .G3524 2021 (print) | LCC T385 (ebook) | DDC
006.6dc23
LC record available at https://lccn.loc.gov/2020056364
LC ebook record available at https://lccn.loc.gov/2020056365
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The information in this book is distributed on an As Is basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
To my dad (19472007), architect and self-taught programmer, who got me started on this path.
My dad, my two-and-a-half-year-old self, and the ZX81.
My earliest documented program ever, written at six-and-a-half years old, drew some lines on the screen of my ZX Spectrum+.
About the Author
Gabriel Gambetta started coding games around the age of 5 on a ZX Spectrum. After studying computer science and working at a respectable local company in his native Uruguay, he started a game development company and ran it for 10 years while teaching computer graphics at his alma mater. More recently, Gambetta has been working at Google Zrich since 2011, except for a stint as an early engineer at London-based multiplayer game tech unicorn Improbable, and a year in Madrid focusing on acting and filmmaking.
About the Technical Reviewer
Alejandro Segovia Azapian is a software engineer with 14+ years of experience in computer graphics. He has worked for several industry-leading companies in the field of 3D graphics including Autodesk, Electronic Arts, PDI/ DreamWorks, and WB Games, across a variety of realtime graphics projects spanning apps, games, game engines, and frameworks. Alejandro currently works in the GPU Software group at a leading consumer electronics company based in Cupertino, California.
BRIEF CONTENTS
CONTENTS IN DETAIL
1
INTRODUCTORY CONCEPTS
PART I
RAYTRACING
2
BASIC RAYTRACING
3
LIGHT
4
SHADOWS AND REFLECTIONS
5
EXTENDING THE RAYTRACER
PART II
RASTERIZATION
6
LINES
7
FILLED TRIANGLES
8
SHADED TRIANGLES
9
PERSPECTIVE PROJECTION
10
DESCRIBING AND RENDERING A SCENE
11
CLIPPING
12
HIDDEN SURFACE REMOVAL
13
SHADING
14
TEXTURES
15
EXTENDING THE RASTERIZER
ACKNOWLEDGMENTS
Few books happen overnight; the one youre about to read has been almost 20 years in the making. As you might suspect, many people have been part of its story, in one way or another, and I want to thank them. In chronological order:
Omar Paganini and Ernesto Ocampo Edye As the Dean of the School of Engineering and the Director of Computer Science at Universidad Catlica del Uruguay, they put considerable trust in me by letting me take the reins of Computer Graphics when I was but a fourth-year student, and by letting me completely reshape its curriculum in the way I thought best. Fellow professor Roberto Lublinerman was a great mentor throughout my first year of teaching.
My students from 2003 to 2008 Besides being the unwitting guinea pigs of my continuously evolving teaching methodology, they accepted and respected a professor barely a year older than them (and, in some cases, younger than them). The joy in their faces when they created their first raytraced images made it all worth it.
Alejandro Segovia Azapian A student turned teaching assistant turned friend, his input has helped me evolve the material over time; having been a tiny part of his subsequent, very successful professional career specialized in realtime rendering and performance optimization fills me with pride. He was also a technical reviewer of this book, and his contributions ranged from fixing typos to suggesting deep structural improvements of some chapters.
JC Van Winkel He did his own editing pass and came up with a lot of valuable suggestions for improvement.
The readers ofHacker News My lecture notes, diagrams, and demos made the front page of Hacker News, and attracted considerable attentionincluding that of No Starch Press. If this hadnt happened, this book might have never existed.
Bill Pollock, Alex Freed, Kassie Andreadis, and the entire No StarchPress team They guided me through cleaning up and reshaping my lecture notes and diagrams, which in my mind were essentially ready to be published as a book, into an actual book. They took the raw materials to a whole new level; I had no idea this took so much work and effort, and Alex, Kassie, and the team did a stellar job. My name is the only one on the cover, but make no mistake, this was a group effort.