newrender.c
上传用户:zbjingming
上传日期:2010-01-02
资源大小:2436k
文件大小:15k
- #include "radio.h"
- #include <string.h>
- #include "math.h"
- #include "image.h"
- #include <time.h>
- int gWinWidth, gWinHeigth;
- TEXTURE_2D **gTextureList;
- OBJECT *gObjectList; /* ObjectList[0]:isolated surfaces*/
- INT4S gObjectNum;
- int gTexnum;
- float gEx = 40.0, gEy = 1.0, gEz = 45.0,
- gCx = -5.0, gCy = 1.0, gCz = 5.0;
-
- char gEnergyFile[30];
- char gImageName[30];
- double gMatrixVP[16];
- double gMatrixC1[16];
- double gProject[16];
- double gModelview[16];
- double gViewport[16];
- void ReadData()
- {
- int i,j,l;
- FILE *fp;
- char stemp[100];
- POINT *plist;
- INT4U nAllVertexNum;
- INT4U *pchlist;
-
- fp = fopen( gEnergyFile, "r" );
- if ( fp == NULL )
- {
- printf( "n Can not open energy data file:%s", gEnergyFile);
- exit(0);
- }
- fseek( fp, 0, SEEK_SET);
-
- /****** read texture list ******/
- fscanf( fp, "%s", stemp);
- while( strcmp( stemp,"texnum" ) != 0) fscanf( fp, "%s", stemp);
- fscanf( fp, "%d", &gTexnum );
- gTextureList = (TEXTURE_2D **)malloc( sizeof(TEXTURE_2D)*(gTexnum+1));
- for(i=1; i<=gTexnum; i++)
- {
- gTextureList[i] = (TEXTURE_2D *)malloc( sizeof(TEXTURE_2D));
- fscanf( fp, "%s%s", gTextureList[i]->fname, stemp );
- if ( strcmp( stemp,"REPEAT_TEXTURE" ) == 0)
- gTextureList[i]->type = 1;
- else if ( strcmp( stemp,"CLAMP_TEXTURE" ) == 0)
- gTextureList[i]->type = 0;
- }
- /****** Read object list ******/
- fscanf( fp, "%s", stemp);
- while( strcmp( stemp,"ObjectNum" ) != 0) fscanf(fp,"%s",stemp);
- fscanf( fp, "%ld", &gObjectNum);
- gObjectList = (OBJECT *)malloc( sizeof(OBJECT ) * gObjectNum);
- for(i = 0; i < gObjectNum; i ++ )
- {
- fscanf( fp, "%s", stemp);
- while( strcmp( stemp,"SurfaceNum" ) != 0) fscanf(fp,"%s",stemp);
- fscanf( fp, "%ld", &(gObjectList[i].SurfNum) );
- gObjectList[i].surflist = (SURFACE *)malloc( sizeof(SURFACE) * gObjectList[i].SurfNum);
- for(j = 0; j < gObjectList[i].SurfNum; j ++ )
- {
- /****** Read surface infor ******/
- fscanf( fp, "%s", stemp);
- while( strcmp( stemp,"TextureId" ) != 0) fscanf(fp,"%s",stemp);
- fscanf( fp, "%d", &(gObjectList[i].surflist[j].texId) );
- fscanf( fp, "%s", stemp);
- while( strcmp( stemp,"pointnum" ) != 0) fscanf(fp,"%s",stemp);
- fscanf( fp, "%d", &(gObjectList[i].surflist[j].pointn) );
- fscanf( fp, "%s", stemp);
- while( strcmp( stemp,"triangle" ) != 0) fscanf(fp,"%s",stemp);
- fscanf( fp, "%d", &(gObjectList[i].surflist[j].triangle) );
- fscanf( fp, "%s", stemp);
- while( strcmp( stemp,"quadrangle" ) != 0) fscanf(fp,"%s",stemp);
- fscanf( fp, "%d", &(gObjectList[i].surflist[j].quadric) );
-
- /****** Read point list ******/
- gObjectList[i].surflist[j].pointlist = (POINT*)malloc(sizeof(POINT) *
- gObjectList[i].surflist[j].pointn);
- plist = gObjectList[i].surflist[j].pointlist;
- for( l = 0; l < gObjectList[i].surflist[j].pointn ; l ++ )
- fscanf( fp, "%f%f%f%f%f%f%f%f",
- &(plist[l].r), &(plist[l].g), &(plist[l].b),
- &(plist[l].u), &(plist[l].v),
- &(plist[l].x), &(plist[l].y), &(plist[l].z) );
- /****** Read patchlist ******/
- nAllVertexNum = gObjectList[i].surflist[j].triangle * 3 +
- gObjectList[i].surflist[j].quadric *4 ;
- gObjectList[i].surflist[j].patchlist = (INT4U *)malloc( sizeof(INT4U) * nAllVertexNum);
- pchlist = gObjectList[i].surflist[j].patchlist;
- for( l = 0; l < nAllVertexNum; l ++ )
- fscanf( fp, "%ld", &(pchlist[l]) );
-
- }
-
- }
-
-
- fclose(fp);
- }
- void InitRenderWin()
- {
- static int mark = 1;
- FILE *fp;
- /* fp = fopen(sLookAtFN, "rb");
- if (fp == NULL)
- {
- ex = ey = ez =1.0f;
- cx = cy = cz =0.0f;
- near = 0.1;
- angle = 30.0f;
- }
- else fscanf(fp, "%f%f%f%f%f%f%f%f", &angle, &near, &ex, &ey, &ez, &cx, &cy, &cz);
- fclose(fp);*/
- glShadeModel ( GL_SMOOTH );
- glDepthFunc ( GL_LESS );
- glEnable ( GL_DEPTH_TEST );
- glViewport(0, 0, gWinWidth, gWinHeigth);
- glMatrixMode ( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective ( 30.0, (float)gWinWidth/(float)gWinHeigth, 10.0, 1000000000.0);
- if (mark)
- {
- glGetDoublev( GL_PROJECTION_MATRIX, gProject );
- gViewport[0] = gWinWidth / 2 ; gViewport[1] = 0.0; gViewport[2] = 0.0; gViewport[3] = 0.0;
- gViewport[4] = 0.0; gViewport[5] = gWinHeigth / 2 ; gViewport[6] = 0.0; gViewport[7] = 0.0;
- gViewport[8] = 0.0; gViewport[9] = 0.0; gViewport[10] = 1.0; gViewport[11] = 0.0;
- gViewport[12] = gWinWidth / 2 ; gViewport[13] = gWinHeigth / 2; gViewport[14] = 0.0; gViewport[15] = 1.0;
- glPushMatrix();
- glLoadMatrixd(gViewport);
- glMultMatrixd(gProject);
- glGetDoublev( GL_PROJECTION_MATRIX , gMatrixVP );
- glPopMatrix();
- mark = 0;
- }
- }
- void Render(void)
- {
- int i, j, k, l, m, TexIndex;
- POINT *plist;
- INT4U *pchlist;
-
-
- glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_ACCUM_BUFFER_BIT);
-
- for(i = 0; i < gObjectNum; i ++ )
- for(j = 0; j < gObjectList[i].SurfNum; j ++ )
- {
- TexIndex = gObjectList[i].surflist[j].texId;
- if( TexIndex > 0 )
- InitTex( TexIndex );
-
- plist = gObjectList[i].surflist[j].pointlist;
- pchlist = gObjectList[i].surflist[j].patchlist;
-
- l = 0;
- for ( k = 0; k < gObjectList[i].surflist[j].triangle; k ++)
- {
- glBegin( GL_TRIANGLES );
- for( m = 0; m < 3; m ++ )
- {
- glColor3f ( plist[pchlist[l]].r,
- plist[pchlist[l]].g,
- plist[pchlist[l]].b );
- glTexCoord2f( plist[pchlist[l]].u,
- plist[pchlist[l]].v );
- glVertex3f( plist[pchlist[l]].x,
- plist[pchlist[l]].y,
- plist[pchlist[l]].z );
- l ++;
- }/* m */
- glEnd();
- }/* k */
-
- for ( k = 0; k < gObjectList[i].surflist[j].quadric; k ++)
- {
- glBegin( GL_QUADS );
- for( m = 0; m < 4; m ++ )
- {
- glColor3f ( plist[pchlist[l]].r,
- plist[pchlist[l]].g,
- plist[pchlist[l]].b );
- glTexCoord2f( plist[pchlist[l]].u,
- plist[pchlist[l]].v );
- glVertex3f( plist[pchlist[l]].x,
- plist[pchlist[l]].y,
- plist[pchlist[l]].z );
- l ++;
- }/* m */
- glEnd();
- }/* k */
- glFlush();
- CloseTex();
-
- }
- }
- void freelist()
- {
- int i, j;
-
- for( i=0; i<gObjectNum; i++)
- {
- for( j=0; j<gObjectList[i].SurfNum; j++)
- {
- free(gObjectList[i].surflist[j].pointlist);
- free(gObjectList[i].surflist[j].patchlist);
- }
- free( gObjectList[i].surflist );
- }
- free(gObjectList);
-
- for(i=1; i<=gTexnum; i++)
- free(gTextureList[i]);
- free(gTextureList);
-
- auxQuit();
- }
- void markDiscontinuous(short xlen, short ylen, float *depth, unsigned char **mark)
- {
- int x,y,i,l,m, n;
- float **buf, temp;
- static int index[]={1,0,1,1,0,1,-1,1};
- i = 0;
- buf = (float **) malloc (ylen * sizeof(float *));
- for( y=0; y<ylen; y++)
- {
- buf[y] = (float *) malloc (xlen * sizeof(float));
- for(x = 0; x < xlen; x ++)
- {
- buf[y][x] = depth[i];
- i ++;
-
- }
- /* if( y == 76)
- for(x = 0; x < xlen; x ++)
- {
- if( x % 10 == 0) printf("n");
- printf("%f,", buf[y][x]);
- }
- */ }
- for( y=0; y<ylen; y++)
- {
- for(x = 0; x < xlen; x ++)
- {
- for( i = 0; i < 4; i ++)
- {
- m = x + index[i << 1];
- n = y + index[(i << 1) + 1];
- if( (m >= 0) && (m < xlen) && (n >= 0) && (n < ylen) )
- { /*************/
- temp = buf[y][x] - buf[n][m]; /* |3|2|1| */
- if(temp > 0.007) /* |4| |0| */
- mark[y][x] = mark[y][x] |((char)1 << i) ; /* |5|6|7| */
- else if(temp < -0.007) /*************/
- mark[n][m] = mark[n][m] | ((char)1 << (4 + i));
- }
- }
- }
- }
- for(y=0; y<ylen; y++) free(buf[y]);
- free(buf);
- i = 0;
- for( y=0; y<ylen; y++)
- for(x = 0; x < xlen; x ++)
- if(mark[y][x] )
- {
- if( i % 10 == 0) printf("n");
- printf("(%1d,%1d,%3x)",y, x, buf[y][x]);
- i ++;
- }
- }
- /****************************************/
- /* function :write_rgb */
- /****************************************/
- void write_rgb()
- {
- static int num = 0;
- short viewx,viewy;
- short xx,y,x;
- int i;
- short Xlen, Ylen, colNum, rowNum;
- char temp[4], imageFile[30], depthFile[30];
- GLint port[4];
- double begin, wx, wy, wz, w;
- IMAGE *image;
- FILE *fp;
- unsigned short *rbuf;
- unsigned short *gbuf;
- unsigned short *bbuf;
- short *dbuf;
- float *depth;
- unsigned char *demo, **disMark;
- long colNumAddress, rowNumAddress;
- num ++;
- temp[0] = '.';
- i = num / 10;
- temp[1] = (char)('0' + i);
- i = num % 10;
- temp[2] = (char)('0' + i);
- temp[3] = ' ';
- strcpy(imageFile,gImageName);
- strcpy(&(imageFile[strlen(imageFile)]), temp);
- strcpy(depthFile,imageFile);
- strcpy(&(imageFile[strlen(imageFile)]), ".rgb");
- strcpy(&(depthFile[strlen(depthFile)]), ".depth");
- glGetDoublev( GL_MODELVIEW_MATRIX, gModelview );
- glPushMatrix();
- glLoadMatrixd(gMatrixVP);
- glMultMatrixd(gModelview);
- glGetDoublev( GL_MODELVIEW_MATRIX, gMatrixC1 );
- glPopMatrix();
- gMatrixC1[2] = 0.0; gMatrixC1[6] = 0.0; gMatrixC1[10] = 1.0; gMatrixC1[14] = 14.737;
- glGetIntegerv(GL_VIEWPORT,port);
- viewx = port[2]-port[0];
- viewy = port[3]-port[1];
- Xlen = viewx;
- Ylen = viewy;
- fp = fopen( depthFile, "wb"); /**************************************/
- fwrite( &colNumAddress, sizeof(long), 1, fp);
- fwrite( &Xlen, sizeof(short), 1, fp); /* write viewx, viewy & matrix C^ */
- fwrite( &Ylen, sizeof(short), 1, fp); /**************************************/
- fwrite(gMatrixC1, sizeof(double), 16, fp);
-
- depth = (float *)malloc( viewx*viewy * sizeof(float) );
- demo = (unsigned char *)malloc(viewx*viewy*3);
- disMark = (unsigned char **)malloc(viewy * sizeof(unsigned char *));
- for(i = 0; i < viewy; i ++)
- {
- disMark[i] = (unsigned char *)malloc(viewx);
- for(x = 0 ; x < viewx; x ++)
- disMark[i][x] = 0;
- }
- rbuf = (unsigned short*)malloc(Xlen*sizeof(short));
- gbuf = (unsigned short*)malloc(Xlen*sizeof(short));
- bbuf = (unsigned short*)malloc(Xlen*sizeof(short));
- dbuf = (short*)malloc(Xlen*sizeof(short));
- glReadPixels(0,0,viewx,viewy,GL_RGB,GL_UNSIGNED_BYTE,demo);
- glReadPixels( 0, 0, viewx, viewy, GL_DEPTH_COMPONENT, GL_FLOAT, depth );
- markDiscontinuous(viewx, viewy, depth, disMark);
- printf( "n end mark discontiniousn");
- colNum = 0;
- colNumAddress = ftell(fp);
- fwrite( &colNum, sizeof(short), 1, fp); /* Write length of colum */
- for( y=0; y<Ylen; y++)
- {
- rowNum = 0;
- rowNumAddress = ftell(fp);
- fwrite( &rowNum, sizeof(short), 1, fp); /* Write length of row */
- fwrite( &y, sizeof(short), 1, fp); /* Write row's No. */
- for(xx=0; xx<Xlen; xx++)
- {
- if(disMark[y][xx] != 0)
- {
- fwrite( &xx, sizeof(short), 1, fp); /* Write col's No. and discontinuious Mark */
- fwrite( &disMark[y][xx], sizeof(char), 1, fp);
- rowNum ++;
- }
- }
- if( rowNum == 0)
- fseek(fp, rowNumAddress, SEEK_SET);
- else
- {
- fseek(fp, rowNumAddress, SEEK_SET);
- fwrite( &rowNum, sizeof(short), 1, fp);
- fseek(fp, 0, SEEK_END);
- colNum ++;
- }
- }
- fseek(fp, colNumAddress, SEEK_SET);
- fwrite( &colNum, sizeof(short), 1, fp);
- fseek(fp, 0, SEEK_END);
- printf( "n end Wriete discontinious mark ");
-
- colNumAddress = ftell(fp);
- fseek(fp, 0, SEEK_SET);
- fwrite(&colNumAddress, sizeof(long), 1, fp);
- fseek(fp, 0, SEEK_END);
- i=0;
- for( y=0; y<Ylen; y++)
- {
- for(xx=0; xx<Xlen; xx++)
- {
- gluUnProject(xx ,y, depth[i+xx], gModelview, gProject, port, &wx, &wy, &wz);
- w = gMatrixC1[3] * wx + gMatrixC1[7] * wy + gMatrixC1[11] * wz + gMatrixC1[15];
- depth[i+xx] = ( gMatrixC1[2] * wx + gMatrixC1[6] * wy
- + gMatrixC1[10] * wz + gMatrixC1[14] ) / w;
- }
- i += Xlen;
- }
- image = iopen(imageFile , "w", RLE(1), 3, Xlen, Ylen, 3);
- i=0;
- for( y=0; y<Ylen; y++)
- {
- for(xx=0; xx<Xlen; xx++)
- {
- rbuf[xx] = (short)(demo[(i+xx)*3]);
- gbuf[xx] = (short)(demo[(i+xx)*3+1]);
- bbuf[xx] = (short)(demo[(i+xx)*3+2]);
- dbuf[xx] = (short)(depth[i+xx]*10000);
- }
- i += Xlen;
- putrow(image, rbuf,y,0);
- putrow(image, gbuf,y,1);
- putrow(image, bbuf,y,2);
- fwrite(dbuf, sizeof(short), Xlen, fp);
- }
- iclose(image);
- fclose(fp);
- printf( "n end write rgb ");
-
- free(demo);
- for(i = 0; i < viewy; i ++) free(disMark[i]);
- free(disMark);
- free(depth);
- free(rbuf);
- free(gbuf);
- free(bbuf);
- free(dbuf);
- }
- void leftFunc(void)
- {
- gEx -= 2.0;
-
- }
- void rightFunc(void)
- {
- gEx += 2.0;
-
- }
- void upFunc(void)
- {
- gEz -= 2.0;
-
- }
- void downFunc(void)
- {
- gEz += 2.0;
- }
- void leftCFunc(void)
- {
- gCx -= 2.0;
-
- }
- void rightCFunc(void)
- {
- gCx += 2.0;
-
- }
- void upCFunc(void)
- {
- gCz -= 2.0;
-
- }
- void downCFunc(void)
- {
- gEz += 2.0;
- }
- void inputEye(void)
- {
- printf("nPlease input view point(x,y,z):");
- scanf("%f%f%f",&gEx, &gEy, &gEz);
- }
- void inputCenter(void)
- {
- printf("nPlease input center point(x,y,z):");
- scanf("%f%f%f",&gCx, &gCy, &gCz);
- }
- void getXYZ(AUX_EVENTREC *event)
- {
- GLint i,j;
- double x,y,z;
- GLint port[4];
- float depth;
- i = event->data[AUX_MOUSEX];
- j = gWinHeigth - event->data[AUX_MOUSEY];
- glGetIntegerv(GL_VIEWPORT,port);
- glGetDoublev( GL_MODELVIEW_MATRIX, gModelview );
- glReadPixels( i, j, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth );
- gluUnProject(i , j, depth, gModelview, gProject, port, &x, &y, &z);
- printf( "n the world coordination of (%1d, %1d, %f) is (%f, %f, %f)", i, j, depth, x, y, z);
-
- }
- /****************************************/
- /* function : Shoot */
- /* calculate the Fij, */
- /* (i=0,pindex-1; j=0,pindex-1) */
- /****************************************/
- void Shoot(void)
- {
- system ("date");
- glMatrixMode ( GL_MODELVIEW );
- glLoadIdentity();
- gluLookAt( gEx, gEy, gEz, gCx, gCy, gCz, 0.0, 1.0, 0.0);
- printf("Viewpoint(%3.1f, %3.1f, %3.1f), Centerpoint(%1.1f, %1.1f, %1.1f)",
- gEx, gEy, gEz, gCx, gCy, gCz);
- /* DrawBkGroundPic(); */
- Render();
- printf("n****** Finish Render; Current time is:");
- system ("date");
- /* write_rgb(gImageName);
- /***************
- PrintMessage();
- ***************/
-
- }
- /****************************************/
- /* function: myReshape1 */
- /****************************************/
- void myReshape1(int w, int h)
- {
- InitRenderWin();
- }
- /****************************************/
- /* function: Hemi_mode */
- /****************************************/
- void Hemi_mode()
- {
- auxInitDisplayMode ( AUX_SINGLE | AUX_RGBA | AUX_DEPTH );
- auxInitPosition (0, 0, gWinWidth, gWinHeigth);
- auxInitWindow ( gImageName );
- auxKeyFunc (AUX_LEFT, leftFunc);
- auxKeyFunc (AUX_RIGHT, rightFunc);
- auxKeyFunc (AUX_UP, upFunc);
- auxKeyFunc (AUX_DOWN, downFunc);
- auxKeyFunc (AUX_j, leftCFunc);
- auxKeyFunc (AUX_l, rightCFunc);
- auxKeyFunc (AUX_k, upCFunc);
- auxKeyFunc (AUX_m, downCFunc);
- auxKeyFunc (AUX_e, inputEye);
- auxKeyFunc (AUX_c, inputCenter);
- auxKeyFunc (AUX_ESCAPE, freelist);
- auxKeyFunc (AUX_w, write_rgb);
- auxMouseFunc( AUX_LEFTBUTTON, AUX_MOUSEDOWN, getXYZ);
- auxReshapeFunc (myReshape1);
-
- auxMainLoop(Shoot);
- }