#include <stdio.h>
#include <stdlib.h>
double *cross(double *p, double *q) {
double *r;
r= (double *)malloc(3*sizeof(double));
r[0]= p[1]*q[2]-p[2]*q[1];
r[1]=-p[0]*q[2]+p[2]*q[0];
r[2]= p[0]*q[1]-p[1]*q[0];
return r;
}
double *sub(double *p, double *q) {
double *r;
r= (double *)malloc(3*sizeof(double));
r[0]= p[0]-q[0];
r[1]= p[1]-q[1];
r[2]= p[2]-q[2];
return r;
}
int isinCircle(double *q) {
return (q[0]*q[0]+q[1]*q[1]<0.25)?1:0;
}
int isin6(double *q)
{
double p[6][3]= {
{ 0.0, 0.50, 0.},
{ 0.5, 0.25, 0.},
{ 0.5,-0.25, 0.},
{ 0.0,-0.50, 0.},
{-0.5,-0.25, 0.},
{-0.5, 0.25, 0.}};
double *r;
int i, c= 0;
for(i= 0; i<5; i++) {
r= cross(sub(p[i], q), sub(p[i+1], q));
c+= r[2]<0?0:1;
}
r= cross(sub(p[5], q), sub(p[0], q));
c+= r[2]<0?0:1;
return (c==0)?1:0;
}
int main(int argc, char* argv[]) {
double q[3]= {0., 0., 0.};
int j,k,c= 0;
int r= 5;
printf("uniform sampler2D RenderedTexture;\nuniform sampler2D DepthTexture;\n");
printf("const float blurclamp = 1.;\nconst float bias = 10.;\nuniform float focus;\n");
printf("uniform float pixelsWide;\nuniform float pixelsHigh;\n");
printf("vec2 texcel = vec2(1./pixelsWide, 1./pixelsHigh);\n");
printf("void main()\n{\n");
printf("\tvec4 depth = texture2D(DepthTexture,gl_TexCoord[0].xy );\n");
printf("\tfloat factor = ( depth.x - focus );\n");
printf("\tvec2 dofblur = vec2 (clamp( factor * bias, -blurclamp, blurclamp ));\n");
printf("\tvec4 col = vec4(0.0);\n");
for(k= -r; k<r; k++) {
for(j= -r; j<r; j++) {
q[0]= k/(r*2.);
q[1]= j/(r*2.);
if (isin6(q)==1) {
if (0) {
printf("\tcol += texture2D(RenderedTexture, gl_TexCoord[0].xy + (vec2(");
printf("%2d., %2d.", k, j);
printf(")*texcel) * dofblur);\n");
}
else {
printf("\tcol.r += texture2D(RenderedTexture, gl_TexCoord[0].xy + (vec2(");
printf("%2d., %2d.", k, j);
printf(")*texcel) * dofblur*vec2( .000, .50)).r;\n");
printf("\tcol.g += texture2D(RenderedTexture, gl_TexCoord[0].xy + (vec2(");
printf("%2d., %2d.", k, j);
printf(")*texcel) * dofblur*vec2( .866,-.25)).g;\n");
printf("\tcol.b += texture2D(RenderedTexture, gl_TexCoord[0].xy + (vec2(");
printf("%2d., %2d.", k, j);
printf(")*texcel) * dofblur*vec2(-.866,-.25)).b;\n");
}
c++;
}
}
}
printf("\tgl_FragColor = col/%d.;\n\tgl_FragColor.a = 1.0;\n}\n", c);
return 0;
}
0 件のコメント:
コメントを投稿