OpenGL Game Development Tutorials

OpenGL Book: A OpenGL Tutorial Reference Book
Easily understand OpenGL and learn how to create 3D worlds and games from scratch.

OpenGL Gems is the authorative guide on OpenGL programming published by Learning Curve / Education for software developers! This book in 3D C++ programming series clearly, and in easy to follow language, explains fundamental principles used behind programming 3D games using the OpenGL cross-platform framework.

Preorder Now in PDF format for Kindle and get a discount.
OpenGL Gems is coming out on April 15th, 2017.
The paperback version will be available via this Amazon page, please bookmark it!

OpenGL Base Code

Before we begin please take a look at this GLSL diagram below, that explains the general structure of GLSL command flow. First we compile our shaders. Then we pass them, together with our data in an ordered progression.

Note: Shader executables are not Windows API executables (they are not files with .exe extention, but rather they are executable programs that OpenGL GLSL Shader Processor can understand and execute, a process that happens in real time as your OpenGL program is running.)

GLSL Shader 330 Example

You can skip the Geometry Processor step by eliminating it from the pipeline altogether. In other words, it is perfectly fine for Vertex Processor to output data directly into Fragment Processor. That's it. No Geometry compilation or processor step.

Geometry Processor does have its unique function (tessellation, or polygon clipping, for example,) however if you're writing your first OpenGL shader application, you probably want to first make the Vertex/Fragment combo work, without the Geometry shader. Then later, when you understand how these two work together to build one pipeline, you can add the geometry shader in between. But you can completely skip the geometry shader altogether in your first test.

Each shader processor has IN and OUT data. IN is what you supply it with (vertices data array, texture data, color data, etc.) OUT is what it outputs after processing IN data.

What goes in must go out. This is a principle at work in shader processors. You pass some data in, and expect it to output some data. This new output data is then used as an input data passed to the next process. And so forth, until we are ready to output it on the screen.

First we compile all 3 shaders (vertex, geometry and fragment) into executables. (But again, geometry shader can be skipped, if you'd like, I am just explaining the full process.) Then, first vertex data - our 3D model - is processed. This is where you pass your vertices to precompiled vertex shader as IN data. It outputs OUT data.

This OUT data from vertex processor is then passed on to the Geometry Shader as IN data. The Geometry Shader outputs OUT data, which it finally passes to the Fragment (Pixel) Shader as IN data.

Whatever the Fragment Shader outputs as OUT data, is usually rendered either on the screen or on a separate texture (not visible on the screen.) This depends on current OpenGL pipeline state. You can set fragment shader to "render to texture" or render directly to the screen buffer (which will be instantly visible on the screen.)

Switching between render to texture or screen will be covered later in my tutorials.

These shaders are compiled right from inside your OpenGL source code. OpenGL provides a function called glCompileShader that takes the numeric integer ID (int or GLint) that was obtained by compiling the source code of your shader using the function glCreateShader.

Each shader first needs a "shader program" created. This is accomplished using glCreateProgram() OpenGL function. It returns a numeric ID, a handle to the program that can be used later to refer to this program.

The shader source code itself is stored in a text string variable (const char or const GLchar) which is passed to glShaderSource(const GLchar) function, right before glCompileShader function is called.

Here is a brief example of how a fragment shader could be constructed:


/* Source code for our Fragment Shader Example 1 - For Texturing */
const GLchar *fragmentShaderSource =
"#version 330 core\n"
"in vec3 ourColor;\n"
"in vec2 TexCoord;\n"
"out vec4 color;\n"
"uniform sampler2D ourTexture;\n"
"void main() { color = texture(ourTexture, TexCoord) * vec4(ourColor, 1.0f); } ";

// Create a blank shader program, and grab its OpenGL-generated numeric ID
GLuint shaderProgramID = glCreateProgram();

// Create a new FRAGMENT SHADER from scratch
GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

// Compile this shader
glShaderSource(fragmentShaderID, 1, &fragmentShaderSource, 0);
glCompileShader(fragmentShaderID);

// Our new fragmentShader is stored in numeric handle fragmentShaderID
// It is now available for use, so let's attach it to our shaderProgram
glAttachShader(shaderProgramID, fragmentShaderID);

As you can see there are some in and out variables. These are the parameters that are passed into the shader. These are the links between IN and OUT data. For example, the color vec4 in this shader is the placeholder for the data that this shader will produce. It is what will be passed to the Frame Buffer (to render on the screen) or to a texture, depending on the current state of your OpenGL program.

You will also see one "uniform" parameter called ourTexture, which is of type sampler2D. This is just a data type that stores our texture's pixel data. This is the texture that is passed to this shader. We'll see how this is done later on. This is just a general case example.

This fragment shader in particular blends texture pixels, stored in TexCoord parameter which is an "in" parameter of type vec2, with arbitrary color stored in ourColor which is an "in" parameter of type vec3.

OpenGL Book: A OpenGL Tutorial Reference Book
OpenGL Book - A OpenGL Tutorial Reference Book

If tutorials on this site are not enough, or you simply like reading from a physical book or a digital device (Kindle, iPad, tablets, etc.) check out OpenGL Book. Written by the author of tutorials on this site.

This book is a OpenGL tutorial and a reference that guides the reader through the process of setting up and initializing OpenGL, drawing 3D primitives and creating 3D computer games.

Preorder Here
© 2017 Copyright OpenGL Tutorials (www.falloutsoftware.com)

All content and graphics on this website are the property of www.falloutsoftware.com - please provide a back link when referencing on other sites.