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

📄 zrendv10.c

📁 [Game.Programming].Academic - Graphics Gems (6 books source code)
💻 C
📖 第 1 页 / 共 3 页
字号:
out_zmin = out_zmax = curpt[Z];}void update_bounds(MAT3fvec curpt, float xval, float yval, float zval,	           MAT3fmat vo_inverse){ MAT3fmult_hvec(curpt,xval,yval,zval,vo_inverse); out_xmin = min(curpt[X],out_xmin);out_xmax = max(curpt[X],out_xmax);out_ymin = min(curpt[Y],out_ymin);out_ymax = max(curpt[Y],out_ymax);out_zmin = min(curpt[Z],out_zmin);out_zmax = max(curpt[Z],out_zmax);}/* ------------------------------------------------------------------ *//* Reading Input */void readNFFFile (char *filename, MAT3fvec from, float *frustumf, 		  float *frustumr, MAT3vec vrp, MAT3vec vpn, MAT3vec vupv,		  double *ellp, double *Hitherp, double *Yonp, double *kayp,   	       Lights lights, BackGroundColor *backgndcolor, int16 *datasizep,               MtlProp *activeProp, int16 *currentlight, SpecLightP spec_lightp){FILE *infile;char string[80];int16 i, count, state = WAITING, currentTriangle=-1;double angle, delta[3], lx, ly, lz;*currentlight = 0;infile = fopen (filename,"r");while (fgets (&string[0],80,infile) != NULL)   {  if (state == WAITING)     {    switch (string[0])      {      case 'v':        state = VIEWING; /* Read View Specification */        break;      case 'b':  /* Background Color */        sscanf (&string[1]," %lf %lf %lf\n", &lx, &ly, &lz);        backgndcolor->red   = MAX_INTENSITY * lx;        backgndcolor->green = MAX_INTENSITY * ly;        backgndcolor->blue  = MAX_INTENSITY * lz;        break;      case 'l': /* Light Source */        sscanf (&string[1], " %f %f %f %f %f %f",		&lights[*currentlight].location[0], 		&lights[*currentlight].location[1],		&lights[*currentlight].location[2],		&lights[*currentlight].red,		&lights[*currentlight].green,		&lights[*currentlight].blue);	(*currentlight)++;	break;      case 's': /* PHONG Light Source */	sscanf (&string[1], " %f %f %f %f %f %f %f %d",		&spec_lightp->location[0],		&spec_lightp->location[1],		&spec_lightp->location[2],		&spec_lightp->red,		&spec_lightp->green,		&spec_lightp->blue,		&spec_lightp->ks,		&spec_lightp->spec_exp);	spec_lightp->location[3] = 1.0;	break;	      case 'f':  /* Lighting Constants */	sscanf (&string[1], " %f %f %f %f %f %f %f",		&(*activeProp).red,		&(*activeProp).green,		&(*activeProp).blue, 		&(*activeProp).diffuseK,		&(*activeProp).ambientK,		&(*activeProp).c1,		&(*activeProp).c2);	break;      case 'p': /* Triangle to Follow */	state = TRIDATA;	count = 0;	currentTriangle++;	break;      } /* switch on first char of line */  } /* if in waiting state */  else if (state == VIEWING)     {    switch(string[0])      {      case 'f':   /* View point location */	sscanf (&string[4], " %f %f %f\n", &from[0], &from[1], &from[2]);	from[W] = 1.0;	break;	      case 'a':	if (string[1] == 't') /* Look at */          sscanf(&string[2], " %lf %lf %lf\n", &vrp[0], &vrp[1], &vrp[2]);	else if (string[1] == 'n')     /* Angle */          sscanf(&string[5], "%lf\n", &angle);	break;      case 'u': /* Up Vector */	sscanf (&string[2], " %lf %lf %lf\n", &vupv[0], &vupv[1], &vupv[2]);	break;      case 'h':  /* Front or Hither Clipping Plane */	sscanf (&string[6], " %lf\n", Hitherp);	break;	      case 'y': /* Back or Yon Clipping Plane */	sscanf (&string[3], " %lf\n", Yonp);	break;	      case 'r':  /* resolution is currently ignored */	for (i=0; i < 3; i++)	  delta[i] = from[i] - vrp[i];	*ellp = sqrt(delta[0]*delta[0]+delta[1]* delta[1]+delta[2]*delta[2]);	for (i=0; i < 3; i++)	  vpn[i] = (from[i] - vrp[i])/ (*ellp);	*kayp = (*ellp) * tan(angle*M_PI/360.0);	*frustumf = (*kayp) * (*Hitherp)/(*ellp);	*frustumr = (*kayp) * (*Yonp)/(*ellp); 	state = WAITING;	break;      } /* switch first char in line */  } /* else if in viewing state */  else if (state == TRIDATA)    {    sscanf (string,"%f%f%f%f%f%f\n", 	    &(localSet[currentTriangle].vertices[count].vertex[0]), 	    &(localSet[currentTriangle].vertices[count].vertex[1]),	    &(localSet[currentTriangle].vertices[count].vertex[2]), 	    &(localSet[currentTriangle].vertices[count].normal[0]),	    &(localSet[currentTriangle].vertices[count].normal[1]),	    &(localSet[currentTriangle].vertices[count].normal[2]));    if (count == 2)      state = WAITING;    else      count++;  } /* else if just read in triangle data */} /* while not eof */fclose(infile);*datasizep = currentTriangle + 1;} /* readNFFFile *//* ------------------------------------------------------------------ *//* Backface Culling, Trivial Accept and Reject, Lighting,    Viewing Transformation and Clip Check *//* *  Processes a triangle. It also orients the triangle edges so that *  y(vertex 0) <= y(vertex 1) <= y(vertex 2). */void ProcessTriangle (OrigTriangleP tp, MAT3fvec from,			MAT3fmat transform, float b,			float Iar, float Iag, float Iab,			float c1, float c2, int16 num_lights, Lights lights){ColorTriangle cur_t;int16           j,k;MAT3fvec   temp1;float    dist, tmp3;if (BFCULL)  if ((from[X]*tp->normal[X]+from[Y]*tp->normal[Y]+from[Z]*tp->normal[Z])      < tp->v0dotn)    return;if (TRIVIAL_REJECT) {  if ((tp->vertices[0].vertex[X] < out_xmin) &&      (tp->vertices[1].vertex[X] < out_xmin) &&      (tp->vertices[2].vertex[X] < out_xmin)) {    return;	        }  if ((tp->vertices[0].vertex[X] > out_xmax) &&      (tp->vertices[1].vertex[X] > out_xmax) &&      (tp->vertices[2].vertex[X] > out_xmax)) {    return;	        }  if ((tp->vertices[0].vertex[Y] < out_ymin) &&      (tp->vertices[1].vertex[Y] < out_ymin) &&      (tp->vertices[2].vertex[Y] < out_ymin)) {    return;	        }  if ((tp->vertices[0].vertex[Y] > out_ymax) &&      (tp->vertices[1].vertex[Y] > out_ymax) &&      (tp->vertices[2].vertex[Y] > out_ymax)) {    return;	        }  if ((tp->vertices[0].vertex[Z] < out_zmin) &&      (tp->vertices[1].vertex[Z] < out_zmin) &&      (tp->vertices[2].vertex[Z] < out_zmin)) {    return;	        }  if ((tp->vertices[0].vertex[Z] > out_zmax) &&      (tp->vertices[1].vertex[Z] > out_zmax) &&      (tp->vertices[2].vertex[Z] > out_zmax)) {    return;	        }}for (j=0; j < 3; j++)  cur_t.normal[j] = tp->normal[j];for (j = 0; j < 3; j++) {  cur_t.vertices[j].red = Iar;  cur_t.vertices[j].green = Iag;   cur_t.vertices[j].blue = Iab;  for (k = 0; k < num_lights; k++) {    MAT3_SUB_VEC(temp1,lights[k].location,tp->vertices[j].vertex);        dist = sqrt (temp1[X]*temp1[X] + temp1[Y]*temp1[Y] + temp1[Z]*temp1[Z]);    dist = 1 / (dist * (1 + dist * (c1 + c2*dist)));    tmp3 = dist * ZFABS(MAT3_DOT_PRODUCT(temp1,tp->vertices[j].normal));    cur_t.vertices[j].red += lights[k].red * tmp3;    cur_t.vertices[j].green += lights[k].green * tmp3;    cur_t.vertices[j].blue += lights[k].blue * tmp3;  } /* for k */} /* for j */{float x[3], y[3], z[3], w[3];	for( j = 0; j < 3; j++) {  mathpointmult (x[j],y[j],w[j],tp->vertices[j].vertex,transform)    z[j] = b - w[j]*b; } /* for j */    if ((ZFABS(x[0]) <= ZFABS(w[0])) && (ZFABS(y[0]) <= ZFABS(w[0])) &&     (ZFABS(z[0])<=ZFABS(w[0])) && ((w[0] >= 0.0)?(z[0] >= 0.0):(z[0]<0.0)) &&     (ZFABS(x[1]) <= ZFABS(w[1])) && (ZFABS(y[1]) <= ZFABS(w[1])) &&     (ZFABS(z[1])<=ZFABS(w[1])) && ((w[1] >= 0.0)?(z[1] >= 0.0):(z[1]<0.0)) &&     (ZFABS(x[2]) <= ZFABS(w[2])) && (ZFABS(y[2]) <= ZFABS(w[2])) &&     (ZFABS(z[2])<=ZFABS(w[2])) && ((w[2] >= 0.0)?(z[2] >= 0.0):(z[2]<0.0))     ) {  for (j=0; j < 3; j++) {     x[j] = (x[j] + w[j]) * (WW - 1)/2.0;     y[j] = (y[j] + w[j]) * (WH - 1)/2.0;   }  for (j=0; j < 3; j++) {    cur_t.vertices[j].vertex[X] = x[j]/w[j];    cur_t.vertices[j].vertex[Y] = y[j]/w[j];    cur_t.vertices[j].vertex[Z] = z[j]/w[j];  }  for (j=0; j < 3; j++) {		    cur_t.vertices[j].vertex[X] = ZFABS(cur_t.vertices[j].vertex[X]);    cur_t.vertices[j].vertex[Y] = ZFABS(cur_t.vertices[j].vertex[Y]);    cur_t.vertices[j].vertex[Z] = ZFABS(cur_t.vertices[j].vertex[Z]);  }   sort_and_rasterize_triangle ((ColorTriangleP) (&cur_t));}else if (CLIP) {  for (j=0; j < 3; j++) {		    cur_t.vertices[j].vertex[X] = x[j];    cur_t.vertices[j].vertex[Y] = y[j];    cur_t.vertices[j].vertex[Z] = z[j];    cur_t.vertices[j].vertex[W] = w[j];  }  Clip_Process_Rasterize_Triangle((ColorTriangleP) (&cur_t));  }}} /* ProcessTriangle *//* *  The function processes a set of triangles one at a time. */void PipelineCompute (int16 count, MAT3fvec from,		       MAT3fmat transform, float b,		       float Iar, float Iag, float Iab, float c1, float c2,		       int16 num_lights, Lights lights){int16 i;for (i = 0; i < count; i ++)     ProcessTriangle (&localSet[i], from, transform, b,		       Iar, Iag, Iab, c1, c2, num_lights, lights);} /* Pipeline Compute *//* ------------------------------------------------------------------ */void main(int argc, char **argv){int16 i, j, tmp;int16 datasize, num_lights, scale_factor;MAT3vec vrp, vpn, vupv;MAT3fvec from, temp1, temp2, curpt, Lvector, Vvector, SpecLocNPC; double ell, Hither, Yon, kay;MAT3mat transform;float mag, b, frustumf, frustumr, ffplane, fbplane;MAT3fmat vo_inverse, ftransform;MtlProp activeProp;BackGroundColor backgndcolor;color fbackgndcolor;LightPoint lights[NUM_LIGHT_SOURCES];if (argc < 7) {  printf ("Usage: ZRndv10 <bf> <tr> <clip> <phong> <datafile> <outputfile>\n");  return;}if (strcmp(argv[1],"-bf"))   BFCULL = 0;else  BFCULL = 1;if (strcmp(argv[2],"-tr"))   TRIVIAL_REJECT = 0;else  TRIVIAL_REJECT = 1;  if (strcmp(argv[3],"-clip"))   CLIP = 0;else  CLIP = 1;if (strcmp(argv[4],"-phong"))   PHONG = 0;else  PHONG = 1;    readNFFFile (argv[5], from, &frustumf, &frustumr, vrp, vpn, vupv,	     &ell, &Hither, &Yon, &kay, lights, &backgndcolor,	     &datasize, &activeProp, &num_lights, &SpecSource);for (i=0; i < datasize; i++) {  MAT3_SUB_VEC(temp1,localSet[i].vertices[1].vertex,	       localSet[i].vertices[0].vertex);  MAT3_SUB_VEC(temp2,localSet[i].vertices[2].vertex,	       localSet[i].vertices[0].vertex);  MAT3_CROSS_PRODUCT(localSet[i].normal,temp1,temp2);  MAT3_NORMALIZE_VEC(localSet[i].normal,mag);    localSet[i].v0dotn=localSet[i].vertices[0].vertex[X]*localSet[i].normal[X]+     localSet[i].vertices[0].vertex[Y]*localSet[i].normal[Y]+       localSet[i].vertices[0].vertex[Z]*localSet[i].normal[Z]; }if (PHONG)  scale_factor = num_lights + 2;else  scale_factor = num_lights + 1;activeProp.red = activeProp.red * activeProp.ambientK * MAX_INTENSITY /   scale_factor;activeProp.green = activeProp.green * activeProp.ambientK * MAX_INTENSITY /  scale_factor;activeProp.blue = activeProp.blue * activeProp.ambientK * MAX_INTENSITY /   scale_factor;for (j=0; j < num_lights; j++) {  lights[j].red *= activeProp.diffuseK * MAX_INTENSITY / scale_factor;  lights[j].green *= activeProp.diffuseK * MAX_INTENSITY / scale_factor;  lights[j].blue *= activeProp.diffuseK * MAX_INTENSITY / scale_factor;}fbackgndcolor  = ((int32) backgndcolor.red) +   (((int32) backgndcolor.green) << 8) + (((int32) backgndcolor.blue) << 16);EvaluateViewTransformationMatrix (vrp, vpn, vupv, kay, ell, Hither, Yon,				    vo_inverse, transform, &b);    			    for (i=0; i < 4; i++)  for (j=0; j < 4; j++)    ftransform[i][j] = transform[i][j];if (PHONG) {  SpecSource.red *= SpecSource.ks * MAX_INTENSITY/scale_factor;  SpecSource.green *= SpecSource.ks * MAX_INTENSITY/scale_factor;  SpecSource.blue *= SpecSource.ks * MAX_INTENSITY/scale_factor;  mathpointmult (SpecLocNPC[X],SpecLocNPC[Y],SpecLocNPC[W],		 SpecSource.location,ftransform)  SpecLocNPC[Z] = b - SpecLocNPC[W]*b;    SpecLocNPC[X] = (SpecLocNPC[X] + SpecLocNPC[W]);  SpecLocNPC[Y] = (SpecLocNPC[Y] + SpecLocNPC[W]);  for (i=0; i < 3; i++)     SpecLocNPC[i] /= SpecLocNPC[W];  MAT3_SUB_VEC(Lvector,SpecSource.location,vrp);  MAT3_SUB_VEC(Vvector,from,vrp);  for(i=0; i < 3; i++)    Hvector[i] = Lvector[i] + Vvector[i];  MAT3_NORMALIZE_VEC(Hvector,mag);}ffplane = ell - Hither; fbplane = ell - Yon;set_bounds (curpt,frustumf,frustumf,ffplane,vo_inverse);update_bounds (curpt,-frustumf,-frustumf,ffplane,vo_inverse);update_bounds (curpt,-frustumf,frustumf,ffplane,vo_inverse);update_bounds (curpt,frustumf,-frustumf,ffplane,vo_inverse);update_bounds (curpt,frustumr,frustumr,fbplane,vo_inverse);update_bounds (curpt,-frustumr,-frustumr,fbplane,vo_inverse);update_bounds (curpt,-frustumr,frustumr,fbplane,vo_inverse);update_bounds (curpt,frustumr,-frustumr,fbplane,vo_inverse);for (i=0; i < WH; i++)  for (j=0; j < WW; j++)    fb[i][j] = fbackgndcolor;for (i=0; i < WH; i++)  for (j=0; j < WW; j++)    zb[i][j] = 0;  PipelineCompute (datasize, from, ftransform, b,		 activeProp.red, activeProp.green, activeProp.blue, 		 activeProp.c1, activeProp.c2, num_lights, lights);write_tga_buffer (fb, argv[6]);} 

⌨️ 快捷键说明

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