mat_t *mat_scale(float s, mat_t *a){ a->x = vec_scale(s,a->x); a->y = vec_scale(s,a->y); a->z = vec_scale(s,a->z); a->w = vec_scale(s,a->w); return a; }
void auto_orient (void) { int face; for (face = 0; face < 6; face++) { float u[3], a_minus_b[3], a_minus_c[3]; vec_sub (a_minus_b, points[tristrips[face][0]], points[tristrips[face][1]]); vec_scale (a_minus_b, a_minus_b, texcoords[0][1] - texcoords[2][1]); vec_sub (a_minus_c, points[tristrips[face][0]], points[tristrips[face][2]]); vec_scale (a_minus_c, a_minus_c, texcoords[0][1] - texcoords[1][1]); vec_sub (u, a_minus_b, a_minus_c); /* Not really sure if this is right. */ if (((texcoords[0][0] - texcoords[1][0]) * (texcoords[0][1] - texcoords[2][1]) - (texcoords[0][0] - texcoords[2][0]) * (texcoords[0][1] - texcoords[1][1])) < 0.0f) vec_scale (u, u, -1.0f); vec_normalize (u, u); printf ("face %d: calculated [ %f %f %f ]\n", face, u[0], u[1], u[2]); printf (" previously [ %f %f %f ]\n", face_orient[face][0], face_orient[face][1], face_orient[face][2]); } }
void SMat<CoeffRing>::column2by2(size_t c1, size_t c2, elem a1, elem a2, elem b1, elem b2) /* column(c1) <- a1 * column(c1) + a2 * column(c2), column(c2) <- b1 * column(c1) + b2 * column(c2) */ { // Make first column: v1 = a1*c1+a2*c2 sparsevec *v1 = vec_copy(columns_[c1]); sparsevec *v2 = vec_copy(columns_[c2]); vec_scale(v1, a1); vec_scale(v2, a2); vec_add_to(v1, v2); // Second column: w1 = b1*c1 + b2*c2 sparsevec *w1 = columns_[c1]; sparsevec *w2 = columns_[c2]; vec_scale(w1, b1); vec_scale(w2, b2); vec_add_to(w1, w2); // Set the matrices: columns_[c1] = v1; columns_[c2] = w1; }
/* Computes ambiant occlusion */ static void ga_shade_ao(vec_t *color, const ga_scene_t *s, const ga_material_t *mat, const vec_t *pos, const vec_t *norm, float importance){ int sample = mat->ao_sample; float rlen,fact; vec_t ao_color = vec_new(0,0,0,1); vec_t dsample,d1,d2; vec_fperp(norm,&d1,&d2); while(sample--){ dsample = vec_new(0,0,0,1); vec_ffadd(&dsample,random()*RAND_NORM,norm); vec_ffadd(&dsample,(2.0f*random()*RAND_NORM) -1.0f,&d1); vec_ffadd(&dsample,(2.0f*random()*RAND_NORM) -1.0f,&d2); vec_fnorm(&dsample); rlen = ga_ray_length(s,*pos,dsample); if (rlen <= mat->ao_min_dist){ ao_color = vec_add(ao_color,mat->ao_min_color); }else if (rlen >= mat->ao_max_dist){ ao_color = vec_add(ao_color,mat->ao_max_color); }else{ fact = (rlen - mat->ao_min_dist) / (mat->ao_max_dist - mat->ao_min_dist) ; ao_color = vec_add(ao_color, vec_add(vec_scale(fact,mat->ao_max_color), vec_scale(1.0f-fact,mat->ao_min_color))); } } *color = vec_add(*color, vec_scale(1.0f/mat->ao_sample*mat->ao_factor,ao_color)); }
BOOL crLottiUpdateFly (ENTITY* e) { VECTOR v, w; vec_set(&w, e->crAccel); vec_scale(&w, time_step); vec_add(e->crSpeed, &w); vec_set(&v, e->crSpeed); vec_scale(&v, time_step); vec_add(e->x, &v); VECTOR feet; feet.x = feet.y = 0; feet.z = 0.5 * e->min_z; vec_rotate(feet, e->pan); vec_add(feet, e->x); ent_animate(e, "drown", 5 * total_ticks + e->crInit, ANM_CYCLE); effect(credits_fire, 2, feet, e->crSpeed); // explode near screen border VECTOR v; vec_set(&v, e->x); vec_to_screen(&v, camera); return (v.x < e->crPercent * screen_size.x || v.x > (1 - e->crPercent) * screen_size.x || v.y < e->crPercent * screen_size.y || v.y > (1 - e->crPercent) * screen_size.y); }
void compute_ray(RAY* ray, VECTOR3D* view_point, VIEWPORT* viewport, PIXEL* pixel, VEC_BASIS* camera_frame, double distance) { float u, v; VECTOR3D v1, v2, v3, v4, dir; // 1. calculate u and v coordinates of the pixels on the image plane: u = (double)(viewport->xvmin) + (double)(pixel->i) + 0.5 ; v = (double)(viewport->yvmin) + (double)(pixel->j) + 0.5 ; // 2. calculate ray direction vec_scale(-distance, &v1, &camera_frame->n); vec_scale(u, &v2, &camera_frame->u); vec_scale(v, &v3, &camera_frame->v); ray->origin.x = view_point->x; ray->origin.y = view_point->y; ray->origin.z = view_point->z; vec_add(&v4, &v1, &v2); vec_add(&dir, &v4, &v3); normalize_vector(&dir); ray->direction.x = dir.x; ray->direction.y = dir.y; ray->direction.z = dir.z; }
void integrate(particle_t *src, particle_t *dst, vec3f acceleration, float dt){ // euler-cromer method // v' = v + a*dt -- v, v' and a are vectors, dt is a scalar. dst->velocity = vec_add(src->velocity, vec_scale(acceleration, dt)); // x' = x + v'*dt -- x', x, v' are vectors, dt is a scalar. dst->position = vec_add(src->position, vec_scale(dst->velocity, dt)); }
Vector getReflectedRay(Vector surface_norm, Vector light_ray) { float cosTheta1 = vec_dot(surface_norm, vec_scale(light_ray,-1)); Vector reflect_ray = vec_plus(light_ray, vec_scale(surface_norm, 2*cosTheta1)); return reflect_ray; }
// Calculate normal vector in the point of intersection: void intersection_normal(SPHERE *sphere, SPHERE_INTERSECTION* intersection, RAY* ray) { double lambda, scale; VECTOR3D v1, v2, point, normal; lambda = intersection->lambda_in; vec_scale(lambda, &v1, &ray->direction); vec_add(&point, &v1, &ray->origin); intersection->point.x = point.x; intersection->point.y = point.y; intersection->point.z = point.z; vec_sub(&v2, &point, &sphere->center); scale = 1.0 / sphere->radius; vec_scale(scale, &normal, &v2); normalize_vector(&normal); intersection->normal.x = normal.x; intersection->normal.y = normal.y; intersection->normal.z = normal.z; }
box_t box_bounds(box_t b) { vec_t corner = vec_add(vec_abs(vec_scale(b.axis0, b.size.x)), vec_abs(vec_scale(b.axis1, b.size.y))); box_t r; r.pos = b.pos; r.size = corner; r.axis0 = vec_new(corner.x, 0); r.axis1 = vec_new(0, corner.y); return r; }
//reflect a vector from a surface plane] void vec_reflect(vec_t *n, vec_t *w, vec_t *v) { vec_t *u = (vec_t *)malloc(sizeof(vec_t)); vec_unit(w, u); vec_scale(-1, u, u); vec_scale((2*vec_dot(u, n)), n, v); vec_diff(u, v, v); free(u); }
/* Computes the phong hilight */ static void ga_shade_phong(vec_t *color, const ga_material_t *mat,const vec_t *lcolor, const vec_t *ldir, const vec_t *dir, const vec_t *norm, float factor){ vec_t r; float fact; float power; r = vec_sub( vec_scale(2.0f,vec_scale(vec_dot(*norm,*ldir),*norm)), *ldir); if((fact = vec_dot(r,vec_neg(*dir))) > 0.0f){ power = powf(fact,mat->spec_power)*mat->spec_factor; *color = vec_add(*color, vec_scale( power*factor, vec_mult(mat->spec_color,*lcolor))); } }
rgb lighting(scene s, ray r, hit_test h) { rgb result; if (h.miss) return s.bg; vec hit_position = ray_position(r, h.dist); if (shadow(hit_position, s.light, s.spheres)) { result = rgb_modulate(h.surf, s.amb); } else { double dot = vec_dot(h.surf_norm, s.light.direction); double d = double_max(0, dot); rgb diffuse_light = rgb_scale(d, s.light.color); rgb lsum = rgb_add(s.amb, diffuse_light); result = rgb_modulate(h.surf, lsum); } /**** === implement specular reflection here === ****/ if (rgb_nonzero(h.shine)) { rgb ss; vec N = h.surf_norm; vec L = s.light.direction; rgb S = h.shine; vec R = vec_sub( vec_scale(2* vec_dot(N,L),N),L); vec V = vec_neg(r.direction); if (vec_dot(N,L)>0){ ss = rgb_scale( pow( double_max( vec_dot(R,V),0), 6), S); //rgb_print(k); } else ss = rgb_expr(0,0,0); return rgb_add(result,ss); } return result; }
void item_fade() { var vTicks = total_ticks; VECTOR vecDist; var fadeScale = my->scale_x; vec_set(&vecDist, &my->x); vec_sub(&vecDist, &player->x); while (my->scale_x > 0) { wait(1); //scale down item my->scale_x -= 0.1 * time_step * fadeScale; my->scale_y = my->scale_x; my->scale_z = my->scale_x; my->pan += (total_ticks - vTicks) * 10 * time_step; //move item towards player vec_set(&my->x, &vecDist); vec_scale(&my->x, my->scale_x / fadeScale); vec_add(&my->x, &player->x); } itemCounter--; ptr_remove(me); }
double plane_t::hits(vec_t *base, vec_t* dir){ double ndotd; double t; double ndotb; ndotq = vec_dot(&normal, &point); ndotd = vec_dot(dir, &normal); /* ndotd = 0 -> ray is parallel to the plane */ if (ndotd == 0) return(-1); ndotb = vec_dot(&normal, base); t = (ndotq - ndotb) / ndotd; if (t <= 0) return(-1); vec_scale(t, dir, &last_hitpt); vec_sum(&last_hitpt, base, &last_hitpt); if ((last_hitpt.z > 0.01) && (strcmp(obj_type, "projector"))) return(-1); return(t); }
void lightsource_fake_phong (float *vertex, float *norm, const float *light_pos, const float *light_up, const float *eye_pos, matrix_t *invcamera, vertex_attrs *vertex_info, unsigned int idx) { float norm_light[3]; float eye_to_vertex[3], tmp[3], reflection[3]; float light_to_vertex[3]; float eye_dot_norm; vec_sub (light_to_vertex, light_pos, vertex); vec_normalize (norm_light, light_to_vertex); vec_sub (eye_to_vertex, eye_pos, vertex); vec_normalize (eye_to_vertex, eye_to_vertex); eye_dot_norm = vec_dot (eye_to_vertex, norm); vec_scale (tmp, norm, 2.0 * eye_dot_norm); vec_sub (reflection, tmp, eye_to_vertex); if (eye_dot_norm < 0) vertex_info[idx].fakephong.transformed_z = -0.01; else fakephong_texcoords (&vertex_info[idx].fakephong.texc[0], &vertex_info[idx].fakephong.texc[1], reflection, norm_light, light_up, &vertex_info[idx].fakephong.transformed_z); }
vec *ray_position(ray *r, double t) { vec *w = vec_scale(t, r->direction); vec *result = vec_add(r->origin, w); vec_free(w); return result; }
double plane_t::hits( vec_t *base, /* ray base */ vec_t *dir) /* unit direction vector */ { double ndotd; double t; double ndotb; ndotq = vec_dot(&normal, &point); ndotd = vec_dot(dir, &normal); /* ndotd = 0 -> ray is parallel to the plane */ if (ndotd == 0) return(-1); ndotb = vec_dot(&normal, base); t = (ndotq - ndotb) / ndotd; if (t <= 0) return(-1); vec_scale(t, dir, &hitloc); vec_sum(&hitloc, base, &hitloc); if (hitloc.z > 0.001) return(-1); return(t); }
/* Construct a unit vector v2 in direction of input vector v1 */ void vec_unit( vec_t *v1, /* Input vector */ vec_t *v2) /* output unit vec */ { double mult = 1 / vec_len(v1); vec_scale(mult, v1, v2); }
void SMat<CoeffRing>::vec_column_op(sparsevec *&v, const elem &a, sparsevec *w) const // v := v + a*w { sparsevec *w1 = vec_copy(w); vec_scale(w1, a); vec_add_to(v, w1); }
vec_t vec_normalize(vec_t a){ if (vec_zero(a)){ return a; }else{ return vec_scale(a,1.0/vec_len(a)); } }
//Brief: QR decomposition void qrdec(mat *A, mat* R){ double proj = 0; double norm = 0; // Allocates pointers of type *vec //vec *coli = (vec*)malloc(sizeof(vec)); //vec *colj = (vec*)malloc(sizeof(vec)); vec* coli = vec_new(A->row); vec* colj = vec_new(A->row); for(int i=0; i<A->col; i++){ mat_get_col(coli, A, i); norm = vec_norm(coli); mat_set(R, i, i, norm); vec_scale(coli, 1.0/norm); mat_set_col(A, coli, i); for(int j=i+1; j<A->col; j++){ mat_get_col(colj, A, j); proj = vec_dot(coli,colj); vec_add(colj,coli,-proj); mat_set_col(A, colj, j); mat_set(R, i, j, proj); } } // Free pointers vec_free(coli); vec_free(colj); }
/* Function compute_means. * This function recomputes the means based on the current clustering * of the points. * * Inputs: * n : number of input descriptors * dim : dimension of each input descriptor * k : number of means * v : array of pointers to dim-dimensional descriptors * clustering : current assignment of descriptors to means (should * range between 0 and k-1) * * Output: * means_out : array of output means. You need to fill this * array. The means should be concatenated into one * long array of length k*dim. */ double compute_means(int n, int dim, int k, unsigned char **v, unsigned int *clustering, double *means_out) { int i; double max_change = 0.0; int *counts = (int *) malloc(sizeof(int) * k); for (i = 0; i < k * dim; i++) means_out[i] = 0.0; for (i = 0; i < k; i++) counts[i] = 0; for (i = 0; i < n; i++) { unsigned int cluster = clustering[i]; vec_accum(dim, means_out + cluster * dim, v[i]); counts[cluster]++; } /* Normalize new means */ for (i = 0; i < k; i++) { if (counts[i] == 0) { continue; } vec_scale(dim, means_out + i * dim, 1.0 / counts[i]); } free(counts); return max_change; }
vec_t vec_norm(vec_t a){ float len = vec_len(a); if(len == 0.0f){ return vec_new(1.0f,0.0f,0.0f,0.0f); }else{ return vec_scale(1.0f/len,a); } }
/* diffuse shading */ static void ga_shade_diffuse(vec_t *color, const ga_material_t *mat,const vec_t *lcolor, const vec_t *ldir, const vec_t *norm, float factor){ float fact = vec_dot(*norm,*ldir); if(fact > 0.0f){ *color = vec_add(*color, vec_scale(fact*factor*mat->diff_factor ,vec_mult(mat->diff_color,*lcolor))); } }
//project a vector onto a plane void vec_project(vec_t *n, vec_t *v, vec_t *p) { vec_t *r = (vec_t *)malloc(sizeof(vec_t)); vec_scale(vec_dot(n, v), n, r); vec_diff(r, v, p); free(r); }
Vector getRefractedRay(float initial_refraction, Spheres *sph, Vector surface_norm, Vector light_ray) { float refraction_index = sph->refraction_index; float n = initial_refraction/refraction_index; Vector refract_ray; //n = .9; //light_ray.x = 0.707107; //light_ray.y = -0.707107; //light_ray.z = 0; //surface_norm.x = 0; //surface_norm.y = 1; //surface_norm.z = 0; float cosTheta1 = vec_dot(surface_norm, vec_scale(light_ray,-1)); float cosTheta2 = sqrt(1.0f-pow(n,2)*(1-(cosTheta1*cosTheta1))); Vector a = vec_scale(light_ray, n); Vector b = vec_scale(surface_norm, n*cosTheta1 - cosTheta2); if(cosTheta1 > 0.0f) { refract_ray = vec_plus(vec_scale(light_ray,n), vec_scale(surface_norm, n*cosTheta1-cosTheta2)); } else { refract_ray = vec_minus(vec_scale(light_ray,n), vec_scale(surface_norm, n*cosTheta1-cosTheta2)); } return refract_ray; }
static gameComponentT* createPhysicsComponent(void) { gameComponentT* component = newPhysicsComponent(1.0f * Kilogram); physicsComponentDataT* phys = component->data; vec2 pos; vec2 vel; switch (rand() % 4) { case 0: { // Spawn above screen. pos = (vec2) { -0.5f + 1.0f * rand()/(float)RAND_MAX, 0.6f }; vel = (vec2) { -1.0f + 2.0f * rand()/(float)RAND_MAX, -0.2f - 1.0f * rand()/(float)RAND_MAX }; break; }; case 1: { // Spawn to the right of screen. pos = (vec2) { 1.0f, -0.5f + 1.0f * rand()/(float)RAND_MAX }; vel = (vec2) { -0.2f - 1.0f * rand() / (float)RAND_MAX, -1.0f + 2.0f * rand() / (float)RAND_MAX }; break; }; case 2: { // Spawn below screen. pos = (vec2) { -0.5f + 1.0f * rand()/(float)RAND_MAX, -0.6f }; vel = (vec2) { -1.0f + 2.0f * rand()/(float)RAND_MAX, 0.2f + 1.0f * rand()/(float)RAND_MAX }; break; }; case 3: { // Spawn to the left of screen. pos = (vec2) { -1.0f, -0.5f + 1.0f * rand()/(float)RAND_MAX }; vel = (vec2) { 0.2f + 1.0f * rand() / (float)RAND_MAX, -1.0f + 2.0f * rand() / (float)RAND_MAX }; break; }; } vec_scale(&pos, 6.0f, &pos); //bodySetOrientation(((physicsComponentDataT*)phys)->body, 17 * 3.141592f / 180.0f); //pos.x = -1.0f; //pos.y = 0.0f; //vel.x = -1.0f; //vel.y = 0.0f; bodySetPosition(((physicsComponentDataT*)phys)->body, pos); //vec_scale(&vel, 0.1f, &vel); bodySetVelocity(((physicsComponentDataT*)phys)->body, vel); return (component); }
vec_t vec_homog(vec_t a){ if(a.w == 0.0f){ return a; }else{ a = vec_scale(1.0/a.w,a); a.w = 1.0; return a; } }
vec_t vec_vec(vec_t a){ if (a.w == 0.0f){ return a; }else{ a = vec_scale(1.0f/a.w,a); a.w = 0.0f; return a; } }