Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

OpenGL Development Cookbook
OpenGL Development Cookbook
OpenGL Development Cookbook
Ebook743 pages5 hours

OpenGL Development Cookbook

Rating: 5 out of 5 stars

5/5

()

Read preview

About this ebook

The book is written in a Cookbook format with practical recipes aimed at helping you exploit OpenGL to its full potential.This book is targeted towards intermediate OpenGL programmers. However, those who are new to OpenGL and know an alternate API like DirectX might also find these recipes useful to create OpenGL animations.
LanguageEnglish
Release dateJun 25, 2013
ISBN9781849695053
OpenGL Development Cookbook

Related to OpenGL Development Cookbook

Related ebooks

Data Modeling & Design For You

View More

Related articles

Reviews for OpenGL Development Cookbook

Rating: 5 out of 5 stars
5/5

1 rating0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    OpenGL Development Cookbook - Muhammad Mobeen Movania

    Table of Contents

    OpenGL Development Cookbook

    Credits

    About the Author

    About the Reviewers

    www.PacktPub.com

    Support files, eBooks, discount offers and more

    Why Subscribe?

    Free Access for Packt account holders

    Preface

    What this book covers

    What you need for this book

    Who this book is for

    Conventions

    Reader feedback

    Customer support

    Downloading the example code

    Downloading the color images of this book

    Errata

    Piracy

    Questions

    1. Introduction to Modern OpenGL

    Introduction

    Setting up the OpenGL v3.3 core profile on Visual Studio 2010 using the GLEW and freeglut libraries

    How to do it...

    There's more…

    Designing a GLSL shader class

    Getting ready

    How to do it…

    How it works…

    There's more…

    Rendering a simple colored triangle using shaders

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Doing a ripple mesh deformer using the vertex shader

    Getting ready

    How to do it…

    How it works…

    There's more

    Dynamically subdividing a plane using the geometry shader

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Dynamically subdividing a plane using the geometry shader with instanced rendering

    Getting ready

    How to do it…

    How it works…

    See also

    Drawing a 2D image in a window using the fragment shader and the SOIL image loading library

    Getting ready

    How to do it…

    How it works…

    There's more…

    2. 3D Viewing and Object Picking

    Introduction

    Implementing a vector-based camera with FPS style input support

    Getting ready

    How to do it…

    There's more…

    See also

    Implementing the free camera

    Getting ready

    How to do it…

    There's more…

    See also

    Implementing the target camera

    Getting ready

    How to do it…

    There's more…

    See also

    Implementing view frustum culling

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Implementing object picking using the depth buffer

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Implementing object picking using color

    Getting ready

    How to do it…

    How it works…

    See also

    Implementing object picking using scene intersection queries

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    3. Offscreen Rendering and Environment Mapping

    Introduction

    Implementing the twirl filter using the fragment shader

    Getting ready

    How to do it…

    How it works…

    There's more...

    Rendering a skybox using static cube mapping

    Getting ready

    How to do it…

    How it works…

    There's more…

    Implementing a mirror with render-to-texture using FBO

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Rendering a reflective object using dynamic cube mapping

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Implementing area filtering (sharpening/blurring/embossing) on an image using convolution

    Getting ready

    How to do it…

    How it works...

    There's more…

    See also

    Implementing the glow effect

    How to do it…

    How it works…

    There's more…

    See also

    4. Lights and Shadows

    Introduction

    Implementing per-vertex and per-fragment point lighting

    Getting started

    How to do it…

    How it works…

    There's more…

    See also

    Implementing per-fragment directional light

    Getting started

    How to do it…

    How it works…

    There's more…

    See also

    Implementing per-fragment point light with attenuation

    Getting started

    How to do it…

    How it works…

    There's more…

    See also

    Implementing per-fragment spot light

    Getting started

    How to do it…

    How it works…

    There's more…

    See also

    Implementing shadow mapping with FBO

    Getting started

    How to do it…

    How it works…

    There's more…

    See also

    Implemeting shadow mapping with percentage closer filtering (PCF)

    Getting started

    How to do it…

    How it works…

    There's more…

    See also

    Implementing variance shadow mapping

    Getting started

    How to do it…

    How it works…

    There's more…

    See also

    5. Mesh Model Formats and Particle Systems

    Introduction

    Implementing terrains using the height map

    Getting started

    How to do it…

    How it works…

    There's more…

    See also

    Implementing 3ds model loading using separate buffers

    Getting started

    How to do it…

    How it works…

    There's more…

    See also

    Implementing OBJ model loading using interleaved buffers

    Getting started

    How to do it…

    How it works…

    There's more…

    See also

    Implementing EZMesh model loading

    Getting started

    How to do it...

    How it works…

    There's more…

    See also

    Implementing simple particle system

    Getting started

    How to do it…

    How it works…

    There's more…

    See also

    6. GPU-based Alpha Blending and Global Illumination

    Introduction

    Implementing order-independent transparency using front-to-back peeling

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Implementing order-independent transparency using dual depth peeling

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Implementing screen space ambient occlusion (SSAO)

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Implementing global illumination using spherical harmonics lighting

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Implementing GPU-based ray tracing

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Implementing GPU-based path tracing

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    7. GPU-based Volume Rendering Techniques

    Introduction

    Implementing volume rendering using 3D texture slicing

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Implementing volume rendering using single-pass GPU ray casting

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Implementing pseudo-isosurface rendering in single-pass GPU ray casting

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Implementing volume rendering using splatting

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Implementing transfer function for volume classification

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Implementing polygonal isosurface extraction using the Marching Tetrahedra algorithm

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Implementing volumetric lighting using the half-angle slicing

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    8. Skeletal and Physically-based Simulation on the GPU

    Introduction

    Implementing skeletal animation using matrix palette skinning

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Implementing skeletal animation using dual quaternion skinning

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Modeling cloth using transform feedback

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Implementing collision detection and response on a transform feedback-based cloth model

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Implementing a particle system using transform feedback

    Getting ready

    How to do it…

    How it works…

    There's more…

    See also

    Index

    OpenGL Development Cookbook


    OpenGL Development Cookbook

    Copyright © 2013 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, and its dealers and distributors will be held liable for any damages caused or alleged to be 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.

    First published: June 2013

    Production Reference: 1180613

    Published by Packt Publishing Ltd.

    Livery Place

    35 Livery Street

    Birmingham B3 2PB, UK.

    ISBN 978-1-84969-504-6

    www.packtpub.com

    Cover Image by Duraid Fatouhi (<duraidfatouhi@yahoo.com>)

    Credits

    Author

    Muhammad Mobeen Movania

    Reviewers

    Bastien Berthe

    Dimitrios Christopoulos

    Oscar Ripolles Mateu

    Acquisition Editor

    Erol Staveley

    Commisioning Editor

    Shreerang Deshpande

    Lead Technical Editor

    Madhuja Chaudhari

    Technical Editors

    Jeeten Handu

    Sharvari H. Baet

    Ankita R. Meshram

    Priyanka Kalekar

    Project Coordinator

    Rahul Dixit

    Proofreaders

    Stephen Silk

    Lauren Tobon

    Indexer

    Tejal R. Soni

    Graphics

    Abhinash Sahu

    Production Coordinator

    Aparna Bhagat

    Cover Work

    Aparna Bhagat

    About the Author

    Muhammad Mobeen Movania received his PhD degree in Advance Computer Graphics and Visualization from Nanyang Technological Unviversity (NTU), Singapore. He completed his Bachelors of Science Honors (BCS(H)) in Computer Sciences from Iqra University, Karachi with majors in Computer Graphics and Multimedia. Before joining NTU, he was a junior graphics programmer at Data Communication and Control (DCC) Pvt. Ltd., Karachi, Pakistan. He was working on DirectX and OpenGL API for producing real-time interactive tactical simulators and dynamic integrated training simulators. His research interests include GPU-based volumetric rendering techniques, GPU technologies, real-time soft body physics, real-time dynamic shadows, real-time collision detection and response, and hierarchical geometric data structures. He authored a book chapter in a recent OpenGL book (OpenGL Insights: AK Peters/CRC Press). He is also the author of the OpenCloth project (http://code.google.com/p/opencloth), which implements various cloth simulation algorithms in OpenGL. His blog (http://mmmovania.blogspot.com) lists a lot of useful graphics tips and tricks. When not involved with computer graphics, he composes music and is an avid squash player. He is currently working at a research institute in Singapore.

    I would like to thank 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), my nephews/nieces, and my new born baby daughter (Muntaha Movania).

    About the Reviewers

    Bastien Berthe is a young and passionate 3D programmer. Always attracted by 3D and video games, after a few years of studying in France, he went to the Sherbrooke University in Canada and received a postgraduate degree in Computer Science, specializing in real-time systems, 3D visualization, and video games development.

    He is now working as a 3D Graphics Specialist Consultant at CAE (Montreal, QC) since 2012 and, more precisely, he is working on a new generation simulator's visualization system using mainly OpenSceneGraph and OpenGL.

    CAE (http://www.cae.com) is a global leader in modeling, simulation, and training for civil aviation, defence, healthcare, and mining.

    Dimitrios Christopoulos studied Computer Engineering and informatics at the University of Patras, Greece and holds a Master of Science (MSc) in Virtual Reality and Computer Graphics from the University of Hull in Great Britain. He started game programming in the '80s, and has been using OpenGL since 1997 for games, demos, European Union research projects, museum exhibits, and virtual reality productions. His research interests include virtual reality, human computer interaction, computer graphics, and games, with numerous publications in relevant conferences and journals. He coauthored the book More OpenGL Game Programming, Cengage Learning PTR and has also contributed to OpenGL Game Programming. He currently works as a virtual reality and 3D graphics software engineer producing games, educational applications, and cultural heritage productions for virtual reality installations.

    Oscar Ripolles received his degree in Computer Engineering in 2004 and his Ph.D. in 2009 at the Universitat Jaume I in Castellon, Spain. He has also been a researcher at the Université de Limoges, France and at the Universidad Politecnica de Valencia, Spain. He is currently working in neuroimaging at Neuroelectrics in Barcelona, Spain. His research interests include multiresolution modeling, geometry optimization, hardware programming, and medical imaging.

    www.PacktPub.com

    Support files, eBooks, discount offers and more

    You might want to visit www.PacktPub.com for support files and downloads related to your book.

    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.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at for more details.

    At www.PacktPub.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.

    http://PacktLib.PacktPub.com

    Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books. 

    Why Subscribe?

    Fully searchable across every book published by Packt

    Copy and paste, print and bookmark content

    On demand and accessible via web browser

    Free Access for Packt account holders

    If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.

    Preface

    This book is based on modern OpenGL v3.3 and above. It covers a myriad of topics of interest ranging from basic camera models and view frustum culling to advanced topics, such as dual quaternion skinning and GPU based simulation techniques. The book follows the cookbook format whereby a number of steps are detailed showing how to accomplish a specific task and are later dissected to show how the whole technique works.

    The book starts with a gentle introduction to modern OpenGL. It then elaborates how to set up a basic shader application. Following this discussion, all shader stages are introduced using practical examples so that readers may understand how the different stages of the modern GPU pipeline work. Following the introductory chapter, a vector-based camera viewing model is presented with two camera types: target and free camera. In addition, we also detail how to carry out picking in modern OpenGL using depth buffer, color buffer, and scene intersection queries.

    In simulation applications and games in particular, skybox is a very useful object. We will detail its implementation in a simple manner. For reflective objects, such as mirrors and dynamic reflections, render-to-texture functionality using FBO and dynamic cube mapping are detailed. In addition to graphics, image processing techniques are also presented to implement digital convolution filters using the fragment shader, and basic transformation, such as twirl is also detailed. Moreover, effects such as glow are also covered to enable rendering of glowing geometry.

    Seldom do we find a graphics application without light. Lights play an important role in portraying the mood of a scene. We will cover point, directional, and spot lights with attenuation and both per-vertex and per-fragment approaches. In addition, shadow mapping techniques are also covered including support of percentage closer filtering (PCF) and variance shadow mapping.

    In typical applications, more complex mesh models are used which are stored in external model files modeled in a 3D modeling package. We elaborate two techniques for loading such models by using separate and interleaved buffer objects. Concrete examples are given by parsing 3DS and OBJ model formats. These model loaders provide support for most attributes, including materials. Skeletal characters are introduced by a new skeletal animation format (the EZMesh format). We will see how to load such models with animation using both matrix palette skinning and dual quaternion skinning. Wherever possible, the recipes also detail pointers to external libraries and web addresses for more information. Fuzzy objects, such as smoke are often used to add special effects. Such objects are typically handled using a particle system. We introduce a stateless and a state-preserving particle system in detail.

    When a scene with a high depth complexity is presented, normal alpha blending techniques fail miserably. Hence, approaches such as depth peeling are used to render the geometry in the correct depth order with correct blending. We will take a look at the implementation of both the conventional front-to-back depth peeling as well as the more recent dual depth peeling approach. All steps needed in the process are detailed.

    With computer graphics, we are always pushing the limits of hardware to get a true life-like rendering. Lighting is one thing that can convincingly represent such a depiction. Unfortunately however, normal everyday lighting is impossible to simulate in real-time. The computer graphics community has developed various approximation methods for modeling of such lighting. These are grouped under global illumination techniques. The recipes elaborate two common approaches, spherical harmonics and screen space ambient occlusion, on the modern GPU. Finally, we present two additional methods for rendering scenes, namely, ray tracing and path tracing. Both of these methods have been detailed and implemented on the modern GPU.

    Computer graphics have influenced several different fields ranging from visual effects in movies to biomedical and engineering simulations. In the latter domain in particular, computer graphics and visualization methods have been widely adopted. Modern GPUs have tremendous horsepower, which can be utilized for advanced visualization methods, and volume rendering is one of them. We will take a look at several algorithms for volume rendering, namely view-aligned 3D texture slicing, single-pass GPU ray casting, pseudo-isosurface rendering, splatting, polygonal isosurface extraction using the Marching Tetrahedra algorithm, and half-angle slicing method for volumetric lighting.

    Physically-based simulations are an important class of algorithms that enable us to predict the motion of objects through approximations of the physical models. We harness the new transform feedback mechanism to carry out two physically-based simulations entirely on the GPU. We first present a model for cloth simulation (with collision detection and response) and then a model for particle system simulation on the modern GPU.

    In summary, this book contains a wealth of information from a wide array of topics. I had a lot of fun writing this book and I learned a lot of techniques on the way. I do hope that this book serves as a useful resource for others in the years to come.

    What this book covers

    Chapter 1, Introduction to Modern OpenGL, details how to set up a modern OpenGL v3.3 core profile application on Visual Studio 2010 professional version.

    Chapter 2, 3D Viewing and Object Picking, discusses how to implement a vector-based camera model for a viewing system. Two camera types are explained along with view frustum culling. Finally, object picking methods are also detailed.

    Chapter 3, Offscreen Rendering and Environment Mapping, explains how to use the framebuffer object (FBO) for offscreen rendering. Mirror and dynamic cube mapping are implemented. In addition, image processing using digital convolution and environment mapping using static cube mapping are also elaborated.

    Chapter 4, Lights and Shadows, discusses how to implement point, spot, and directional lights with attenuation. Moreover, methods of rendering dynamic shadows, such as shadow mapping, percentage close filtered (PCF) shadow maps, and variance shadow mapping are also covered in detail.

    Chapter 5, Mesh Model Formats and Particle Systems, shows how to parse standard model formats, such as 3DS and OBJ models using separate and interleaved buffer object formats. Skeletal animation format using the EZMesh model format is also detailed along with the simple particle system.

    Chapter 6, GPU-based Alpha Blending and Global Illumination, explains how to implement order-independent transparency with front-to-back and dual depth peeling. It also covers screen space ambient occlusion (SSAO) and the spherical harmonics method for image-based lighting and global illumination. Finally, alternate methods to render geometry, that is, GPU ray tracing and GPU path tracing are presented.

    Chapter 7, GPU-based Volume Rendering Techniques, discusses how to implement several volume rendering algorithms in modern OpenGL including view-aligned 3D texture slicing, single-pass GPU ray casting, splatting, pseudo-isosurface as well as polygonal isosurface rendering using Marching Tetrahedra algorithm. Volume classification and volume lighting using the half-angle slicing technique are also detailed.

    Chapter 8, Skeletal and Physically-based Simulation on the GPU, describes how to implement skeletal animation using matrix palette skinning and dual quaternion skinning on the modern GPU. In addition, it details how to use the transform feedback mode of the modern GPU for implementing a cloth simulation system with collision detection and response as well as particle systems entirely on the GPU.

    What you need for this book

    The book assumes that the reader has basic knowledge of using the OpenGL API. The example code distributed with this book contains Visual Studio 2010 Professional version project files. In order to build the source code, you will need freeglut, GLEW, GLM, and SOIL libraries. The code has been tested on a Windows 7 platform with an NVIDIA graphics card and the following versions of libraries:

    freeglut v2.8.0 (latest version available from: http://freeglut.sourceforge.net)

    GLEW v1.9.0 (latest version available from: http://glew.sourceforge.net)

    GLM v0.9.4.0 (latest version available from: http://glm.g-truc.net)

    SOIL (latest version available from: http://www.lonesock.net/soil.html)

    We recommend using the latest version of these libraries. The code should compile and build fine with the latest libraries.

    Who this book is for

    This book is for intermediate graphics programmers who have working experience of any graphics API, but experience of OpenGL will be a definite plus. Introductory knowledge of GPU and graphics shaders will be an added advantage. The book and the accompanying code have been written with simplicity in mind. We have tried to keep it simple to understand. A wide array of topics are covered and step-by-step instructions are given on how to implement each technique. Detailed explanations are given that helps in comprehending the content of the book.

    Conventions

    In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

    Code words in text are shown as follows: The maximum number of color attachments supported on any GPU can be queried using the GL_MAX_COLOR_ATTACHMENTS field.

    A block of code is set as follows:

    for(int i=0;i<16;i++) {

      float indexA = (random(vec4(gl_FragCoord.xyx, i))*0.25);

      float indexB = (random(vec4(gl_FragCoord.yxy, i))*0.25);

      sum += textureProj(shadowMap, vShadowCoords +

            vec4(indexA, indexB, 0, 0));

    }

    When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

    void main()

    {

      vEyeSpacePosition = (MV*vec4(vVertex,1)).xyz;

      vEyeSpaceNormal  = N*vNormal;

      vShadowCoords    = S*(M*vec4(vVertex,1));

     

      gl_Position      = MVP*vec4(vVertex,1);

    }

    New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: by going to the Properties menu item in the Project menu.

    Note

    Warnings or important notes appear in a box like this.

    Tip

    Tips and tricks appear like this.

    Reader feedback

    Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

    To send us general feedback, simply send an e-mail to <feedback@packtpub.com>, and mention the book title via the subject of your message.

    If there is a topic that you have expertise in

    Enjoying the preview?
    Page 1 of 1