I can't find where is wrong of my GLSL code?

Viewed 1268

I am an iOS developer, use Xcode to compile some GLSL code. The shaderv.vsh code likes below:

attribute vec4 position;
attribute vec4 positionColor;
attribute vec2 textCoordinate;
uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;
varying lowp vec2 varyTextCoord;
varying lowp vec4 varyColor;

void main() {
    varyTextCoord = textCoordinate;
    varyColor = positionColor;

    vec4 vPos;
    vPos = projectionMatrix * modelViewMatrix * position;
    gl_Position = vPos;
}

And the shaderf.fsh code likes below:

precision lowp float;

varying lowp vec2 varyTextCoord;
varying lowp vec4 varyColor;
uniform sampler2D colorMap;

void main() {
    vec4 cs = texture2D(colorMap,varyTextCoord);
    vec4 cd = varyColor;
    float s = 0.2;
    float d = 0.5;
    vec4 color = (cs * s) + (cd * d);
    gl_FragColor = color;
}

When compile this code, it will be come into some error:

ERROR: 0:15: 'premature EOF' : syntax error syntax error

ERROR: 0:5: 'premature EOF' : syntax error syntax error

I can't find where is wrong. It has troubled me for a long time. Help me!

1 Answers

I have solved this problem. My shader code is right. One swift function I used by mistake.

let content = try? String(contentsOfFile: file, encoding: String.Encoding.utf8)
var source = UnsafePointer<GLchar>(content)
shader = glCreateShader(type)

glShaderSource(shader, 1,&source, nil)

When compile shader string I should convert it to cString.

let content = try? String(contentsOfFile: file, encoding: String.Encoding.utf8)
let contentCString = content?.cString(using: .utf8)
var source = UnsafePointer<GLchar>(contentCString)

Thanks to everyone hinted me.

Related