HTML

the future?

sc3n3 2011.12.28. 16:25

Nos, lehet, hogy nem ertek a raytracinghez, de akkor biztos csak veletlenul megy 2.5 fps-el majdnem 300000 haromszog egyszeru CG shaderben, egy 17000Ft-os videokartyan,

Olvasgatom, hogy egyesek szerint hogy kellene mindenfele nyakatekert modszerrel multiprocesszoros rendszerekre programot irni. Nos, ez a rajtrace egyetlen shader, es majdnem ugy nez ki, mint ami a CPU-n fut. A C nyelv tokeletes. Akik megszoktak a objektumok kenyelmet, most csunyakat mondanak, de kit erdekel. Ez az igazsag.

A CUDA es OpenCL semmi olyan pluszt nem tud, amit CG-ben ne lehetne megoldani.

 

A buddha file innen szerezheto meg.

http://graphics.stanford.edu/data/3Dscanrep/

ftp://graphics.stanford.edu/pub/3Dscanrep/happy/happy_recon.tar.gz

happy_vrip_res2.ply

ply
format ascii 1.0
comment generated by ply_writer
element vertex 144647
property float x
property float y
property float z
element face 293232
property list uchar int vertex_indices
end_header

 

Csak a shadert linkelem, a foprogram csak egyetlen lapot renderel. A kdtree epito nem publikus. Sorry.

int chk_bbox(float3 src,float3 ray,float3 bbox1,float3 bbox2,float tmin)
{
float t5=0.0;//01;

if(src.x>=bbox1.x)
if(src.x<=bbox2.x)
if(src.y>=bbox1.y)
if(src.y<=bbox2.y)
if(src.z>=bbox1.z)
if(src.z<=bbox2.z) return 1;


int o=0;
float3 obj;
float tx,ty,tz,t1;
if(ray.x>0.0) {tx=(bbox1.x - src.x)/ray.x;}
else {tx=(bbox2.x - src.x)/ray.x;}
if(ray.y>0.0) {ty=(bbox1.y - src.y)/ray.y;}
else {ty=(bbox2.y - src.y)/ray.y;}
if(ray.z>0.0) {tz=(bbox1.z - src.z)/ray.z;}
else {tz=(bbox2.z - src.z)/ray.z;}
if(tx>ty) if(tx>tz) {o=1;t1=tx;}
if(ty>tx) if(ty>tz) {o=2;t1=ty;}
if(tz>ty) if(tz>tx) {o=3;t1=tz;}

if(o==1)
if(t1>t5)
if(t1<tmin)
{
obj.y=src.y + ray.y*t1;

if(obj.y>=bbox1.y)
if(obj.y<=bbox2.y)
{
obj.z=src.z + ray.z*t1;
if(obj.z>=bbox1.z)
if(obj.z<=bbox2.z) return 1;
}
}

if(o==2)
if(t1>t5) / />
if(t1<tmin) //<
{
obj.x=src.x + ray.x*t1;

if(obj.x>=bbox1.x)
if(obj.x<=bbox2.x)
{
obj.z=src.z + ray.z*t1;
if(obj.z>=bbox1.z)
if(obj.z<=bbox2.z) return 1;
}
}

if(o==3)
if(t1>t5)
if(t1<tmin)
{
obj.x=src.x + ray.x*t1;

if(obj.x>=bbox1.x)
if(obj.x<=bbox2.x)
{
obj.y=src.y + ray.y*t1;
if(obj.y>=bbox1.y)
if(obj.y<=bbox2.y) return 1;
}
}
return 0;
}


