Why my OpenGL custom shader gives me this error?

醉酒当歌 提交于 2020-07-21 09:08:27

问题


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: ERROR: 0:21: 'vec4' : syntax error: syntax error

regarding myShader.vert

I don't understand, seems good to me.


回答1:


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

and the fragment shader:

#ifdef GL_ES
 precision lowp float;
#endif

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

// [...]

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;

// [...]


来源:https://stackoverflow.com/questions/61247278/why-my-opengl-custom-shader-gives-me-this-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!