OpenGL 4 Shading Language Cookbook - Third Edition, published by Packt

Overview

Example code from the OpenGL 4 Shading Language Cookbook, 3rd Edition

The example code from the OpenGL 4 Shading Language Cookbook, 3rd Edition, by David Wolff and published by Packt Publishing.

Requirements

To compile these examples, you'll need the following:

  • The GLM Mathematics Library version 0.9.6 or later. Note that versions prior to 0.9.6 may not work properly because of a switch from degrees to radians. GLM 0.9.5 will work, but you'll need to add #define GLM_FORCE_RADIANS prior to including the glm header files.
  • GLFW version 3.0 or later.

Compiling the examples

The example code builds with CMake. Note that the examples for Chapter 10 will not function on MacOS due to lack of support for compute shaders on that platform.

  1. Install GLFW by following the instructions on their web site.
  2. Install the latest version of GLM. Note that for CMake to find GLM correctly, you need to run the install "build" (e.g. make install) or install GLM from your favorite package manager. Otherwise, the CMake config files will not be created/available.
  3. Download this example code from github, or clone using git.
  4. Run cmake. If cmake has difficulties finding the GLFW or GLM installations, set the variable CMAKE_PREFIX_PATH to help cmake find them.
    It can be tricky to get CMake to find the GLM libraries, unfortunately. See below for tips.
  5. Compile by running make.

Any problems, create an issue on github.

Tips for getting CMake to find GLM

When searching for GLM, CMake looks for the files glmConfig.cmake and glmConfigVersion.cmake.

If you install GLM using a package manager such as Homebrew on macOS, or a Linux package manager the cmake files should already be included.

Otherwise, if you're using the GLM source distribution, you'll have to run GLM through CMake to get it to generate the glmConfig.cmake and glmConfigVersion.cmake files. It is frustrating, I wish that the GLM maintainers didn't require this.

  1. Download GLM and extract it to some location: GLM_SRC
  2. cd $GLM_SRC
  3. mkdir build
  4. cd build
  5. cmake -D GLM_TEST_ENABLE=OFF -D CMAKE_INSTALL_PREFIX=MY_GLM_LOCATION ..
  6. cmake --build . --target install

Replace GLM_SRC above with the place where you extracted the GLM zip file, and replace MY_GLM_LOCATION with the location where you want to install GLM. This should generate the needed cmake files and install all of GLM to MY_GLM_LOCATION.

Tips for compiling for Windows with Visual Studio

  • Use the Visual Studio target in CMake: -G "Visual Studio...", open the Visual Studio solution. You should see one project per chapter.
  • Each chapter requires a command line argument to choose a recipe. When running in VS, be sure to set the 'Command Argument' under 'Properties' for the appropriate recipe.

OpenGL Function Loading

An OpenGL header file and a function loader for a 4.3 core profile are included with this project. They were generated using GLAD. This loader should also work on MacOS under a 4.1 core profile, but of course not all functions will load.

The code has been tested with OpenGL 4.3 on Windows/Linux and OpenGL 4.1 on MacOS.

OpenGL 4 Shading Language Cookbook - Third Edition

This is the code repository for OpenGL 4 Shading Language Cookbook - Third Edition, published by Packt.

Build high-quality, real-time 3D graphics with OpenGL 4.6, GLSL 4.6 and C++17

What is this book about?

OpenGL 4 Shading Language Cookbook, Third Edition provides easy-to-follow recipes that first walk you through the theory and background behind each technique, and then proceed to showcase and explain the GLSL and OpenGL code needed to implement them.

This book covers the following exciting features:

  • Compile, debug, and communicate with shader programs
  • Use compute shaders for physics, animation, and general computing
  • Learn about features such as shader storage buffer objects and image load/store
  • Utilize noise in shaders and learn how to use shaders in animations
  • Use textures for various effects including cube maps for reflection or refraction

If you feel this book is for you, get your copy today!

https://www.packtpub.com/

Instructions and Navigations

All of the code is organized into folders. For example, Chapter02.

The code will look like the following:

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(800, 600, "Title", nullptr, nullptr);

Following is what you need for this book: If you are a graphics programmer looking to learn the GLSL shading language, this book is for you. A basic understanding of 3D graphics and programming experience with C++ are required.

