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

📄 gleps.c

📁 Delaunay三角形的网格剖分程序
💻 C
📖 第 1 页 / 共 2 页
字号:
static char *rcsid= "$Id: gleps.c,v 2.15 2000/01/13 17:36:41 fuhrmann Exp $";/* This code has been cloned from rendereps.c by Mark Kilgard which had the following   header notice.*//****BEGIN MJK HEADER*//* Copyright (c) Mark J. Kilgard, 1997. *//* This program is freely distributable without licensing fees    and is provided without guarantee or warrantee expressed or    implied. This program is -not- in the public domain. *//* Example showing how to use OpenGL's feedback mode to capture   transformed vertices and output them as Encapsulated PostScript.   Handles limited hidden surface removal by sorting and does   smooth shading (albeit limited due to PostScript). *//****END MJK HEADER*//*  From Mark's code, handling of lines&polygons had been taken.  String handling is new.    For more information about this stuff, see   http://reality.sgi.com/opengl/tips/Feedback.html  */#include <assert.h>#include <math.h>#include <time.h>#include <stdlib.h>#include <string.h>#include "glwin.h"#include "gleps.h"#define Max(a,b) (((a)>(b))?(a):(b))	#define EPS_SMOOTH_LINE_FACTOR 0.06  /* Lower for better smooth                                         lines. */#define EPS_GOURAUD_THRESHOLD 0.1  /* Lower for better (slower)                                       smooth shading. */	#define PS_UPI 72.0#define A4_XINCH 8.27#define A4_YINCH 11.69/* OpenGL's GL_3D_COLOR feedback vertex format. */typedef struct _FBColorVertex {  GLfloat x;  GLfloat y;  GLfloat z;  GLfloat red;  GLfloat green;  GLfloat blue;  GLfloat alpha;} FBColorVertex;#define FBColorVertexSize (sizeof(FBColorVertex)/sizeof(GLfloat))static GLfloat pointSize;static char *gouraudtriangleEPS[] ={  "/bd{bind def}bind def /triangle { aload pop   setrgbcolor  aload pop 5 3",  "roll 4 2 roll 3 2 roll exch moveto lineto lineto closepath fill } bd",  "/computediff1 { 2 copy sub abs threshold ge {pop pop pop true} { exch 2",  "index sub abs threshold ge { pop pop true} { sub abs threshold ge } ifelse",  "} ifelse } bd /computediff3 { 3 copy 0 get 3 1 roll 0 get 3 1 roll 0 get",  "computediff1 {true} { 3 copy 1 get 3 1 roll 1 get 3 1 roll 1 get",  "computediff1 {true} { 3 copy 2 get 3 1 roll  2 get 3 1 roll 2 get",  "computediff1 } ifelse } ifelse } bd /middlecolor { aload pop 4 -1 roll",  "aload pop 4 -1 roll add 2 div 5 1 roll 3 -1 roll add 2 div 3 1 roll add 2",  "div 3 1 roll exch 3 array astore } bd /gouraudtriangle { computediff3 { 4",  "-1 roll aload 7 1 roll 6 -1 roll pop 3 -1 roll pop add 2 div 3 1 roll add",  "2 div exch 3 -1 roll aload 7 1 roll exch pop 4 -1 roll pop add 2 div 3 1",  "roll add 2 div exch 3 -1 roll aload 7 1 roll pop 3 -1 roll pop add 2 div 3",  "1 roll add 2 div exch 7 3 roll 10 -3 roll dup 3 index middlecolor 4 1 roll",  "2 copy middlecolor 4 1 roll 3 copy pop middlecolor 4 1 roll 13 -1 roll",  "aload pop 17 index 6 index 15 index 19 index 6 index 17 index 6 array",  "astore 10 index 10 index 14 index gouraudtriangle 17 index 5 index 17",  "index 19 index 5 index 19 index 6 array astore 10 index 9 index 13 index",  "gouraudtriangle 13 index 16 index 5 index 15 index 18 index 5 index 6",  "array astore 12 index 12 index 9 index gouraudtriangle 17 index 16 index",  "15 index 19 index 18 index 17 index 6 array astore 10 index 12 index 14",  "index gouraudtriangle 18 {pop} repeat } { aload pop 5 3 roll aload pop 7 3",  "roll aload pop 9 3 roll 4 index 6 index 4 index add add 3 div 10 1 roll 7",  "index 5 index 3 index add add 3 div 10 1 roll 6 index 4 index 2 index add",  "add 3 div 10 1 roll 9 {pop} repeat 3 array astore triangle } ifelse } bd",  NULL};#define EPS 1.0e-3#define NEQ(a,b) ((a)<(b)-EPS || (a)>(b)+EPS)/* put primitive at fb buffer location loc */static char crdformat[32]={'%','.','1','f',' ','\0'};static char colformat[32]={'%','.','2','f',' ','\0'};void glepsSetOutputFormat(char * crd, char *col){  strcpy(crdformat,crd);  strcpy(colformat,col);}#define PutCrd(f,x) fprintf(file,crdformat,(x));#define PutCol(f,x) fprintf(file,colformat,(x));#define PutCmd(f,x) fprintf(file,"%s",(x));static GLfloat *PutPrimitive(glWindow w, FILE * file, GLfloat * loc){  int token;  int nvertices, i;  GLfloat red, green, blue;  int smooth;  GLfloat dx, dy, dr, dg, db, absR, absG, absB, colormax;  int steps;  FBColorVertex *vertex;  GLfloat xstep, ystep, rstep, gstep, bstep;  GLfloat xnext, ynext, rnext, gnext, bnext, distance;  GLfloat colfac;  token = *loc;  loc++;  switch (token)     {    case GL_LINE_RESET_TOKEN:        case GL_LINE_TOKEN:      vertex = (FBColorVertex *) loc;           colfac=1.0;      {	for (i = 0; i < 2; i++) 	  if (vertex[i].red>2.0|| vertex[i].blue>2.0|| vertex[i].green>2.0) 	    {	      colfac=2.0/65535.0;	      break;	    }      }	       dr = (vertex[1].red*colfac - vertex[0].red*colfac);      dg = (vertex[1].green*colfac - vertex[0].green*colfac);      db = (vertex[1].blue*colfac - vertex[0].blue*colfac);            if (NEQ(dr,0.0) || NEQ(dg,0.0) || NEQ(db , 0.0)) {	/* Smooth shaded line. */	dx = vertex[1].x - vertex[0].x;	dy = vertex[1].y - vertex[0].y;	distance = sqrt(dx * dx + dy * dy);		absR = fabs(dr);	absG = fabs(dg);	absB = fabs(db);		colormax = Max(absR, Max(absG, absB));	steps = Max(1.0, colormax * distance * EPS_SMOOTH_LINE_FACTOR);		xstep = dx / steps;	ystep = dy / steps;		rstep = dr / steps;	gstep = dg / steps;	bstep = db / steps;		xnext = vertex[0].x;	ynext = vertex[0].y;	rnext = vertex[0].red*colfac;	gnext = vertex[0].green*colfac;	bnext = vertex[0].blue*colfac;		/* Back up half a step; we want the end points to be	   exactly the their endpoint colors. */	xnext -= xstep / 2.0;	ynext -= ystep / 2.0;	rnext -= rstep / 2.0;	gnext -= gstep / 2.0;	bnext -= bstep / 2.0;      } else {	/* Single color line. */	steps = 0;      }      PutCrd(file,vertex[0].red*colfac);      PutCrd(file,vertex[0].green*colfac);      PutCrd(file,vertex[0].blue*colfac);      PutCmd(file,"C\n");      PutCrd(file,vertex[0].x);      PutCrd(file,vertex[0].y);      PutCmd(file,"M\n");            for (i = 0; i < steps; i++) {	xnext += xstep;	ynext += ystep;	rnext += rstep;	gnext += gstep;	bnext += bstep;	PutCrd(file,xnext);	PutCrd(file,ynext);	PutCmd(file,"LS\n");        PutCol(file,rnext);        PutCol(file,gnext);        PutCol(file,bnext);	PutCmd(file,"C\n");	PutCrd(file,xnext);	PutCrd(file,ynext);	PutCmd(file,"M\n");      }      PutCrd(file,vertex[1].x);      PutCrd(file,vertex[1].y);      PutCmd(file,"LS\n");      loc += 2*FBColorVertexSize;             break;    case GL_POLYGON_TOKEN:      nvertices = *loc;      loc++;            vertex = (FBColorVertex *) loc;      /* this is a hack to correct a bug in (older ?) Mesa versions.*/      if (nvertices > 0) 	{ 	  /* no colfac calculations for exact comparison!!! */	  i=0;	  red = vertex[i].red;	  green = vertex[i].green;	  blue =  vertex[i].blue;	  	  smooth = 0;	  for (i = 1; i < nvertices; i++) 	    if (NEQ(red,vertex[i].red) || NEQ(green,vertex[i].green) || NEQ(blue,vertex[i].blue)) 	      {		smooth=1; 		break;	      }	  	  colfac=1.0;	  /*if (glwIsMesa(w))*/	  {	    for (i = 0; i < nvertices; i++) 	      if (vertex[i].red>2.0|| vertex[i].blue>2.0|| vertex[i].green>2.0) 		{		  colfac=2.0/65535.0;		  printf("*");		  break;		}	  }      	  	  	  if (smooth) 	    {	      /* Smooth shaded polygon; varying colors at vertices. */	      int triOffset;	      	      /* Break polygon into "nvertices-2" triangle fans. */	      for (i = 0; i < nvertices - 2; i++) 		{		  triOffset = i * FBColorVertexSize;		  PutCmd(file,"[");		  PutCrd(file,vertex[0].x);		  PutCrd(file,vertex[i + 1].x);		  PutCrd(file,vertex[i + 2].x);		  PutCrd(file,vertex[0].y);		  PutCrd(file,vertex[i + 1].y);		  PutCrd(file,vertex[i + 2].y);		  PutCmd(file,"] [");		  PutCol(file,vertex[0].red*colfac);		  PutCol(file,vertex[0].green*colfac);		  PutCol(file,vertex[0].blue*colfac);		  PutCmd(file,"] [");		  PutCol(file,vertex[i+1].red*colfac);		  PutCol(file,vertex[i+1].green*colfac);		  PutCol(file,vertex[i+1].blue*colfac);		  PutCmd(file,"] [");		  PutCol(file,vertex[i+2].red*colfac);		  PutCol(file,vertex[i+2].green*colfac);		  PutCol(file,vertex[i+2].blue*colfac);		  PutCmd(file,"] GT\n");		}	    } 	  else 	    {	      	      /* Flat shaded polygon; all vertex colors the same. */	      PutCol(file,red*colfac);	      PutCol(file,green*colfac);	      PutCol(file,blue*colfac);	      PutCmd(file,"C\n");	      	      /* Draw a filled polygon. */	      PutCmd(file,"NP\n");	      PutCrd(file,vertex[0].x);	      PutCrd(file,vertex[0].y);	      PutCmd(file,"M\n");	      for (i = 1; i < nvertices; i++) 		  {		    PutCrd(file,vertex[i].x);		    PutCrd(file,vertex[i].y);		    PutCmd(file,"L\n");		  }	      PutCmd(file,"CF\n\n");	    }	}      loc += nvertices * FBColorVertexSize;  /* Each vertex element in the						feedback buffer is 7 GLfloats. */      break;          case GL_POINT_TOKEN:      vertex = (FBColorVertex *) loc;      colfac=1.0;      if (vertex[0].red>2.0|| vertex[0].blue>2.0|| vertex[0].green>2.0) 	{	  colfac=2.0/65535.0;	}         PutCol(file,vertex[0].red*colfac);      PutCol(file,vertex[0].green*colfac);      PutCol(file,vertex[0].blue*colfac);      PutCmd(file,"C\n");      PutCrd(file,vertex[0].x);      PutCrd(file,vertex[0].y);      PutCrd(file,pointSize/2.0);      PutCmd(file,"0 360 AF\n\n");      loc += FBColorVertexSize;           /* Each vertex element in the feedback					     buffer is 7 GLfloats. */      break;    case GL_PASS_THROUGH_TOKEN:       vertex=(FBColorVertex *) (loc-FBColorVertexSize-1);      colfac=1.0;      if (vertex[0].red>2.0 || vertex[0].blue>2.0 || vertex[0].green>2.0) 	{	  colfac=2.0/65535.0;	}      fprintf(file, "%g FS\n",glwGetFBStringSize(w,(int)*loc));      PutCol(file,vertex[0].red*colfac);      PutCol(file,vertex[0].green*colfac);      PutCol(file,vertex[0].blue*colfac);      PutCmd(file,"C\n");      PutCrd(file,vertex[0].x);      PutCrd(file,vertex[0].y);      PutCmd(file,"M\n");      fprintf(file,"(%s) S\n",glwGetFBString(w,(int)*loc));	loc++; 	break;    case GL_BITMAP_TOKEN:     case GL_DRAW_PIXEL_TOKEN:     case GL_COPY_PIXEL_TOKEN:       loc+=FBColorVertexSize;       break;                default:      printf("gleps: PutPrimitive: Unexpected token (%d).\n",token);      return NULL;    }  return loc;

⌨️ 快捷键说明

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