void ps_realraytrace_ultrafast(float3 position : TEXCOORD0,
float3 normal : TEXCOORD1,
float2 txc : TEXCOORD2,
uniform sampler2D tx_bboxmap : TEX0,
uniform sampler2D tx_bbox1 : TEX1,
uniform sampler2D tx_bbox2 : TEX2,

uniform sampler2D tx_vert1 : TEX3,
uniform sampler2D tx_edge1 : TEX4,
uniform sampler2D tx_norm2 : TEX5,
uniform sampler2D tx_tang1 : TEX6,

uniform sampler2D tx_col5 : TEX7,
uniform sampler2D tx_rnd1 : TEX8,
uniform sampler2D tx_decal1 : TEX9,

uniform float3 eye,
uniform float3 camx,
uniform float3 camy,
uniform float3 camz,
uniform float3 xdir,
uniform float3 rnd1,
uniform float3 time3,

uniform float txsizexi,// 1/txsize!
uniform float txsizeyi,// 1/txsize!
uniform float3 dtx_aliasing,


// uniform int render_mode,
uniform int txc2x_limit,
uniform int txc2y_limit,

out float4 color0 : COLOR)
{
color0.w=1;

// float3 ray=camz*0.7+ camx*(txc.x-0.5)*800.0/600.0 + camy*(txc.y-0.5);
float3 ray=camz*0.7+ camx*(txc.x-0.5+dtx_aliasing.x)*500.0/400.0 + camy*(txc.y-0.5+dtx_aliasing.y);

ray=normalize(ray);


float3 ray2=float3(0,0,0);
float3 eye2=float3(0,0,0);


float3 snorm=float3(0,0,1);
float3 sobj=float3(0,0,0);
float3 scol=float3(0,0,0);

float3 sref=float3(0,0,0);
float3 col3=float3(0,0,0);
float3 col4=float3(0,0,0);
float3 gcol=float3(0,0,0);
float3 col5=tex2Dlod( tx_col5,float4(txc,0,0)).rgb;
#ifdef realtime
col5=0;//realt
#endif

float sz=1,sshadow=1,tmin2;

#define rt_def 0
#define rt_shad 1
#define rt_occl 2
int ttrend[]={rt_def,rt_shad,rt_occl, rt_def,rt_shad,rt_occl,0,0};


#ifdef occltest
for(int q5=0;q5<3;q5+=1)//occl test, no ref
#else
for(int q5=0;q5<6;q5+=1)//normal
#endif
{
float tmin=1e16;
float hit=0;
int render_type=ttrend[q5];

if(render_type==rt_shad) tmin=tmin2;

int4 txc2=int4(0,0,0,0);//sorba
int4 txc2b=int4(0,0,0,0);//linear
int4 seltxc=int4(0,0,0,0);

int j2=0,end=0;
while(end==0 )//!!!!!!!!!!!!!!!!!!!!!!!!!!!
{
int3 bbox_data=tex2Dfetch( tx_bboxmap,txc2).rgb;
float3 bbox1=tex2Dfetch( tx_bbox1,txc2).rgb;
float3 bbox2=tex2Dfetch( tx_bbox2,txc2).rgb;


//is_tri==0 &&
if(chk_bbox(eye,ray,bbox1,bbox2,tmin)==0)//bbox skip 4 fps
{
txc2.xy=bbox_data.xy;//skip coords, !!recursion like!! thekey!!!!
if(txc2.y>txc2y_limit || (txc2.y==txc2y_limit && txc2.x>=txc2x_limit)) {end=1;}// *txsizeyi binarytree
}
else
{
int is_tri=(int)bbox_data.z;//4.27!!!!

if(is_tri!=0)
{
float3 norm2=tex2Dfetch( tx_norm2,txc2).rgb;//norm2
int4 txc22=txc2;
txc22.x<<=2;
float3 v1=tex2Dfetch( tx_vert1,txc22).rgb;


float t1=(dot(norm2,v1-eye))/dot(norm2,ray);
if(t1>0.01)//self 0.001
if(t1<tmin)
{
float3 obj=eye+ray*t1;
float3 edge1=(tex2Dfetch( tx_edge1,txc22).rgb);//edge


if(dot(edge1,obj-v1)>=0.0)//tri
{
txc22.x++;
float3 v2=tex2Dfetch( tx_vert1,txc22).rgb;
float3 edge2=(tex2Dfetch( tx_edge1,txc22).rgb);
if(dot(edge2,obj-v2)>=0.0)
{
txc22.x++;
float3 v3=tex2Dfetch( tx_vert1,txc22).rgb;
float3 edge3=(tex2Dfetch( tx_edge1,txc22).rgb);
if(dot(edge3,obj-v3)>=0.0)
{
tmin=t1;
if(render_type==rt_def)//reflect&occl-hoz nem kell semmi
{
sobj=obj;
snorm=norm2;
scol=float3(0.7,0.0,0.0);//0.7 0 0
seltxc=txc22;
}

hit=1;
} //tri
}//dot
}//dot
}//t1<min
}//is_tri

txc2.x+=1;
if(txc2.x>=512)
{
txc2.x=0;
txc2.y+=1;
}//txc2
if(txc2.y>txc2y_limit || (txc2.y==txc2y_limit && txc2.x>=txc2x_limit)) {end=1;}

}//chkbox else
}//end




// float3 light=(float3(1280,150,1240));//120,270,240));
// float3 light=(float3(1300-50,110,1200-50));//120,270,240));
// float3 light=(float3(1300+70,110,1200+30));//120,270,240));
float3 light=(float3(1300+200,310,1200+60));


if(render_type==rt_shad)
{
if(hit==1) sshadow=0.0;
else sshadow=1.0;

gcol.rgb+=col3*sshadow;

ray=ray2;
eye=eye2;
}
if(render_type==rt_occl)
{
#ifdef occltest
if(hit==0) gcol.rgb+=float3(0.7,0.7,0.7);//occl test
#else
if(hit==0) gcol.rgb+=col4;//afloat diffuse 4skyoccl
#endif
ray=ray2;
eye=eye2;
}
if(render_type==rt_def)
{
if(hit==0)
{
if(ray.y>0.0)
{

scol.x=0.4+pow(1.0-ray.y,3.0)*2.6;//16
scol.y=0.8+pow(1.0-ray.y,3.0)*2.6;
scol.z=0.9+pow(1.0-ray.y,3.0)*2.6;
scol*=0.6;//sotetit

gcol.rgb+=scol*sz;

gcol*=0.25;// !!!!!!!!!!!!!!!!!!!!!!! exit
color0.rgb=col5+saturate(gcol);
return;
// break;
}
else
{
float t1=(0.0-eye.y)/ray.y;
sobj=eye+ray*t1;
#if 0
float u=fmod(sobj.x/150.0,1.0); //kockas padlo
float v=fmod(sobj.z/150.0,1.0);
if((u>0.5&&v>0.5 )||(u<0.5&&v<0.5 )) scol=float3(1,1,1);
else scol=float3(0,0,1);
#else
scol=tex2Dlod(tx_decal1,float4(sobj.xz/100.0,0,0)).xyz;//wood
#endif
snorm=float3(0,1,0);
}
}
else//hit==1 tang->norm interp
{
#if 0
seltxc.x-=2;//!!
float3 v1=tex2Dfetch( tx_vert1,seltxc).rgb;
float3 tang1=(tex2Dfetch( tx_tang1,seltxc).rgb);seltxc.x++;
float3 tang2=(tex2Dfetch( tx_tang1,seltxc).rgb);seltxc.x++;
float3 tang3=(tex2Dfetch( tx_tang1,seltxc).rgb);seltxc.x++;
float3 tang4=(tex2Dfetch( tx_tang1,seltxc).rgb);

float3 w1=sobj - v1;
snorm.x=tang4.x + dot(tang1,w1);//tangent space ->normal interp
snorm.y=tang4.y + dot(tang2,w1);
snorm.z=tang4.z + dot(tang3,w1);
snorm=normalize(snorm);
#endif
}
if(dot(snorm,eye-sobj)<0.0) snorm=-snorm;//normal az eye fele!

//diffuse light
float3 w2=sobj-light;
float3 w1=normalize(w2);
float t1=length(w2);
col3=scol*fabs(dot(snorm,w1))*float3(1.0,0.9,0.7)*sz*35e4/(t1*t1);//diffuse
//ambient
col4=scol*float3(0.7,0.9,1.0)*1.4*sz;//ambient 0.3

//reflect ray
float3 w4=normalize(sobj-eye);
sref=w4-snorm*dot(w4,snorm)*2;
sref+=tex2Dlod(tx_rnd1,float4(txc+time3.xy,0,0)).xyz*0.01;//rnd 4ref
// sref+=(scol-float3(0.5,0.5,0.5))*0.3;//color rnd 4 ref
sref=normalize(sref);

//specular
float3 w3=-w1;//normalize(light-sobj)
col3+=saturate(float3(1.0,0.9,0.7)*pow(dot(sref,w3),12.0))*7.0;//specular

#ifdef occltest
col3=0;//occl test
#endif
ray=sref;
eye=sobj;

//setup shadow ray
if(render_type==rt_def)//next shadow
{
ray2=ray;//forender utan, store
eye2=eye;

ray=normalize(light-sobj);//to light
tmin2=length(light-sobj);

ray+=tex2Dlod(tx_rnd1,float4(txc+time3.xy,0,0)).xyz*0.01;//rnd 4shad
ray=normalize(ray);

sz*=0.6;//0.3
}

// gcol.rgb+=(col3+col4);//ref no shad & occl
}//shadow

//setup occl ray
if(render_type==rt_shad)//next occl
{
eye=eye2;
#if 1
ray=tex2Dlod(tx_rnd1,float4(txc+time3.xy,0,0)).xyz;//rnd4occl
#else
ray=tex2Dlod(tx_rnd1,float4(txc+time3.xy,0,0)).xyz*0.8;//rnd4occl 0.8
ray+=snorm;
#endif
ray=normalize(ray);
if(dot(ray,snorm)<0.0) ray=-ray;
}
}//q5

gcol*=0.25;// !!!!!!!!!!!!!!!!!!!!!!! exit
color0.rgb=col5+saturate(gcol);


}


 

 


 

 

 

 

 

 

 

 

 

 

 

 

 

komment

süti beállítások módosítása