1

我从 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

4

1 回答 1

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"+

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

于 2012-09-29T19:35:43.253 回答