GLSL Programming/Blender/Introduction

From Wikibooks, open books for an open world
Jump to navigation Jump to search

About GLSL[edit]

GLSL (OpenGL Shading Language) is one of several commonly used shading languages for real-time rendering (other examples are Cg and HLSL). These shading languages are used to program shaders (i.e. more or less small programs) that are executed on a GPU (graphics processing unit), i.e. the processor of the graphics system of a computer – as opposed to the CPU (central processing unit) of a computer.

GPUs are massively parallel processors, which are extremely powerful. Most of today's real-time graphics in games and other interactive graphical applications would not be possible without GPUs. However, to take full advantage of the performance of GPUs, it is necessary to program them directly. This means that small programs (i.e. shaders) have to be written that can be executed by GPUs. The programming languages to write these shaders are shading languages. GLSL is one of them. In fact, it is the shading language of several 3D graphics APIs (application programming interfaces), namely OpenGL, OpenGL ES 2.x, and WebGL. Therefore, GLSL is commonly used in applications for desktop computers, mobile devices, and the web.

About this Wikibook[edit]

This wikibook was written with students in mind, who like neither programming nor mathematics. The basic motivation for this book is the observation that students are much more motivated to learn programming environments, programming languages and APIs if they are working on specific projects. Such projects are usually developed on specific platforms and therefore the approach of this book is to present GLSL within Blender.

The first part of this book consist of tutorials with working examples that produce certain effects. Note that these tutorials assume that you read them in the order in which they are presented, i.e. each tutorial will assume that you are familiar with the concepts and techniques introduced by previous tutorials. If you are new to GLSL or Blender you should at least read through the tutorials in Chapter “Basics”.

An appendix includes details on the OpenGL pipeline and GLSL syntax in general, which are not specific to Blender. Readers who are not familiar with OpenGL or GLSL might want to at least skim this part since a basic understanding of the OpenGL pipeline and GLSL syntax is very useful for understanding the tutorials.

About GLSL in Blender[edit]

Blender is a popular open source modeling and animation tool for Windows, MacOS X, Linux, and FreeBSD that includes a game engine. (Via GameKit it might also support game development for Android and iOS in the future.) Since meshes, textures, light sources, etc. can be edited in a graphical user interface, it is easier to define complex scenes than in an OpenGL (ES) or WebGL application. Blender supports vertex and fragment shaders in GLSL (i.e. “GLSL programs”; not to be confused with the built-in “GLSL material” or “GLSL shading”).

Blender can be downloaded at the Blender Foundation's download page. Some points should be noted:

  • First, Blender's Python API (which is necessary to specify GLSL programs and OpenGL states) has been dramatically changed in version 2.5 of Blender; thus, most of the tutorials and examples about GLSL programming in Blender that you might find in the internet won't work with the current version.
  • Second, Blender's Python API supports only a single render pass and it is unclear to me how to set OpenGL states (other than blending and uniform variables) for specific materials or objects. Thus, the possibilities are somewhat limited.
  • Third, GLSL programs are limited to Blender's game engine; thus, they are only displayed when the game is started. (This is particularly inconvenient when editing shader parameters.)

Martin Kraus, May 2012

Sphere with soft shadow.jpg