sal*_*inx 0 opengl-es glsl ios opengl-es-2.0 swift
我正在尝试深入研究着色器语言,为我的 iOS 9+ / Sprite-Kit 应用程序编写一些简单的片段着色器。但我已经坚持尝试调用一个简单的函数。这是我的着色器代码,我使用 Sprite-Kit 中的 SKShader 对象将其绑定到 Swift 2.2 应用程序中:
void main(void) {
gl_FragColor = getColor();
}
float getColor() {
return vec4(1.0, 1.0, 1.0, 1.0);
}
Run Code Online (Sandbox Code Playgroud)
当尝试编译时,我收到以下错误:
2016-06-22 12:53:35.606 ShaderTestGLSL[8425:2478215] Jet: Error Domain=MTLLibraryErrorDomain Code=3 "Compilation failed:
program_source:8:12: error: use of undeclared identifier 'getColor'
return getColor();
^
program_source:12:7: warning: no previous prototype for function 'getColor'
float getColor() {
^
program_source:13:16: error: excess elements in scalar initializer
return vec4(1.0, 1.0, 1.0, 1.0);
^ ~~~~~~~~~~~~~~~~
" UserInfo={NSLocalizedDescription=Compilation failed:
program_source:8:12: error: use of undeclared identifier 'getColor'
return getColor();
^
program_source:12:7: warning: no previous prototype for function 'getColor'
float getColor() {
^
program_source:13:16: error: excess elements in scalar initializer
return vec4(1.0, 1.0, 1.0, 1.0);
^ ~~~~~~~~~~~~~~~~
}
2016-06-22 12:53:35.608 ShaderTestGLSL[8425:2478215] SKShader failed to compile:
Compilation failed:
program_source:8:12: error: use of undeclared identifier 'getColor'
return getColor();
^
program_source:12:7: warning: no previous prototype for function 'getColor'
float getColor() {
^
program_source:13:16: error: excess elements in scalar initializer
return vec4(1.0, 1.0, 1.0, 1.0);
^ ~~~~~~~~~~~~~~~~
2016-06-22 12:53:35.629 ShaderTestGLSL[8425:2478215] <SKMetalLayer: 0x154e749d0>: calling -display has no effect.
Run Code Online (Sandbox Code Playgroud)
我的函数定义有什么问题?
你有两个问题。
首先,就像在许多其他过程语言中一样,您必须在使用函数之前声明它。getColor被叫进来的情况下main,还没有声明。通过在文件中移动getColor上述定义可以轻松解决此问题。main
其次,getColor返回 a float,但是,您的return语句尝试返回 a vec4。根据您示例中的用法,我假设您只想将函数的返回类型更改getColor为vec4.
您的固定示例如下所示:
vec4 getColor() {
return vec4(1.0, 1.0, 1.0, 1.0);
}
void main(void) {
gl_FragColor = getColor();
}
Run Code Online (Sandbox Code Playgroud)