⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 游戏编程精髓数学部分代码和原程序不错的!不错!
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		float M[4][4] =		{			{ R0.x, R0.y, R0.z, 0 },			{ R1.x, R1.y, R1.z, 0 },			{ R2.x, R2.y, R2.z, 0 },			{ O.x, O.y, O.z, 1 },		};		glMultMatrixf( (float*)M );		glDrawElements( GL_TRIANGLES, 3*gBoatGeometry.FCount, GL_UNSIGNED_SHORT, gBoatGeometry.F );	}	glPopMatrix();	glDisableClientState( GL_NORMAL_ARRAY );	glDisableClientState( GL_VERTEX_ARRAY );}/////////////////////////////////////////////////////////////////////////////////////void display(){	//load camera	glMatrixMode( GL_MODELVIEW );	const VECTOR3 R0 = gCamera.R[0];	const VECTOR3 R1 = gCamera.R[2];//swap and negate to match	const VECTOR3 R2 = -gCamera.R[1];//OGL's camera convention	const VECTOR3& O = gCamera.O;//position	float M[4][4] =	{		{ R0.x, R1.x, R2.x, 0 },//X		{ R0.y, R1.y, R2.y, 0 },//Y		{ R0.z, R1.z, R2.z, 0 },//Z		{ -R0.dot(O), -R1.dot(O), -R2.dot(O), 1 }//translation	};	glLoadMatrixf( (float*)M );	float light_position[]	= { 10,10,10,1 };	glLightfv( GL_LIGHT0, GL_POSITION, light_position );	//clear frame buffer	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );	drawScene();	//flush and page flip	glutSwapBuffers();}/////////////////////////////////////////////////////////////////////////////////////////void initializeWater(){	long i,j;	//triangle vertices, normals, z-values	for( i=0 ; i<N ; i++ )	{		for( j=0 ; j<N ; j++ )		{			VECTOR3& v = V[i][j];			VECTOR3& n = VN[i][j];			//vertices, center of			//grid is at x=0, y=0			v.x = i*h - L/2;			v.y = j*h - L/2;			v.z = 0;			//normal			n.z = 2*h;			//later we will let the openGL			//driver normalize them for us			//z-values			z[i][j] = 0;			z1[i][j] = 0;			//damping coefficients			d[i][j] = 0.9999f;			//create a circular island in			//the center of the pool			const float xc = v.x - 1;			const float yc = v.y - 1;			if( (xc*xc + yc*yc) <= 10 )			{				d[i][j] = 0;			}		}	}	//normals at the edge of the	//grid point straight up	for( i=0 ; i<N ; i++ )	{		VN[i][0].z = 1;		VN[i][N-1].z = 1;	}	for( j=0 ; j<N ; j++ )	{		VN[0][j].z = 1;		VN[N-1][j].z = 1;	}	//triangle strip elements	for( i=0 ; i<N-1; i++ )	{		for( j=0 ; j<N; j++ )		{			Tri[i][j][0] = i*N + j;			Tri[i][j][1] = (i+1)*N + j;		}	}	//create a big splash	z[N/3][N/3] = z1[N/3][N/3] = 10;}/////////////////////////////////////////////////////////////////////////////////////void initializeBoat(){	//Physical Properties	gBoat = RIGID_BODY(	15000, VECTOR3(6,4,2) );	gBoat.position( -5, -5, 0 );	//Geometry	//allocate arrays	gBoatGeometry.FCount = 12;	gBoatGeometry.F = new Triangle[gBoatGeometry.FCount];//faces	gBoatGeometry.VCount = 8;	gBoatGeometry.V = new VECTOR3[gBoatGeometry.VCount];//vertices	gBoatGeometry.N = new VECTOR3[gBoatGeometry.VCount];//vertex normals	gBoatGeometry.dA = new float[gBoatGeometry.VCount];//area patches	//triangles	gBoatGeometry.F[0] = Triangle(0,1,2);	gBoatGeometry.F[1] = Triangle(0,2,3);	gBoatGeometry.F[2] = Triangle(4,5,6);	gBoatGeometry.F[3] = Triangle(4,6,7);	gBoatGeometry.F[4] = Triangle(4,7,1);	gBoatGeometry.F[5] = Triangle(4,1,0);	gBoatGeometry.F[6] = Triangle(7,6,2);	gBoatGeometry.F[7] = Triangle(7,2,1);	gBoatGeometry.F[8] = Triangle(6,5,3);	gBoatGeometry.F[9] = Triangle(6,3,2);	gBoatGeometry.F[10] = Triangle(5,4,0);	gBoatGeometry.F[11] = Triangle(5,0,3);	//vertices	gBoatGeometry.V[0] = VECTOR3( 3, 2, 1);	gBoatGeometry.V[1] = VECTOR3(-3, 2, 1);	gBoatGeometry.V[2] = VECTOR3(-3,-2, 1);	gBoatGeometry.V[3] = VECTOR3( 3,-2, 1);	gBoatGeometry.V[4] = VECTOR3( 3, 2,-1);	gBoatGeometry.V[5] = VECTOR3( 3,-2,-1);	gBoatGeometry.V[6] = VECTOR3(-3,-2,-1);	gBoatGeometry.V[7] = VECTOR3(-3, 2,-1);	//vertex normals	//(pointing outward through	//the vertices)	gBoatGeometry.N[0] = gBoatGeometry.V[0].unit();	gBoatGeometry.N[1] = gBoatGeometry.V[1].unit();	gBoatGeometry.N[2] = gBoatGeometry.V[2].unit();	gBoatGeometry.N[3] = gBoatGeometry.V[3].unit();	gBoatGeometry.N[4] = gBoatGeometry.V[4].unit();	gBoatGeometry.N[5] = gBoatGeometry.V[5].unit();	gBoatGeometry.N[6] = gBoatGeometry.V[6].unit();	gBoatGeometry.N[7] = gBoatGeometry.V[7].unit();	//area patches (1/8th of	//total surface area)	gBoatGeometry.dA[0] =	gBoatGeometry.dA[1] =	gBoatGeometry.dA[2] =	gBoatGeometry.dA[3] =	gBoatGeometry.dA[4] =	gBoatGeometry.dA[5] =	gBoatGeometry.dA[6] =	gBoatGeometry.dA[7] = 2*(6*4 + 6*2 + 4*2) / 8;}/////////////////////////////////////////////////////////////////////////////////////void initScene(){	//init lights	float black[]			= { 0,0,0,0 };	float white[]			= { 1,1,1,1 };	float amb_light[]		= { 0.1f,0.1f,0.1f,1 };	glShadeModel( GL_SMOOTH );	//global ambient light	glLightModelfv( GL_LIGHT_MODEL_AMBIENT, amb_light );	glMaterialfv( GL_FRONT, GL_AMBIENT, white );	glMaterialfv( GL_FRONT, GL_DIFFUSE, white );	//update material color with glColor()	glColorMaterial( GL_FRONT, GL_DIFFUSE );	glEnable( GL_COLOR_MATERIAL );	//light0 properties	glLightfv( GL_LIGHT0, GL_AMBIENT, amb_light );	glLightfv( GL_LIGHT0, GL_DIFFUSE, white );	glEnable( GL_LIGHTING );	glEnable( GL_LIGHT0 );	glEnable( GL_DEPTH_TEST );	glDepthFunc( GL_LEQUAL );	glDisable( GL_CULL_FACE );	//camera	gCamera.position( 13, -13, 13 );	gCamera.R[0] = VECTOR3( 0.31f, 0.95f, 0 );	gCamera.R[1] = VECTOR3( -0.80f, 0.26f, -0.54f);	gCamera.R[2] = VECTOR3( -0.51f, 0.16f, 0.84f );	//water grid	initializeWater();	//boat	initializeBoat();}/////////////////////////////////////////////////////////////////////////////////////void keyboard( unsigned char key, int x, int y ){	switch( key )	{	case 27:		exit(0);		break;	}}/////////////////////////////////////////////////////////////////////////////////////void special( int key, int x, int y ){	switch( key )	{	case GLUT_KEY_UP:		gCamera.translate( 0.1f * gCamera.R[1] );//forward		break;	case GLUT_KEY_DOWN:		gCamera.translate( -0.1f * gCamera.R[1] );//backward		break;	case GLUT_KEY_RIGHT:		gCamera.translate( 0.1f * gCamera.R[0] );//strafe right		break;	case GLUT_KEY_LEFT:		gCamera.translate( -0.1f * gCamera.R[0] );//strafe left		break;	}}int gx0, gy0;//for mouse movement/////////////////////////////////////////////////////////////////////////////////////void mouseMove( int x1, int y1 ){	const int dx = x1 - gx0;	const int dy = y1 - gy0;	//turn the camera left and right as the mouse moves	gCamera.rotateAboutX( -0.01f*dy );//look up/down	gCamera.rotate( -0.01f*VECTOR3(0,0,1)*dx );//turn right/left	//remember for next time	gx0 = x1;	gy0 = y1;}/////////////////////////////////////////////////////////////////////////////////////void mouseClick( int button, int state, int x1, int y1 ){	//catch button state	switch( state )	{	case GLUT_DOWN:		//reset so the rotation is continuous		gx0 = x1;		gy0 = y1;		switch( button )		{		case GLUT_LEFT_BUTTON:			break;		case GLUT_RIGHT_BUTTON:			break;		}		break;	case GLUT_UP:		switch( button )		{		case GLUT_LEFT_BUTTON:			break;		case GLUT_RIGHT_BUTTON:			break;		}		break;	}}/////////////////////////////////////////////////////////////////////////////////////void visible( int vis ){	if( vis == GLUT_VISIBLE )	{		glutIdleFunc( idle );	}	else	{		glutIdleFunc( NULL );	}}/////////////////////////////////////////////////////////////////////////////////////void reshape( int w, int h ){	const float D = gCamera.D;//near plane	const float F = gCamera.F;//far plane	const float Q = F / (F-D);	const float a = float(h) / w;//aspect ratio	glViewport( 0, 0, w, h );	glMatrixMode( GL_PROJECTION );	glLoadIdentity();	glFrustum( -D, D, -a*D, a*D, D, F );}/////////////////////////////////////////////////////////////////////////////////////////int main( int argc, char **argv ){	//initialize display mode	glutInit( &argc, argv );	glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL );	glutInitWindowSize( 800, 600 );	glutInitWindowPosition( 100, 100 );	glutCreateWindow( "Floating Boat" );	//setup callbacks	glutDisplayFunc( display );	glutKeyboardFunc( keyboard );	glutSpecialFunc( special );	glutMouseFunc( mouseClick );	glutMotionFunc( mouseMove );	glutReshapeFunc( reshape );	glutVisibilityFunc( visible );	//initialize the scene and objects	initScene();	glutMainLoop();	return 0;}//EOF

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -