Giter VIP home page Giter VIP logo

antons_opengl_tutorials_book's Introduction

Anton's OpenGL 4 Tutorials book demo code

This series of demos accompanies the e-book "Anton's OpenGL 4 Tutorials": antongerdelan.net/opengl

Info

See LICENCE.txt for licence information.

Each chapter with major demonstration code has a corresponding demo here. There is also an example of code for Hello Triangle for OpenGL 2.1 for reference.

Each demo has easy-to-read Makefiles for Linux, MacOS, and Windows. You may need to download newer versions of the libraries in the third_party/ folder.

This code is some years old now and builds may fall out of date. I try to maintain this so that it functions, but be aware that Makefiles and build details may differ slightly from book text for this reason.

Compiling Demos

Linux

  • Install a C and C++ compiler - usually by installing a build-essential bundle package via the package manager on your distribution. E.g. for Ubuntu:

sudo apt install build-essential

  • Install the GLEW, GLFW3, FreeType, and Assimp development libraries: sudo apt install libglew-dev libglfw3-dev libfreetype-dev libassimp-dev

  • Open a terminal and cd to the demo of choice, then: make -f Makefile.linux64

  • To build all the demos you can run ./build_all_linux_osx.sh from the main directory.

Apple macOS

  • Install Clang or GNU compiler and tools - usually by installing Apple XCode through the App Store.

  • You will probably wish to install libraries via Homebrew, similarly to Linux, above. brew install glew glfw assimp freetype

  • Open a terminal and cd to the demo of choice: make -f Makefile.osx

  • To build all the demos you can run ./build_all_linux_osx.sh from the main directory.

Windows with Visual Studio

Create a new Empty, C++, Console project. You can then easily install the required libraries with NuGet, under the Project menu of a new C++ project, and you're ready to draw a triangle in 5 minutes.

Install Libraries with NuGet

  1. In the Browse tab search for and install; glfw, and glew. For later tutorials you can also find assimp, and freetype, when required.
  2. You need to add the text opengl32.lib to your linker input string. You can find this in Project->Properties->Configuration Properties->Linker->Input->Additional Dependencies. Just add opengl32.lib; to the front of the long string of dependencies so that it changes to opengl32.lib;kernel32.lib;user32;....
  3. You do not need to add the libraries you installed via NuGet to the linker string.
  4. You can now compile a "Hello Triangle" demo for OpenGL using GLFW and GLEW.

Install Libraries Manually

If you would rather do things the old fashioned way, without using a package manager, I have recorded a 2020 video stream tutorial where I show how to get Visual Studio set up and start programming OpenGL, including downloading and setting up libraries.

Tutorial: Intro to 3D Graphics Programming with OpenGL 4 (with Anton). Stream Recording.

This includes a very verbose set-up of Visual Studio 2019 with helper libraries.

Windows with GCC

  • Install the GNU Compiler Collection - usually by installing MinGW. I suggest the minimal MinGW GCC distro at https://nuwen.net/mingw.html.
  • Open a console and cd to the demo of choice.
  • make -f Makefile.win64

If you have trouble linking supporting libraries you may need to download and recompile GLFW, GLEW, AssImp, and FreeType. It's a good idea to do this anyway to stay up to date.

Caveats and Errata

  • Since publication the most reliable version of newer OpenGL that will work everywhere, including macOS, is 4.1 Core. I suggest setting window hints to try this version first.
  • Code is directly copy-pasted from book sections. This means that there will be redundant OpenGL calls to bind things etc., but I think it's easier to follow along like this.
  • Code explained in prior examples is moved to a file called gl_utils.cpp to avoid cluttering main.cpp. This means that gl_utils.cpp is not necessarily the same in each demo, but is built up gradually.
  • Out-of-date build files have been removed; 32-bit builds, and older Visual Studio files.
  • Sometimes people ask for C examples. OpenGL is a C API, and I would have used C if writing the text later. Readers wishing to use a C compiler should do so - only very minor code convention changes are required.

Contributors

Special thanks to all the readers over the years that have submitted additions, bug reports, fixes, and feedback. If you have submitted a correction and don't mind having your name/@ printed here please let me know (or if you'd like to change these details).

