Example #1
0
void        Init(GLContext    *_context)
{
	_context->userObject = new   UserData();
	UserData      *_user = (UserData *)_context->userObject;
	_user->object = GLProgram::createWithFile("shader/sun/sun.vsh", "shader/sun/sun.fsh");
	_user->mvpLoc = _user->object->getUniformLocation("u_mvpMatrix");
	_user->mvLoc = _user->object->getUniformLocation("u_mvMatrix");
	_user->baseMapLoc = _user->object->getUniformLocation("u_baseMap");
	_user->lightColorLoc = _user->object->getUniformLocation("u_lightColor");

	TGAImage       _earthMap("tga/Earth512x256.tga");
	_user->earthMapId = _earthMap.genTextureMap();

	TGAImage       _moonMap("tga/Moon256x128.tga");
	_user->moonMapId = _moonMap.genTextureMap();

	_user->earthAngle = 0.0f;
	_user->moonAngle = 0.0f;

	_user->mSphere = GLSphere::createWithSlice(256, 0.6f);
	//
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);
//	glEnable(GL_BLEND);
	//	return    true;
	esMatrixLookAt(&_user->viewMatrix, &GLVector3(0.0f,0.0f,0.0f),&GLVector3(0.0f,0.0f,-5.0f),&GLVector3(0.0f,1.0f,0.0f));
}
Example #2
0
void        Init(GLContext    *_context)
{
	UserData	*_user = new   UserData();
	_context->userObject = _user;
//
	_user->object = GLProgram::createWithFile("shader/normal/normal.vsh", "shader/normal/normal.fsh");
	_user->u_mvpMatrixLoc = _user->object->getUniformLocation("u_mvpMatrix");
	_user->u_baseMapLoc = _user->object->getUniformLocation("u_baseMap");
	_user->u_normalMapLoc = _user->object->getUniformLocation("u_normalMap");
	_user->u_lightVectorLoc = _user->object->getUniformLocation("u_lightVector");
	_user->u_normalMatrixLoc = _user->object->getUniformLocation("u_normalMatrix");
//TGAImage
//	TGAImage		_baseMap("tga/normal/IceMoon.tga");
//	_user->baseMapId = _baseMap.genTextureMap();
	_user->baseMapId = 0;
	_user->baseTexture = GLTexture::createWithFile("tga/normal/IceMoon.tga");
	_user->baseTexture->genMipmap();
//Normal Texture
//	TGAImage	    _normalMap("tga/normal/IceMoonBump.tga");
//	_user->normalMapId = _normalMap.genTextureMap();
	_user->normalMapId = 0;
	_user->normalTexture = GLTexture::createWithFile("tga/normal/IceMoonBump.tga");
	_user->normalTexture->genMipmap();
//Sphere
	_user->vSphere = GLSphere::createWithSlice(128, 0.6f);
//
	_user->lightVector = normalize(&GLVector3(1.0f,1.0f,1.0f));
//
	_user->angleArc = 0.0f;
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	glEnable(GL_CULL_FACE);
}
void  ChoppySimulate::initProgram()
{
	_choppyProgram = GLProgram::createWithFile("shader/wave/choppy.vsh", "shader/wave/choppy.fsh");
	_modelMatrixLoc = _choppyProgram->getUniformLocation("u_modelMatrix");
	_viewProjMatrixLoc = _choppyProgram->getUniformLocation("u_viewProjMatrix");
	_normalMatrixLoc = _choppyProgram->getUniformLocation("u_normalMatrix");
	_skyboxTexLoc = _choppyProgram->getUniformLocation("u_skyboxTexCube");
	_eyePositionLoc = _choppyProgram->getUniformLocation("u_eyePosition");
	_freshnelParamLoc = _choppyProgram->getUniformLocation("u_freshnelParam");
	_refractRatioLoc = _choppyProgram->getUniformLocation("u_refractRatio");
	_waterColorLoc = _choppyProgram->getUniformLocation("u_waterColor");
//设置默认的值
	_modelMatrix.rotate(-90.0f, 1.0f, 0.0f, 0.0f);
	_modelMatrix.translate(0.0f,0.0f,-WAVE_SIZE);
	_normalMatrix = _modelMatrix.normalMatrix();
//视图,透视矩阵
	_eyePosition = GLVector3(0.0f,WAVE_SIZE/1.5f,0.0f);
	_viewProjMatrix.lookAt(_eyePosition, GLVector3(0.0f,0.0f,-WAVE_SIZE/1.2f),GLVector3(0.0f,1.0f,0.0f));
	Size   _size = GLContext::getInstance()->getWinSize();
	_viewProjMatrix.perspective(60.0f, _size.width/_size.height,0.1f,500.0f);
//天空盒
	//const   char   *skyboxFile[6] = {
	//	"tga/water/choppy_sky/stormydays_rt.tga", //+X
	//	"tga/water/choppy_sky/stormydays_lf.tga",//-X
	//	"tga/water/choppy_sky/stormydays_up.tga",//+Y
	//	"tga/water/choppy_sky/stormydays_dn.tga",//-Y
	//	"tga/water/choppy_sky/stormydays_ft.tga",//+Z
	//	"tga/water/choppy_sky/stormydays_bk.tga",//-Z
	//};
	const   char   *skyboxFile[6] = {
		"tga/water/sky/xpos.bmp", //+X
		"tga/water/sky/xneg.bmp",//-X
		"tga/water/sky/ypos.bmp",//+Y
		"tga/water/sky/yneg.bmp",//-Y
		"tga/water/sky/zpos.bmp",//+Z
		"tga/water/sky/zneg.bmp",//-Z
	};
	_skyboxTex = GLCubeMap::createWithFiles(skyboxFile);
//freshnel参数
	_freshnelParam = GLVector3(0.12f, 0.88f, 2.0f);
	_waterColor = GLVector4(0.7f, 0.78f, 0.91f, 1.0f);
//光线的折射系数
	_refractRatio = 1.0f / 1.33f;
//Mesh
	_meshWave = Mesh::createWithIntensity(WAVE_SIZE, WAVE_SIZE, WAVE_SIZE, 1.0f);
}
Example #4
0
GLVector3 GLMatrix::GetScale() const
{
   // Get local axis scaling factors
   GLVector3 x(fVals[0], fVals[1], fVals[2]);
   GLVector3 y(fVals[4], fVals[5], fVals[6]);
   GLVector3 z(fVals[8], fVals[9], fVals[10]);
   return GLVector3(x.Mag(), y.Mag(), z.Mag());
}
Example #5
0
void GLCamera::SetCenterVec(double x, double y, double z)
{
  // Set camera center vector.

  GLMatrix bt = fCamBase*fCamTrans;
  fCamBase.SetBaseVec(4, GLVector3(x, y, z));

  GLMatrix binv = fCamBase; binv.Invert();
  fCamTrans = binv*bt;
}
Example #6
0
void        Init(GLContext    *_context)
{
	UserData	*_user = new   UserData();
	_context->userObject = _user;
    
	_user->_lightRender = new   LightShadowRender();
	_user->_groundShape = Mesh::createWithIntensity(4, 1.0f, 1.0f, 1.0f);
	_user->_sphereShape = Sphere::createWithSlice(128, 1.0f);
	_user->_groundTexture = GLTexture::createWithFile("tga/map/ground.tga");
	_user->_sphereTexture = GLTexture::createWithFile("tga/Earth512x256.tga");
//	_user->_testSprite = Sprite::createWithFile("tga/global_bg_big.tga");
//关于光源的矩阵
	_user->_lightEyePosition = GLVector3(3.0,15.0,-10.0);
	_user->_lightViewPosition= GLVector3(0.0f,0.0f,-10.0f);
	_user->_lightViewMatrix.lookAt(_user->_lightEyePosition, _user->_lightViewPosition, GLVector3(0.0f, 1.0, 0.0f));
//设置关于眼睛的参数
//正交矩阵使用
//	_user->_eyePosition = GLVector3(3.0f,8.0f,-5.0f);
//	_user->_viewPosition = GLVector3(0.0f,0.0f,-10.0f);
//透视矩阵使用
	_user->_eyePosition = GLVector3(0.0f,16.0f,0.0f);
	_user->_viewPosition = GLVector3(0.0f,0.0f,-6.0f);
	_user->_eyeViewMatrix.lookAt(_user-> _eyePosition, _user->_viewPosition, GLVector3(0.0f,1.0f,0.0f));
//	_user->_eyeViewMatrix.lookAt(_user->_lightEyePosition, _user->_lightViewPosition, GLVector3(0.0f, 1.0f, 0.0f));
//设置正交投影矩阵
	_user->_projectMatrix.orthoProject(-15.0f,15.0f,-15.0f,15.0f,-10.0f,30.0f);
	Size     _size = _context->getWinSize();
//	_user->_projectMatrix.frustum(-10.0f, 10.0f, -10.0f, 10.0f, 0.1f, 30.0f);
//	_user->_projectMatrix.perspective(60.0f, _size.width / _size.height, 0.1f,30.0f);
//	_user->_otherProjMatrix.perspective(60.0f, _size.width / _size.height, 0.1f, 30.0f);
	_user->_otherProjMatrix.orthoProject(-15.0f, 15.0f, -15.0f, 15.0f, -10.0f, 30.0f);
	_context->setNearFarPlane(GLVector2(0.1f,30.0f));
//和地面相关的矩阵
	_user->_groundModelMatrix.scale(15.0f, 15.0f, 15.0f);
	_user->_groundModelMatrix.rotate(-90.0f, 1.0f, 0.0f, 0.0f);
	_user->_groundModelMatrix.translate(0.0f, 0.0f, -10.0f);
	_user->_groundNormalMatrix = _user->_groundModelMatrix.normalMatrix();
//设置和光线相关的数据
	_user->_lightColor = GLVector3(1.0f,1.0f,1.0f);
	_user->_lightDirection =(_user->_lightEyePosition-_user->_lightViewPosition).normalize() ;
	_user->_ambientColor = GLVector3(0.2f,0.2f,0.2f);
	_user->_specularCoeff = 0.36f;
	_user->_angle = 0.0f;
//
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	glEnable(GL_CULL_FACE);
//注意,在阴影的生成中,一般都需要要开启多边形偏移,因为在一般的情况下,都会因为深度的冲突而造成阴影的混乱,读者可以尝试把下面的
//两句代码注释掉重编译,看一下生成的结果
	glEnable(GL_POLYGON_OFFSET_FILL);
	glPolygonOffset(4.0f, 100.0f);
}
Example #7
0
void        Init(GLContext    *_context)
{
	UserData	*_user = new   UserData();
	_context->userObject = _user;
	_user->_lightPosition = GLVector3(0.0f,1.0f,-4.0f);
	_user->_lightColor = GLVector3(5.0f,0.0f,4.0f);
	_user->_ambientColor = GLVector3(0.15f,0.15f,0.15f);
	_user->_performTimes = 10;
	_user->_exposure = 0.5f;
//	_user->_testTexture = GLTexture::createWithFile("tga/basemap.tga");
//	_user->_sprite = Sprite::createWithFile("tga/basemap.tga");
//
	_user->_userChest = Chest::createChest("tga/basemap.tga");
	_user->_userChest->setScale(0.7f);
	_user->_userChest->setPosition(GLVector3(-3.0f,-0.5f,-4.0f));
	_user->_userChest->setRotateAngle(30.0f, GLVector3(1.0f,1.0f,1.0f));
	_user->_userChest->setAmbientColor(_user->_ambientColor);
	_user->_userChest->setLightColor(_user->_lightColor);
	_user->_userChest->setLightPosition(_user->_lightPosition);
	_user->_userChest->setSpecularCoeff(0.45f);

	_user->_userChest2 = Chest::createChest("tga/basemap.tga");
	_user->_userChest2->setScale(0.7f);
	_user->_userChest2->setRotateAngle(-30.0f, GLVector3(1.0f,1.0f,1.0f));
	_user->_userChest2->setPosition(GLVector3(1.0f,-0.5f,-4.0f));
	_user->_userChest2->setAmbientColor(_user->_ambientColor);
	_user->_userChest2->setLightColor(_user->_lightColor);
	_user->_userChest2->setLightPosition(_user->_lightPosition);
	_user->_userChest->setSpecularCoeff(0.36f);

	_user->_lightChest = LightChest::createLightChest(_user->_lightColor);
	_user->_lightChest->setPosition(_user->_lightPosition);
	_user->_lightChest->setScale(0.4f);
	_user->_lightChest->rotate(90.0f, GLVector3(-1.0f,1.0f,-1.0f)-GLVector3(1.0f,-1.0f,1.0f));
//hdr + mrt
	Size        _size = _context->getWinSize();
	_user->_hdrmrt = HdrMrt::createHdrMrt(_size.width,_size.height);
    _user->_gauPerform = GaussianPerform::createGaussianPerform(_size.width, _size.height, _user->_performTimes);
	_user->_hdrMerge = HdrMerge::createHdrMerge(_user->_exposure);
//
	_user->_projectMatrix.identity();
	_user->_projectMatrix.orthoProject(-4.0f, 4.0f, -4.0f, 4.0f, 0.0f, 10.0f);
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	glEnable(GL_CULL_FACE);
}
Example #8
0
GLVector3 GLMatrix::GetTranslation() const
{
   // Return the translation component of matrix
   //
   // . . . X()
   // . . . Y()
   // . . . Z()
   // . . . .

   return GLVector3(fVals[12], fVals[13], fVals[14]);
}
Example #9
0
void    Pyramid::init(float lengOfEdge)
{
	float   sqrtf_3 = sqrtf(3.0f);
	float   sqrtf_6 = sqrtf(6.0f);
	float   one_d_2sqrt3 = 1.0f / (2.0f*sqrtf_3);

	const GLVector3    v_1(-lengOfEdge * 0.5f,0, lengOfEdge*one_d_2sqrt3);
	const GLVector3    v_2(lengOfEdge*0.5f,0, lengOfEdge*one_d_2sqrt3);
	const GLVector3    v_3(0,0,-lengOfEdge / sqrtf_3);
	const GLVector3    v_4(0, lengOfEdge*sqrtf_6/3.0f,0);
	_aabb._minBox = GLVector3(v_1.x, v_1.y,v_3.z);
	_aabb._maxBox = GLVector3(v_2.x,v_4.y,v_2.z);
	//四个平面的法线
	const GLVector3   n_1 = (v_3 - v_1).cross(v_2 - v_1).normalize();
	const GLVector3   n_2 = (v_2 - v_1).cross(v_4 - v_1).normalize();
	const GLVector3   n_3 = (v_3-v_2).cross(v_4 -v_2).normalize();
	const GLVector3   n_4 = (v_1 - v_3).cross(v_4 - v_3).normalize();
	//12个顶点/法线/纹理坐标
	float    vertex_data[] = {
		v_2.x,v_2.y,v_2.z,n_1.x,n_1.y,n_1.z,0,1,//1
		v_1.x,v_1.y,v_1.z,n_1.x,n_1.y,n_1.z,0,0,//2
		v_3.x,v_3.y,v_3.z,n_1.x,n_1.y,n_1.z,1,1,//3

		v_4.x,v_4.y,v_4.z,n_3.x,n_3.y,n_3.z,0,1,//4
		v_2.x,v_2.y,v_2.z,n_3.x,n_3.y,n_3.z,0,0,//5
		v_3.x,v_3.y,v_3.z,n_3.x,n_3.y,n_3.z,1,0,//6

		v_4.x,v_4.y,v_4.z,n_4.x,n_4.y,n_4.z,0,1,//7
		v_3.x,v_3.y,v_3.z,n_4.x,n_4.y,n_4.z,1,0,//8
		v_1.x,v_1.y,v_1.z,n_4.x,n_4.y,n_4.z,1,1,//9

		v_4.x,v_4.y,v_4.z,n_2.x,n_2.y,n_2.z,1,1,//10
		v_1.x,v_1.y,v_1.z,n_2.x,n_2.y,n_2.z,0,0,//11
		v_2.x,v_2.y,v_2.z,n_2.x,n_2.y,n_2.z,1,0,//12
	};

	_numberOfVertex = 12;
	glGenBuffers(1, &_vertexVBO);
	glBindBuffer(GL_ARRAY_BUFFER,_vertexVBO);
	glBufferData(GL_ARRAY_BUFFER, _numberOfVertex * sizeof(float) * 8, vertex_data,GL_STATIC_DRAW);
}
Example #10
0
void      Sphere::initWithSlice(int   slices,float  radius)
{
	float   *vertex, *texCoord, *normal,*tangent;
	int     *indice;

	_aabb._minBox = GLVector3(-radius,-radius,-radius);
	_aabb._maxBox = GLVector3(radius, radius, radius);
	_numberOfIndice = esGenSphere(slices, radius, &vertex, &normal, &tangent, &texCoord, &indice, &_numberOfVertex);

	glGenBuffers(1, &_vertexVBO);
	glBindBuffer(GL_ARRAY_BUFFER, _vertexVBO);
	glBufferData(GL_ARRAY_BUFFER, 3 * sizeof(float)*_numberOfVertex, vertex, GL_STATIC_DRAW);
//
	glGenBuffers(1, &_texCoordVBO);
	glBindBuffer(GL_ARRAY_BUFFER, _texCoordVBO);
	glBufferData(GL_ARRAY_BUFFER, 2 * sizeof(float)*_numberOfVertex, texCoord, GL_STATIC_DRAW);
//
	glGenBuffers(1, &_normalVBO);
	glBindBuffer(GL_ARRAY_BUFFER, _normalVBO);
	glBufferData(GL_ARRAY_BUFFER, 3 * sizeof(float)*_numberOfVertex, normal, GL_STATIC_DRAW);
//
	glGenBuffers(1, &_indiceVBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indiceVBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int)*_numberOfIndice, indice, GL_STATIC_DRAW);
