Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shader differences on iOS

Tags:

ios

opengl-es

I am learning about 3D programming with the book Learning Modern 3D Graphics Programming, but I am having no luck with the shaders and GLES 2.0 in iOS. I am working from the xcode 4 opengl game template, though with changes to make sense to the first example in the book.

The first shaders in the book will not compile with lots of different errors. The first vertex shader

#version 330

layout(location = 0) in vec4 position;
void main()
{
    gl_Position = position;
}

Complains about the version statement and refuses to allow using layout as a specifier. I finally managed to get this to build.

attribute vec4 position;
void main()
{
    gl_Position = position;
}

Again the first fragment shader refuses to build due to the version, and will not allow the output in a global segment

#version 330
out vec4 outputColor;
void main()
{
   outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
}

With the error

ERROR: 0:10: Invalid qualifiers 'out' in global variable context

Okay so I managed to get the first example ( a simple triangle) to work with the following shaders.

vertex shader

#version 100
void main()
{
    gl_FragColor = vec4(1.0,1.0,1.0,1.0);
}

fragment shader

attribute vec4 position;
void main()
{
    gl_Position = position;
}

So those worked and I tried the first coloured example in the next chapter.

#version 330
out vec4 outputColor;
void main() {
    float lerpValue = gl_FragCoord.y / 500.0f;
    outputColor = mix(vec4(1.0f, 1.0f, 1.0f, 1.0f),
        vec4(0.2f, 0.2f, 0.2f, 1.0f), lerpValue);
}

Even working around the fixed problems from eariler, the version, f's in floats not being allowed, The shader still refuses to build with this error

ERROR: 0:13: 'float' : declaration must include a precision qualifier for type

Effectively it is complaining about float.

I have tried googling to find an explanation of the differences, but none of these come up. I have also read through the apple docs looking for advice and found no help. I am not sure where else to look, or what I am really doing wrong.

like image 423
botptr Avatar asked Jan 09 '12 11:01

botptr


1 Answers

add this at the top of the shader: precision mediump float;

or precision highp float;

depends on your needs.

like image 152
Or Arbel Avatar answered Nov 09 '22 05:11

Or Arbel