CodeForge QQ客服 CodeForge 400电话 客服电话 4006316121

main.cpp ( 文件浏览 )

文件源自:opengl喷泉代码
  • OrangeCHHC 发布于2014-06-10
  • 浏览次数:278
  • 下载次数:7
  • 下载需 3 积分
  • 侵权举报
			/**********************************************************************

  Foutain + Water Simulation

  May, 7th, 2003

  This opengl sample was written by Philipp Crocoll
  Contact: 
	philipp.crocoll@web.de
	www.codecolony.de

  Every comment would be appreciated.

  If you want to use parts of any code of mine:
	let me know and
	use it!

***********************************************************************

Keys to use
===========

  1-7    : Re-initialize scene with another shape of the fountain

  w,s	 : move camera forward/backward
  a,d    : turn camera right/left
  r,f    : move camera up/down
  x,y	 : turn camera up/down
  c,v	 : strafe left/right

  Esc	 : Exit



How does it work?
=================

The classes CCamera and COGLTexture are described in tutorials on 
  www.codecolony.de/OpenGL

The files vectors.h/.cpp provide help with vector maths.

The class CPool was made from my SwimmingPool example (also on CodeColony).

The class CAirFountain was made from my Fountain Tutorial (online, too).

Changes in those classes:
 -> Fountain gets a "Pool Pointer". Each time a drop falls into the water
    (y < 0) I search for the pool's oscillator which is closest to the 
	drops position. This oscillator is put down a bit (y-value is decreased).
 ->	When a fountain has many drops, a whole pool area is often put so strongly 
    down that it is below the bowl and becomes invisible! This is unrealistic 
	anyway, so the "AffectOscillator" method does not allow to put an oscillator
	too deep. The corresponding line of code should be replaced when using
	the pool for other purposes.
 -> The pool has a kind of damping (which is not physically correct). Otherwise
    the waves would become too strong after a while.

The method "RenderBowl" is not very interesting - it simply renders a bowl!

***********************************************************************/

#include <GL\glut.h>

#include <math.h>
#include <time.h>
#include <stdlib.h>
#include "stdio.h"

#include "pool.h"
#include "AirFountain.h"
#include "camera.h"  //This is my old camera, but it's easier to control 
					 //for the user and the third rotation axis is not required here
#include "textures.h"

#include "mmsystem.h"				//导入声音头的文件 
#pragma comment(lib,"winmm.lib")	//导入声音头的文件库 

//lighting:
GLfloat LightAmbient[]=		{
 0.2f, 0.2f, 0.2f, 0.0f 
};
GLfloat LightDiffuse[]=		{
 0.8f, 0.8f, 0.8f, 0.0f 
};
GLfloat LightPosition[]=	{
 1.0f, -0.5f, -0.5f, 0.0f 
};


//Constants:
#define NUM_X_OSCILLATORS		170
#define NUM_Z_OSCILLATORS		170
#define OSCILLATOR_DISTANCE		0.015
#define OSCILLATOR_WEIGHT       0.0001

#define MAXX					(NUM_X_OSCILLATORS*OSCILLATOR_DISTANCE)
#define MAXZ					(NUM_Z_OSCILLATORS*OSCILLATOR_DISTANCE)

#define POOL_HEIGHT				0.3
GLuint g_texture[1];


//Camera object:
CCamera Camera;

//The "pool" which represents the water within the fountain bowl
CPool Pool;

//water outside the bowl is in the air:
CAirFountain AirFountain;

//Textures:
COGLTexture WaterTexture;  //the image does not contain a water texture, 
						   //but it is applied to the water
COGLTexture RockTexture;
COGLTexture GroundTexture;//**********
COGLTexture SkyTexture;


bool  g_bRain		   = true;	
bool  g_bFillModePoints = true;
bool  g_bLighting		= true;


