newrender.cpp
上传用户:zbjingming
上传日期:2010-01-02
资源大小:2436k
文件大小:7k
- #include "radio.h"
- #include <string.h>
- #include <math.h>
- #include <time.h>
- int WinWidth, WinHeigth;
- TEXTURE_2D **TextureList;
- OBJECT *ObjectList; /* ObjectList[0]:isolated surfaces*/
- bmphead *bmp;
- INT4S ObjectNum;
- char gEnergyFile[30];
- char sLookAtFN[100];
- char ImageName[30];
-
- void ReadData()
- {
- int i,j,l;
- FILE *fp;
- char stemp[100];
- POINT3D *plist;
- INT4U nAllVertexNum;
- INT4U *pchlist;
- char ch[30];
-
- fp = fopen( gEnergyFile, "r" );
- if ( fp == NULL )
- {
- printf( "n Can not open energy data file:%sn", 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", &texnum );
- TextureList = (TEXTURE_2D **)malloc( sizeof(TEXTURE_2D)*(texnum+1));
- for(i=1; i<=texnum; i++)
- {
- TextureList[i] = (TEXTURE_2D *)malloc( sizeof(TEXTURE_2D));
- fscanf( fp, "%s%s", TextureList[i]->fname, stemp );
- if ( strcmp( stemp,"REPEAT_TEXTURE" ) == 0)
- TextureList[i]->type = 1;
- else if ( strcmp( stemp,"CLAMP_TEXTURE" ) == 0)
- TextureList[i]->type = 0;
- }
- /****** Read object list ******/
- fscanf( fp, "%s", stemp);
- while( strcmp( stemp,"ObjectNum" ) != 0) fscanf(fp,"%s",stemp);
- fscanf( fp, "%ld", &ObjectNum);
- ObjectList = (OBJECT *)malloc( sizeof(OBJECT ) * ObjectNum);
- for(i = 0; i < ObjectNum; i ++ )
- {
- fscanf( fp, "%s", stemp);
- while( strcmp( stemp,"SurfaceNum" ) != 0) fscanf(fp,"%s",stemp);
- fscanf( fp, "%ld", &(ObjectList[i].SurfNum) );
- ObjectList[i].surflist = (SURFACE *)malloc( sizeof(SURFACE) * ObjectList[i].SurfNum);
- for(j = 0; j < ObjectList[i].SurfNum; j ++ )
- {
- /****** Read surface infor ******/
- fscanf( fp, "%s", stemp);
- while( strcmp( stemp,"TextureId" ) != 0) fscanf(fp,"%s",stemp);
- fscanf( fp, "%d", &(ObjectList[i].surflist[j].texId) );
- fscanf( fp, "%s", stemp);
- while( strcmp( stemp,"pointnum" ) != 0) fscanf(fp,"%s",stemp);
- fscanf( fp, "%d", &(ObjectList[i].surflist[j].pointn) );
- fscanf( fp, "%s", stemp);
- while( strcmp( stemp,"triangle" ) != 0) fscanf(fp,"%s",stemp);
- fscanf( fp, "%d", &(ObjectList[i].surflist[j].triangle) );
- fscanf( fp, "%s", stemp);
- while( strcmp( stemp,"quadrangle" ) != 0) fscanf(fp,"%s",stemp);
- fscanf( fp, "%d", &(ObjectList[i].surflist[j].quadric) );
-
- /****** Read point list ******/
- ObjectList[i].surflist[j].pointlist = (POINT3D*)malloc(sizeof(POINT3D) *
- ObjectList[i].surflist[j].pointn);
- plist = ObjectList[i].surflist[j].pointlist;
- for( l = 0; l < ObjectList[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 = ObjectList[i].surflist[j].triangle * 3 +
- ObjectList[i].surflist[j].quadric *4 ;
- ObjectList[i].surflist[j].patchlist = (INT4U *)malloc( sizeof(INT4U) * nAllVertexNum);
- pchlist = ObjectList[i].surflist[j].patchlist;
- for( l = 0; l < nAllVertexNum; l ++ )
- fscanf( fp, "%ld", &(pchlist[l]) );
- }
-
- }
-
-
- fclose(fp);
- }
- void InitRenderWin()
- {
- float angle, Near, ex, ey, ez, cx, cy, cz;
- 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, WinWidth, WinHeigth);
- glMatrixMode ( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective ( angle, (float)WinWidth/(float)WinHeigth, Near, 1000000000.0);
- glMatrixMode ( GL_MODELVIEW );
- glLoadIdentity();
- gluLookAt( ex, ey, ez, cx, cy, cz, 0.0, 1.0, 0.0);
- /* DrawBkGroundPic(); */
- }
- void Render(void)
- {
- int i, j, k, l, m, TexIndex;
- POINT3D *plist;
- INT4U *pchlist;
-
-
-
- glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_ACCUM_BUFFER_BIT);
- for(i = 0; i < ObjectNum; i ++ )
- for(j = 0; j < ObjectList[i].SurfNum; j ++ )
- {
- TexIndex = ObjectList[i].surflist[j].texId;
- if( TexIndex > 0 )
- InitTex( TexIndex );
- plist = ObjectList[i].surflist[j].pointlist;
- pchlist = ObjectList[i].surflist[j].patchlist;
-
- l = 0;
- for ( k = 0; k < ObjectList[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 < ObjectList[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();
- }
- auxSwapBuffers();
- }
- void freelist()
- {
- int i, j;
-
- for( i=0; i<ObjectNum; i++)
- {
- for( j=0; j<ObjectList[i].SurfNum; j++)
- {
- free(ObjectList[i].surflist[j].pointlist);
- free(ObjectList[i].surflist[j].patchlist);
- }
- free( ObjectList[i].surflist );
- }
- free(ObjectList);
- for(i=1; i<=texnum; i++)
- free(TextureList[i]);
- free(TextureList);
- }
- /****************************************/
- /* function : Shoot */
- /* calculate the Fij, */
- /* (i=0,pindex-1; j=0,pindex-1) */
- /****************************************/
- void CALLBACK Shoot(void)
- {
- time_t ltime;
- struct tm *today;
- time( <ime );
- today = localtime( <ime );
- printf("Start at: %dn", today->tm_sec);
- Render();
- time( <ime );
- today = localtime( <ime );
- printf("End at: %dn", today->tm_sec);
- }
- /****************************************/
- /* function: myReshape */
- /****************************************/
- void CALLBACK myReshape(int w, int h)
- {
- InitRenderWin();
- }
- /****************************************/
- /* function: Hemi_mode */
- /****************************************/
- void Hemi_mode()
- {
- auxInitDisplayMode ( AUX_DOUBLE | AUX_RGBA | AUX_DEPTH );
- auxInitPosition (0, 0, WinWidth, WinHeigth);
- auxInitWindow ( "Windows" );
- auxReshapeFunc (myReshape);
- auxMainLoop(Shoot);
- }