With the following software and hardware list you can run all code files present in the book (Chapter 1-11).

Software and Hardware List

Chapter Software required OS required
All GLM Mathematics Library Windows, Mac OS X, and Linux (Any)
GLFW Windows, Mac OS X, and Linux (Any)
CMake Windows, Mac OS X, and Linux (Any)
Visual Studio 2017 Windows, Mac OS X, and Linux (Any)

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. Click here to download it.

Related products

Get to Know 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.

Other books by the author

Suggestions and Feedback

Click here if you have any feedback or suggestions.

Comments
  • cmake configure error in windows

    cmake configure error in windows

    error:

    CMake Error at CMakeLists.txt:18 (find_package): Could not find a package configuration file provided by "glm" with any of the following names:

    glmConfig.cmake
    glm-config.cmake
    

    Add the installation prefix of "glm" to CMAKE_PREFIX_PATH or set "glm_DIR" to a directory containing one of the above files. If "glm" provides a separate development package or SDK, be sure it has been installed.

    i can cmake glm package sucessfully but do not get the two cmake files below. this is in windows. not linux or mac. i am using cmake gui version.

    opened by zekeliu 10
  • Unable to create OpenGL context

    Unable to create OpenGL context

    $ ./chapter01 basic
    Unable to create OpenGL context.
    $./chapter02 basic-attrib
    Unable to create OpenGL context.
    $ ./chapter03 diffuse
    Unable to create OpenGL context.
    

    In spite of success executions (but with a segfault sometimes) of examples from the 2nd edition of this book.

    opened by unegare 7
  • Segmentation fault at close

    Segmentation fault at close

    Hi, the examples all work on Ubuntu 18.04 but they don't exit cleanly starting from Chapter 2. Only Chapter 1's example exits properly without any issues. When the X is clicked with the mouse or when I hit ESC, the app always segmentation faults at close. Here's the output for Chapter 2's example:

    $ ./chapter02 basic-attrib

    GL Vendor : NVIDIA Corporation GL Renderer : GeForce GTX 1070 Ti/PCIe/SSE2 GL Version : 4.6.0 NVIDIA 418.56 GL Version : 4.6 GLSL Version : 4.60 NVIDIA MSAA samples : 0 MSAA buffers : 0

    Active attributes: 1 VertexColor (vec3) 0 VertexPosition (vec3) OpenGL:OtherNOTIFY: Buffer detailed info: Buffer object 1 (bound to GL_ARRAY_BUFFER_ARB, usage hint is GL_STATIC_DRAW) will use VIDEO memory as the source for buffer object operations. OpenGL:OtherNOTIFY: Buffer detailed info: Buffer object 2 (bound to GL_ARRAY_BUFFER_ARB, usage hint is GL_STATIC_DRAW) will use VIDEO memory as the source for buffer object operations. OpenGL:PerformanceMED: Program/shader state performance warning: Vertex shader in program 1 is being recompiled based on GL state. App:MarkerNOTIFY: End debug Segmentation fault

    opened by ghost 5
  • Suggestions and feedback broken link

    Suggestions and feedback broken link

    The link to "suggestions and feedback" at the bottom of the README.md is broken.

    https://docs.google.com/forms/d/e/1FAIpQLSdy7dATC6QmEL81FIUuymZ0Wy9vH1jHkvpY57OiMeKGqib_Ow/viewform

    Is there anywhere else I can send suggestions/feedback? I'm getting a lot out of the book, it's proving to be a vauable resource. I would suggest a more thorough discussion of SPIRV shaders for use in OpenGL.

    One of the reasons I purchased this book was because of the following on the amazon page: What you will learn

    • Understand physically based reflection models and the SPIR-V Shader binary

    But the discussion of SPIRV feels a little lacking, partly because in practice - getting SPIRV shaders working correctly on OpenGL is a bit of a fools errand, with support across vendors and platforms seeming to be incredibly lacking/broken.

    It also makes no mention that raw uniforms are not supported, in favour of uniform buffer objects.

    opened by tim-rex 1
  • "Unable to create OpenGL context."

    I just installed a new Ubuntu 18.04 operating system with Cuda 10.1 and its associated graphics driver 4.18. I have also git cloned this repository and compiled it with cmake without a hitch. I am even able to run the an executable, except that I run into the error Unable to create OpenGL context.. I have been told that old drivers cause this, but I just installed all of the newest drivers.

    What should i do to fix this?

    Edit:

    I decided to do more investigative work on the issue and I noticed that OpenGL simply will not link properly when -lGL is passed to a command line compile option. Well, I got to thinking: are the drivers REALLY correct?

    So that I searched on checking OpenGL versions.

    Running

    glxinfo | grep "OpenGL version"
    

    On my machine that does work gives me this:

    OpenGL version string: 4.6.0 NVIDIA 410.48
    

    and

    Running

    glxinfo | grep "OpenGL version"
    

    On my machine that does NOT work gives me this:

    OpenGL version string: 3.0 Mesa 19.0.8
    

    I know that you can have Mesa aptitude packages installed, but I somehow am still not getting NVidia to load the OpenGL context properly.

    How do you force OpenGL loaders?

    P.S.

    I tried to compile the NVIDIA CUDA 10.1 examples right off of the home directory and the OpenGL example:

    ~/NVIDIA_CUDA-10.1_Samples/2_Graphics/marchingCubes
    

    Does NOT compile. Here is the full error:

    make[1]: Entering directory '/home/devmachine/NVIDIA_CUDA-10.1_Samples/2_Graphics/marchingCubes'
    /usr/local/cuda/bin/nvcc -ccbin g++   -m64      -gencode arch=compute_30,code=sm_30 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_52,code=sm_52 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_61,code=sm_61 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_75,code=sm_75 -gencode arch=compute_75,code=compute_75 -o marchingCubes marchingCubes.o marchingCubes_kernel.o   -lGL -lGLU -lglut
    /usr/bin/ld: cannot find -lGL
    collect2: error: ld returned 1 exit status
    Makefile:314: recipe for target 'marchingCubes' failed
    make[1]: *** [marchingCubes] Error 1
    make[1]: Leaving directory '/home/devmachine/NVIDIA_CUDA-10.1_Samples/2_Graphics/marchingCubes'
    Makefile:51: recipe for target '2_Graphics/marchingCubes/Makefile.ph_build' failed
    make: *** [2_Graphics/marchingCubes/Makefile.ph_build] Error 2
    
    

    I know that only the first question is related to the book, but I think the extra context helps.

    opened by thatrobotguy 1
  • Linux and c++17 fixes

    Linux and c++17 fixes

    The gitignore file actually ignores the cmake directory. The CMake file now defaults to modern OpenGL on Unix machines. Before this fix CMake would tell you that 2 versions were available, but it is not clear which one is being used. The random.h file now uses correct C++17 default random number generator. Before this fix C++17 would not compile because the definition of std::shuffle no longer exists. The updated code uses a version of std::shuffle that does exist. This has been tested and solved on Ubuntu 18.04 LTS with GCC 9.x.x and CMake 3.15.2.

    opened by thatrobotguy 1
  • Some model files are missing

    Some model files are missing

    The ssao example in chapter06 fails to run. Because both the model files dragon.obj and bunny.obj do not exist in this repo. And there is no instruction in README to help me how I can get access to these models.

    opened by unknownue 1
