OpenGL ES 2.0奇怪的着色器毛刺

时间:2012-09-29 17:57:08

标签: android opengl-es

我从android SDK附带的API演示中复制了三角形渲染器,并用我自己的raymarcher替换了着色器。然而,我得到了最令人头疼的故障,似乎只是影响了红色。

这是我的着色器:

private final String fs =
"  precision mediump float;\n"+
  "uniform float t;\n"+
  "varying vec2 vtex;\n"+
  "mat3 genRotMat(float a0,float x,float y,float z){\n"+
  "  float a=a0*3.1415926535897932384626433832795/180.0;\n"+
  "  return mat3(\n"+
  "    1.0+(1.0-cos(a))*(x*x-1.0),\n"+
  "    -z*sin(a)+(1.0-cos(a))*x*y,\n"+
  "    y*sin(a)+(1.0-cos(a))*x*z,\n"+
  "    z*sin(a)+(1.0-cos(a))*x*y,\n"+
  "    1.0+(1.0-cos(a))*(y*y-1.0),\n"+
  "    -x*sin(a)+(1.0-cos(a))*y*z,\n"+
  "    -y*sin(a)+(1.0-cos(a))*x*z,\n"+
  "    x*sin(a)+(1.0-cos(a))*y*z,\n"+
  "    1.0+(1.0-cos(a))*(z*z-1.0)\n"+
  "  );\n"+
  "}\n"+
  "float cubeDist(vec3 p){\n"+
  "  float t0 = 0.5;\n"+
  "  float t1 = 0.6;\n"+
  "  float max = max(abs(p.x),max(abs(p.y),abs(p.z)));\n"+
  "  if(max > t1) return 0.0;\n"+
  "  else if(max > t0) return (t1-max)/(t1-t0);\n"+
  "  else return 1.0;\n"+
  "};\n"+
  "void main() {\n"+
  "  float spread = 1.0;\n"+
  "  float val=0.0;\n"+
  "  float delta="+delta+";\n"+
  "  float cameraz="+cameraz+";\n"+
  "  float nearz="+nearz+";\n"+
  "  float farz="+farz+";\n"+
  "  float r=0.0,g=0.0,b=0.0;"+
  "  vec3 ray=vec3(0.0,0.0,0.0);\n"+
  "  mat3 rot=genRotMat(sin(t/3.13)*360.0,1.0,0.0,0.0);\n"+
  "  rot=rot*genRotMat(sin(t/3.64)*360.0,0.0,1.0,0.0);\n"+
  "  rot=rot*genRotMat(sin(t/3.24)*360.0,0.0,0.0,1.0);\n"+
  "  ray.z=nearz;\n"+
  "  ray.xy+=vtex.xy*spread*(nearz-cameraz);\n"+
  "  for(int i=0;i<"+iterations+";i++){\n"+
  "    vec3 temp;\n"+
  "    ray.xy+=vtex.xy*spread*delta;\n"+
  "    temp=ray*rot;\n"+
  "    val+=cubeDist(temp);\n"+
  "    r+=abs(temp.x*delta*delta*val);\n"+
  "    g+=abs(temp.y*delta*delta*val);\n"+
  "    b+=abs(temp.z*delta*delta*val);\n"+
  "    ray.z+=delta;\n"+
  "  }\n"+
  "  gl_FragColor=vec4(r,g,b,1.0);\n"+
  "}\n";

这是毛刺的图像:http://imgur.com/2zcps.png

整个文件在这里:http://pastebin.com/JF7rnSdE

1 个答案:

答案 0 :(得分:1)

想出来,这不是编码问题,而是nvidia硬件/驱动程序方面的一个完全未知和奇怪的故障。

变化:

  "    r+=abs(temp.x*delta*delta*val);\n"+
  "    g+=abs(temp.y*delta*delta*val);\n"+
  "    b+=abs(temp.z*delta*delta*val);\n"+

为:

  "    g+=abs(temp.y*delta*delta*val);\n"+
  "    r+=abs(temp.x*delta*delta*val);\n"+
  "    b+=abs(temp.z*delta*delta*val);\n"+

出于一些完全未知的原因解决了这个问题。另外,改变循环迭代长度似乎也会影响这个故障。这可能是因为循环展开。