0

I've created a subclass of GLKViewController to render some waveforms using GLKBaseEffect which all works fine.

I want to use my own vertex and fragment shaders, so I used the boilerplate code from the default OpenGL Game project. Both shaders compile, link, and are usable. I can alter the color in the vertex shader without issue.

The problem I'm having is passing my own uniforms into the fragment shader. I can pass it to the vertex shader and use it there, but when I declare the same variable in the

Code:

// Vertex shader

attribute vec4 position;
uniform float amplitudeMax;

void main() {
    gl_Position = position + amplitudeMax; // This works and offsets the drawing
}

// Fragment shader

uniform float amplitudeMax; // Fails to compile
//uniform float amplitudeMax; // Compiles fine

void main()
{
    gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);
}

If curious, here is how I set up the uniforms and shaders

func loadShaders() -> Bool {

    // Copied from default OpenGL Game project

    // link program ...

    uniforms[UNIFORM_AMPLITUDE_MAX] = glGetUniformLocation(program, "amplitudeMax")

    // release shaders ...

}

// The draw loop
func render() {

    configureShaders()

    // ... draw
}

func configureShaders() {

    glUseProgram(program)

    let max = AudioManager.sharedInstance.bufferManager.currentPower)
    glUniform1f(uniforms[UNIFORM_AMPLITUDE_MAX], max)

}

I had another idea of passing the value from the vertex shader to the fragment shader using a varying float. Again I can declare and use the variable in the vertex shader but delaring it in the fragment shader cause it to fail compiling.

Edit: --------------------------------------------------

Through trial and error I found that if (in my fragment shader) qualify my uniform declaration with a precision that it works for both uniforms and varying (can pass from vertex to shader).

uniform lowp float amplitudeMax;

void main() {
    gl_FragColor = vec4(amplitudeMax, 0.0, 1.0, 1.0);
}
VaporwareWolf
  • 10,143
  • 10
  • 54
  • 80
  • Possible duplicate of [In OpenGL ES 2.0 / GLSL, where do you need precision specifiers?](http://stackoverflow.com/questions/5366416/in-opengl-es-2-0-glsl-where-do-you-need-precision-specifiers) – Reto Koradi Sep 02 '16 at 01:58
  • The post referenced above does indeed contain useful information relevant to my Edit above. – VaporwareWolf Sep 02 '16 at 03:23
  • Sounds like when shader compilation fails you're not seeing compile errors. You ought to modify your shader compilation code to use GL_INFO_LOG_LENGTH/glGetShaderInfoLog to get the compile errors. Alternatively, paste your shader into some other tool to get info on compile errors (e.g. PowerVR's PVRShaderEditor). – Columbo Sep 02 '16 at 08:00
  • You are absolutely correct. I only realized I could get compile details today. Thanks. – VaporwareWolf Sep 02 '16 at 20:40

0 Answers0