Mark Sachs (ksleet) wrote,
Mark Sachs
ksleet

  • Mood:
  • Music:

OpenGL Shader Builder

For some reason this is an infuriating program to work with. Apple's documentation is minimal and contradictory, referring to features that I couldn't find anywhere in the actual application. So I wanted to share two useful things I learned.

First, this is a decent tutorial for just getting the application up and running and understanding how the different UI components interact.

Second, OpenGL shaders make use of many built-in variables. For example, gl_LightSource[0] is a structure containing information about light source number 0, such as gl_LightSource[0].position, the position of the light source. You might think to yourself, "Self," you'd think, "so that means I can just write an ordinary shader, and then I can easily tweak the light source settings and other built-in variables to see how they react." A few increasingly rage-filled minutes later, though, you'd realize that you were wrong. As far as I can tell there are no knobs anywhere to fool with the built-in variables.

You can, however, create new variables. So in this variation of the default vertex shader:

varying vec3 lightDir, normal;

uniform vec3 lightPos;

void main()
{
    normal = normalize(gl_NormalMatrix * gl_Normal);
    lightDir = normalize(lightPos);

    gl_TexCoord[0] = gl_MultiTexCoord0;
    gl_Position = ftransform();
}


...we've replaced gl_LightSource[0].position with the new uniform variable lightPos. That variable will then appear in the Symbols pane of the shader builder and you can fool with it to your heart's content, seeing how the light source reacts.

Of course, this means that the shader you're writing in the Shader Builder isn't quite the same one you'll be making in your program, since you're using these fake variables that will have to be swapped back out. But I guess nobody said shader programming would be easy.

Oh, and also, because the shader tool spatters so many tiny little windows around I'd say this is a good excuse to turn on the Mac's Spaces feature and begin keeping things a bit more orderly.
Tags: neon galaxy, nerd
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 1 comment