4 static inline PN_stdfloat clampf(PN_stdfloat a,PN_stdfloat min,PN_stdfloat max)
7 else if (a>max)
return max;
18 PN_stdfloat dist,tmp,att,dot,dot_spot,dot_spec;
19 int twoside = c->light_model_two_side;
23 n.v[0]=v->normal.v[0];
24 n.v[1]=v->normal.v[1];
25 n.v[2]=v->normal.v[2];
27 R=m->emission.v[0]+m->ambient.v[0]*c->ambient_light_model.v[0];
28 G=m->emission.v[1]+m->ambient.v[1]*c->ambient_light_model.v[1];
29 B=m->emission.v[2]+m->ambient.v[2]*c->ambient_light_model.v[2];
30 A=clampf(m->diffuse.v[3],0,1);
32 for(l=c->first_light;l!=NULL;l=l->next) {
36 lR=l->ambient.v[0] * m->ambient.v[0];
37 lG=l->ambient.v[1] * m->ambient.v[1];
38 lB=l->ambient.v[2] * m->ambient.v[2];
40 if (l->position.v[3] == 0) {
42 d.v[0]=l->position.v[0];
43 d.v[1]=l->position.v[1];
44 d.v[2]=l->position.v[2];
48 d.v[0]=l->position.v[0]-v->ec.v[0];
49 d.v[1]=l->position.v[1]-v->ec.v[1];
50 d.v[2]=l->position.v[2]-v->ec.v[2];
51 dist = sqrtf(d.v[0]*d.v[0]+d.v[1]*d.v[1]+d.v[2]*d.v[2]);
58 att=1.0f/(l->attenuation[0]+dist*(l->attenuation[1]+
59 dist*l->attenuation[2]));
61 dot=d.v[0]*n.v[0]+d.v[1]*n.v[1]+d.v[2]*n.v[2];
62 if (twoside && dot < 0) dot = -dot;
65 lR+=dot * l->diffuse.v[0] * m->diffuse.v[0];
66 lG+=dot * l->diffuse.v[1] * m->diffuse.v[1];
67 lB+=dot * l->diffuse.v[2] * m->diffuse.v[2];
70 if (l->spot_cutoff != 180) {
71 dot_spot=-(d.v[0]*l->norm_spot_direction.v[0]+
72 d.v[1]*l->norm_spot_direction.v[1]+
73 d.v[2]*l->norm_spot_direction.v[2]);
74 if (twoside && dot_spot < 0) dot_spot = -dot_spot;
75 if (dot_spot < l->cos_spot_cutoff) {
80 if (l->spot_exponent > 0) {
81 att=att*pow(dot_spot,l->spot_exponent);
88 if (c->local_light_model) {
90 vcoord.v[0]=v->ec.v[0];
91 vcoord.v[1]=v->ec.v[1];
92 vcoord.v[2]=v->ec.v[2];
94 s.v[0]=d.v[0]-vcoord.v[0];
95 s.v[1]=d.v[1]-vcoord.v[0];
96 s.v[2]=d.v[2]-vcoord.v[0];
102 dot_spec=n.v[0]*s.v[0]+n.v[1]*s.v[1]+n.v[2]*s.v[2];
103 if (twoside && dot_spec < 0) dot_spec = -dot_spec;
107 tmp=sqrt(s.v[0]*s.v[0]+s.v[1]*s.v[1]+s.v[2]*s.v[2]);
109 dot_spec=dot_spec / tmp;
115 specbuf = specbuf_get_buffer(c, m->shininess_i, m->shininess);
116 idx = (int)(dot_spec*SPECULAR_BUFFER_SIZE);
117 if (idx > SPECULAR_BUFFER_SIZE) idx = SPECULAR_BUFFER_SIZE;
118 dot_spec = specbuf->buf[idx];
119 lR+=dot_spec * l->specular.v[0] * m->specular.v[0];
120 lG+=dot_spec * l->specular.v[1] * m->specular.v[1];
121 lB+=dot_spec * l->specular.v[2] * m->specular.v[2];
130 v->color.v[0]=clampf(R*v->color.v[0],0,1);
131 v->color.v[1]=clampf(G*v->color.v[1],0,1);
132 v->color.v[2]=clampf(B*v->color.v[2],0,1);
133 v->color.v[3]=clampf(A*v->color.v[3],0,1);