int IsVertexIntoSegment(TVertex * S1,TVertex * S2, TVertex * P) { TVertex Dir1; TVertex Dir2; float mag; if((P->x - S1->x == 0) && (P->y - S1->y == 0) && (P->z - S1->z == 0)) return 2; if((P->x - S2->x == 0) && (P->y - S2->y == 0) && (P->z - S2->z == 0)) return 3; Dir1.x = S2->x - S1->x; Dir1.y = S2->y - S1->y; Dir1.z = S2->z - S1->z; Dir2.x = P->x - S1->x; Dir2.y = P->y - S1->y; Dir2.z = P->z - S1->z; if(Dir2.x > Dir1.x || Dir2.y > Dir1.y || Dir2.z > Dir1.z) { //If P is contained, it cant be farer that S2 from S1 return 0; } v_normalize(Dir1,mag,Dir1); v_normalize(Dir2,mag,Dir2); if(Dir1.x - Dir2.x || Dir1.y - Dir2.y || Dir1.z - Dir2.z) { //diferent, no contained return 0; } return 1; }
static unsigned int calc_vol(struct _asset_file *f, uint16_t tri[3], idx_t *out) { unsigned int i; vec3_t s[3]; idx_t surf[3]; idx_t esurf[3]; vec3_t a, b, c, d; for(i = 0; i < 3; i++) { surf[i] = tri[i]; } for(i = 0; i < 3; i++) { s[i][0] = f->f_verts[tri[i]].v_vert[0]; s[i][1] = f->f_verts[tri[i]].v_vert[1]; s[i][2] = f->f_verts[tri[i]].v_vert[2]; } for(i = 0; i < 3; i++) { esurf[i] = tri[i] + f->f_hdr->h_verts; } /* don't cast shadows for triangles not facing light */ v_sub(a, s[1], s[0]); v_sub(b, s[2], s[0]); v_cross_product(c, a, b); v_normalize(c); for(i = 0; i < 3; i++) d[i] = f->f_lightpos[i]; v_normalize(d); if ( v_dot_product(c, d) < 0 ) return 0; #if SHADOW_CAPS emit_tri(&out, esurf[2], esurf[1], esurf[0]); emit_tri(&out, surf[0], surf[1], surf[2]); #endif for(i = 0; i < 3; i++) { unsigned int a, b; a = (i % 3); b = ((i + 1) % 3); emit_tri(&out, surf[a], esurf[a], surf[b]); emit_tri(&out, surf[b], esurf[a], esurf[b]); } #if SHADOW_CAPS return 8; #else return 6; #endif }
void CLightFocus::Update() { TVertex temp; float mag; temp.x=Dest.x-Orig.x;temp.y=Dest.y-Orig.y;temp.z=Dest.z-Orig.z; v_normalize(temp,mag,temp); position[0]=temp.x;position[1]=temp.y;position[2]=temp.z;position[3]=Type; }
static void translate_light_pos(renderer_t r, vec3_t light_pos) { vec3_t res; renderer_xlat_world_to_obj(r, res, light_pos); v_normalize(res); light_pos[0] = -res[0]; light_pos[1] = -res[1]; light_pos[2] = -res[2]; }
CLightFocus::CLightFocus(float * _ambient,float * _diffuse,float * _specular,TVertex * _Orig, TVertex * _Dest,unsigned int _Type) { ambient[0]=_ambient[0];ambient[1]=_ambient[1];ambient[2]=_ambient[2];ambient[3]=_ambient[3]; diffuse[0]=_diffuse[0];diffuse[1]=_diffuse[1];diffuse[2]=_diffuse[2];diffuse[3]=_diffuse[3]; specular[0]=_specular[0];specular[1]=_specular[1];specular[2]=_specular[2];specular[3]=_specular[3]; Orig.x=_Orig->x;Orig.y=_Orig->y;Orig.z=_Orig->z; Dest.x=_Dest->x;Dest.y=_Dest->y;Dest.z=_Dest->z; TVertex temp; float mag; temp.x=Dest.x-Orig.x;temp.y=Dest.y-Orig.y;temp.z=Dest.z-Orig.z; v_normalize(temp,mag,temp); position[0]=temp.x;position[1]=temp.y;position[2]=temp.z;position[3]=_Type; Type=_Type; ApplyCam=1; }
/** * Prepare a mesh for drawing. Compute mesh's final vertex positions * given a skeleton. Put the vertices in vertex arrays. */ static void PrepareMesh(const struct md5_mesh_part *mesh, const struct md5_joint_t *skeleton) { unsigned int i, j, k; /* Setup vertex indices */ for (k = 0, i = 0; i < mesh->num_tris; ++i) { for (j = 0; j < 3; ++j, ++k) vertexIndices[k] = mesh->triangles[i].index[j]; } /* Setup vertices */ for (i = 0; i < mesh->num_verts; ++i) { vec3_t vert = { 0.0f, 0.0f, 0.0f }; vec3_t norm = { 0.0f, 0.0f, 0.0f }; // vec3_t tan = { 0.0f, 0.0f, 0.0f }; /* Calculate final vertex to draw with weights */ for (j = 0; j < mesh->vertices[i].count; ++j) { const struct md5_weight_t *weight = &mesh->weights[mesh->vertices[i].start + j]; const struct md5_joint_t *joint = &skeleton[weight->joint]; vec3_t wv; /* Calculate transformed vertex for this weight */ Quat_rotatePoint(joint->orient, weight->pos, wv); /* The sum of all weight->bias should be 1.0 */ vert[0] += (joint->pos[0] + wv[0]) * weight->bias; vert[1] += (joint->pos[1] + wv[1]) * weight->bias; vert[2] += (joint->pos[2] + wv[2]) * weight->bias; Quat_rotatePoint(joint->orient, weight->normal, wv); v_scale(wv, weight->bias); v_add(norm, norm, wv); // Quat_rotatePoint(joint->orient, weight->tangent, wv); // v_scale(wv, weight->bias); // v_add(tan, tan, wv); } v_normalize(norm); // v_normalize(tan); vertexArray[i][0] = vert[0]; vertexArray[i][1] = vert[1]; vertexArray[i][2] = vert[2]; normalArray[i][0] = norm[0]; normalArray[i][1] = norm[1]; normalArray[i][2] = norm[2]; #if 0 tangentArray[i][0] = tan[0]; tangentArray[i][1] = tan[1]; tangentArray[i][2] = tan[2]; #endif texArray[i][0] = mesh->vertices[i].st[0]; texArray[i][1] = 1.0f - mesh->vertices[i].st[1]; } }
//TODO: don't pass scene.... void illuminate(vector point, Object* s, vector normal, Scene scene, float initColor[], vector color){ // printf("ic: %f %f %f\n", initColor[0],initColor[1],initColor[2]); //construct ray from the point in space //where this collision occured float diffuse=0; float specular=0; for(int i=0; i<scene.numLights; i++){ //TODO: divide by type of light switch(scene.lights[i]->type){ case DIRECTIONAL: { DirLight* dl = static_cast<DirLight*>(scene.lights[i]->li); vector dir; v_copy(dir, dl->dir); v_normalize(dir); v_scale(dir, dir, -1); vector re, e; v_subtract(e, scene.cam.pos, point); v_normalize(e); v_scale(re, normal, v_dot(e, normal)*2); v_subtract(re, re, e); v_normalize(re); float dotprod = v_dot(dir, normal); if(dotprod>0){ Ray rayToLight; v_copy(rayToLight.o, point); v_copy(rayToLight.d, dir); float scale = shadowRay(rayToLight); diffuse+=dotprod*dl->i; float x=-1; if(s->n!=0){ float dotprodSpec = v_dot(re,dir); if(dotprodSpec<0) dotprodSpec=0; x = pow(dotprodSpec, s->n)*dl->i; } if(x>0) specular+=x; diffuse*=scale; specular*=scale; } } break; case SPOT: { SpotLight* sl = static_cast<SpotLight*>(scene.lights[i]->li); vector lightPoint; v_copy(lightPoint, sl->pos); vector lightVector; vector lightToPoint; v_subtract(lightVector,lightPoint,point); v_normalize(lightVector); vector re, e; v_subtract(e, scene.cam.pos, point); v_normalize(e); v_scale(re, normal, v_dot(e, normal)*2); v_subtract(re, re, e); v_normalize(re); float dotprod = v_dot(lightVector,normal); //if positive, we are facing the light if(dotprod>0){ Ray rayToLight; v_copy(rayToLight.o, point); v_copy(rayToLight.d, lightVector); float scale = shadowRay(rayToLight); vector D; vector dist; v_subtract(dist,lightPoint,point); v_copy(D,sl->dir); v_scale(D,D,-1); float intensity = pow(v_dot(D,lightVector), sl->falloff); if(intensity<0) intensity=0; diffuse+=dotprod*intensity*sl->i; float x=-1; if(s->n!=0){ float dotprodSpec = v_dot(re,lightVector); if(dotprodSpec<0) dotprodSpec=0; x = pow(dotprodSpec, s->n)*intensity*sl->i; } if(x>0){ specular+=x; } diffuse*=scale; specular*=scale; } } break; case POINT: { PointLight* pl = static_cast<PointLight*>(scene.lights[i]->li); vector lightPoint; v_copy(lightPoint, pl->pos); vector lightVector; v_subtract(lightVector,lightPoint,point); v_normalize(lightVector); lightVector[3]=0; point[3]=1; vector re, e; v_subtract(e, scene.cam.pos, point); v_normalize(e); v_scale(re, normal, v_dot(e, normal)*2); v_subtract(re, re, e); v_normalize(re); float dotprod = v_dot(lightVector,normal); //if positive, we are facing the light if(dotprod>0){ Ray rayToLight; v_copy(rayToLight.o, point); if(pl->size==0){ v_copy(rayToLight.d, lightVector); } else { vector lightTemp; v_subtract(lightTemp,lightPoint,point); // printf("Pre Jitter:\n"); // v_print(lightTemp); // printf("size: %f\n",pl->size); float sx=randFloat(-pl->size,pl->size); float sy=randFloat(-pl->size,pl->size); float sz=randFloat(-pl->size,pl->size); vector jitter; jitter[0]=sx; jitter[1]=sy; jitter[2]=sz; // v_print(jitter); v_add(lightTemp, lightTemp, jitter); // printf("Post Jitter:\n"); // v_print(lightTemp); v_normalize(lightTemp); lightTemp[3]=0; v_copy(rayToLight.d, lightTemp); } float scale = shadowRay(rayToLight); diffuse+=dotprod*pl->i; float x=-1; if(s->n!=0){ float dotprodSpec = v_dot(re,lightVector); if(dotprodSpec<0) dotprodSpec=0; x = pow(dotprodSpec, s->n)*pl->i; } if(x>0){ specular+=x; } diffuse*=scale; specular*=scale; } } break; } } color[0]=s->kd*(scene.amb + diffuse)*initColor[0]; color[0]+=s->ks*specular; color[1]=s->kd*(scene.amb + diffuse)*initColor[1]; color[1]+=s->ks*specular; color[2]=s->kd*(scene.amb + diffuse)*initColor[2]; color[2]+=s->ks*specular; //printf("c %f\n",color[0]); }