⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zrendv10.c

📁 [Game.Programming].Academic - Graphics Gems (6 books source code)
💻 C
📖 第 1 页 / 共 3 页
字号:
bxy0  = db_by_dx * fdx;for (x=xmin; x <= xmax; x++) {  rgboffset[x] = ((int32) rxy0) + (((int32) gxy0) << 8) +     (((int32) bxy0) << 16) + specterm;  rxy0 += dr_by_dx;  gxy0 += dg_by_dx;  bxy0 += db_by_dx;}rx0yminf = sh_float_to_fix (*((fixpoint *)&rx0ymin));gx0yminf = sh_float_to_fix (*((fixpoint *)&gx0ymin));bx0yminf = sh_float_to_fix (*((fixpoint *)&bx0ymin));zx0yminf = shzfloat_to_fix (*((fixpoint *)&zx0ymin));dr_by_dy_f = sh_float_to_fix (*((fixpoint *)&dr_by_dy));dg_by_dy_f = sh_float_to_fix (*((fixpoint *)&dg_by_dy));db_by_dy_f = sh_float_to_fix (*((fixpoint *)&db_by_dy));dz_by_dy_f = shzfloat_to_fix (*((fixpoint *)&dz_by_dy));dz_by_dx_f = shzfloat_to_fix (*((fixpoint *)&dz_by_dx));fdx = dz_by_dx*fx0;dz_by_dx_x0f = shzfloat_to_fix (*((fixpoint *)&fdx));for (y=ymin; y <= ymax; y++) {  col = (fp_floor_pos(rx0yminf) + (fp_floor_pos(gx0yminf)<<8) +	 (fp_floor_pos(bx0yminf)<< 16));  z = zx0yminf + xleft[y]*dz_by_dx_f - dz_by_dx_x0f;  for (x = xleft[y]; x <= xright[y]; x++) {    if (zb[y][x] < z) {      zb[y][x] = z;      fb[y][x] = col + rgboffset[x];    }    z += dz_by_dx_f;  }  rx0yminf += dr_by_dy_f;  gx0yminf += dg_by_dy_f;  bx0yminf += db_by_dy_f;  zx0yminf += dz_by_dy_f;}}/* rasterize_sorted_triangle */void  sort_and_rasterize_triangle (ColorTriangleP tp){int16       minyv = 0, midyv = 1, maxyv = 2, tmp = 0;int16       minxv = 0, midxv = 1, maxxv = 2;if ((tp->vertices[minyv]).vertex[Y] > (tp->vertices[midyv]).vertex[Y])   TSWAP (minyv, midyv, tmp); if ((tp->vertices[minyv]).vertex[Y] > (tp->vertices[maxyv]).vertex[Y])   TSWAP (minyv, maxyv, tmp);if ((tp->vertices[midyv]).vertex[Y] > (tp->vertices[maxyv]).vertex[Y])   TSWAP (midyv, maxyv, tmp);if ((tp->vertices[minxv]).vertex[X] > (tp->vertices[midxv]).vertex[X])   TSWAP (minxv, midxv, tmp); if ((tp->vertices[minxv]).vertex[X] > (tp->vertices[maxxv]).vertex[X])   TSWAP (minxv, maxxv, tmp);if ((tp->vertices[midxv]).vertex[X] > (tp->vertices[maxxv]).vertex[X])   TSWAP (midxv, maxxv, tmp);if ((ZFABS((tp->vertices[maxxv]).vertex[X] - (tp->vertices[minxv]).vertex[X])     > MAT3_EPSILON) &&    (ZFABS((tp->vertices[maxyv]).vertex[Y] - (tp->vertices[minyv]).vertex[Y])     > MAT3_EPSILON))  rasterize_sorted_triangle (minyv, midyv, maxyv, tp);}/* ------------------------------------------------------------------ *//* Clipping */int16 ClassifyVertex (MAT3fvec v, int16 plane, int16 val){switch (plane)  {  case X : switch (val)     {      case 1  : return ( (v[W] >= 0.0) ? (v[X] > v[W]) : (v[X] < v[W]) );           case -1 :	return ( (v[W] >= 0.0) ? (v[X] < -v[W]) : (v[X] > -v[W]) );  }  case Y : switch (val)     {      case 1  : return ( (v[W] >= 0.0) ? (v[Y] > v[W]) : (v[Y] < v[W]) );           case -1 :	return ( (v[W] >= 0.0) ? (v[Y] < -v[W]) : (v[Y] > -v[W]) );  }  case Z : switch (val)     {      case 1  : return ( (v[W] >= 0.0) ? (v[Z] > v[W]) : (v[Z] < v[W]) );           case 0 :	return ( (v[W] >= 0.0) ? (v[Z] < 0.0) : (v[Z] > 0.0) );    }}return (0);}int16 ClassifyTriangle (ColorTriangleP tp, int16 plane, int16 val){return (	  (ClassifyVertex (tp->vertices[0].vertex, plane, val) << 2) |  (ClassifyVertex (tp->vertices[1].vertex, plane, val) << 1) |  (ClassifyVertex (tp->vertices[2].vertex, plane, val)) 	  );}void CopyVertex (Color_VertexP source, Color_VertexP dest){int i;for (i=0; i < 4; i++)  dest->vertex[i] = source->vertex[i];dest->red = source->red;dest->green = source->green;dest->blue = source->blue;}void CopyNormal (ColorTriangleP source, ColorTriangleP dest){int i;for (i=0; i < 3; i++)  dest->normal[i] = source->normal[i];}void InterpolateVertex (Color_VertexP start, Color_VertexP end, float t, 		        Color_VertexP dest){int i;for (i=0; i < 4; i++)  dest->vertex[i] = start->vertex[i] + t*(end->vertex[i] - start->vertex[i]);dest->red = start->red + t * (end->red - start->red);dest->green = start->green + t * (end->green - start->green);dest->blue = start->blue + t * (end->blue - start->blue);}float ComputeT (MAT3fvec vstart, MAT3fvec vend, int16 plane, int16 val){switch (plane)  {  case X:  case Y: switch (val)    {    case 1 : return ((vstart[plane] - vstart[W]) /	             (vend[W] - vstart[W] - vend[plane] + vstart[plane]));    case -1: return ((vstart[plane] + vstart[W]) /	             (vstart[plane] - vend[plane] + vstart[W] - vend[W]));}   case Z: switch(val)     {    case 1 : return ((vstart[plane] - vstart[W]) /	             (vend[W] - vstart[W] - vend[plane] + vstart[plane]));    case 0 : return (vstart[plane] / (vstart[plane] - vend[plane] ));     }}return (0.0);}void Clip_Process_Rasterize_Triangle(ColorTriangleP tp){ColorTriangle cur_t[7];int16 k, plane, val, l, maxtriangles, curmax, clipcode, inout[7], j;float t1, t2;CopyNormal(tp, &(cur_t[0]));for (k=0; k < 3; k++)  CopyVertex ((Color_VertexP) (&(tp->vertices[k])), 	  (Color_VertexP) (&(cur_t[0].vertices[k])));curmax = maxtriangles = 1;inout[0] = 0;for (plane = 0; plane < 3; plane++) {  for (k = -1; k < 2; k += 2) {    val = k;    if ((plane == 2) && (val == -1)) val = 0;    for (l = 0; l < maxtriangles; l++) {      if (inout[l] == 0) {	          clipcode = ClassifyTriangle ((ColorTriangleP) (&cur_t[l]), plane, val);      switch (clipcode) {        case 0: break;	case 1: t1 = ComputeT (cur_t[l].vertices[2].vertex, 		           cur_t[l].vertices[0].vertex, plane, val);                t2 = ComputeT (cur_t[l].vertices[1].vertex, 		           cur_t[l].vertices[2].vertex, plane, val);		CopyVertex((Color_VertexP) (&(cur_t[l].vertices[0])),			(Color_VertexP) (&(cur_t[curmax].vertices[0])));		InterpolateVertex((Color_VertexP) (&(cur_t[l].vertices[2])),			(Color_VertexP) (&(cur_t[l].vertices[0])), t1,			(Color_VertexP) (&(cur_t[curmax].vertices[2])));		InterpolateVertex((Color_VertexP) (&(cur_t[l].vertices[1])),			(Color_VertexP) (&(cur_t[l].vertices[2])), t2,			(Color_VertexP) (&(cur_t[curmax].vertices[1])));		CopyVertex((Color_VertexP) (&(cur_t[curmax].vertices[1])),			(Color_VertexP) (&(cur_t[l].vertices[2])));	        CopyNormal(&(cur_t[l]), &(cur_t[curmax]));		inout[curmax] = 0;		curmax++;		break;	case 2: t1 = ComputeT (cur_t[l].vertices[0].vertex, 		           cur_t[l].vertices[1].vertex, plane, val);                t2 = ComputeT (cur_t[l].vertices[1].vertex, 		           cur_t[l].vertices[2].vertex, plane, val);		CopyVertex((Color_VertexP) (&(cur_t[l].vertices[0])),			(Color_VertexP) (&(cur_t[curmax].vertices[0])));		InterpolateVertex((Color_VertexP) (&(cur_t[l].vertices[0])),			(Color_VertexP) (&(cur_t[l].vertices[1])), t1,			(Color_VertexP) (&(cur_t[curmax].vertices[1])));		InterpolateVertex((Color_VertexP) (&(cur_t[l].vertices[1])),			(Color_VertexP) (&(cur_t[l].vertices[2])), t2,			(Color_VertexP) (&(cur_t[curmax].vertices[2])));		CopyVertex((Color_VertexP) (&(cur_t[curmax].vertices[2])),			(Color_VertexP) (&(cur_t[l].vertices[1])));	        CopyNormal(&(cur_t[l]), &(cur_t[curmax]));		inout[curmax] = 0;		curmax++;		break;	case 3: t1 = ComputeT (cur_t[l].vertices[0].vertex, 		           cur_t[l].vertices[1].vertex, plane, val);                t2 = ComputeT (cur_t[l].vertices[2].vertex, 		           cur_t[l].vertices[0].vertex, plane, val);		InterpolateVertex((Color_VertexP) (&(cur_t[l].vertices[0])),			(Color_VertexP) (&(cur_t[l].vertices[1])), t1,			(Color_VertexP) (&(cur_t[l].vertices[1])));		InterpolateVertex((Color_VertexP) (&(cur_t[l].vertices[2])),			(Color_VertexP) (&(cur_t[l].vertices[0])), t2,			(Color_VertexP) (&(cur_t[l].vertices[2])));		break;	case 4: t1 = ComputeT (cur_t[l].vertices[2].vertex, 		           cur_t[l].vertices[0].vertex, plane, val);                t2 = ComputeT (cur_t[l].vertices[0].vertex, 		           cur_t[l].vertices[1].vertex, plane, val);		CopyVertex((Color_VertexP) (&(cur_t[l].vertices[1])),			(Color_VertexP) (&(cur_t[curmax].vertices[0])));		InterpolateVertex((Color_VertexP) (&(cur_t[l].vertices[2])),			(Color_VertexP) (&(cur_t[l].vertices[0])), t1,			(Color_VertexP) (&(cur_t[curmax].vertices[1])));		InterpolateVertex((Color_VertexP) (&(cur_t[l].vertices[0])),			(Color_VertexP) (&(cur_t[l].vertices[1])), t2,			(Color_VertexP) (&(cur_t[curmax].vertices[2])));		CopyVertex((Color_VertexP) (&(cur_t[curmax].vertices[1])),			(Color_VertexP) (&(cur_t[l].vertices[0])));	        CopyNormal(&(cur_t[l]), &(cur_t[curmax]));		inout[curmax] = 0;		curmax++;		break;	case 5: t1 = ComputeT (cur_t[l].vertices[0].vertex, 		           cur_t[l].vertices[1].vertex, plane, val);                t2 = ComputeT (cur_t[l].vertices[1].vertex, 		           cur_t[l].vertices[2].vertex, plane, val);		InterpolateVertex((Color_VertexP) (&(cur_t[l].vertices[0])),			(Color_VertexP) (&(cur_t[l].vertices[1])), t1,			(Color_VertexP) (&(cur_t[l].vertices[0])));		InterpolateVertex((Color_VertexP) (&(cur_t[l].vertices[1])),			(Color_VertexP) (&(cur_t[l].vertices[2])), t2,			(Color_VertexP) (&(cur_t[l].vertices[2])));		break;	case 6: t1 = ComputeT (cur_t[l].vertices[2].vertex, 		           cur_t[l].vertices[0].vertex, plane, val);                t2 = ComputeT (cur_t[l].vertices[1].vertex, 		           cur_t[l].vertices[2].vertex, plane, val);		InterpolateVertex((Color_VertexP) (&(cur_t[l].vertices[2])),			(Color_VertexP) (&(cur_t[l].vertices[0])), t1,			(Color_VertexP) (&(cur_t[l].vertices[0])));		InterpolateVertex((Color_VertexP) (&(cur_t[l].vertices[1])),			(Color_VertexP) (&(cur_t[l].vertices[2])), t2,			(Color_VertexP) (&(cur_t[l].vertices[1])));		break;	case 7: inout[l] = 1;	        break;	}}}   maxtriangles = curmax;}}for (k=0; k < maxtriangles; k++) if (inout[k] == 0) {   for (j=0; j < 3; j++) {      cur_t[k].vertices[j].vertex[X] = (cur_t[k].vertices[j].vertex[X] +              	    cur_t[k].vertices[j].vertex[W]) * (WW - 1)/2.0;     cur_t[k].vertices[j].vertex[Y] = (cur_t[k].vertices[j].vertex[Y] +              	    cur_t[k].vertices[j].vertex[W]) * (WH - 1)/2.0;   }   for (j=0; j < 3; j++) {      cur_t[k].vertices[j].vertex[X] = cur_t[k].vertices[j].vertex[X] /			      cur_t[k].vertices[j].vertex[W];      cur_t[k].vertices[j].vertex[Y] = cur_t[k].vertices[j].vertex[Y] /			      cur_t[k].vertices[j].vertex[W];      cur_t[k].vertices[j].vertex[Z] = cur_t[k].vertices[j].vertex[Z] /		      cur_t[k].vertices[j].vertex[W];      cur_t[k].vertices[j].vertex[X] = ZFABS(cur_t[k].vertices[j].vertex[X]);      cur_t[k].vertices[j].vertex[Y] = ZFABS(cur_t[k].vertices[j].vertex[Y]);      cur_t[k].vertices[j].vertex[Z] = ZFABS(cur_t[k].vertices[j].vertex[Z]);   }   sort_and_rasterize_triangle ((ColorTriangleP) (&(cur_t[k]))); }}/* ------------------------------------------------------------------ *//* Viewing Transformation */#define mathpointmult(x,y,w,vec,mat)  {\  (x) = (vec)[0] * (mat)[0][0] + \                    (vec)[1] * (mat)[0][1] + \                    (vec)[2] * (mat)[0][2] + \                    (mat)[0][3]; \                                 \  (y) = (vec)[0] * (mat)[1][0] + \                    (vec)[1] * (mat)[1][1] + \                    (vec)[2] * (mat)[1][2] + \                    (mat)[1][3]; \                                 \  (w) = (vec)[0] * (mat)[3][0] + \                    (vec)[1] * (mat)[3][1] + \                    (vec)[2] * (mat)[3][2] + \                    (mat)[3][3]; \} /* mathpointmult */void EvaluateViewTransformationMatrix (MAT3vec view_ref_point, 				       MAT3vec view_plane_normal,				       MAT3vec view_up_vector,				       double kay, double ell,				       double Hither, double Yon,				       MAT3fmat vo_inverse,  				       MAT3mat transform, float *b){MAT3vec     uvec, vvec;MAT3mat     trans, rot, vo_matrix, transinv, rotinv, vo_inverse_d, vm_matrix;double      det, zmin, tmp1;int16       i, j;MAT3_NORMALIZE_VEC(view_plane_normal, tmp1);MAT3_CROSS_PRODUCT(uvec, view_up_vector, view_plane_normal);MAT3_NORMALIZE_VEC(uvec, tmp1);MAT3_CROSS_PRODUCT(vvec,  view_plane_normal, uvec);MAT3identity (trans);for (i=0; i < 3; i++)  trans[i][3] = -view_ref_point[i];MAT3identity (rot);for(i=0; i < 3; i++)  rot[0][i] = uvec[i];for(i=0; i < 3; i++)  rot[1][i] = vvec[i];for(i=0; i < 3; i++)  rot[2][i] = view_plane_normal[i];MAT3mult (vo_matrix,  rot, trans); MAT3identity (transinv);for (i=0; i < 3; i++)  transinv[i][3] = view_ref_point[i];MAT3identity (rotinv);det = rot[0][0] * Det2D(rot[1][1],rot[1][2],rot[2][1],rot[2][2])  -rot[0][1] * Det2D(rot[1][0],rot[1][2],rot[2][0],rot[2][2])    +rot[0][2] * Det2D(rot[1][0],rot[1][1],rot[2][0],rot[2][1]);rotinv[0][0] = Det2D(rot[1][1],rot[1][2],rot[2][1],rot[2][2])/det;rotinv[1][0] = -Det2D(rot[1][0],rot[1][2],rot[2][0],rot[2][2])/det;rotinv[2][0] = Det2D(rot[1][0],rot[1][1],rot[2][0],rot[2][1])/det;rotinv[0][1] = -Det2D(rot[0][1],rot[0][2],rot[2][1],rot[2][2])/det;rotinv[1][1] = Det2D(rot[0][0],rot[0][2],rot[2][0],rot[2][2])/det;rotinv[2][1] = -Det2D(rot[0][0],rot[0][1],rot[2][0],rot[2][1])/det;rotinv[0][2] = Det2D(rot[0][1],rot[0][2],rot[1][1],rot[1][2])/det;rotinv[1][2] = -Det2D(rot[0][0],rot[0][2],rot[1][0],rot[1][2])/det;rotinv[2][2] = Det2D(rot[0][0],rot[0][1],rot[1][0],rot[1][1])/det;MAT3mult(vo_inverse_d,transinv,rotinv);for (i=0; i < 4; i++)  for (j=0; j < 4; j++)    vo_inverse[i][j] = vo_inverse_d[i][j];MAT3identity  (vm_matrix);vm_matrix[0][0] = vm_matrix[1][1] =   ell/ (kay * Yon);vm_matrix[2][2] = 1/Yon;vm_matrix[2][3] = -ell/Yon;zmin = - Hither/Yon;MAT3mult (transform, vm_matrix, vo_matrix);for (j=0; j < 4; j++)  transform[3][j] = -transform[2][j];*b = -zmin/(1.0 + zmin);for (j=0; j < 3; j++)  transform[2][j] *= (1.0+*b);transform[2][3] = (1.0 + *b) * transform[2][3] + *b; }/* ------------------------------------------------------------------ *//* Trivial Accept and Reject */void MAT3fmult_hvec(MAT3fvec result_vec, float a, float b, float c,		    MAT3fmat  mat){result_vec[0] = a*mat[0][0] + b*mat[0][1] + c*mat[0][2] + mat[0][3];result_vec[1] = a*mat[1][0] + b*mat[1][1] + c*mat[1][2] + mat[1][3];result_vec[2] = a*mat[2][0] + b*mat[2][1] + c*mat[2][2] + mat[2][3];result_vec[3] = 1.0;}void set_bounds(MAT3fvec curpt, float xval, float yval, float zval,		MAT3fmat vo_inverse){MAT3fmult_hvec(curpt,xval,yval,zval,vo_inverse);out_xmin = out_xmax = curpt[X];out_ymin = out_ymax = curpt[Y];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -