tags:

views:

109

answers:

3

The OpenGL Superbible 5th Edition was recently released, and it documents OpenGL 3.3. Unfortunately, OS X only supports OpenGL 2.1 and GLSL version 1.20. The very first non-trivial vertex shader they give you fails to compile with the error message:

ERROR: 0:5: '' :  Version number not supported by GL2
ERROR: 0:8: 'in' : syntax error syntax error

The shader is, as written:

// Simple Diffuse lighting Shader
// Vertex Shader
// Richard S. Wright Jr.
// OpenGL SuperBible
#version 130

// Incoming per vertex... position and normal
in vec4 vVertex;
in vec3 vNormal;

// Set per batch
uniform vec4 diffuseColor; 
uniform vec3 vLightPosition;
uniform mat4 mvpMatrix;
uniform mat4 mvMatrix;
uniform mat3 normalMatrix;

// Color to fragment program
smooth out vec4 vVaryingColor;

void main(void) 
    { 
    // Get surface normal in eye coordinates
    vec3 vEyeNormal = normalMatrix * vNormal;

    // Get vertex position in eye coordinates
    vec4 vPosition4 = mvMatrix * vVertex;
    vec3 vPosition3 = vPosition4.xyz / vPosition4.w;

    // Get vector to light source
    vec3 vLightDir = normalize(vLightPosition - vPosition3);

    // Dot product gives us diffuse intensity
    float diff = max(0.0, dot(vEyeNormal, vLightDir));

    // Multiply intensity by diffuse color
    vVaryingColor.rgb = diff * diffuseColor.rgb;
    vVaryingColor.a = diffuseColor.a;

    // Let's not forget to transform the geometry
    gl_Position = mvpMatrix * vVertex;
    }
+1  A: 

Short of changing the #version to match 120, you also need to change in to attribute and out to varying. I may miss something else, but that's all that shows for me right now.

Bahbar
+3  A: 

replace the glsl version by :

#version 120

but in 1.2 the keyword in and out were not defined yet, it was attribute and varying.

smooth varying vec4 vVaryingColor;

You'll probably need to make similar changes in the fragment shader

For vVertex and vNormal, these are custom names, which means they have been bound in the C++ code. The easiest way to work around this is to rename them gl_Vertex and gl_Normal

Calvin1602
Thanks! I'll give this a shot when I'm back at my computer and get back to stack with the results.
sludge
A: 

Unfortunately, I've come to believe this is a fool's errand. The book uses a GLTools library (distributed on the website) which enforces the passing in of various parameters in a way that is fundamentally incompatible with OpenGL 2.1.

If it were one example, it could be rewritten, but it's a number of examples and the effort would be overwhelming for the return if you were trying to teach yourself OpenGL.

You have two options:

  1. Buy a Windows machine which supports OpenGL 3 and put your Mac in the corner until Apple steps up to support the newer standard.
  2. Buy the 4th Edition of the book which is still in print.

From the website:

If you are still interested in the deprecated functionality of pre-OpenGL 3.x, we recommend the fourth edition, which is still in print, and which covers OpenGL 2.1 and the fixed function pipeline quite thoroughly.

Meta: I am going to avoid marking my answer as the "approved" answer since the question can still be answered accurately by someone who understands the differences between the shader versions. However, it should remain as wise advice when considering the bigger picture.

sludge