OpenGL 4 Shading Language Cookbook
Third Edition
Build high-quality, real-time 3D graphics with OpenGL 4.6, GLSL 4.6 and C++17
David Wolff
BIRMINGHAM - MUMBAI
OpenGL 4 Shading Language CookbookThird Edition
Copyright 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Kunal Chaudhari
Acquisition Editor: Trusha Shriyan
Content Development Editor: Arun Nadar
Technical Editor: Prajakta Mhatre
Copy Editor: Safis Editig
Project Coordinator: Sheejal Shah
Proofreader: Safis Editing
Indexer: Mariammal Chettiyar
Production Coordinator: Shraddha Falebhai
First published: July 2011
Second edition: December 2013
Third edition: September 2018
Production reference: 1260918
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78934-225-3
www.packtpub.com
mapt.io
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Why subscribe?
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
Packt.com
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.packt.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at customercare@packtpub.com for more details.
At www.packt.com , you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Contributors
About the author
David Wolff is a professor in the computer science department at Pacific Lutheran University (PLU). He received a PhD in Physics and an MS in computer science from Oregon State University. He has been teaching computer graphics to undergraduates at PLU for over 17 years, using OpenGL.
About the reviewers
Dr. Muhammad Mobeen Movania received his PhD degree in Advance Computer Graphics and Visualization from Nanyang Technological University (NTU), Singapore. After his graduation, he joined the Institute for Infocomm Research, A-Star Singapore as a research scientist.
Dr. Movania has published several international conference and journal papers in the area of computer graphics and visualization. He has written two books on OpenGL namely, OpenGL Development Cookbook and OpenGL Build High Performance Graphics . He has also contributed book chapters in OpenGL Insights, WebGL Insights, and Game Engine Gems 3. He is associated with DHA Suffa University since 2013 and is currently serving as the Head of Computer Science Department. His GitHub ID is mmmovania.
I would like to thank Allah for his countless blessings on me. My thanks are then due on my family: my parents (Mr. and Mrs. Abdul Aziz Movania), my wife (Tanveer Taji), my brothers and sisters (Mr. Muhammad Khalid Movania, Mrs. Azra Saleem, Mrs. Sajida Shakir, and Mr. Abdul Majid Movania), all my nephews/nieces, and my children (Muntaha and Muhammad Faizan Movania).
Johannes Unterguggenberger is a University Assistant at TU Wien's Rendering and Modeling Group, Institute of Visual Computing and Human-Centered Technology. He is training students to become skilled graphics developers, which includes gaining expertise in OpenGL and GLSL. He always orients towards the future, be it by refusing to use any C++ version older than 17, developing Augmented Reality rendering techniques and frameworks, or exploring the limits of new graphics hardware.
After working in various fields across the IT industry, he is now fully devoted to education
and research, having set his sights on achieving greater visual gloriousness in future games
and other real-time applications. Furthermore, he really likes good CG-books and
congratulates you for picking one of them up.
Packt is searching for authors like you
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Preface
The OpenGL Shading Language (GLSL) is now a fundamental and critical part of programming with OpenGL. It provides us with unprecedented flexibility and power by making the formerly fixed-function graphics pipeline programmable. With GLSL, we can leverage the Graphics Processing Unit (GPU) to implement advanced and sophisticated rendering techniques and even do arbitrary computation. With GLSL 4.x, programmers can do more with the GPU than ever before thanks to shader stages such as tessellation shaders and compute shaders.
In this book, we cover the full spectrum of GLSL programming. Starting with the basics of shading with the vertex and fragment shaders, we take you from simple to advanced techniques. From textures, shadows, and image processing, to noise and particle systems, we cover practical examples to give you the tools you need to leverage GLSL in your projects. We also cover how to use geometry shaders, tessellation shaders, and the recent addition to GLSL: compute shaders. With these, you can make use of the GPU for a variety of tasks that go beyond just shading. With geometry and tessellation shaders, we can create additional geometry or modify geometry, and with compute shaders we can do arbitrary computation on the GPU.
Next page