antons_opengl_tutorials_book's People

Contributors

0xbama avatar capnramses avatar define-private-public avatar kevinmoran avatar pavroza avatar postfixnotation avatar sarangbaheti avatar workshoft avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

antons_opengl_tutorials_book's Issues

Amazon Purchase Issue

Hi,

sorry if this was the wrong place to ask for this but couldn't find a better place since you don't seem to have a public email, and twitter doesn't allow long messages.

I'm trying to purchase the book from Amazon but to my horror they don't seem to deal with Paypal, it's also a bit tricky to use a card/bank (living in Georgia the country). I have friends in US I usually send them the money to whatever I want to buy from Amazon and they do the purchase for me.

Is there any other place than Amazon to purchase the book? If not, is it ever possible that I could pay you directly to your paypal and get a pdf version or something?

I'd really love to read the book! Thanks!

[Edit, just noticed twitter has a private message system, duh! (new to twitter)]

[Edit, just noticed I totally missed your email in the README.md, well that was embarrassing Lol, sigh...]

Hello Triangle and glew

Hi,

`
~/c/opengl/antons/hello_triangle$ make

~~~~~~~~~~~~~~~~~~~~~~~~~~~Building GNU/LINUX 64-bit~~~~~~~~~~~~~~~~~~~~~~~~~~~~

gcc -Wall -pedantic -o hellot main.c -I ../common/include ../common/linux_x86_64/libGLEW.a -lglfw -lGL -lm
gcc: error: ../common/linux_x86_64/libGLEW.a: Nie ma takiego pliku ani katalogu
Makefile:10: polecenia dla obiektu 'all' nie powiodły się
make: *** [all] Błąd 1

`

but :
sudo apt-get install libglew-dev ... ibglew-dev is already the newest version (1.13.0-2).
My system :
Ubuntu 16.04 LTS 64-bit , GeForce GTX 770/PCIe/SSE2
gcc --version
gcc (Ubuntu 5.4.1-2ubuntu1~16.04) 5.4.1 20160904
glxinfo | grep "OpenGL version"
OpenGL version string: 4.5.0 NVIDIA 384.90

Any tips ?

TIA

On linux, with `radeon` driver, the same glfwWindowHint workaround is required.

Hi,

I'm running Arch, with dual Radeon R9 290X cards (although I dare say only one works), with the open-source driver. In order to get it to chose opengl 4 I had to employ the same workaround that you have listed for apple. Not strictly an "issue", but might be worth dropping into the comments around that bit of code.

Thx.

Remove binaries of libraries & update ReadMe accordingly.

  • remove all binaries and includes.
  • macOS should suggest e.g. HomeBrew by default.
  • windows could default to NuGet, and MinGW could point to ...
  • ... a generic manual compile & install subsection for all platforms.
  • be sure to include assimp, freetype, etc for all sections.
  • update docs with any copyright/licence info for dependencies.

[question] which texture unit should be used?

In example 12, these 4 textures are bound to texture unit GL_TEXTURE1-4 respectively:

// load texture
GLuint tex_diff, tex_spec, tex_amb, tex_emiss;
glActiveTexture( GL_TEXTURE0 );
( load_texture( "boulder_diff.png", &tex_diff ) );
glActiveTexture( GL_TEXTURE1 );
( load_texture( "boulder_spec.png", &tex_spec ) );
glActiveTexture( GL_TEXTURE2 );
( load_texture( "ao.png", &tex_amb ) );
glActiveTexture( GL_TEXTURE3 );
( load_texture( "tileable9b_emiss.png", &tex_emiss ) );

but when loading these textures via load_texture, GL_TEXTURE0 is being reactive again:

glActiveTexture( GL_TEXTURE0 );

so what's the purpose here, the book doesn't tell much about this example?

GL_MAX_VARYING_FLOATS is 0 on MacOS

i'm working on EI Capitan. after running 01_extended_init example, i find the GL_MAX_VARYING_FLOATS is 0, which seems an unexpected result.

GL_LOG_FILE log. local time Fri Oct  9 17:06:29 2015
build version: Oct  2 2015 19:54:21

