///////////////////////////////////////////////////////
gltools.h
///////////////////////////////////////////////////////
#ifndef __GLTOOLS__
#define __GLTOOLS__
#include<windows.h>
#include<stdio.h>
#include"glew.h"
#include"glut.h"
#include"math.h"
#pragma comment( lib , "glew32.lib" )
GLubyte *gltLoadBMP( const char *szFileName , GLint *iWidth , GLint *iHeight , GLint *iComponents , GLenum *eFormat )
{
GLubyte *pbBmp = NULL;//返回值
tagBITMAPFILEHEADER bmpFileHeader;
tagBITMAPINFOHEADER bmpInfoHeader;
tagRGBQUAD bmpRGBQUAD;
int iRead = 0 , sDepth = 0;
FILE *rp = fopen( szFileName , "rb" );
if( NULL == rp )
{
MessageBox( NULL , L"无法打开文件!" , L"GL_Tools" , MB_OK );
return NULL;
}//if
iRead = fread( &bmpFileHeader , sizeof(tagBITMAPFILEHEADER) , 1 , rp );
iRead = fread( &bmpInfoHeader , sizeof(tagBITMAPINFOHEADER) , 1 , rp );
fseek( rp , bmpFileHeader.bfOffBits , SEEK_SET );
*iWidth = bmpInfoHeader.biWidth;
*iHeight = bmpInfoHeader.biHeight;
sDepth = bmpInfoHeader.biBitCount / 8;
if( 24 != bmpInfoHeader.biBitCount && 32 != bmpInfoHeader.biBitCount )
return NULL;
int biSizeImage = bmpInfoHeader.biHeight * ( ( bmpInfoHeader.biWidth * bmpInfoHeader.biBitCount / 8 + 3 ) / 4 ) * 4;
pbBmp = new GLubyte[ biSizeImage ];
if( NULL == pbBmp )
return NULL;
int ret = fread( pbBmp , 1 , biSizeImage , rp );
switch( sDepth )
{
case 3:
*eFormat = GL_BGR_EXT;
*iComponents = GL_RGB8;
break;
case 4:
*eFormat = GL_BGRA_EXT;
*iComponents = GL_RGBA8;
break;
}//switch
fclose( rp );
/*FILE *wp = fopen( "C:\\Documents and Settings\\iI0000002\\My Documents\\Visual Studio 2005\\Projects\\gl_3\\gl_3\\out.bmp" , "wb" );
fwrite( &bmpFileHeader , sizeof(tagBITMAPFILEHEADER) , 1 , wp );
fwrite( &bmpInfoHeader , sizeof(tagBITMAPINFOHEADER) , 1 , wp );
fwrite( pbBmp , bmpInfoHeader.biSizeImage , 1 , wp );
fclose( wp );*/
return pbBmp;
}
#endif
//////////////////////////////////////////////////
draw.h
//////////////////////////////////////////////////
#include"gltools.h"
#define SIZE_SPACE_X 200.0f
#define SIZE_SPACE_Y 90.0f
#define SIZE_SPACE_Z 200.0f
#define STEP_GROUND 25.0f
#define STEP_WALL 100.0f
#define STEP_SKY 5.0f
#define BUFFER_COUNT 7
#define VERTEX_DATA 0
#define TEXTURE_DATA_GROUND 1
#define TEXTURE_DATA_WALL 2
#define TEXTURE_DATA_SKY 3
#define INDEX_DATA_GROUND 4
#define INDEX_DATA_WALL 5
#define INDEX_DATA_SKY 6
GLfloat vertexSpace[ 24 ] = {
-SIZE_SPACE_X , 0 , SIZE_SPACE_Z ,
SIZE_SPACE_X , 0 , SIZE_SPACE_Z ,
SIZE_SPACE_X , 0 , -SIZE_SPACE_Z ,
-SIZE_SPACE_X , 0 , -SIZE_SPACE_Z ,
-SIZE_SPACE_X , SIZE_SPACE_Y , SIZE_SPACE_Z ,
SIZE_SPACE_X , SIZE_SPACE_Y , SIZE_SPACE_Z ,
SIZE_SPACE_X , SIZE_SPACE_Y , -SIZE_SPACE_Z ,
-SIZE_SPACE_X , SIZE_SPACE_Y , -SIZE_SPACE_Z
};
GLuint indexGround[ 4 ] = {
0 , 1 , 2 , 3 //下
};
GLfloat texCoordGround[ 8 ] = {//下
0 , 0 ,
2*SIZE_SPACE_X/STEP_GROUND , 0 ,
2*SIZE_SPACE_X/STEP_GROUND , 2*SIZE_SPACE_Z/STEP_GROUND ,
0 , 2*SIZE_SPACE_Z/STEP_GROUND
};
GLuint indexWall[ 16 ] = {
3 , 2 , 6 , 7 , //后
0 , 1 , 5 , 4 , //前
2 , 1 , 5 , 6 , //右
0 , 3 , 7 , 4 //左
};
GLfloat texCoordWall[ 16 ] = {
0 , 0 ,
2*SIZE_SPACE_X/STEP_WALL , 0 ,
0 , 0 ,
2*SIZE_SPACE_X/STEP_WALL , 0 ,
0 , 2*SIZE_SPACE_Y/STEP_WALL ,
2*SIZE_SPACE_X/STEP_WALL , 2*SIZE_SPACE_Y/STEP_WALL ,
0 , 2*SIZE_SPACE_Y/STEP_WALL ,
2*SIZE_SPACE_X/STEP_WALL , 2*SIZE_SPACE_Y/STEP_WALL
};
GLuint indexSky[ 4 ] = {
4 , 5 , 6 , 7 //上
};
GLfloat texCoordSky[ 16 ] = {//上
0 , 0 ,
0 , 0 ,
0 , 0 ,
0 , 0 ,
0 , 0 ,
1 , 0 ,
1 , 1 ,
0 , 1
};
GLuint vboSpace[ BUFFER_COUNT ] = { 0 };
#define TEXTURE_GROUND 0
#define TEXTURE_WALL 1
#define TEXTURE_SKY 2
#define TEXTURE_COUNT 3
GLuint texObj[ TEXTURE_COUNT ] = { 0 };
const char *szTexFiles[ TEXTURE_COUNT ] = {
"wood.bmp" ,
"wall2.bmp" ,
"sky2.bmp"
};
void DrawSpace()
{
glColor4f( 1 , 1 , 1 , 1 );
glPolygonMode( GL_FRONT_AND_BACK , GL_FILL );
//glBegin( GL_TRIANGLES );
// glVertex3f( 10 , 0 , 0 );
// glVertex3f( 0 , 10 , 0 );
// glVertex3f( 0 , 0 , 10 );
//glEnd();
glBindBuffer( GL_ARRAY_BUFFER , vboSpace[ VERTEX_DATA ] );
glVertexPointer( 3 , GL_FLOAT , 0 , 0 );
glBindBuffer( GL_ARRAY_BUFFER , vboSpace[ TEXTURE_DATA_GROUND ] );
glTexCoordPointer( 2 , GL_FLOAT , 0 , 0 );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER , vboSpace[ INDEX_DATA_GROUND ] );
glBindTexture( GL_TEXTURE_2D , texObj[ TEXTURE_GROUND ] );
glDrawElements( GL_QUADS , 4 , GL_UNSIGNED_INT , 0 );
glBindBuffer( GL_ARRAY_BUFFER , vboSpace[ TEXTURE_DATA_WALL ] );
glTexCoordPointer( 2 , GL_FLOAT , 0 , 0 );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER , vboSpace[ INDEX_DATA_WALL ] );
glBindTexture( GL_TEXTURE_2D , texObj[ TEXTURE_WALL ] );
glDrawElements( GL_QUADS , 16 , GL_UNSIGNED_INT , 0 );
glBindBuffer( GL_ARRAY_BUFFER , vboSpace[ TEXTURE_DATA_SKY ] );
glTexCoordPointer( 2 , GL_FLOAT , 0 , 0 );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER , vboSpace[ INDEX_DATA_SKY ] );
glBindTexture( GL_TEXTURE_2D , texObj[ TEXTURE_SKY ] );
glDrawElements( GL_QUADS , 4 , GL_UNSIGNED_INT , 0 );
}
//////////////////////////////////////////////
config.h
//////////////////////////////////////////////
#include"draw.h"
/////////////////////////////
#define START_X 0.0f
#define START_Y -30.0f
#define START_Z -30.0f
#define KEY_STEP 5.0f
/////////////////////////////
GLfloat speeed[ 4 ] = { 0 };
GLint iWin;
GLint mx=0 , my=0;
GLfloat yRot = 0;
GLint iList = 0;
GLint iCount = 0;
void Timer( int value )
{
printf( "%d fps\n" , iCount );
iCount = 0;
glutTimerFunc( 1000 , Timer , 0 );
}
void Display()
{
++iCount;
GLfloat lastMatrix[16];
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();
glMatrixMode( GL_MODELVIEW );
glGetFloatv( GL_MODELVIEW_MATRIX , lastMatrix );
glLoadIdentity();
glRotatef( yRot , 1 , 0 , 0 );
glMultMatrixf( lastMatrix );
DrawSpace();
glPopMatrix();
glutSwapBuffers();
}
void Reshape( int w , int h )
{
GLfloat fAspect;
if( 0 == h ) h = 1;
glViewport( 0 , 0 , w , h );
fAspect = (GLfloat)w / (GLfloat)h;
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 60 , fAspect , 1 , 800 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( 0 , START_Y , START_Z );
glutPostRedisplay();
}
void Motion( int x , int y )
{
GLfloat lastMatrix[ 16 ];
glMatrixMode( GL_MODELVIEW );
glGetFloatv( GL_MODELVIEW_MATRIX , lastMatrix );
glLoadIdentity();
yRot += (y - my) / 5;
if( yRot >= 90 )
yRot = 90;
else if( yRot <= -90 )
yRot = -90;
glRotatef( (GLfloat)(x-mx) , 0.0f , 1.0f , 0.0f );
glMultMatrixf(lastMatrix);
glutPostRedisplay();
mx = x;
my = y;
}
void Mouse( int button , int state , int x , int y )
{
if( GLUT_LEFT_BUTTON == button )
{
if( GLUT_DOWN == state )
{
mx = x;
my = y;
}//if
}//if
}
void Keyboard( unsigned char key , int x , int y )
{
GLfloat lastMatrix[16] = {0};
glMatrixMode( GL_MODELVIEW );
glGetFloatv( GL_MODELVIEW_MATRIX , lastMatrix );
glLoadIdentity();
switch( key )
{
case 'w':
glTranslatef( 0 , 0 , KEY_STEP );
break;
case 'a':
glTranslatef( KEY_STEP , 0 , 0 );
break;
case 's':
glTranslatef( 0 , 0 , -KEY_STEP );
break;
case 'd':
glTranslatef( -KEY_STEP , 0 , 0 );
break;
case 'f':
glutFullScreen();
glutSetCursor( GLUT_CURSOR_NONE );
break;
case 'q':
glutSetCursor( GLUT_CURSOR_LEFT_ARROW );
glutReshapeWindow( 800 , 600 );
glutPositionWindow( 200 , 100 );
default:
break;
}//switch
glMultMatrixf( lastMatrix );
glutPostRedisplay();
}
static GLfloat stepSlowdown[3] = { 0 };
static GLuint slowdownFlag = 0;
void SlowdownTimer( int value )
{
slowdownFlag = 1;
if( 0 == stepSlowdown[0] && 0 == stepSlowdown[1] && 0 == stepSlowdown[2] )
{
slowdownFlag = 0;
return;
}//if
GLfloat lastMatrix[16] = {0};
glMatrixMode( GL_MODELVIEW );
glGetFloatv( GL_MODELVIEW_MATRIX , lastMatrix );
glLoadIdentity();
glTranslatef( stepSlowdown[0] , stepSlowdown[1] , stepSlowdown[2] );
glMultMatrixf( lastMatrix );
glutPostRedisplay();
if( 0 < stepSlowdown[0] )
{
stepSlowdown[0] -= 0.5;
}//if
else if( 0 > stepSlowdown[0] )
{
stepSlowdown[0] += 0.5;
}
if( 0.001 > stepSlowdown[0] && -0.001 < stepSlowdown[0])
stepSlowdown[0] = 0;
if( 0 < stepSlowdown[2] )
{
stepSlowdown[2] -= 0.5;
}//if
else if( 0 > stepSlowdown[2] )
{
stepSlowdown[2] += 0.5;
}
if( 0.001 > stepSlowdown[2] && -0.001 < stepSlowdown[2])
stepSlowdown[2] = 0;
glutTimerFunc( 30 , SlowdownTimer , 0 );
}
void KeyboardUp( unsigned char key , int x , int y )
{
switch( key )
{
case 'w':
stepSlowdown[2] = KEY_STEP;
if( 0 == slowdownFlag )
{
glutTimerFunc( 1 , SlowdownTimer , 0 );
}//if
break;
case 'a':
stepSlowdown[0] = KEY_STEP;
if( 0 == slowdownFlag )
{
glutTimerFunc( 1 , SlowdownTimer , 0 );
}//if
break;
case 's':
stepSlowdown[2] = -KEY_STEP;
if( 0 == slowdownFlag )
{
glutTimerFunc( 1 , SlowdownTimer , 0 );
}//if
break;
case 'd':
stepSlowdown[0] = -KEY_STEP;
if( 0 == slowdownFlag )
{
glutTimerFunc( 1 , SlowdownTimer , 0 );
}//if
break;
case 'f':
glutFullScreen();
glutSetCursor( GLUT_CURSOR_NONE );
break;
case 'q':
glutSetCursor( GLUT_CURSOR_LEFT_ARROW );
glutReshapeWindow( 800 , 600 );
glutPositionWindow( 200 , 100 );
//glutDestroyWindow( iWin );
break;
default:
break;
}//switch
}
////////////////////////////////////////////////
main.cpp
////////////////////////////////////////////////
#include"gltools.h"
#include"config.h"
void Init()
{
glEnable( GL_DEPTH_TEST );
glClearColor( 0 , 0 , 0 , 0 );
//////////////////////////////////////
//纹理初始化
GLubyte *pBytes;
GLint iWidth , iHeight , iComponents;
GLenum eFormat;
glEnable( GL_TEXTURE_2D );
glTexEnvi( GL_TEXTURE_ENV , GL_TEXTURE_ENV_MODE , GL_REPLACE );
glGenTextures( TEXTURE_COUNT , texObj );
for( int i=0; i<TEXTURE_COUNT; ++i )
{
glBindTexture( GL_TEXTURE_2D , texObj[ i ] );
pBytes = gltLoadBMP( szTexFiles[ i ] , &iWidth , &iHeight , &iComponents , &eFormat );
glTexImage2D( GL_TEXTURE_2D , 0 , iComponents , iWidth , iHeight , 0 , eFormat , GL_UNSIGNED_BYTE , pBytes );
glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_WRAP_S , GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_WRAP_T , GL_REPEAT );
delete[] pBytes;
}//for
//////////////////////////////////////
//VBO初始化
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glGenBuffers( BUFFER_COUNT , vboSpace );
glBindBuffer( GL_ARRAY_BUFFER , vboSpace[ VERTEX_DATA ] );
glBufferData( GL_ARRAY_BUFFER , sizeof(GLfloat)*24 , vertexSpace , GL_STATIC_DRAW );
glBindBuffer( GL_ARRAY_BUFFER , vboSpace[ TEXTURE_DATA_GROUND ] );
glBufferData( GL_ARRAY_BUFFER , sizeof(GLfloat)*8 , texCoordGround , GL_STATIC_DRAW );
glBindBuffer( GL_ARRAY_BUFFER , vboSpace[ TEXTURE_DATA_WALL ] );
glBufferData( GL_ARRAY_BUFFER , sizeof(GLfloat)*16 , texCoordWall , GL_STATIC_DRAW );
glBindBuffer( GL_ARRAY_BUFFER , vboSpace[ TEXTURE_DATA_SKY ] );
glBufferData( GL_ARRAY_BUFFER , sizeof(GLfloat)*16 , texCoordSky , GL_STATIC_DRAW );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER , vboSpace[ INDEX_DATA_GROUND ] );
glBufferData( GL_ELEMENT_ARRAY_BUFFER , sizeof(GLuint)*4 , indexGround , GL_STATIC_DRAW );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER , vboSpace[ INDEX_DATA_WALL ] );
glBufferData( GL_ELEMENT_ARRAY_BUFFER , sizeof(GLuint)*16 , indexWall , GL_STATIC_DRAW );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER , vboSpace[ INDEX_DATA_SKY ] );
glBufferData( GL_ELEMENT_ARRAY_BUFFER , sizeof(GLuint)*4 , indexSky , GL_STATIC_DRAW );
//////////////////////////////////////
}
void Exit()
{
glDeleteTextures( TEXTURE_COUNT , texObj );
glDeleteBuffers( BUFFER_COUNT , vboSpace );
}
int main( int argc , char** argv )
{
glutInit( &argc , argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE );
glutInitWindowSize( 800 , 600 );
glutInitWindowPosition( 200 , 100 );
glutCreateWindow( "Tom" );
iWin = glutGetWindow();
glewInit();
glutDisplayFunc( Display );
glutReshapeFunc( Reshape );
glutMouseFunc( Mouse );
glutMotionFunc( Motion );
glutKeyboardFunc( Keyboard );
glutKeyboardUpFunc( KeyboardUp );
//glutTimerFunc( 1000 , Timer , 0 );
Init();
glutMainLoop();
Exit();
return 0;
}
#include"glut.h"
#include"math.h"
#define RADIUS 500
#define PI 3.141592654f
#define R 10.0f
#define Y_GROUND -3.0f
#define F_EXTENT 70.0f
#define F_STEP 5.0f
GLfloat Rot = 0.0f;
GLint mx = 0 , my = 0;
GLint mz = -RADIUS;
GLfloat keyStep = 0.5f;
GLfloat lightPos[] = { -20 , 30 , -10 , 1 };
GLfloat spotDir[] = { 50 , -200 , 50 };
GLfloat specular[] = { 1 , 1 , 1 , 1 };
GLfloat specref[] = { 1 , 1 , 1 , 1 };
GLfloat ambientLight[] = { 0.5 , 0.5 , 0.5 , 1 };
//void MyTimer( int value )
//{
// if( abs(Rot) >= 60 )
// step *= -1;
// Rot += step;
// glutPostRedisplay();
// glutTimerFunc( 100 , MyTimer , 0 );
//}
void init()
{
glEnable( GL_DEPTH_TEST );
glEnable( GL_BLEND );
/*glFrontFace( GL_CCW );
glEnable( GL_CULL_FACE );*/
glEnable( GL_LIGHTING );
glLightModelfv( GL_LIGHT_MODEL_AMBIENT , ambientLight );//定义一个全局光源,让所有物体都能够被看见
glLightfv( GL_LIGHT0 , GL_DIFFUSE , ambientLight );
glLightfv( GL_LIGHT0 , GL_SPECULAR , specular );
glLightfv( GL_LIGHT0 , GL_POSITION , lightPos );
//glLightfv( GL_LIGHT0 , GL_SPOT_DIRECTION , spotDir );
//glLightf( GL_LIGHT0 , GL_SPOT_CUTOFF , 90.0f );//光源点的cutoff角
glEnable( GL_LIGHT0 );
glEnable( GL_COLOR_MATERIAL );
glColorMaterial( GL_FRONT , GL_AMBIENT_AND_DIFFUSE );
glMaterialfv( GL_FRONT , GL_SPECULAR , specular );
glMateriali( GL_FRONT , GL_SHININESS , 180 );
glClearColor( 0.0f , 0.0f , 0.0f , 0.0f );
}
void DrawGround()
{
GLint iLine;
glColor4f( 0.5 , 0.5 , 0.5 , 0.6 );
glPolygonMode( GL_FRONT_AND_BACK , GL_FILL );
glBegin( GL_QUADS );
glVertex3f( -F_EXTENT , Y_GROUND-0.05 , -F_EXTENT );
glVertex3f( -F_EXTENT , Y_GROUND-0.05 , F_EXTENT );
glVertex3f( F_EXTENT , Y_GROUND-0.05 , F_EXTENT );
glVertex3f( F_EXTENT , Y_GROUND-0.05 , -F_EXTENT );
glEnd();
glColor4f( 1.0 , 1.0 , 1.0 , 0.6 );
glPolygonMode( GL_FRONT_AND_BACK , GL_LINE );
glBegin( GL_LINES );
for( iLine = -F_EXTENT; iLine <= F_EXTENT; iLine += F_STEP )
{
glVertex3f( iLine , Y_GROUND , F_EXTENT );
glVertex3f( iLine , Y_GROUND , -F_EXTENT );
glVertex3f( F_EXTENT , Y_GROUND , iLine );
glVertex3f( -F_EXTENT , Y_GROUND , iLine );
}//for
glEnd();
}
void Display()
{
GLfloat x1 , y1 , x2 , y2 , z , r , angle;
GLfloat xx1 , zz1 , xx2 , zz2;
glEnable( GL_DEPTH_TEST );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
//加入fog效果
GLfloat fBackColor[] = { 0.0f , 0.0f , 0.0f , 0.0f };
glEnable( GL_FOG );
glFogfv( GL_FOG_COLOR , fBackColor );
glFogf( GL_FOG_START , 5.0f );
glFogf( GL_FOG_END , 70.0f );
glFogi( GL_FOG_MODE , GL_LINEAR );
glPushMatrix();
glColor4f( 0.2 , 0.5 , 0.8 , 0.0 );
for( angle = 0; angle <= 2*PI; angle += (PI/18) )
{
glPolygonMode( GL_FRONT , GL_FILL );
glBegin( GL_TRIANGLE_STRIP );
for( z = -R; z <= R; z += 0.5 )
{
r = sqrt( R*R - z*z );
x1 = r*cos(angle);
y1 = r*sin(angle)+7;
x2 = r*cos(angle+(PI/18));
y2 = r*sin(angle+(PI/18))+7;
glVertex3f( x1 , -y1-7 , z );
glVertex3f( x2 , -y2-7 , z );
}//for
glEnd();
}//for
glDisable( GL_LIGHTING );
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA );
DrawGround();
glDisable( GL_BLEND );
glEnable( GL_LIGHTING );
glPopMatrix();
glPushMatrix();
//glLoadIdentity();
glLightfv( GL_LIGHT0 , GL_POSITION , lightPos );
glLightfv( GL_LIGHT0 , GL_SPOT_DIRECTION , spotDir );
glTranslatef( lightPos[0] , lightPos[1] , lightPos[2] );
glPushAttrib( GL_LIGHTING_BIT );
glDisable( GL_LIGHTING );
glColor3ub( 255 , 255 , 0 );
glPolygonMode( GL_FRONT , GL_FILL );
glutSolidSphere( 3.0f , 15 , 15 );
glPopAttrib();
glPopMatrix();
//glPolygonMode( GL_FRONT , GL_FILL );
for( angle = 0; angle <= 2*PI; angle += (PI/18) )
{
glPolygonMode( GL_FRONT , GL_FILL );
glColor3f( 0.2 , 0.5 , 0.8 );
glBegin( GL_TRIANGLE_STRIP );
for( z = -R; z <= R; z += 0.5 )
{
r = sqrt( R*R - z*z );
x1 = r*cos(angle);
y1 = r*sin(angle)+7;
x2 = r*cos(angle+(PI/18));
y2 = r*sin(angle+(PI/18))+7;
glVertex3f( x1 , y1 , z );
glVertex3f( x2 , y2 , z );
}//for
glEnd();
//画影子
glColor3f( 0.0 , 0.0 , 0.0 );
glDisable( GL_LIGHTING );
glBegin( GL_TRIANGLE_STRIP );
for( z = -R; z <= R; z += 0.5 )
{
r = sqrt( R*R - z*z );
x1 = r*cos(angle);
y1 = r*sin(angle)+7;
x2 = r*cos(angle+(PI/18));
y2 = r*sin(angle+(PI/18))+7;
xx1 = ( (x1-lightPos[0])/(y1-lightPos[1]) ) * ( Y_GROUND-lightPos[1] ) + x1;
zz1 = ( (z-lightPos[0])/(y1-lightPos[1]) ) * ( Y_GROUND-lightPos[1] ) + z;
xx2 = ( (x2-lightPos[0])/(y2-lightPos[1]) ) * ( Y_GROUND-lightPos[1] ) + x2;
zz2 = ( (z-lightPos[0])/(y2-lightPos[1]) ) * ( Y_GROUND-lightPos[1] ) + z;
glVertex3f( xx1 , Y_GROUND , zz1 );
glVertex3f( xx2 , Y_GROUND , zz2 );
}//for
glEnd();
glEnable( GL_LIGHTING );
}//for
/*glBegin( GL_POINTS );*/
//GLfloat i , j , k;
//for( i=0; i<255; i+=3 )
// for( j=0; j<255; j+=3 )
// for( k=0; k<255; k+=3 )
// {
// glColor3ub( i , j , k );
// glVertex3f( i/50 , j/50 , k/50-10 );
// }//for
/*glEnd();*/
glutSwapBuffers();
}
void Resize( int w , int h )
{
GLfloat fAspect;
if( 0 == h ) h = 1;
glViewport( 0 , 0 , w ,h );
fAspect = (GLfloat)w / (GLfloat)h;
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 60.0f , fAspect , 1.0 , 400.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( 0 , 0 , -30 );
::glutPostRedisplay();
}
void Key( unsigned char k , int x , int y )
{
GLfloat lastMatrix[ 16 ];
glMatrixMode( GL_MODELVIEW );
glGetFloatv( GL_MODELVIEW_MATRIX , lastMatrix );
glLoadIdentity();
switch( k )
{
case 'w':
glTranslatef( 0.0f , 0.0f , keyStep );
break;
case 'a':
glTranslatef( keyStep , 0.0f , 0.0f );
break;
case 's':
glTranslatef( 0.0f , 0.0f , -keyStep );
break;
case 'd':
glTranslatef( -keyStep , 0.0f , 0.0f );
break;
default:
break;
}//switch
glMultMatrixf( lastMatrix );
glutPostRedisplay();
}
void Motion( int x , int y )
{
GLfloat lastMatrix[ 16 ];
glMatrixMode(GL_MODELVIEW);
glGetFloatv(GL_MODELVIEW_MATRIX,lastMatrix);
glLoadIdentity();
glRotatef( (GLfloat)(y-my)/10 , 1.0f , 0.0f , 0.0f );
glRotatef( (GLfloat)(x-mx) , 0.0f , 1.0f , 0.0f );
glMultMatrixf(lastMatrix);
glutPostRedisplay();
mx = x;
my = y;
}
void Mouse( int button , int state , int x , int y )
{
if( GLUT_LEFT_BUTTON == button )
{
if( GLUT_DOWN == state )
{
mx = x;
my = y;
}//if
}//if
}
int main( int argc , char** argv )
{
glutInit( &argc , argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE );
glutInitWindowSize( 800 , 600 );
glutInitWindowPosition( 200 , 100 );
glutCreateWindow( "Tom" );
init();
glutDisplayFunc( Display );
glutReshapeFunc( Resize );
glutKeyboardFunc( Key );
glutMotionFunc( Motion );
glutMouseFunc( Mouse );
//glutTimerFunc( 100 , MyTimer , 0 );
glutMainLoop();
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#pragma comment( lib , "kernel32.lib" )
#define PIPE_NAME_SERVER_READ L"\\\\.\\Pipe\\Read"
#define PIPE_NAME_SERVER_WRITE L"\\\\.\\Pipe\\Write"
::HANDLE qqPipeHandleRead , qqPipeHandleWrite;
::DWORD WINAPI procRead( ::LPVOID lpParameter )
{
::printf( "Client reading is running!\n" );
::DWORD BytesRead;
::CHAR Buffer[ 1024 ];
while(1)
{
::printf( "Client reading is running!\n" );
if( ::ReadFile( qqPipeHandleRead , Buffer , sizeof(Buffer) , &BytesRead , NULL ) > 0 )
{
Buffer[ BytesRead ] = '\0';
::printf( "%s\t\t%d\n" , Buffer , BytesRead );
continue;
}//if
}//while
return 0;
}
::DWORD WINAPI procWrite( ::LPVOID lpParameter )
{
::printf( "Client writing is running!\n" );
::DWORD BytesWritten;
::CHAR Buffer[ 1024 ];
int count = 0;
while(1)
{
::printf( "Client writing is running!\n" );
::scanf( "%s" , Buffer );
count = 0;
for( int i=0; Buffer[i] != '\0' ; ++i )
++count;
if( ::WriteFile( qqPipeHandleWrite , Buffer , count , &BytesWritten , NULL ) == 0 )
{
::printf( "WirteFile failed with error %d !\n" , ::GetLastError() );
continue;
}//if
}//while
return 0;
}
int main()
{
::HANDLE hRead , hWrite;
::DWORD idRead , idWrite;
if( ::WaitNamedPipe( PIPE_NAME_SERVER_WRITE , NMPWAIT_WAIT_FOREVER ) == 0 )
{
::printf( "WaitNamedPipe failed with error %d !" , ::GetLastError() );
return -1;
}//if
if( ::WaitNamedPipe( PIPE_NAME_SERVER_READ , NMPWAIT_WAIT_FOREVER ) == 0 )
{
::printf( "WaitNamedPipe failed with error %d !" , ::GetLastError() );
return -1;
}//if
qqPipeHandleRead = ::CreateFile( PIPE_NAME_SERVER_WRITE , GENERIC_READ | GENERIC_WRITE , 0 , (LPSECURITY_ATTRIBUTES)NULL ,
OPEN_EXISTING , FILE_ATTRIBUTE_NORMAL , (HANDLE)NULL );
if( qqPipeHandleRead == INVALID_HANDLE_VALUE )
{
::printf( "CreateFile failed with error %d !" , ::GetLastError() );
return -1;
}//if
qqPipeHandleWrite = ::CreateFile( PIPE_NAME_SERVER_READ , GENERIC_READ | GENERIC_WRITE , 0 , (LPSECURITY_ATTRIBUTES)NULL ,
OPEN_EXISTING , FILE_ATTRIBUTE_NORMAL , (HANDLE)NULL );
if( qqPipeHandleWrite == INVALID_HANDLE_VALUE )
{
::printf( "CreateFile failed with error %d !" , ::GetLastError() );
return -1;
}//if
::printf( "Client is running!\n" );
hRead = ::CreateThread( NULL , 0 , procRead , NULL , 0 , &idRead );
if( hRead == NULL )
{
::printf( "CreateThread failed with error %d !\n" , ::GetLastError() );
return -1;
}//if
hWrite = ::CreateThread( NULL , 0 , procWrite , NULL , 0 , &idWrite );
if( hWrite == NULL )
{
::printf( "CreateThread failed with error %d !\n" , ::GetLastError() );
return -1;
}//if
while(1)
{
}//while
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#pragma comment( lib , "kernel32.lib" )
#define PIPE_NUM 10
#define PIPE_NAME_SERVER_READ L"\\\\.\\Pipe\\Read"
#define PIPE_NAME_SERVER_WRITE L"\\\\.\\Pipe\\Write"
::HANDLE qqPipeHandleRead , qqPipeHandleWrite;
::DWORD WINAPI procRead( ::LPVOID lpParameter )
{
::printf( "Server reading is running!\n" );
::DWORD BytesRead;
::CHAR Buffer[ 1024 ];
while(1)
{
::printf( "Server reading is running!\n" );
if( ::ReadFile( qqPipeHandleRead , Buffer , sizeof(Buffer) , &BytesRead , NULL ) > 0 )
{
Buffer[ BytesRead ] = '\0';
::printf( "%s\t\t%d\n" , Buffer , BytesRead );
continue;
}//if
}//while
return 0;
}
::DWORD WINAPI procWrite( ::LPVOID lpParameter )
{
::printf( "Server writing is running!\n" );
::DWORD BytesWritten;
::CHAR Buffer[ 1024 ];
int count = 0;
while(1)
{
::printf( "Server writing is running!\n" );
::scanf( "%s" , Buffer );
count = 0;
for( int i=0; Buffer[i] != '\0' ; ++i )
++count;
if( ::WriteFile( qqPipeHandleWrite , Buffer , count , &BytesWritten , NULL ) == 0 )
{
::printf( "WirteFile failed with error %d !\n" , ::GetLastError() );
continue;
}//if
}//while
return 0;
}
int main()
{
::HANDLE hRead , hWrite;
::DWORD idRead , idWrite;
qqPipeHandleRead = ::CreateNamedPipe( PIPE_NAME_SERVER_READ ,
PIPE_ACCESS_DUPLEX ,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE ,
PIPE_NUM , 0 , 0 , 1000 , NULL );
if( INVALID_HANDLE_VALUE == qqPipeHandleRead )
{
::printf( "CreateNamedPipeW failed with error %d !\n" , ::GetLastError() );
return -1;
}//if
qqPipeHandleWrite = ::CreateNamedPipe( PIPE_NAME_SERVER_WRITE ,
PIPE_ACCESS_DUPLEX ,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE ,
PIPE_NUM , 0 , 0 , 1000 , NULL );
if( INVALID_HANDLE_VALUE == qqPipeHandleWrite )
{
::printf( "CreateNamedPipeW failed with error %d !\n" , ::GetLastError() );
return -1;
}//if
::printf( "Server is running!\n" );
hRead = ::CreateThread( NULL , 0 , procRead , NULL , 0 , &idRead );
if( hRead == NULL )
{
::printf( "CreateThread failed with error %d !\n" , ::GetLastError() );
return -1;
}//if
hWrite = ::CreateThread( NULL , 0 , procWrite , NULL , 0 , &idWrite );
if( hWrite == NULL )
{
::printf( "CreateThread failed with error %d !\n" , ::GetLastError() );
return -1;
}//if
if( ::ConnectNamedPipe( qqPipeHandleRead , NULL ) == 0 )
{
::printf( "ConnectNamedPipe failed with error %d !\n" , ::GetLastError() );
}//if
if( ::ConnectNamedPipe( qqPipeHandleWrite , NULL ) == 0 )
{
::printf( "ConnectNamedPipe failed with error %d !\n" , ::GetLastError() );
}//if
while(1)
{
}//while
return 0;
}