00001 #include "zgl.h"
00002 #include <math.h>
00003
00004 static inline PN_stdfloat clampf(PN_stdfloat a,PN_stdfloat min,PN_stdfloat max)
00005 {
00006 if (a<min) return min;
00007 else if (a>max) return max;
00008 else return a;
00009 }
00010
00011
00012 void gl_shade_vertex(GLContext *c,GLVertex *v)
00013 {
00014 PN_stdfloat R,G,B,A;
00015 GLMaterial *m;
00016 GLLight *l;
00017 V3 n,s,d;
00018 PN_stdfloat dist,tmp,att,dot,dot_spot,dot_spec;
00019 int twoside = c->light_model_two_side;
00020
00021 m=&c->materials[0];
00022
00023 n.v[0]=v->normal.v[0];
00024 n.v[1]=v->normal.v[1];
00025 n.v[2]=v->normal.v[2];
00026
00027 R=m->emission.v[0]+m->ambient.v[0]*c->ambient_light_model.v[0];
00028 G=m->emission.v[1]+m->ambient.v[1]*c->ambient_light_model.v[1];
00029 B=m->emission.v[2]+m->ambient.v[2]*c->ambient_light_model.v[2];
00030 A=clampf(m->diffuse.v[3],0,1);
00031
00032 for(l=c->first_light;l!=NULL;l=l->next) {
00033 PN_stdfloat lR,lB,lG;
00034
00035
00036 lR=l->ambient.v[0] * m->ambient.v[0];
00037 lG=l->ambient.v[1] * m->ambient.v[1];
00038 lB=l->ambient.v[2] * m->ambient.v[2];
00039
00040 if (l->position.v[3] == 0) {
00041
00042 d.v[0]=l->position.v[0];
00043 d.v[1]=l->position.v[1];
00044 d.v[2]=l->position.v[2];
00045 att=1;
00046 } else {
00047
00048 d.v[0]=l->position.v[0]-v->ec.v[0];
00049 d.v[1]=l->position.v[1]-v->ec.v[1];
00050 d.v[2]=l->position.v[2]-v->ec.v[2];
00051 dist = sqrtf(d.v[0]*d.v[0]+d.v[1]*d.v[1]+d.v[2]*d.v[2]);
00052 if (dist>1E-3) {
00053 tmp=1/dist;
00054 d.v[0]*=tmp;
00055 d.v[1]*=tmp;
00056 d.v[2]*=tmp;
00057 }
00058 att=1.0f/(l->attenuation[0]+dist*(l->attenuation[1]+
00059 dist*l->attenuation[2]));
00060 }
00061 dot=d.v[0]*n.v[0]+d.v[1]*n.v[1]+d.v[2]*n.v[2];
00062 if (twoside && dot < 0) dot = -dot;
00063 if (dot>0) {
00064
00065 lR+=dot * l->diffuse.v[0] * m->diffuse.v[0];
00066 lG+=dot * l->diffuse.v[1] * m->diffuse.v[1];
00067 lB+=dot * l->diffuse.v[2] * m->diffuse.v[2];
00068
00069
00070 if (l->spot_cutoff != 180) {
00071 dot_spot=-(d.v[0]*l->norm_spot_direction.v[0]+
00072 d.v[1]*l->norm_spot_direction.v[1]+
00073 d.v[2]*l->norm_spot_direction.v[2]);
00074 if (twoside && dot_spot < 0) dot_spot = -dot_spot;
00075 if (dot_spot < l->cos_spot_cutoff) {
00076
00077 continue;
00078 } else {
00079
00080 if (l->spot_exponent > 0) {
00081 att=att*pow(dot_spot,l->spot_exponent);
00082 }
00083 }
00084 }
00085
00086
00087
00088 if (c->local_light_model) {
00089 V3 vcoord;
00090 vcoord.v[0]=v->ec.v[0];
00091 vcoord.v[1]=v->ec.v[1];
00092 vcoord.v[2]=v->ec.v[2];
00093 gl_V3_Norm(&vcoord);
00094 s.v[0]=d.v[0]-vcoord.v[0];
00095 s.v[1]=d.v[1]-vcoord.v[0];
00096 s.v[2]=d.v[2]-vcoord.v[0];
00097 } else {
00098 s.v[0]=d.v[0];
00099 s.v[1]=d.v[1];
00100 s.v[2]=d.v[2]+1.0f;
00101 }
00102 dot_spec=n.v[0]*s.v[0]+n.v[1]*s.v[1]+n.v[2]*s.v[2];
00103 if (twoside && dot_spec < 0) dot_spec = -dot_spec;
00104 if (dot_spec>0) {
00105 GLSpecBuf *specbuf;
00106 int idx;
00107 tmp=sqrt(s.v[0]*s.v[0]+s.v[1]*s.v[1]+s.v[2]*s.v[2]);
00108 if (tmp > 1E-3) {
00109 dot_spec=dot_spec / tmp;
00110 }
00111
00112
00113
00114
00115 specbuf = specbuf_get_buffer(c, m->shininess_i, m->shininess);
00116 idx = (int)(dot_spec*SPECULAR_BUFFER_SIZE);
00117 if (idx > SPECULAR_BUFFER_SIZE) idx = SPECULAR_BUFFER_SIZE;
00118 dot_spec = specbuf->buf[idx];
00119 lR+=dot_spec * l->specular.v[0] * m->specular.v[0];
00120 lG+=dot_spec * l->specular.v[1] * m->specular.v[1];
00121 lB+=dot_spec * l->specular.v[2] * m->specular.v[2];
00122 }
00123 }
00124
00125 R+=att * lR;
00126 G+=att * lG;
00127 B+=att * lB;
00128 }
00129
00130 v->color.v[0]=clampf(R,0,1);
00131 v->color.v[1]=clampf(G,0,1);
00132 v->color.v[2]=clampf(B,0,1);
00133 v->color.v[3]=A;
00134 }
00135