starting GLFW
3.0.2 Cocoa NSGL chdir menubar
renderer: Intel HD Graphics 4000 OpenGL Engine
version: 4.1 INTEL-10.8.77
GL Context Params:
GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS 80
GL_MAX_CUBE_MAP_TEXTURE_SIZE 16384
GL_MAX_DRAW_BUFFERS 8
GL_MAX_FRAGMENT_UNIFORM_COMPONENTS 4096
GL_MAX_TEXTURE_IMAGE_UNITS 16
GL_MAX_TEXTURE_SIZE 16384
GL_MAX_VARYING_FLOATS 0
GL_MAX_VERTEX_ATTRIBS 16
GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS 16
GL_MAX_VERTEX_UNIFORM_COMPONENTS 4096
GL_MAX_VIEWPORT_DIMS 16384 16384
GL_STEREO 0
-----------------------------

GL_MAX_VARYING_FLOATS
data returns one value, the maximum number of interpolators available for processing varying variables used by vertex and fragment shaders. This value represents the number of individual floating-point values that can be interpolated; varying variables declared as vectors, matrices, and arrays will all consume multiple interpolators. The value must be at least 32.

the doc says this value should be at least 32 and the output of my julia code is
GL_MAX_VARYING_FLOATS: 16384. any idea?

Tutorial 00 doesn't build on a mac

I couldn't build tutorial 00 on a Mac using the profiled osx file.
I had to change LOC_LIB to resemble tutorial 01
LOC_LIB = $(LIB_PATH)libGLEW.a $(LIB_PATH)libglfw3.a

and remove DYN_LIB

Uncomfortable camera movement

Hello again! 😄

Erroneous file: 05_virtual_camera/main.cpp

Working with the camera explained in the book is rather uncomfortable. Specifically, I'm talking about the way it can be moved around the scene. In most of the graphical softwares and games the user can move forward in the direction the camera's facing. Other movement directions (called by pressing A, S and D respectively) are also relative to the actual direction of the eye. However in the book these movement directions are constant, the direction where we look does not influence them.
In my opinion, a camera that moves with respect to where it looks is much more comfortable to use and does not introduce unbearable complexity.

Also, I'd like to point out that redeclaring the T, R and view_mat variables is a very bad practice and makes the code harder to understand and modify (talking about lines 133-136 and lines 209-212).

If you like this addition, then I can make a pull request with the modified camera movement code. It's actually pretty simple, but rather long (the length of the rendering loop) so I'd not post it here, just explain how it works.
First it extracts the direction where we look at from the view_mat. Then depending on the button pressed, a vector based on this direction is calculated (perpendicular to the right, perpendicular to the left, or opposite direction in the case of S). In the last step this vector gets scaled by the cam_speed and the elapsed_seconds and is added to the cam_pos vector.

CMake

Is there any interest on porting these to CMake and perhaps using project files and sources instead linking binary ones? I am doing such on my little sample using git submodules which I feel it is highly portable across platforms. If there would any interest, I like to do that, using git submodules or downloading dependecies via CMake or any other preferred way. If there is not any interest, simply please close this

use stbi flip function

for ( int row = 0; row < half_height; row++ ) {
top = image_data + row * width_in_bytes;
bottom = image_data + ( y - row - 1 ) * width_in_bytes;
for ( int col = 0; col < width_in_bytes; col++ ) {
temp = *top;
*top = *bottom;
*bottom = temp;
top++;
bottom++;
}
}

a "stbi_set_flip_vertically_on_load(true);" before loading the image "stbi_load(...)" flips the image, so there's no need for these lines. I'm studying your OpenGL book purchased from Amazon, it's a great learning resource, thanks!

Running on Windows

I created Empty C++ project and installed all the libraries with NuGet. Am I supposed to copy-paste all the examples into my empty project now to run them?

Mac OS X build failed.

