Panda3D
td_light.cxx
1 #include "zgl.h"
2 #include <math.h>
3 
4 static inline PN_stdfloat clampf(PN_stdfloat a,PN_stdfloat min,PN_stdfloat max)
5 {
6  if (a<min) return min;
7  else if (a>max) return max;
8  else return a;
9 }
10 
11 /* non optimized lighting model */
12 void gl_shade_vertex(GLContext *c,GLVertex *v)
13 {
14  PN_stdfloat R,G,B,A;
15  GLMaterial *m;
16  GLLight *l;
17  V3 n,s,d;
18  PN_stdfloat dist,tmp,att,dot,dot_spot,dot_spec;
19  int twoside = c->light_model_two_side;
20 
21  m=&c->materials[0];
22 
23  n.v[0]=v->normal.v[0];
24  n.v[1]=v->normal.v[1];
25  n.v[2]=v->normal.v[2];
26 
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);
31 
32  for(l=c->first_light;l!=NULL;l=l->next) {
33  PN_stdfloat lR,lB,lG;
34 
35  /* ambient */
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];
39 
40  if (l->position.v[3] == 0) {
41  /* light at infinity */
42  d.v[0]=l->position.v[0];
43  d.v[1]=l->position.v[1];
44  d.v[2]=l->position.v[2];
45  att=1;
46  } else {
47  /* distance attenuation */
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]);
52  if (dist>1E-3) {
53  tmp=1/dist;
54  d.v[0]*=tmp;
55  d.v[1]*=tmp;
56  d.v[2]*=tmp;
57  }
58  att=1.0f/(l->attenuation[0]+dist*(l->attenuation[1]+
59  dist*l->attenuation[2]));
60  }
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;
63  if (dot>0) {
64  /* diffuse light */
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];
68 
69  /* spot light */
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) {
76  /* no contribution */
77  continue;
78  } else {
79  /* TODO: optimize */
80  if (l->spot_exponent > 0) {
81  att=att*pow(dot_spot,l->spot_exponent);
82  }
83  }
84  }
85 
86  /* specular light */
87 
88  if (c->local_light_model) {
89  V3 vcoord;
90  vcoord.v[0]=v->ec.v[0];
91  vcoord.v[1]=v->ec.v[1];
92  vcoord.v[2]=v->ec.v[2];
93  gl_V3_Norm(&vcoord);
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];
97  } else {
98  s.v[0]=d.v[0];
99  s.v[1]=d.v[1];
100  s.v[2]=d.v[2]+1.0f;
101  }
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;
104  if (dot_spec>0) {
105  GLSpecBuf *specbuf;
106  int idx;
107  tmp=sqrt(s.v[0]*s.v[0]+s.v[1]*s.v[1]+s.v[2]*s.v[2]);
108  if (tmp > 1E-3) {
109  dot_spec=dot_spec / tmp;
110  }
111 
112  /* TODO: optimize */
113  /* testing specular buffer code */
114  /* dot_spec= pow(dot_spec,m->shininess);*/
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];
122  }
123  }
124 
125  R+=att * lR;
126  G+=att * lG;
127  B+=att * lB;
128  }
129 
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);
134 }
135 
Definition: zmath.h:22
Definition: zgl.h:141
Definition: zgl.h:48
Definition: zgl.h:106
Definition: zgl.h:64
Definition: zgl.h:41