void KeyDown(unsigned char key, int x, int y)
{
	
	switch(key)
	{

	case 27:	//ESC
		exit(0);
		break;
	case 'a':		
		Camera.RotateY(5.0f);
		break;
	case 'd':		
		Camera.RotateY(-5.0f);
		break;
	case 'w':		
		Camera.MoveForwards(-0.15f ) ;
		break;
	case 's':		
		Camera.MoveForwards( 0.15f ) ;
		break;
	case 'x':		
		Camera.RotateX(5.0f);
		break;
	case 'y':		
		Camera.RotateX(-5.0f);
		break;
	case 'c':		
		Camera.StrafeRight(-0.05f);
		break;
	case 'v':		
		Camera.StrafeRight(0.05f);
		break;
	case 'f':
		Camera.Move(F3dVector(0.0,-0.1,0.0));
		break;
	case 'r':
		Camera.Move(F3dVector(0.0,0.1,0.0));
		break;

	//*************************************
	//Several initialization calls:
	case '1':
		Pool.Reset();
		AirFountain.Delete();
		AirFountain.Initialize(4,100,45,76,90,0.2,0.11);
		break;
	case '2':
		Pool.Reset();
		AirFountain.Delete();
		AirFountain.Initialize(1,20,100,70,70,5.0,0.15);
		break;
	case '3':
		Pool.Reset();
		AirFountain.Initialize(1,20,200,85,85,10,0.1);
		break;
	case '4':
		Pool.Reset();
		AirFountain.Initialize(5,20,85,90,90,1.0,0.15);
		break;
	case '5':
		Pool.Reset();
		AirFountain.Initialize(2,20,50,40,70,1.5,0.2);
		break;
	case '6':
		Pool.Reset();
		AirFountain.Initialize(3,50,25,76,90,0.2,0.11);
		break;
	case '7':
		Pool.Reset();
		AirFountain.Initialize(3,8,35,76,90,0.5,0.11);
		break;

	
	
}

}


AUX_RGBImageRec *LoadBMP(char * Filename) // 载入位图图象 
{
 
	FILE *File=NULL;
	if (!Filename) 
		return NULL; 
	File=fopen(Filename,"r");
	if (File) 
	{
 
		fclose(File); 
		return auxDIBImageLoad(Filename);  
	
}  
	return NULL;

}  



// 载入位图(调用上面的代码)并转换成纹理 
int LoadGLTextures(GLuint& unTexture, const char* chFileName)                
{

	int Status=FALSE; // Status 状态指示器  
    AUX_RGBImageRec *TextureImage;										//保存贴图数据的指针
	char* file_name =(char*) chFileName;
	if(  TextureImage = LoadBMP(file_name) )					//载入贴图数据
	{
 
		Status =1;
		glGenTextures(1, &unTexture);										// 创建一个纹理,unTexture
		glBindTexture(GL_TEXTURE_2D, unTexture);							//绑定纹理,然后对该纹理区添加纹理数据
		// 生成纹理 
		glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage->sizeX, TextureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);		//设置滤波为线性滤波
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);		//线性滤波
		if (TextureImage)													//释放资源
		{

			if (TextureImage->data)
			{

				free(TextureImage->data);
			
}
			free(TextureImage);
		
}
	
}
    return Status;

}



void PlayMusic() 
{
 
	char buf[128]; 
	char str[128] = {
0 
}; 
	int i = 0; 
	
	
	MCI_OPEN_PARMS mciOpen; 
	MCIERROR mciError; 
	mciOpen.lpstrDeviceType = "mpegvideo"; 
	mciOpen.lpstrElementName = "Because Of You.wav"; 
	//使用 mciSendCommand 函数
	mciError = mciSendCommand(0,MCI_OPEN,MCI_OPEN_TYPE | MCI_OPEN_ELEMENT,(DWORD)&mciOpen); 
	if(mciError) {
 
		mciGetErrorString(mciError,buf,128); 
		printf("发送 MCI_OPEN command failed:%s\n",buf); 
		return; 
	
} 
	UINT DeviceID = mciOpen.wDeviceID ; 
	MCI_PLAY_PARMS mciPlay; 
	
	mciError = mciSendCommand(DeviceID,MCI_PLAY,0 ,(DWORD)&mciPlay); 
	if(mciError) {
 
		printf("send MCI_PLAY command failed\n"); 
		return; 
	
} 

}