This is the log:
main.cpp:107:3: warning: bool literal returned from 'main' [-Wmain]
return false;
^ ~~~~~
2 warnings generated.
warning: include path for stdlibc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard library instead [-Wstdlibcxx-not-found]
1 warning generated.
warning: include path for stdlibc++ headers not found; pass '-stdlib=libc++' on the command line to use the libc++ standard library instead [-Wstdlibcxx-not-found]
1 warning generated.
ld: library not found for -lstdc++
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [all] Error 1

check demos still work on macOS after removing common/ files

  • Static builds of libraries for macOS were removed.
  • Instructions now suggest installing dependencies via Homebrew.
  • Makefiles for macOS are updated, but have not been tested on a macOS machine.
  • Help is requested to build and test all of the demos using the included Makefiles.
  • And tweak any that are incorrect e.g. wrong paths.
  • And check the Homebrew instruction on the main ReadMe is correct.
  • Note that the compute shader and extensions demo (numbers 33 and 40) do not have macOS Makefiles because the need newer versions of OpenGL than those available on macOS.

Various Windows Build Issues

Something was broken in a recent update and GCC on windows doesn't build on a 64-bit windows.

  • There are no 64-bit windows makefiles.
  • glew .dll is missing for 64-bit and the .dll included is for 32-bit.
  • glew .a stub is missing for 64-bit.

Quick Fix

  • Download glew binaries and update them.
  • Remove 32-bit window stuff entirely.

or Part I

  • Remove 32-bit Windows build parts.
  • Static compile glew into examples. Remove dll and .a etc. entirely.
  • Note changes on readme e.g. -Dglew_static or whatever flag changes required.

Part II

  • Consolidate Makefiles into a single file per demo - not 1 per platform.
  • Create a global Makefile that can build everything for every demo.
  • Create a global .bat file that can build everything using cl
  • Update docs.

Phong shading - Broken fragment shader

Hello!

Erroneous file: 08_phong/test_fs.glsl

While making our way through the material of this book in class, we've noticed that there is something off with the fragment shader of the Phong shading example.

When running the example program of the book with one triangle (and the same, unit-length normals for every vertex), the shading was correct. However it failed to produce a correct result for a sphere that consists of triangles with different per vertex normals.

After we've made sure that our normal calculations are correct, we've turned to the fragment shader. Basically we've compared it to the Phong shading code published in the OpenGL 4.0 Shading Language Cookbook written by David Wolff (First Edition, pages 89-90).
The erroneous shading is caused by the non-unit normal vector lengths. The length of the normal_eye variable is not guaranteed to be 1.0 because it is an interpolated value calculated from the vertex normals on a per-fragment basis. So it must be normalized before using it in the dot and reflect calls in line 30 and line 37. The documentation of the reflect function explicitly states that

N should be normalized in order to achieve the desired result.

Original shader
Fixed shader

quaternions code / chapter has error

olivier points out this bug

/* multiply quaternions to get another one. result=R*S */
void mult_quat_quat (float* result, float* r, float* s) {
	result[0] = s[0] * r[0] - s[1] * r[1] - s[2] * r[2] - s[3] * r[3];
	result[1] = s[0] * r[1] + s[1] * r[0] - s[2] * r[3] + s[3] * r[2];
	result[2] = s[0] * r[2] + s[1] * r[3] + s[2] * r[0] - s[3] * r[1];
	result[3] = s[0] * r[3] - s[1] * r[2] + s[2] * r[1] + s[3] * r[0];
	// re-normalise in case of mangling
	normalise_quat (result);
}

read/write conflict calling mult...(quaternion, other, quaternion) . should really be using structs but can fix

-> also check text to fix there too on next update

Unformatted code

The main.cpp files appear to be unformatted, which makes some parts a bit hard to read.

I suggest applying clang-format to these, I have been trying it locally and the results are very good.

All that is needed is choosing a formatting style from llvm, google, chromium, mozilla, webkit, and microsoft.

I have tested it with llvm and it turns code like this:

Screenshot from 2020-07-05 12-53-15

into this:

GLfloat proj_mat[] = { Sx, 0.0f, 0.0f, 0.0f, 0.0f, Sy, 0.0f, 0.0f, 0.0f, 0.0f, Sz, -1.0f, 0.0f, 0.0f, Pz, 0.0f };

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.