Chapter 5. Graphics – Modern Method with OpenGL 3.0+

This chapter will cover the following recipes:

  • Loading and using GLSL shaders
  • Using uniform variables with shaders
  • Writing a vertex shader
  • Writing a fragment (pixel) shader
  • Drawing primitives by using vertex buffers
  • Rendering to texture
  • Applying highlights and shadows to the scene
  • Bumpmapping

Introduction

This chapter will deal with programming and using dynamic rendering pipeline in OpenGL. While shaders have been available since OpenGL 2.0, their first versions are now considered deprecated. A wide variety of graphic cards now support at least OpenGL 3.3, which implements the currently valid specification of GLSL shaders. This chapter will focus on GLSL version 3.3, which is relevant for OpenGL 3.3.

Shaders are small programs that define the behavior of the graphic card for scene rendering. They are usually written in C-like language and compiled into binary form by the graphical driver. Shader programs are compiled at runtime. Just out of curiosity, a newer version of OpenGL 4.1 allows you to compile shader programs into binary form, which can be saved into a file and used later without re-compilation.

Shaders provide substantially better flexibility than a fixed pipeline and present a door to parallel graphical processing on GPU.

The immediate mode for the fixed rendering pipeline used rendering commands enclosed in the gl.Begin and gl.End pairs. Dynamic rendering pipeline no longer uses these commands. Instead, it relies on massive usage of vertex buffers for data storage. Usually, you fill the vertex buffer with vertex data such as color, position, texture coordinates, and normal vectors. This data is used by shader programs to render vertices. This approach is much faster because you're encouraged to transfer vertex data in batches. Frequent calls from the application to the GPU cause stalling of rendering processes, and therefore, decrease the overall performance.

This chapter will also show a few tricks with rendering to texture, which can be used for postprocessing effects. The final set of recipes will deal with the Phong shading effect and the bumpmapping effect.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.230.81