1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
| #version 130
uniform vec2 resolution;
uniform vec3 pos1;
uniform vec3 pos2;
uniform vec3 pos3;
uniform vec3 pos4;
vec4 fpar00[16];
#define size 0.4
struct s_ray
{
vec3 origin;
vec3 direction;
};
s_ray ray;
float pot(vec3 at, vec3 ballCenter)
{
vec3 tmp=at-ballCenter;
float d=dot(tmp,tmp);
return d;
}
float field(vec3 p)
{
float potentiel=0.0;
potentiel += size/pot(p,pos1);
potentiel += size/pot(p,pos2);
potentiel += size/pot(p,pos3);
potentiel += size/pot(p,pos4);
return 1.0-potentiel;
}
bool intPlane( in vec4 plane, in s_ray ray, in float tm, out float t )
{
t = -(dot(plane.xyz,ray.origin)+plane.w)/dot(plane.xyz,ray.direction);
return (t > 0.0) && (t < tm);
}
bool trace(s_ray ray, in float maxDistance, in out float t )
{
float minDistance = 0.3;
float distance = 999.0;
while (t < maxDistance)
{
vec3 position = ray.origin + t * ray.direction;
distance = field(position);
if (distance < minDistance && distance>0.0)
{
break;
}
t += 0.02;
}
return (distance < minDistance && distance>0.0);
}
void Render_plans(s_ray ray, out vec3 position,out vec4 ob,out vec4 co, in float tm, out float t )
{
if (intPlane(fpar00[0],ray, tm,t)) //left
{ ob = fpar00[0]; co = vec4(1,1,1,1); tm = t; }
if (intPlane(fpar00[1],ray,tm,t)) //right
{ ob = fpar00[1]; co = vec4(0,1,0,1); tm = t; }
if (intPlane(fpar00[2],ray, tm,t)) //top
{ ob = fpar00[2]; co = vec4(0,0,1,1); tm = t; }
if (intPlane(fpar00[3],ray, tm,t)) //bottom
{ ob = fpar00[3]; co = vec4(1,0,1,1); tm = t; }
if (intPlane(fpar00[4],ray, tm,t)) //back
{ ob = fpar00[4]; co = vec4(1,1,0,1); tm = t; }
}
void Render(s_ray ray, out vec3 position,out vec4 ob,out vec4 co)
{
float tm=11.0;
float t=0.1;
Render_plans(ray, position,ob,co, tm,t);
t=0.1;
if (trace(ray,tm,t))
{
#define epsilon 0.2
vec3 camera_direction=vec3(0.1,0.1,1.0);
vec3 materialDiffuse=vec3(0.2,0.4,0.2);
float materialExponenet=256.0;
//
//normal
//
vec3 hitpoint = ray.origin + t * ray.direction;
vec3 normal;
normal.x=field(hitpoint+vec3(epsilon,0.0,0.0));
normal.y=field(hitpoint+vec3(0.0,epsilon,0.0));
normal.z=field(hitpoint+vec3(0.0,0.0,epsilon));
normal-=field(hitpoint);
normal=normalize(normal);
//
//light
//
vec3 lightsPosition[5];
int numlight=5;
lightsPosition[0]=vec3(2.0,-2.0,-2.0);
lightsPosition[1]=vec3(2.0,-2.0,2.0);
lightsPosition[2]=vec3(-2.0,-2.0,2.0);
lightsPosition[3]=vec3(-2.0,-2.0,-2.0);
lightsPosition[4]=vec3(2.0,2.0,-2.0);
vec3 color=vec3(0.0,0.0,0.0);
vec3 lightsDiffuse[5];
lightsDiffuse[0]=vec3(0.0,1.0,1.0);
lightsDiffuse[1]=vec3(1.0,0.0,1.0);
lightsDiffuse[2]=vec3(1.0,0.0,0.0);
lightsDiffuse[3]=vec3(0.0,0.0,1.0);
lightsDiffuse[4]=vec3(1.0,0.5,1.0);
for(int i=0;i<numlight;i++)
{
vec3 light_dir=normalize(lightsPosition[i]-hitpoint);
//diffuse
float diffuse=max(dot(light_dir,normal),0.0);
color+=(lightsDiffuse[i]*diffuse).xyz*materialDiffuse.xyz;
//white specular
float specular=max(dot(reflect(-light_dir,normal),camera_direction),0.0);
color+=pow(specular,materialExponenet);
}
ray.origin=hitpoint;
ray.direction=normal;
float tm=11.0;
float t=0.1;
vec4 coco;
Render_plans(ray, position,ob,coco, tm,t);
co=vec4(color*0.95,1.0)+vec4(coco.rgb*0.25,1.0);
}
}
void main()
{
vec2 p = -1.0 + 2.0 * gl_FragCoord.xy/ resolution.xy;
ray.origin = vec3(0.0,0.0,-3.0); // ray origin
ray.direction = normalize (vec3(p.x,p.y,-2.0)-ray.origin); // ray direction
//plan (x,y,z,d)
fpar00[0]=vec4(-1.0,0.0,0.0,-5.0); //left
fpar00[1]=vec4(1.0,0.0,0.0,-5.0); //right
fpar00[2]=vec4(0.0,-1.0,0.0,5.0); //top
fpar00[3]=vec4(0.0,1.0,0.0,5.0); //bottom
fpar00[4]=vec4(0.0,0.0,-1.0,4.6); //front
//aabox (x,y,z,lenght)
fpar00[5]=vec4(-8.5,-8.5,-8.5,16.0);
vec4 ob;
vec4 oc=vec4(0.0,0.0,0.0,1.0);
vec3 position=ray.origin;
Render(ray,position,ob,oc);
gl_FragColor=oc;
} |