为什么我的OpenGL自定义着色器给我这个错误? [英] Why my OpenGL custom shader gives me this error?

查看:315
本文介绍了为什么我的OpenGL自定义着色器给我这个错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在XCode11上为macOS应用程序使用了带有cocos2d 3.17的自定义着色器,但遇到了一些麻烦.

I'm using a custom shader with cocos2d 3.17 for my macOS app on XCode11 but I have some troubles.

myShader.frag

#ifdef GL_ES
 precision lowp float;
#endif

uniform sampler2D u_texture;
varying lowp vec4 v_fragmentColor;
uniform mat4 u_rotation;

void main()
{
  mat4 t1= mat4(1);
  mat4 t2= mat4(1);
  t1[3] = vec4(-0.5,-0.5,1,1);
  t2[3] = vec4(+0.5,+0.5,1,1);
  vec2 pos = (t2 * u_rotation * t1 * vec4(gl_PointCoord, 0, 1)).xy;
  gl_FragColor  =  v_fragmentColor * texture2D(u_texture, pos);
}

myShader.vert

attribute vec4 a_position;
uniform float u_pointSize;
uniform lowp vec4 u_fragmentColor;
varying lowp vec4 v_fragmentColor;

void main()
{
  gl_Position     = CC_MVPMatrix * a_position;
  gl_PointSize    = u_pointSize;
  v_fragmentColor = u_fragmentColor;
}

使用此配置,我会遇到此错误:

With this config I have this error:

cocos2d:错误:0:21:'vec4':语法错误:语法错误

cocos2d: ERROR: 0:21: 'vec4' : syntax error: syntax error

关于myShader.vert

regarding myShader.vert

我不明白,对我来说似乎很好.

I don't understand, seems good to me.

推荐答案

着色器是为桌面" OpenGL 编译着色器时,由于精度限定符,将得到错误.

The shader is written for OpenGL ES (GLSL ES 1.00). When you try to compile the shaders with "desktop" OpenGL, then you'll get errors, because of the precision qualifiers.

删除顶点着色器中的精度限定符:

Remove the precision qualifiers in the vertex shader:

attribute vec4 a_position;
uniform float u_pointSize;
uniform vec4 u_fragmentColor;
varying vec4 v_fragmentColor;

void main()
{
  gl_Position     = CC_MVPMatrix * a_position;
  gl_PointSize    = u_pointSize;
  v_fragmentColor = u_fragmentColor;
}

和片段着色器:

#ifdef GL_ES
 precision lowp float;
#endif

uniform sampler2D u_texture;
varying vec4 v_fragmentColor;
uniform mat4 u_rotation;

// [...]

精度限定符已在OpenGL ES的GLSL 1.30(OpenGL 3.0)代码可移植性中添加(不用于功能性),但是,以前的版本中不支持它们. (由于未指定版本,因此您使用的是GLSL 1.00)

Precision qualifiers have been added in GLSL 1.30 (OpenGL 3.0) code portability (not for functionality) with OpenGL ES, however, they are not supported in previous versions.
(Since you have no version specified, you are using GLSL 1.00)

或者,您可以将宏用于精度限定符:

Alternatively you can use a macro for the precision qualifier:

#ifdef GL_ES
#define LOWP lowp 
#else
#define LOWP
#endif

attribute vec4 a_position;
uniform float u_pointSize;
uniform LOWP vec4 u_fragmentColor;
varying LOWP vec4 v_fragmentColor;

// [...]

#ifdef GL_ES
precision lowp float;
#define LOWP lowp 
#else
#define LOWP
#endif

uniform sampler2D u_texture;
varying LOWP vec4 v_fragmentColor;
uniform mat4 u_rotation;

// [...]

这篇关于为什么我的OpenGL自定义着色器给我这个错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