Owner
Packt
Providing books, eBooks, video tutorials, and articles for IT developers, administrators, and users.
Packt
JNI Cookbook Redux

Inside this book you can find JNI related codes that I have written over years of working with JNI. I was typically preparing small samples to test some particular concepts.

Michal 88 Dec 29, 2022
Apache Arrow Cookbook

Cookbooks are a collection of recipes about common tasks that Arrow users might want to do. The cookbook is actually composed of multiple cookbooks, one for each supported platform, which contain the recipes for that specific platform.

The Apache Software Foundation 65 Dec 28, 2022
C++ OpenGL 3D Game Tutorial Series - Learn to code an OpenGL 3D Game in C++ from scratch

C++ OpenGL 3D Game Tutorial Series is a YouTube Tutorial Series, whose purpose is to help all those who want to take their first steps in the game dev

 PardCode 118 Dec 22, 2022
🌼 Homework of Computer Systems: A Programmer's Perspective (3rd Edition) and Autolab solutions of CMU 15-513: Intro to Computer Systems

Exercisebook of Computer Systems: A Programmer's Perspective, 3/E (CS:APP3e) CS:APP3e is written by Randal E. Bryant and David R. O'Hallaron, Carnegie

halfrost 31 Nov 20, 2022
Programming Language T#. Compiled language. In development.

