首页 > 解决方案 > 我找不到我的 GLSL 代码哪里出错了?

问题描述

我是一名 iOS 开发人员,使用Xcode编译一些GLSL代码。shaderv.vsh代码如下:

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;
}

shaderf.fsh代码如下:

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;
}

编译这段代码时,会出现一些错误:

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

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

我找不到哪里错了。它困扰了我很长时间。帮我!

标签: swiftopengl-es

解决方案


我已经解决了这个问题。我的着色器代码是正确的。我错误地使用了一个快速功能。

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

glShaderSource(shader, 1,&source, nil)

编译着色器字符串时,我应该将其转换为 cString。

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

感谢大家提示我。


推荐阅读