void RenderBowl(void)
{

	float bowlheight = 0.2 + POOL_HEIGHT;
	float bowlwidth = 0.2;

	float TexBorderDistance = bowlwidth / (MAXX+2*bowlwidth);
	GroundTexture.SetActive();////////////////////////////////////////////////////////////
//	SkyTexture.SetActive();
	glBegin(GL_QUADS);

		float minX = -4.0;
		float minZ = -4.0;
		float maxX = 8.0;
		float maxZ = 8.0;
		//******************
		//ground
		//******************
		glNormal3f(0.0f,1.0f,0.0);
		glTexCoord2f(0.0,0.0);
		glVertex3f(minX,0.0,minZ);
		glTexCoord2f(1.0,0.0);
		glVertex3f(maxX,0.0,minZ);
		glTexCoord2f(1.0,1.0);
		glVertex3f(maxX,0.0,maxZ);
		glTexCoord2f(0.0,1.0);
		glVertex3f(minX,0.0,maxZ);
	glEnd();
		
	RockTexture.SetActive();////////////////////////////////////////////////////////////////////////////

	glBegin(GL_QUADS);
	
		//******************
		//top
		//******************
		glNormal3f(0.0f,1.0f,0.0);

		glTexCoord2f(TexBorderDistance,TexBorderDistance);
		glVertex3f(0.0f,bowlheight,0.0);
		glTexCoord2f(1.0-TexBorderDistance,TexBorderDistance);
		glVertex3f(MAXX,bowlheight,0.0);
		glTexCoord2f(1.0-TexBorderDistance,0.0);
		glVertex3f(MAXX,bowlheight,-bowlwidth);
		glTexCoord2f(TexBorderDistance,0.0);
		glVertex3f(0.0f,bowlheight,-bowlwidth);

		glTexCoord2f(TexBorderDistance,0.0);
		glVertex3f(0.0f,bowlheight,-bowlwidth);
		glTexCoord2f(0.0,0.0);
		glVertex3f(-bowlwidth,bowlheight,-bowlwidth);
		glTexCoord2f(0.0,1.0-TexBorderDistance);
		glVertex3f(-bowlwidth,bowlheight,MAXZ);		
		glTexCoord2f(TexBorderDistance,1.0-TexBorderDistance);
		glVertex3f(0.0f,bowlheight,MAXZ);
		
		glTexCoord2f(1.0,0.0);
		glVertex3f(MAXX+bowlwidth,bowlheight,-bowlwidth);
		glTexCoord2f(1.0-TexBorderDistance,0.0);
		glVertex3f(MAXX,bowlheight,-bowlwidth);
		glTexCoord2f(1.0-TexBorderDistance,1.0-TexBorderDistance);
		glVertex3f(MAXX,bowlheight,MAXZ);
		glTexCoord2f(1.0,1.0-TexBorderDistance);
		glVertex3f(MAXX+bowlwidth,bowlheight,MAXZ);		

		glTexCoord2f(1.0,1.0-TexBorderDistance);
		glVertex3f(MAXX+bowlwidth,bowlheight,MAXZ);
		glTexCoord2f(0.0,1.0-TexBorderDistance);
		glVertex3f(-bowlwidth,bowlheight,MAXZ);
		glTexCoord2f(0.0,1.0);
		glVertex3f(-bowlwidth,bowlheight,MAXZ+bowlwidth);		
		glTexCoord2f(1.0,1.0);
		glVertex3f(MAXX+bowlwidth,bowlheight,MAXZ+bowlwidth);
		
		//******************
		//front
		//******************
		glNormal3f(0.0f,0.0f,1.0f);

		glTexCoord2f(TexBorderDistance,TexBorderDistance);
		glVertex3f(0.0f,bowlheight,0.0);
		glTexCoord2f(1.0-TexBorderDistance,TexBorderDistance);
		glVertex3f(MAXX,bowlheight,0.0);
		glTexCoord2f(1.0-TexBorderDistance,0.0);
		glVertex3f(MAXX,0.0f,0.0);
		glTexCoord2f(TexBorderDistance,0.0);
		glVertex3f(0.0f,0.0f,0.0);


		glTexCoord2f(0.0,1.0-TexBorderDistance);
		glVertex3f(-bowlwidth,bowlheight,MAXZ+bowlwidth);
		glTexCoord2f(1.0,1.0-TexBorderDistance);
		glVertex3f(MAXX+bowlwidth,bowlheight,MAXZ+bowlwidth);
		glTexCoord2f(1.0,1.0);
		glVertex3f(MAXX+bowlwidth,0.0f,MAXZ+bowlwidth);
		glTexCoord2f(0.0,1.0);
		glVertex3f(-bowlwidth,0.0f,MAXZ+bowlwidth);


		//******************
		//back
		//******************
		glNormal3f(0.0,0.0,-1.0f);

		glTexCoord2f(TexBorderDistance,TexBorderDistance);
		glVertex3f(0.0f,bowlheight,MAXZ);
		glTexCoord2f(1.0-TexBorderDistance,TexBorder
...
...
(文件超长,未完全显示,请下载后阅读剩余部分)
			
...
展开> <收缩

下载源码到电脑,阅读使用更方便

3 积分

快速下载
还剩0行未阅读,继续阅读
云测速

源码文件列表

温馨提示: 点击源码文件名可预览文件内容哦 ^_^
...
名称 大小 修改日期
AirFountain.cpp4.71 kB08-05-03|10:55
AirFountain.h1.11 kB08-05-03|10:21
Because0.00 B27871648|71%
camera.cpp1.88 kB07-05-03|09:41
camera.h1.24 kB09-06-14|19:54
AirFountain.obj11.87 kB18-11-13|21:50
AirFountain.sbr0.00 B18-11-13|21:50
camera.obj25.64 kB18-11-13|21:50
camera.sbr0.00 B18-11-13|21:50
Desktop_.ini8.00 B06-02-07|09:21
FountainWater.bsc4.75 MB10-06-14|01:01
FountainWater.exe348.09 kB10-06-14|01:00
FountainWater.ilk721.05 kB10-06-14|01:00
FountainWater.pch4.50 MB09-06-14|20:48
FountainWater.pdb1.00 MB10-06-14|01:00
main.obj40.61 kB10-06-14|01:00
main.sbr0.00 B10-06-14|01:01
pool.obj32.55 kB09-06-14|20:46
pool.sbr0.00 B09-06-14|20:46
textures.obj13.02 kB18-11-13|21:50
textures.sbr0.00 B18-11-13|21:50
vc60.idb321.00 kB10-06-14|12:25
vc60.pdb148.00 kB10-06-14|01:00
vectors.obj8.91 kB18-11-13|21:50
vectors.sbr0.00 B18-11-13|21:50
Desktop_.ini8.00 B06-02-07|09:21
FountainWater.dsp4.67 kB04-06-09|08:44
FountainWater.dsw549.00 B07-05-03|00:28
FountainWater.exe84.00 kB08-05-03|10:57
FountainWater.htm2.63 kB06-02-07|12:24
FountainWater.ncb177.00 kB10-06-14|12:26
FountainWater.opt151.50 kB10-06-14|12:26
FountainWater.plg1.54 kB10-06-14|01:01
GLglut.h20.39 kB11-11-13|09:22
glut.dll166.00 kB18-08-98|16:25
glut.h20.94 kB18-08-98|16:24
glut.lib77.79 kB18-08-98|16:25
glut32.dll166.00 kB18-08-98|16:25
glut32.lib78.03 kB18-08-98|16:25
ground.bmp192.05 kB07-05-03|10:59
main.cpp17.44 kB10-06-14|01:00
pool.cpp7.82 kB09-06-14|20:46
pool.h855.00 B09-06-14|19:54
rock.bmp192.05 kB08-05-03|11:00
sky.bmp468.80 kB28-06-13|22:51
textures.cpp921.00 B07-05-03|10:55
textures.h257.00 B16-06-00|16:31
vectors.cpp1.61 kB23-02-03|14:06
Vectors.h904.00 B09-06-14|19:54
water.bmp192.05 kB07-05-03|10:49
Debug0.00 B10-06-14|01:00
OpenGL喷泉0.00 B10-06-14|12:26
云测速

main.cpp (29.68 MB)

需要 3 积分
您持有 积分

CodeForge积分(原CF币)全新升级,功能更强大,使用更便捷,不仅可以用来下载海量源代码马上还可兑换精美小礼品了 了解更多

您的积分不足

支付宝优惠套餐快速获取 30 积分

订单支付完成后,积分将自动加入到您的账号。以下是优惠期的人民币价格,优惠期过后将恢复美元价格。

更多付款方式:网银PayPal

上传代码,免费获取

您本次下载所消耗的积分将转交上传作者。

同一源码,30天内重复下载,只扣除一次积分。

登录 CodeForge

还没有CodeForge账号? 立即注册
关注微博
联系客服

Switch to the English version?

Yes
CodeForge 英文版
No
CodeForge 中文版

完善个人资料,获价值¥30元积分奖励!

^_^"呃 ...

Sorry!这位大神很神秘,未开通博客呢,请浏览一下其他的吧
好的