Programming Language T# WARNING! THIS LANGUAGE IS A WORK IN PROGRESS! ANYTHING CAN CHANGE AT ANY MOMENT WITHOUT ANY NOTICE! $ make func main() {

Ibuki Yoshida 1 Feb 1, 2022
T# Programming Language. Interpreted language. In development. I will make this compilable later.

The T# Programming Language WARNING! THIS LANGUAGE IS A WORK IN PROGRESS! ANYTHING CAN CHANGE AT ANY MOMENT WITHOUT ANY NOTICE! Install Install $ make

T# 91 Jun 24, 2022
Design 3D KUET Word in OpenGL C++

Design-3D-KUET-Word-in-OpenGL-CPP Design 3D KUET Word in OpenGL C++ Graphics Operation Rotation: X, Y, Z Scaling Translation Zoom in and out 1: Output

Faisal Ahmed 2 Nov 5, 2021
Competitive Programming Language MM

MM Language MM Languageは、競技プログラミングのために開発中のプログラミング言語です。 どんなことが可能なのかは、examplesおよびexamples_outputsを参照ください。 まだ開発中の言語であるため、諸々不備があり、コンパイルエラーの行数表示さえまともに出せない状

null 21 Aug 22, 2022
simple and fast scripting language

The Aument Language The Aument language is a work-in-progress dynamically-typed scripting language with performance first: this scripting language is

The Aument Project 35 Dec 27, 2022
This repository is a summary of the basic knowledge of recruiting job seekers and beginners in the direction of C/C++ technology, including language, program library, data structure, algorithm, system, network, link loading library, interview experience, recruitment, recommendation, etc.

?? C/C++ 技术面试基础知识总结,包括语言、程序库、数据结构、算法、系统、网络、链接装载库等知识及面试经验、招聘、内推等信息。This repository is a summary of the basic knowledge of recruiting job seekers and beginners in the direction of C/C++ technology, including language, program library, data structure, algorithm, system, network, link loading library, interview experience, recruitment, recommendation, etc.

huihut 27k Dec 31, 2022
A cheatsheet of modern C++ language and library features.

C++20/17/14/11 Overview Many of these descriptions and examples come from various resources (see Acknowledgements section), summarized in my own words

Anthony Calandra 15.4k Jan 6, 2023
The DSiLanguagePatcher increases accessibility to foreign region DSi consoles by providing a mean to change the user interface language.

DSi Language Patcher The DSi Language patcher is a small tool, which runs on your DSi (homebrew execution required) and create a copy of your original

null 20 Nov 7, 2022
A library of language lexers for use with Scintilla

README for Lexilla library. The Lexilla library contains a set of lexers and folders that provides support for programming, mark-up, and data languag

Scintilla 93 Jan 1, 2023
An open source, OOP language with editable syntax.

Copper An Open source compiled programming language, In development. Goals Copper is an general-purpose OOP language. Coppers main goal is to allow ea

null 13 Nov 18, 2022
Applied SDLC using C language

SDLC Activity Based Learning Build Code Quality Unity Git Inspector Folder Structure Folder Description 1_Requirements Documents detailing requirement

Arnob Chowdhury 7 Nov 8, 2022
the kyanite programming language

Kyanite Kyanite is a small passion-project programming language intended to be light-weight and simple to use. You can read more on the language itsel

Kyanite 5 Aug 19, 2021
λQ: A Simple Quantum Programming Language based on QWIRE.

λQ λQ: A Simple Language based on QWIRE which can be compiled to QASM. The name λQ means lambda calculus with quantum circuits. This is a term project

Wenhao Tang 5 Jul 11, 2021
A comprehensive catalog of modern and classic books on C++ programming language

A comprehensive catalog of modern and classic books on C++ programming language

Yurii Cherkasov 384 Dec 28, 2022
A minimal, toy programming language implemented in C++ and STL.

od Programming Language Mod (or ModLang) is a minimal, toy programming language implemented in C++ and STL (Standard Template Library) with no other e

Pranav Shridhar 27 Dec 4, 2022