📄 modelviewer.c
字号:
#include <stdio.h>#include <stdlib.h>#include <math.h>/** ** Note: this is the only operating system dependency in the whole code. ** With this #ifdef, this code runs without change on UNIX, Windows, ** and Mac. ** GLUT gets most of the credit for this... **/#ifdef WIN32#define M_PI 3.14159265#include <windows.h>#endif/* include the OpenGL include files: */#include <GL/glut.h>/** ** ** This is a sample of using the OpenGL and GLUT libraries ** ** ** The left mouse button does rotation ** The middle mouse button does scaling ** The right mouse button brings up a pop-up menu that allows: ** 1. The projection to be changed ** 2. The colors to be changed ** 3. Axes on and off ** 4. The transformations to be reset ** 5. The program to quit ** ** Author: Mike Houston ** **//** ** defines: **//* title of this window: */#define WINDOWTITLE "Test App - Mike Houston"/* the escape key: */#define ESCAPE 0x1b/* upper-left corner of the window: */#define WIN_LEFT 30#define WIN_TOP 30/* initial window size: */#define WINDOW_SIZE 1000/* minimum scale factor allowed: */#define MIN_SCALE 0.01/* multiplication factors for input interaction: *//* (these are known from previous experience) */#define ANGFACT 1.0#define SCLFACT 0.005/* active mouse buttons (or them together): */#define LEFT 4#define MIDDLE 2#define RIGHT 1/* values for menu items: */#define SETCOLORS 1#define RESET 2#define QUIT 3#define ORTHO 1#define PERSP 2#define RED 1#define YELLOW 2#define GREEN 3#define CYAN 4#define BLUE 5#define MAGENTA 6#define WHITE 7#define BLACK 8/* window background color (rgba): */#define BACKGROUND_COLOR 0.,0.,0.,0./* color and line width for the axes: */#define AXES_COLOR 0.,1.,0.#define AXES_SIZE 18.#define AXES_WIDTH 5./* whether the left button is rotate or scale: */#define ROTATE 0#define SCALE 1/* parameters for the torus: */#define RADIUS1 6.0#define RADIUS2 10.0#define SLICES 30#define STACKS 20#define TORUS_WIDTH 2./* handy to have around: */#ifndef FALSE#define FALSE 0#define TRUE ( ! FALSE )#endif#define OFF FALSE#define ON TRUE#define DISABLED FALSE#define ENABLED TRUE/** ** global variables: **/char wire=0;int ActiveButton; /* current button that is down */int AxesList; /* list to hold the axes */int AxesMenu; /* id of the axes pop-up menu */int AxesOnOff; /* ON or OFF */int ColorMenu; /* id of the color pop-up menu */int Debug; /* non-zero means print debug info */int GrWindow; /* window id for top-level window */int MainMenu; /* id of the main pop-up menu */int Projection; /* ORTHO or PERSP */int ProjMenu; /* id of the projection pop-up menu */float Red, Green, Blue; /* star colors */float Scale; /* scaling factor */int ObjList[4]; /* list to hold the 3d object */int whichObj=0;int TransformMode; /* ROTATE or SCALE */int TransformModeMenu; /* id of the transform mode menu */float Xrot, Yrot; /* rotation angles in degrees */int Xmouse, Ymouse; /* mouse values */typedef char *str ;str model[4]={"data","data.cracks","data.liberal","data.conservative"};/** ** function prototypes: **/void Animate( void );void Axes( float length );void Display( void );void DoAxesMenu( int value );void DoColorMenu( int value );void DoMainMenu( int value );void DoProjMenu( int value );void DoRasterString( float x, float y, float z, char *s );void DoStrokeString( float x, float y, float z, float ht, char *s );void DoTransformModeMenu( int value );void InitGraphics( void );void InitLists( void );void Keyboard( unsigned char, int, int );void Keyboard2( int, int, int );void MouseButton( int button, int state, int x, int y );void MouseMotion( int x, int y );void Quit( void );void Reset( void );void Resize( int width, int height );void Visibility( int state );/** ** main program: **/intmain( int argc, char *argv[] ){ int i; /* counter */ /* turn on the glut package: */ /* (do this before checking argc and argv since it might */ /* pull some command line arguments out) */ glutInit( &argc, argv ); /* set defaults: */ Debug = FALSE; /* read the command line: */ for( i=1; i < argc; i++ ) { if( strcmp( argv[i], "-D" ) == 0 ) { Debug = TRUE; continue; }else { if (i==1) { model[0]=argv[i]; } if (i==2) { model[1]=argv[i]; } if (i==3) { model[2]=argv[i]; } }/* fprintf( stderr, "Unknown argument: '%s'\n", argv[i] ); fprintf( stderr, "Usage: %s [-D]\n", argv[0] );*/ } /* setup all the graphics stuff, including callbacks: */ InitGraphics(); /* init the transformations and colors: */ /* (will also post a redisplay) */ Reset(); /* draw the scene once and wait for some interaction: */ /* (will never return) */ glutMainLoop(); /* keep lint happy: */ return 0;}/** ** this is where one would put code that is to be called ** everytime the glut main loop has nothing to do, ie, ** the glut idle function ** ** this is typically where animation happens **/voidAnimate( void ){ /* put animation stuff in here -- set some global variables */ /* for Display() to find: */ glutSetWindow( GrWindow ); glutPostRedisplay();}/** ** draw the complete scene: **/voidDisplay( void ){ int dx, dy, d; /* viewport dimensions */ int xl, yb; /* lower-left corner of viewport */ float red[4]={1,0,0,1}; float white[4]={1,1,1,1}; /* set which window we want to do the graphics into: */ glutSetWindow( GrWindow ); /* erase the background: */ glDrawBuffer( GL_BACK ); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glEnable( GL_DEPTH_TEST ); /* specify shading to be flat: */ glShadeModel( GL_SMOOTH ); /* set the viewport to a square centered in the window: */ dx = glutGet( GLUT_WINDOW_WIDTH ); dy = glutGet( GLUT_WINDOW_HEIGHT ); d = dx < dy ? dx : dy; /* minimum dimension */ xl = ( dx - d ) / 2; yb = ( dy - d ) / 2; glViewport( xl, yb, d, d ); /* set the viewing volume: */ /* remember that the eye is at the origin looking in -Z */ /* remember that the Znear and Zfar values are actually */ /* given as DISTANCES IN FRONT OF THE EYE */ glMatrixMode( GL_PROJECTION ); glLoadIdentity(); if( Projection == ORTHO ) glOrtho( -15., 15., -15., 15., 0.1, 60. ); else gluPerspective( 70., 1., 0.1, 60. ); /* viewing transform: */ glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); gluLookAt( 0., 0., 25., 0., 0., 0., 0., 1., 0. ); /* perform the rotations and scaling about the origin: */ glRotatef( Yrot, 0., 1., 0. ); glRotatef( Xrot, 1., 0., 0. ); glScalef( Scale, Scale, Scale ); /* set the color of the object: */ glColor3f( Red, Green, Blue ); /* draw the object: */ glLightfv(GL_LIGHT0,GL_DIFFUSE,white); glColor4fv(white); if (!wire) { glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); }else { glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glDisable(GL_LIGHTING); glDisable(GL_LIGHT0); } glCallList( ObjList[whichObj] ); glLightfv(GL_LIGHT0,GL_DIFFUSE,red); glColor4fv(red); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glDisable(GL_LIGHTING); glDisable(GL_LIGHT0); glCallList( ObjList[0] ); /* possibly draw the axes: */ if( AxesOnOff == ON ) glCallList( AxesList ); /* draw some text that rotates and scales with the scene: */ glEnable( GL_DEPTH_TEST ); glColor3f( 1., 1., 0. );/* DoStrokeString( 0., AXES_SIZE, 0., AXES_SIZE/10., "Top of Axes" );*/ /* draw some text that just rotates with the scene: */ glDisable( GL_DEPTH_TEST ); glColor3f( 0., 1., 1. ); /*DoRasterString( 0., 0., AXES_SIZE, "Front of Axes" );*/ /* draw some text that is fixed on the screen: */ glDisable( GL_DEPTH_TEST ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluOrtho2D( 0., 100., 0., 100. ); /* setup "percent units"*/ glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); glColor3f( 1., 0., 1. );/* DoRasterString( 5., 5., 0., "Sample App - Mike Houston" );*/ /* swap the double-buffered framebuffers: */ glutSwapBuffers(); /* be sure the graphics buffer has been sent: */ glFlush();}/** ** process the axes pop-up menu: **/voidDoAxesMenu( int value ){ AxesOnOff = value; glutSetWindow( GrWindow ); glutPostRedisplay();}/** ** process the color pop-up menu: **/voidDoColorMenu( int value ){ Red = Green = Blue = 1.; switch( value ) { case RED: Green = Blue = 0.; break; case YELLOW: Blue = 0.; break; case GREEN: Red = Blue = 0.; break; case CYAN: Red = 0.; break; case BLUE: Red = Green = 0.; break; case MAGENTA: Green = 0.; break; case WHITE: break; case BLACK: Red = Green = Blue = 0.; break; default: fprintf( stderr, "Unknown color menu value: %d\n", value ); } glutSetWindow( GrWindow ); glutPostRedisplay();}/** ** process the main pop-up menu: **/voidDoMainMenu( int value ){ switch( value ) { case RESET: Reset(); break; case QUIT: Quit(); break; /* never returns -- "don't need to do this" */ default: fprintf( stderr, "Unknown main menu value: %d\n", value ); }}/** ** process the projection pop-up menu: **/voidDoProjMenu( int value ){ Projection = value; glutSetWindow( GrWindow ); glutPostRedisplay();}/** ** use glut to display a string of characters using a raster font: ** (raster fonts are made of pixels that get "rubber-stamped" onto the screen) **/voidDoRasterString( float x, float y, float z, char *s ){ char c; /* one character to print */ glRasterPos3f( x, y, z ); for( ; ( c = *s ) != '\0'; s++ ) { glutBitmapCharacter( GLUT_BITMAP_TIMES_ROMAN_24, c ); }}/** ** use glut to display a string of characters using a stroke font: ** (stroke fonts are made of 3D lines that get glBegin-glEnd'ed onto the screen) **/voidDoStrokeString( float x, float y, float z, float ht, char *s ){ char c; /* one character to print */ float sf; /* the scale factor */ glPushMatrix(); glTranslatef( x, y, z ); sf = ht / ( 119.05 + 33.33 ); glScalef( sf, sf, sf ); for( ; ( c = *s ) != '\0'; s++ ) { glutStrokeCharacter( GLUT_STROKE_ROMAN, c ); } glPopMatrix();}/** ** process the transform mode pop-up menu: **/voidDoTransformModeMenu( int value ){ TransformMode = value; glutSetWindow( GrWindow ); glutPostRedisplay();}/** ** initialize the glut and OpenGL libraries: ** also setup display lists and callback functions **/voidInitGraphics( void ){ /* setup the display mode: */ /* ( *must* be done before call to glutCreateWindow() ) */ glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH ); /* set the initial window configuration: */ glutInitWindowSize( WINDOW_SIZE, WINDOW_SIZE );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -