lab4.cpp
上传用户:zflamp
上传日期:2008-10-15
资源大小:662k
文件大小:13k
- // lab4.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- using namespace std;
- // 窗口大小
- GLsizei winWidth = 600, winHeight = 500;
- // 观察点及裁剪窗口等
- // GLfloat eyex = 0, eyey = 100, eyez = 70;
- // GLfloat xref = 0.0, yref = 100, zref = 0;
- // GLfloat Vx = 0, Vy = 1, Vz = 0;
- // GLfloat xwMin = -1600, ywMin = -90, xwMax = 160, ywMax = 90;
- GLfloat dnear =1, dfar = 100;
- // 光照参数
- GLfloat light_position[] = { 200.0, 200.0, 100.0, 1.0 };
- GLfloat ambientLight[] = { 0.3, 0.3, 0.3, 1.0 };
- GLfloat diffuseLight[] = { 0.7, 0.7, 0.7, 1.0 };
- GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
- GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- GLuint f[4];
- GLuint tobjects;
- GLUquadricObj *cylinder;
- BYTE *pBytes;
- int nWidth, nHeight;
- int rotTheta = 30;
- int state = 1;
- int angle = 45;
- float xang = 0, yang = 0, zang = 0;
- //////////////////////////////////////////////////////////////////////////
- // 读取bmp文件 (24-bit 彩色bmp)
- BYTE * gltResourceBMPBits( UINT nResource, int *nWidth, int *nHeight )
- {
- HINSTANCE hInstance; // 初始化句柄
- HANDLE hBitmap; // bmp文件句柄
- BITMAPINFO bmInfo;
- BYTE *pData;
- // 打开bmp文件
- hInstance = GetModuleHandle( NULL );
- hBitmap = LoadBitmap( hInstance, MAKEINTRESOURCE( nResource ) );
- if( hBitmap == NULL )
- {
- return NULL;
- }
- GetObject( hBitmap, sizeof( BITMAPINFO ), &bmInfo );
- DeleteObject( hBitmap );
- hBitmap = LoadResource( hInstance, FindResource( hInstance, MAKEINTRESOURCE( nResource ), RT_BITMAP ) );
- if( hBitmap == NULL )
- {
- return NULL;
- }
- pData = ( BYTE * )LockResource( hBitmap );
- pData += sizeof( BITMAPINFO )-1;
- *nWidth = bmInfo.bmiHeader.biWidth;
- *nHeight = bmInfo.bmiHeader.biHeight;
- return pData;
- }
- //////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////
- void init( void )
- {
- //光照
- glLightfv( GL_LIGHT0, GL_POSITION, light_position );
- glLightfv( GL_LIGHT0, GL_AMBIENT, ambientLight );
- glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuseLight);
- glLightfv( GL_LIGHT0, GL_SPECULAR, specular );
- glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE );
- glEnable( GL_LIGHTING );
- //深度
- glDepthFunc( GL_LEQUAL );
- glEnable( GL_DEPTH_TEST );
- glEnable( GL_CULL_FACE );
- //表面
- glEnable( GL_COLOR_MATERIAL );
- glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE );
- glMaterialfv( GL_FRONT_AND_BACK, GL_SPECULAR, specref );
- //纹理
- glEnable( GL_TEXTURE_2D );
- glGenTextures( 1, &tobjects );
- pBytes = gltResourceBMPBits( IDB_BITMAP1, &nWidth, &nHeight );
- glBindTexture( GL_TEXTURE_2D, tobjects );
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
- glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
- gluBuild2DMipmaps( GL_TEXTURE_2D, 3, nWidth, nHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, pBytes );
- // glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, nWidth, nHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, pBytes );
- glClearColor( 0, 0, 0, 1.0f );
- glColor3ub( 255,255,255 );
- // gluLookAt( eyex, eyey, eyez, xref, yref, zref, Vx, Vy, Vz );
- f[0] = glGenLists(1);
- // 底座
- glNewList( f[0], GL_COMPILE );
- glEnable( GL_TEXTURE_2D );
- // 后向面
- glNormal3f( 0, 0, -1 );
- glBegin( GL_QUADS );
- glTexCoord2f( 0.0, 0.0 );
- glVertex3f( -200.0, 0.0, -25.0 );
- glTexCoord2f( 1.0, 0.0 );
- glVertex3f( -200.0, 10.0, -25.0 );
- glTexCoord2f( 1.0, 1.0 );
- glVertex3f( 200.0, 10.0, -25.0 );
- glTexCoord2f( 0.0, 1.0 );
- glVertex3f( 200.0, 0.0, -25.0 );
- glEnd();
-
- // 右侧面
- glNormal3f( 1, 0, 0 );
- glBegin( GL_QUADS );
- glTexCoord2f( 0.0, 0.0 );
- glVertex3f( 200.0, 0.0, -25.0 );
- glTexCoord2f( 0.0, 1.0 );
- glVertex3f( 200.0, 10.0, -25.0 );
- glTexCoord2f( 1.0, 1.0 );
- glVertex3f( 200.0, 10.0, 25.0 );
- glTexCoord2f( 1.0, 0.0 );
- glVertex3f( 200.0, 0.0, 25.0 );
- glEnd();
-
- // 左侧面
- glNormal3f( -1, 0, 0 );
- glBegin( GL_QUADS );
- glTexCoord2f( 0.0, 0.0 );
- glVertex3f( -200.0, 10.0, -25.0 );
- glTexCoord2f( 0.0, 1.0 );
- glVertex3f( -200.0, 0.0, -25.0 );
- glTexCoord2f( 1.0, 1.0 );
- glVertex3f( -200.0, 0.0, 25.0 );
- glTexCoord2f( 1.0, 0.0 );
- glVertex3f( -200.0, 10.0, 25.0 );
- glEnd();
-
- // 前侧面
- glNormal3f( 0, 0, 1 );
- glBegin( GL_QUADS );
- glTexCoord2f( 0.0, 0.0 );
- glVertex3f( -200.0, 10.0, 25.0 );
- glTexCoord2f( 0.0, 1.0 );
- glVertex3f( -200.0, 0.0, 25.0 );
- glTexCoord2f( 1.0, 1.0 );
- glVertex3f( 200.0, 0.0, 25.0 );
- glTexCoord2f( 1.0, 0.0 );
- glVertex3f( 200.0, 10.0, 25.0 );
- glEnd();
-
- // 上侧面
- glNormal3f( 0, 1, 0 );
- glBegin( GL_QUADS );
- glTexCoord2f( 0.0, 0.0 );
- glVertex3f( -200.0, 10.0, -25.0 );
- glTexCoord2f( 0.0, 1.0 );
- glVertex3f( -200.0,10.0, 25.0 );
- glTexCoord2f( 1.0, 1.0 );
- glVertex3f( 200.0, 10.0, 25.0 );
- glTexCoord2f( 1.0, 0.0 );
- glVertex3f( 200.0, 10.0, -25.0 );
- glEnd();
-
- // 下侧面
- glNormal3f( 0, -1, 0 );
- glBegin( GL_QUADS );
- glTexCoord2f( 0.0, 0.0 );
- glVertex3f( -200.0, 0.0, 25.0 );
- glTexCoord2f( 0.0, 1.0 );
- glVertex3f( -200.0, 0.0, -25.0 );
- glTexCoord2f( 1.0, 1.0 );
- glVertex3f( 200.0, 0.0, -25.0 );
- glTexCoord2f( 1.0, 0.0 );
- glVertex3f( 200.0, 0.0, 25.0 );
- glEnd();
- glEndList();
- f[1] = glGenLists(1);
- // 侧柱
- glNewList( f[1], GL_COMPILE );
- glEnable( GL_TEXTURE_2D );
- // 右侧面
- glNormal3f( 1, 0, 0 );
- glBegin( GL_QUADS );
- glTexCoord2f( 0.0, 0.0 );
- glVertex3f( 5.0, 250.0, 5.0 );
- glTexCoord2f( 0.0, 1.0 );
- glVertex3f( 5.0, 0.0, 5.0 );
- glTexCoord2f( 1.0, 1.0 );
- glVertex3f( 5.0, 0.0, -5.0 );
- glTexCoord2f( 1.0, 0.0 );
- glVertex3f( 5.0, 250.0, -5.0 );
- glEnd();
-
- // 左侧面
- glNormal3f( -1, 0, 0 );
- glBegin( GL_QUADS );
- glTexCoord2f( 0.0, 0.0 );
- glVertex3f( -5.0, 0.0, 5.0 );
- glTexCoord2f( 0.0, 1.0 );
- glVertex3f( -5.0, 250.0, 5.0 );
- glTexCoord2f( 1.0, 1.0 );
- glVertex3f( -5.0, 250.0, -5.0 );
- glTexCoord2f( 1.0, 0.0 );
- glVertex3f( -5.0, 0.0, -5.0 );
- glEnd();
- // 前侧面
- glNormal3f( 0, 0, 1 );
- glBegin( GL_QUADS );
- glTexCoord2f( 0.0, 0.0 );
- glVertex3f( -5.0, 250.0, 5.0 );
- glTexCoord2f( 0.0, 1.0 );
- glVertex3f( -5.0, 0.0, 5.0 );
- glTexCoord2f( 1.0, 1.0 );
- glVertex3f( 5.0, 0.0, 5.0 );
- glTexCoord2f( 1.0, 0.0 );
- glVertex3f( 5.0, 250.0, 5.0 );
- glEnd();
- // 后侧面
- glNormal3f( 0, 0, -1 );
- glBegin( GL_QUADS );
- glTexCoord2f( 0.0, 0.0 );
- glVertex3f( -5.0, 0.0, -5.0 );
- glTexCoord2f( 0.0, 1.0 );
- glVertex3f( -5.0, 250.0, -5.0 );
- glTexCoord2f( 1.0, 1.0 );
- glVertex3f( 5.0, 250.0, -5.0 );
- glTexCoord2f( 1.0, 0.0 );
- glVertex3f( 5.0, 0.0, -5.0 );
- glEnd();
- glEndList();
- f[2] = glGenLists(1);
- // 顶柱
- glNewList( f[2], GL_COMPILE );
- glEnable( GL_TEXTURE_2D );
- // 前侧面
- glNormal3f( 0, 0, 1 );
- glBegin( GL_QUADS );
- glTexCoord2f( 0.0, 0.0 );
- glVertex3f( 200.0, 0.0, 5.0 );
- glTexCoord2f( 0.0, 1.0 );
- glVertex3f( 200.0, 10.0, 5.0 );
- glTexCoord2f( 1.0, 1.0 );
- glVertex3f( -200.0, 10.0, 5.0 );
- glTexCoord2f( 1.0, 0.0 );
- glVertex3f( -200.0, 0.0, 5.0 );
- glEnd();
-
- glNormal3f( 0, 0, -1 );
- glBegin( GL_QUADS );
- glTexCoord2f( 0.0, 0.0 );
- glVertex3f( 200.0, 10.0, -5.0 );
- glTexCoord2f( 0.0, 1.0 );
- glVertex3f( 200.0, 0.0, -5.0 );
- glTexCoord2f( 1.0, 1.0 );
- glVertex3f( -200.0, 0.0, -5.0 );
- glTexCoord2f( 1.0, 0.0 );
- glVertex3f( -200.0, 10.0, -5.0 );
- glEnd();
-
- glNormal3f( 0, -1, 0 );
- glBegin( GL_QUADS );
- glTexCoord2f( 0.0, 0.0 );
- glVertex3f( 190.0, 0.0, -5.0 );
- glTexCoord2f( 0.0, 1.0 );
- glVertex3f( 190.0, 0.0, 5.0 );
- glTexCoord2f( 1.0, 1.0 );
- glVertex3f( -190.0, 0.0, 5.0 );
- glTexCoord2f( 1.0, 0.0 );
- glVertex3f( -190.0, 0.0, -5.0 );
- glEnd();
-
- glNormal3f( 0, 1, 0 );
- glBegin( GL_QUADS );
- glTexCoord2f( 0.0, 0.0 );
- glVertex3f( 200.0, 10.0, 5.0 );
- glTexCoord2f( 0.0, 1.0 );
- glVertex3f( 200.0, 10.0, -5.0 );
- glTexCoord2f( 1.0, 1.0 );
- glVertex3f( -200.0, 10.0, -5.0 );
- glTexCoord2f( 1.0, 0.0 );
- glVertex3f( -200.0, 10.0, 5.0 );
- glEnd();
- glEndList();
- f[3] = glGenLists(1);
- glNewList( f[3], GL_COMPILE );
- glPushMatrix();
- glDisable( GL_TEXTURE_2D );
- glRotatef( -82.67, 1, 0, 0 );
- glColor3f( 1.0, 1.0, 1.0 );
- cylinder = gluNewQuadric();
- gluQuadricDrawStyle( cylinder, GLU_FILL );
- // gluQuadricNormals( cylinder, GL_SMOOTH );
- gluCylinder( cylinder, 1, 1, 147, 5, 142 );
- glEnable( GL_TEXTURE_2D );
- glPopMatrix();
- glPushMatrix();
- glDisable( GL_TEXTURE_2D );
- glRotatef( 262.67, 1, 0, 0 );
- glColor3f( 1.0, 1.0, 1.0 );
- cylinder = gluNewQuadric();
- gluQuadricDrawStyle( cylinder, GLU_FILL );
- gluCylinder( cylinder, 1, 1, 142, 5, 142 );
- glEnable( GL_TEXTURE_2D );
- glPopMatrix();
- glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, 128.0 );
- glDisable( GL_TEXTURE_2D );
- glColor3f( 1.0, 0, 0 );
- glutSolidSphere( 17, 50, 50 );
- glEnable( GL_TEXTURE_2D );
- glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, 0 );
- glColor3f( 1.0, 1.0, 1.0 );
- glEndList();
- }
- void displayFcn( void )
- {
- glClear( GL_COLOR_BUFFER_BIT );
- glClear( GL_DEPTH_BUFFER_BIT );
- glEnable( GL_LIGHT0 );
- glEnable( GL_TEXTURE_2D );
- glMaterialf( GL_FRONT_AND_BACK, GL_SHININESS, 0.0 );
- // 底座
- glPushMatrix();
- glCallList( f[0] );
- glPopMatrix();
- // 侧柱
- glPushMatrix();
- glTranslatef( 195.0, 0.0, 20.0 );
- glCallList( f[1] );
- glPopMatrix();
- glPushMatrix();
- glTranslatef( 195.0, 0.0, -20.0 );
- glCallList( f[1] );
- glPopMatrix();
- glPushMatrix();
- glTranslatef( -195.0, 0.0, -20.0 );
- glCallList( f[1] );
- glPopMatrix();
- glPushMatrix();
- glTranslatef( -195.0, 0.0, 20.0 );
- glCallList( f[1] );
- glPopMatrix();
- // 顶柱
- glPushMatrix();
- glTranslatef( 0.0, 240.0, 20.0 );
- glCallList( f[2] );
- glPopMatrix();
- glPushMatrix();
- glTranslatef( 0.0, 240.0, -20.0 );
- glCallList( f[2] );
- glPopMatrix();
- // 圆球
- glPushMatrix();
- glTranslatef( -17.0, 100.0, 0.0 );
- glCallList( f[3] );
- glPopMatrix();
-
- glPushMatrix();
- glTranslatef( 17.0, 100.0, 0.0 );
- glCallList( f[3] );
- glPopMatrix();
-
- if ( rotTheta>=0 )
- {
- glPushMatrix();
- glTranslatef( 51, 240, 0.0 );
- glRotatef( rotTheta, 0, 0, 1 );
- glTranslatef( 0, -140, 0 );
- glCallList( f[3] );
- glPopMatrix();
-
- glPushMatrix();
- glTranslatef( -51, 100, 0.0 );
- glCallList( f[3] );
- glPopMatrix();
- }
- else
- {
- glPushMatrix();
- glTranslatef( 51, 100, 0.0 );
- glCallList( f[3] );
- glPopMatrix();
-
- glPushMatrix();
- glTranslatef( -51, 240, 0 );
- glRotatef( rotTheta, 0, 0, 1 );
- glTranslatef( 0, -140, 0 );
- glCallList( f[3] );
- glPopMatrix();
- }
- glEnable( GL_LIGHT0 );
- glutSwapBuffers();
- glFlush();
- }
- void winReshapeFcn( GLint newWidth, GLint newHeight )
- {
- glViewport( 0, 0, newWidth, newHeight );
- winWidth = newWidth;
- winHeight = newHeight;
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- // glFrustum( xwMin, xwMax, ywMin, ywMax, dnear, dfar );
- gluPerspective( angle, (float)winWidth / (float)winHeight, 1, 1000 );
- // glOrtho( xwMin, xwMax, ywMin, ywMax, dnear, dfar );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glTranslatef( 0, -100, -450 );
- glRotatef( yang, 0, 1, 0 );
- glClear( GL_COLOR_BUFFER_BIT );
- glClear( GL_DEPTH_BUFFER_BIT );
- }
- void play( void )
- {
- for ( long i=0; i<rotTheta * rotTheta * 20000; i++ );
- if ( state )
- {
- rotTheta -= 1;
- }
- else
- {
- rotTheta += 1;
- }
- if ( rotTheta > 30 )
- {
- state = 1;
- rotTheta = 29;
- }
- if ( rotTheta < -30 )
- {
- state = 0;
- rotTheta = -29;
- }
- glutPostRedisplay();
- }
- void specialKeyFcn( GLint specialKey, GLint xMouse, GLint yMouse )
- {
- if ( specialKey==GLUT_KEY_UP )
- {
- angle -= 1;
- if ( angle<10 )
- {
- angle = 10;
- }
- }
- if ( specialKey==GLUT_KEY_DOWN )
- {
- angle += 1;
- if ( angle > 170 )
- {
- angle = 170;
- }
- }
- if ( specialKey==GLUT_KEY_LEFT )
- {
- yang -=1;
- if ( yang<0 )
- {
- yang += 360;
- }
- }
- if ( specialKey==GLUT_KEY_RIGHT )
- {
- yang += 1;
- if ( yang > 360 )
- {
- yang += 360;
- }
- }
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( angle, (float)winWidth / (float)winHeight, 1, 1000 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glTranslatef( 0, -100, -450 );
- glRotatef( yang, 0, 1, 0 );
- glClear( GL_COLOR_BUFFER_BIT );
- glClear( GL_DEPTH_BUFFER_BIT );
- glutIdleFunc( play );
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- glutInit( &argc, argv );
- glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
- glutInitWindowPosition( 100, 100 );
- glutInitWindowSize( winWidth, winHeight );
- glutCreateWindow( "ex4" );
- init();
- glutDisplayFunc( displayFcn );
- glutReshapeFunc( winReshapeFcn );
- // glutMouseFunc( play );
- glutIdleFunc( play );
- glutSpecialFunc( specialKeyFcn );
- glutMainLoop();
- return 0;
- }