//
	glGenBuffers(1, &_tangentVBO);
	glBindBuffer(GL_ARRAY_BUFFER,_tangentVBO);
	glBufferData(GL_ARRAY_BUFFER, 3 * sizeof(float)*_numberOfVertex, tangent,GL_STATIC_DRAW);
//free memory
	delete    vertex;
	delete    texCoord;
	delete   normal;
	delete   indice;
	delete   tangent;
	vertex = NULL;
	texCoord = NULL;
	normal = NULL;
	indice = NULL;
	tangent = NULL;
}
Example #11
0
std::pair<bool, GLLine3> Intersection(const GLPlane & p1, const GLPlane & p2)
{
   // Find 3D line interestion of this plane with 'other'. Returns a std::pair
   //
   // first (bool)                   second (GLLine3)
   // kTRUE - planes intersect         intersection line between planes
   // kFALSE - no intersect (parallel) undefined
   GLVector3 lineDir = Cross(p1.Norm(), p2.Norm());

   if (lineDir.Mag() == 0.0) {
      return std::make_pair(kFALSE, GLLine3(GLVertex3(0.0, 0.0, 0.0),
                                             GLVector3(0.0, 0.0, 0.0)));
   }
   GLVertex3 linePoint = Cross((p1.Norm()*p2.D() - p2.Norm()*p1.D()), lineDir) /
                           Dot(lineDir, lineDir);
   return std::make_pair(kTRUE, GLLine3(linePoint, lineDir));
}
Example #12
0
//固定不变的矩阵
void        SetFixedMatrix(GLContext *_context)
{
	UserData    *_user = (UserData*)_context->userObject;
	Size   _size = _context->getWinSize();
	esPerspective(&_user->projMatrix, 50.0f, _size.width / _size.height, 1.0f, 100.0f);
	//	esOrtho(&_user->projMatrix, -4.0f, 4.0f, -4.0f, 4.0f, 0.0f, 8.0f);
	//光源的视野
	//	esMatrixLookAt(&_user->viewMatrix, &GLVector3(4.0f, 2.5f, -4.0f), &GLVector3(0.0f, 0.0f, -4.0f), &GLVector3(0.0f, 1.0f, 0.0f));
	esMatrixLookAt(&_user->viewMatrix, &GLVector3(2.3f, 2.5f, -4.0f), &GLVector3(2.0f, 0.0f, -4.0f), &GLVector3(0.0f, 1.0f, 0.0f));
	//光线的方向
	ESMatrix3      trunkMatrix;
	esMatrixTrunk(&trunkMatrix, &_user->viewMatrix);
	_user->lightVector = esMatrixMultiplyVector3(&GLVector3(2.3f - 2.0f, 2.5f - 0.0f, -4.0f + 4.0f), &trunkMatrix);// normalize(&GLVector3(2.3f - 2.0f, 2.5f - 0.0f, -4.0f + 4.0f));
	_user->lightVector = normalize(&_user->lightVector);
	ESMatrix    identity;
	///////////////////地面//////////////////
	//旋转
	esRotate(&_user->lightGroundMatrix, -90.0f, 1.0f, 0.0f, 0.0f);
	//平移
	esTranslate(&_user->lightGroundMatrix, 0.0f, -1.0f, -4.0f);
	//视图矩阵
	esMatrixMultiply(&_user->lightGroundMatrix, &_user->lightGroundMatrix, &_user->viewMatrix);
	//获取法线矩阵
	esMatrixTrunk(&_user->normalGroundMatrix, &_user->lightGroundMatrix);
	GLVector3    rotatevector = esMatrixMultiplyVector3(&GLVector3(0.0f, 0.0f, 1.0f), &_user->normalGroundMatrix);
	esMatrixMultiply(&_user->lightGroundMatrix, &_user->lightGroundMatrix, &_user->projMatrix);
////////////////////
	//旋转
	esRotate(&_user->eyeGroundMatrix, -90.0f, 1.0f, 0.0f, 0.0f);
	//平移
	esTranslate(&_user->eyeGroundMatrix, 0.0f, -1.0f, -4.0f);
	//视图矩阵
	esMatrixLookAt(&_user->eyeViewMatrix, &GLVector3(0.0f, 1.0f, 0.0f), &GLVector3(0.0f, 0.0f, -8.0f), &GLVector3(0.0f, 1.0f, 0.0f));
	esMatrixMultiply(&_user->eyeGroundMatrix, &_user->eyeGroundMatrix, &_user->eyeViewMatrix);
	//MVP矩阵
	esMatrixMultiply(&_user->eyeGroundMatrix, &_user->eyeGroundMatrix, &_user->projMatrix);
}
void		ChoppySimulate::updateHeightField(float _deltaTime)
{
	const       int         halfWay = WAVE_SIZE/2 +1;
	for (int i = 0; i < halfWay; ++i)
	{
		for (int j = 0; j < WAVE_SIZE; ++j)
		{
			const float wkt = sqrt(_waveNum[i][j].z   *__GRAVITY_CONSTANT) * this->_deltaTime;
			const float cosValue = cos(wkt);
			const float sinValue = sin(wkt);
			const int    otherX = WAVE_SIZE - i - 1;
			const int    otherY = WAVE_SIZE - j - 1;
//计算真实的高度场
			_choppyField[i][j].real =_heightField[i][j].real * cosValue  - _heightField[i][j].imag*sinValue
											+_heightField[otherX][otherY].real*cosValue +_heightField[otherX][otherY].imag *sinValue;
			_choppyField[i][j].imag = _heightField[i][j].real *sinValue + _heightField[i][j].imag*cosValue
											-_heightField[otherX][otherY].real*sinValue + _heightField[otherX][otherY].imag*cosValue;
//计算 X-Y对称的高度场
			if (i < halfWay - 1)
			{
				_choppyField[otherX][otherY].real = _heightField[otherX][otherY].real * cosValue - _heightField[otherX][otherY].imag*sinValue
																				+ _heightField[i][j].real*cosValue + _heightField[i][j].imag *sinValue;
				_choppyField[otherX][otherY].imag = _heightField[otherX][otherY].real *sinValue + _heightField[otherX][otherY].imag*cosValue
															- _heightField[i][j].real*sinValue + _heightField[i][j].imag*cosValue;
			}
		}
	}
	this->updateOffsetField(_deltaTime);
	if (! FFT2D(_choppyField,WAVE_SIZE,WAVE_SIZE,-1))
	{
		printf("_choppyField FFT2D failed,please check.\n");
		assert(0);
	}
//修正相关的值
	for (int i = 0; i < WAVE_SIZE; ++i)
	{
		for (int j = 0; j < WAVE_SIZE; ++j)
		{
			_choppyField[i][j].real *= __neg2pow(i + j);
		}
	}
//生成法线
	const    float	fixX=2.0f;
	const	float     fixY = 2.0f;
	for (int i = 0; i < WAVE_SIZE; ++i)
	{
		const   int       otherX = i < WAVE_SIZE - 1 ? i + 1 : 0;
		for (int j = 0; j < WAVE_SIZE; ++j)
		{
			const   int       otherY = j < WAVE_SIZE - 1 ? j +1: 0;
			//GLVector3        xVec = GLVector3(fixX + _deltaXField[otherX][j].imag - _deltaXField[i][j].imag,
			//	 _deltaYField[otherX][j].imag - _deltaYField[i][j].imag,
			//	 (_choppyField[otherX][j].imag - _choppyField[i][j].imag)*__HEIGHT_SCALE__
			//	);
			//GLVector3       yVec = GLVector3(_deltaXField[i][otherY].imag - _deltaXField[i][j].imag,
			//	fixY + _deltaYField[i][otherY].imag - _deltaYField[i][j].imag,
			//	(_choppyField[i][otherY].imag - _choppyField[i][j].imag) * __HEIGHT_SCALE__
			//	);
			//GLVector3  xVec = GLVector3(fixX, _deltaYField[i][otherY].imag - _deltaYField[i][j].imag, (_choppyField[otherX][j].real - _choppyField[i][j].real) *__HEIGHT_SCALE__);
			//GLVector3  yVec = GLVector3(_deltaXField[otherX][j].imag - _deltaXField[i][j].imag, fixY, (_choppyField[i][otherY].real - _choppyField[i][j].real)*__HEIGHT_SCALE__);
			GLVector3  xVec = GLVector3(fixX, 0.0f, (_choppyField[otherX][j].real - _choppyField[i][j].real) *__HEIGHT_SCALE__);
			GLVector3  yVec = GLVector3(0.0f, fixY, (_choppyField[i][otherY].real - _choppyField[i][j].real)*__HEIGHT_SCALE__);
			_choppyNormal[i][j] = xVec.cross(yVec);
		}
	}
}
Example #14
0
void          Mesh::initWithMesh(int    size,float scaleX, float scaleY, float texIntensity,MeshType meshType)
{
	int i, j;
	int numIndices = (size - 1) * (size - 1) * 2 * 3;
// Allocate memory for buffers
	int numVertices = size * size;
	_numberOfVertex = numVertices;
	_numberOfIndice = numIndices;
	float   stepSize = ((float)(size - 1)) / 2.0f;
	float   *vertex = new float[3 * numVertices];

	if (meshType == MeshType_XOY)
	{
		_aabb._minBox = GLVector3(-scaleX,-scaleY,0);
		_aabb._maxBox = GLVector3(scaleX,scaleY,0);
	}

	int  index = 0;
	for (i = 0; i < size; ++i) // row
	{
		const    float     locX = scaleY*(i / stepSize - 1.0f);
		for (j = 0; j < size; ++j) // column
		{
			if (meshType == MeshType::MeshType_XOY)
			{
				vertex[index] = scaleX*(j / stepSize - 1.0f);
				vertex[index + 1] = locX;
				vertex[index + 2] = 0.0f;
			}
			else if (meshType == MeshType::MeshType_XOZ)
			{
				vertex[index] = (j/stepSize -1.0f )*scaleX ;
				vertex[index+1] = 0.0f;
				vertex[index + 2] =-locX ;
			}
			else if (meshType == MeshType::MeshType_YOZ)
			{
				vertex[index] = 0.0f;
				vertex[index + 1] = locX;
				vertex[index + 2] = (1.0f - j/stepSize)*scaleX;
			}
			index += 3;
		}
	}
	//计算切线鱼与法线
	if (meshType == MeshType::MeshType_XOY)
	{
		_normal = GLVector3(0,0,1.0f);
		_tangent = GLVector3(1.0f,0.0f,0.0f);
	}
	else if (meshType ==MeshType::MeshType_XOZ)
	{
		_normal = GLVector3(0.0f,1.0f,0.0f);
		_tangent = GLVector3(1.0f,0,0);
	}
	else if (meshType==MeshType::MeshType_YOZ)
	{
		_normal = GLVector3(1,0,0);
		_tangent = GLVector3(0,0,-1);
	}

// Generate the indices
	int   *_indice = new int[numIndices];
	for (i = 0; i < size - 1; ++i)
	{
		for (j = 0; j < size - 1; ++j)
		{
// two triangles per quad
			int    _index = 6 * (j + i * (size - 1));
			_indice[_index] = j + i* size;
			_indice[_index + 1] = j + i* size + 1;
			_indice[_index + 2] = j + (i + 1) * size + 1;

			_indice[_index + 3] = j + (i + 1) * size + 1;
			_indice[_index + 4] = j + (i + 1) * size;
			_indice[_index + 5] = j + i* size;
		}
	}
//Gen Buffers
	glGenBuffers(1, &_vertexVBO);
	glBindBuffer(GL_ARRAY_BUFFER, _vertexVBO);
	glBufferData(GL_ARRAY_BUFFER, 3*sizeof(float)*_numberOfVertex, vertex,GL_STATIC_DRAW);
//
	glGenBuffers(1, &_indiceVBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indiceVBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int)*_numberOfIndice, _indice, GL_STATIC_DRAW);
//tex coord
	float     *texCoord = vertex;
	const   float      _factor = size - 1;
	for (int i = 0; i < _numberOfVertex; ++i)
	{
		int     _row = i / size;
		int     _column = i%size;
		texCoord[i << 1] = texIntensity*_column / _factor;
		texCoord[(i << 1) + 1] = texIntensity*_row / _factor;
	}
	glGenBuffers(1, &_texCoordVBO);
	glBindBuffer(GL_ARRAY_BUFFER, _texCoordVBO);
	glBufferData(GL_ARRAY_BUFFER, 2 * sizeof(float)*_numberOfVertex, texCoord, GL_STATIC_DRAW);

	delete  vertex;
	delete  _indice;
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
Example #15
0
void     Ground::bindTangentObject(int _loc)
{
	glDisableVertexAttribArray(_loc);
	glVertexAttrib3fv(_loc,(float *)&GLVector3(1.0f,0.0f,0.0f));
}
void SceneBuilding::loadSceneBuilding(const char *filename)
{
	//加载数据
	FILE  *file = fopen(filename,"rb");
	assert(file != nullptr);
	int     vertexCount=0;
	fread(&vertexCount, sizeof(int), 1, file);
	GLVector3 *vertexData = nullptr;
	_vertexCount = vertexCount;
	if (vertexCount > 0)
	{
		vertexData = new GLVector3[vertexCount];
		fread(vertexData,sizeof(GLVector3),vertexCount,file);
		//对于每个顶点,需要有法线/纹理坐标/模型坐标
		int                 groupSize = vertexCount * 3;
		GLVector3   *VertexGroup = new GLVector3[groupSize];
		//TBN矩阵的逆矩阵
		Matrix3         tbnReverse;
		int    index = 0;
		for (int k = 0; k < vertexCount; k += 3)
		{
			//求TBN矩阵的逆矩阵
			GLVector3    abVec = vertexData[k + 1] - vertexData[k];
			GLVector3    acVec = vertexData[k+2]-vertexData[k];
			GLVector3    normal = abVec.cross(acVec).normalize();
			Matrix3::reverseTBN(normal,tbnReverse);
			GLVector3    fragCoordA = vertexData[k] * tbnReverse;
			GLVector3    fragCoordB = vertexData[k + 1] * tbnReverse;
			GLVector3    fragCoordC = vertexData[k + 2] * tbnReverse;
			
			VertexGroup[index] = vertexData[k];
			VertexGroup[index + 1] = normal;
			VertexGroup[index + 2] = fragCoordA;

			VertexGroup[index + 3] = vertexData[k+1];
			VertexGroup[index + 4] = normal;
			VertexGroup[index + 5] = fragCoordB;

			VertexGroup[index + 6] = vertexData[k+2];
			VertexGroup[index + 7] = normal;
			VertexGroup[index + 8] = fragCoordC;

			index += 9;
		}
		glGenBuffers(1,&_buildingVertexId);
		glBindBuffer(GL_ARRAY_BUFFER, _buildingVertexId);
		glBufferData(GL_ARRAY_BUFFER, sizeof(GLVector3)*groupSize,VertexGroup,GL_STATIC_DRAW);
		glBindBuffer(GL_ARRAY_BUFFER,0);
		//
		delete[] VertexGroup;
		VertexGroup = nullptr;
	}
	fclose(file);
	//设置摄像机
	auto &winSize = GLContext::getInstance()->getWinSize();
	_camera = Camera2::createWithPerspective(45.0, winSize.width/winSize.height,0.125f,500);
	_camera->lookAt(GLVector3(0,1.75f,7.0f),GLVector3(0,1.75f,0));
	_collisionDetector.init(vertexData,vertexCount,1.75,1.25f,0.125f);
	//加载纹理
	_texture = GLTexture::createWithFile("tga/terrain/concrete.tga");
	struct TexParam  texParam = {0,0,GL_REPEAT,GL_REPEAT};
	_texture->setTexParam(texParam);
	//
	_glProgram = GLProgram::createWithFile("shader/terrain/SceneBuilding_VS.glsl","shader/terrain/SceneBuilding_FS.glsl");
	//事件派发
	_touchListener = TouchEventListener::createTouchListener(this,glk_touch_selector(SceneBuilding::onTouchPressed),glk_move_selector(SceneBuilding::onTouchMoved),glk_release_selector(SceneBuilding::onTouchReleased));
	EventManager::getInstance()->addTouchEventListener(_touchListener,0);
	//
	_keyListener = KeyEventListener::createKeyEventListener(this, glk_key_press_selector(SceneBuilding::onKeyPressed),glk_key_release_selector(SceneBuilding::onKeyRelease));
	EventManager::getInstance()->addKeyEventListener(_keyListener,0);

	delete[]vertexData;
	vertexData = nullptr;
}