Example #1
0
		Air::U1 Actor::Create()
		{
			if (m_Info.strModelName.empty()	||	m_Info.pSection	==	NULL)
			{
				return false;
			}
			m_pNode	=	m_Info.pSection->GetActorNode()->CreateChildSceneNode();
			SetModelName(m_Info.strModelName);

			Physics::Object::Info poInfo;
			poInfo.fMass	=	1;
			poInfo.uiShapeCount	=	1;
			poInfo.pShapeArray[0].SetCylinder(Float3(0,0,0),0.5,1);
			m_pHitShape	=	PhysicsSystem::GetSingleton()->CreateProduct<Physics::Object>(m_strProductName+"_CD",&poInfo);
			m_pHitShape->SetUserData(this);
			m_pNode->attachObject(m_pHitShape);
			return true;
		}
	void RenderSetSorted::AddRenderNodeSolution(RenderNode *nodePtr)
	{
		static bool added = false;
		if(!added)
		{
			PrintConsole::GetReference().AddSolution(L"ED2", "Sorting & Hierarchy", Float3(1.0f, 0.0f, 0.0f), 
				L"RenderSetSorted::AddRenderNodeSolution");
			added = true;
		}

		RenderShape *pShape = (RenderShape *)nodePtr;
		// Empty list
		if (0 == headPtr)
		{
			tailPtr = pShape;
			tailPtr->SetNext(0);
			headPtr = pShape;
		}
		else
		{
			RenderShape *pPrevious = 0;
			RenderShape *pCurrent = (RenderShape *)headPtr;

			// Check if this node is first
			if(!SortFunc(pCurrent, pShape))
			{
				pPrevious = pCurrent;
				pCurrent = (RenderShape *)pCurrent->GetNext();

				// Find where this node goes
				while(pCurrent && !SortFunc(pCurrent, pShape))
				{
					pPrevious = pCurrent;
					pCurrent = (RenderShape *)pCurrent->GetNext();
				}
				pPrevious->SetNext(pShape);
			}
			else
				headPtr = pShape;
			pShape->SetNext(pCurrent);
			if(!pCurrent)
				tailPtr = pShape;
		}
	}
Example #3
0
//----------------------------------------------------------------------------
void ConvexHull3D::LoadData ()
{
    char filename[32];
    if (mCurrentFile < 10)
    {
        sprintf(filename, "data0%d.txt", mCurrentFile);
    }
    else
    {
        sprintf(filename, "data%d.txt", mCurrentFile);
    }

    std::string path = Environment::GetPathR(filename);
    std::ifstream inFile(path.c_str());
    assert(inFile);
    inFile >> mNumVertices;

    delete1(mVertices);
    mVertices = new1<Vector3f>(mNumVertices);
    int i;
    for (i = 0; i < mNumVertices; ++i)
    {
        inFile >> mVertices[i][0];
        inFile >> mVertices[i][1];
        inFile >> mVertices[i][2];
    }

    delete1(mColors);
    mColors = new1<Float3>(mNumVertices);
    for (i = 0; i < mNumVertices; ++i)
    {
        mColors[i] = Float3(Mathf::UnitRandom(), Mathf::UnitRandom(),
            Mathf::UnitRandom());
    }

    // Discard previous scene spheres.
    for (i = 2; i < mLimitedQuantity + 2; ++i)
    {
        mTrnNode->DetachChildAt(i);
    }

    mLimitedQuantity = mNumVertices;
    CreateHull();
}
Example #4
0
Float3 MlRachis::matchNormal(const Vector3F & wv, const Matrix33F & space)
{
	Vector3F ov = space.transform(wv);

	Vector3F va = ov;
	va.y = 0.f;
	va.z -= 0.05f;
	va.normalize();
	float a = acos(Vector3F::XAxis.dot(va));
	if(va.z > 0.f) a = -a;
	
	Vector3F vb = ov;
	vb.z = 0.f;
	vb.normalize();
	float b = acos(Vector3F::XAxis.dot(vb));
	if(vb.y < 0.f) b = -b;
	
	return Float3(a, b, 0.f);
}
Example #5
0
		void Frustum::BuildPlane(){


			Float3 dir,nc,fc,X,Y,Z;

			Z = Float3(-m_vDirection.x,-m_vDirection.y,-m_vDirection.z);
			Z.Normalize();

			X = m_vUp.Cross(Z);
			X.Normalize();

			Y = Z.Cross(X);

			nc = m_vPosition - Z * m_fNear;
			fc = m_vPosition - Z * m_fFar;

			Float3 ntl,ntr,nbl,nbr,ftl,ftr,fbl,fbr;
			float nw,nh,fw,fh;

			float	tang = (float)tan(m_fAngle * 0.5f) ;
			nh = m_fNear * tang;
			nw = nh * m_fAspect; 
			fh = m_fFar  * tang;
			fw = fh * m_fAspect;

			ntl = nc + Y * nh - X * nw;
			ntr = nc + Y * nh + X * nw;
			nbl = nc - Y * nh - X * nw;
			nbr = nc - Y * nh + X * nw;

			ftl = fc + Y * fh - X * fw;
			ftr = fc + Y * fh + X * fw;
			fbl = fc - Y * fh - X * fw;
			fbr = fc - Y * fh + X * fw;

			m_vecPlane[enFS_Top].SetVertex(ntr,ntl,ftl);
			m_vecPlane[enFS_Bottom].SetVertex(nbl,nbr,fbr);
			m_vecPlane[enFS_Left].SetVertex(ntl,nbl,fbl);
			m_vecPlane[enFS_Right].SetVertex(nbr,ntr,fbr);
			m_vecPlane[enFS_Near].SetVertex(ntl,ntr,nbr);
			m_vecPlane[enFS_Far].SetVertex(ftr,ftl,fbl);
		}
LoaderFbxTextureDesc::LoaderFbxTextureDesc()
{
	textureName_ = "Unknown";
	fileName_	 = "Unknown";
	
	scale_					= Float2(0.0f, 0.0f);
	translation_			= Float2(0.0f, 0.0f);
	rotation_				= Float3(0.0f, 0.0f, 0.0f);
	swap_					= false;
	alphaSource_			= 0;
	croppingLeft_			= 0;
	croppingTop_			= 0;
	croppingRight_			= 0;
	croppingBottom_			= 0;
	mappingType_			= 0;
	planarMappingNormal_	= 0;
	blendMode_				= 0;
	defaultAlpha_			= 0.0f;
	materialUse_			= 0;
	textureUse_				= 0;
}
Example #7
0
	virtual void OnUpdate()
	{
		float k = Root::Instance()->GetTime();
		float r = 3.0f;

		float x = Math::Cos(k) * r;
		float z = Math::Sin(k) * r;

		gBillboard->SetPosition(Float3(x, 5, z));

		gLightDir->SetPosition(Float3(x, 5, z));
		gLightDir->SetDirection(-Float3(x, 0, z));

		gLightPoint->SetPosition(Float3(x, 5, z));
		gLightPoint->SetDirection(-Float3(x, 0, z));

		gLightSpot->SetPosition(Float3(x, 5, z));
		gLightSpot->SetDirection(-Float3(x, 0, z));
	}
Example #8
0
void ImGui::beginContainer(int left, int right, int bottom, int top, Container *container)
{
    if (left > right or bottom > top)
    {
        Command command;
        command.type = Command::PushScissor;
        command.scissor.left = 0;
        command.scissor.right = 0;
        command.scissor.bottom = 0;
        command.scissor.top = 0;

        commands.append(command);
        return;
    }

    rectangle(left, right+10, bottom-10, top, 0.44f, 1.0f, 0.0f, 1.0f, Float3(1.0f), Float4(0.0f));

    Command command;
    command.type = Command::PushScissor;
    command.scissor.left = left;
    command.scissor.right = right;
    command.scissor.bottom = bottom;
    command.scissor.top = top;

    commands.append(command);

    scrollX = container->scrollX;
    scrollY = container->scrollY;

    containerLeft = left;
    containerRight = right;
    containerTop = top;
    containerBottom = bottom;

    container->left = left;
    container->right = right;
    container->bottom = bottom;
    container->top = top;
}
Example #9
0
//==================================================================
bool ParamsFindP(	ParamList &params,
					const SymbolList &globalSymbols,
					Float3	*pOut_vectorP,
					int	expectedN,
					int fromIdx )
{
	bool	gotP = false;
	
	for (size_t i=fromIdx; i < params.size(); i += 2)
	{
		DASSERT( params[i].type == Param::STR );
		
		const Symbol* pSymbol = globalSymbols.FindSymbol( params[i] );
		if ( pSymbol && pSymbol->IsName( "P" ) )
		{
			DASSTHROW( (i+1) < params.size(), "Invalid number of arguments" );
			
			const FltVec	&fltVec = params[ i+1 ].NumVec();

			DASSTHROW( (int)fltVec.size() == 3 * expectedN,
							"Invalid number of arguments."
							 " Expecting %i but it's %u", 3 * expectedN, fltVec.size() );

			DASSTHROW( (int)(fltVec.size() % 3) == 0,
							"Invalid number of arguments."
							 " Should be multiple of 3 but it's %u", fltVec.size() );

			size_t	srcN = fltVec.size();

			for (size_t si=0, di=0; si < srcN; si += 3, di += 1)
				pOut_vectorP[ di ] = Float3( &fltVec[ si ] );
			
			return true;
		}
	}
	
	return false;
}
//----------------------------------------------------------------------------
TriMesh* RoughPlaneSolidBox::CreateRamp ()
{
    float x = 8.0f;
    float y = 8.0f;
    float z = y*Mathf::Tan((float)mModule.Angle);

    VertexFormat* vformat = VertexFormat::Create(2,
        VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
        VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0);
    int vstride = vformat->GetStride();

    VertexBuffer* vbuffer = new0 VertexBuffer(6, vstride);
    VertexBufferAccessor vba(vformat, vbuffer);

    vba.Position<Float3>(0) = Float3(-x, 0.0f, 0.0f);
    vba.Position<Float3>(1) = Float3(+x, 0.0f, 0.0f);
    vba.Position<Float3>(2) = Float3(-x, y, 0.0f);
    vba.Position<Float3>(3) = Float3(+x, y, 0.0f);
    vba.Position<Float3>(4) = Float3(-x, y, z);
    vba.Position<Float3>(5) = Float3(+x, y, z);
    vba.TCoord<Float2>(0, 0) = Float2(0.25f, 0.0f);
    vba.TCoord<Float2>(0, 1) = Float2(0.75f, 0.0f);
    vba.TCoord<Float2>(0, 2) = Float2(0.0f, 1.0f);
    vba.TCoord<Float2>(0, 3) = Float2(1.0f, 1.0f);
    vba.TCoord<Float2>(0, 4) = Float2(0.25f, 1.0f);
    vba.TCoord<Float2>(0, 5) = Float2(0.75f, 1.0f);

    IndexBuffer* ibuffer = new0 IndexBuffer(18, sizeof(int));
    int* indices = (int*)ibuffer->GetData();
    indices[ 0] = 0;  indices[ 1] = 1;  indices[ 2] = 4;
    indices[ 3] = 1;  indices[ 4] = 5;  indices[ 5] = 4;
    indices[ 6] = 0;  indices[ 7] = 4;  indices[ 8] = 2;
    indices[ 9] = 1;  indices[10] = 3;  indices[11] = 5;
    indices[12] = 3;  indices[13] = 2;  indices[14] = 4;
    indices[15] = 3;  indices[16] = 4;  indices[17] = 5;

    TriMesh* ramp = new0 TriMesh(vformat, vbuffer, ibuffer);

    std::string path = Environment::GetPathR("Metal.wmtf");
    Texture2D* texture = Texture2D::LoadWMTF(path);
    ramp->SetEffectInstance(Texture2DEffect::CreateUniqueInstance(texture,
        Shader::SF_LINEAR, Shader::SC_REPEAT, Shader::SC_REPEAT));

    return ramp;
}
Example #11
0
	SLSound::SLSound()
		: mCategory(0)
		, mFlags(0)
	{
		mBQObjectI = NULL;
		mBQPlayI = NULL;
		mVolumeI = NULL;
		mBQBufferQueueI = NULL;
		mBQEffectSendI = NULL;

		mPosition = Float3(0, 0, 0);
		mVolume = 1;
		mAttenStart = mAttenEnd = 0;
		mInvAttenDist = 1;

		mPlayTime = 0;
		mPlayOffset = 0;
		mPlayEnd = FALSE;

		mFadeMode = 0;
		mFadeTime = SL_FADETIME;

		i_thread = NULL;
	}
Example #12
0
	// extents, renderable, nearDistance, depth, nearPlaneArea
	void Camera::SetLODSolution(IRenderer* renderable, float maxExtent, float nearDistance, 
		float depth, float minArea)
	{
		static bool added = false;
		if(!added)
		{
			PrintConsole::GetReference().AddSolution(L"ED2", "LOD", Float3(1.0f, 0.0f, 0.0f), 
				L"Camera::SetLODSolution");
			added = true;
		}

		unsigned int numLODs = renderable->GetNumLODs();

		if( numLODs != 1 )
		{
			float projectedRadius = (nearDistance * maxExtent) / depth;
			
			float projectedArea = (float)M_PI * projectedRadius * projectedRadius;

			float areaOccupiedRatio = min( 1.0f, projectedArea / minArea );

			renderable->SetCurrentLOD( (unsigned int)((numLODs-1) * areaOccupiedRatio) );
		}
	}
Example #13
0
	virtual void OnInit()
	{
		SetupUI();

		// Create Mesh
		MeshSourcePtr pMeshSource = MeshManager::Instance()->LoadMesh("Mesh/fox.mesh");
		
		gMesh = new Mesh;
		gMesh->SetSource(pMeshSource);
		gMesh->LoadAnimation("Idle1", "Mesh/fox_idle1.anim");
		gMesh->SetLighting(true);

		gMesh->PlayAnimation("Idle1");

		LookMesh(gMesh);

		// Setup Main Light
		World::Instance()->MainLight()->SetDirection(Float3(-1, 0, 1));
		World::Instance()->MainLight()->SetAmbient(Float3(0.2f, 0.2f, 0.2f));
		World::Instance()->MainLight()->SetDiffuse(Float3(0.2f, 0.2f, 0.2f));

		// Create Lights
		gLightDir = new Light(eLightType::DIRECTION);
		gLightPoint = new Light(eLightType::POINT);
		gLightSpot = new Light(eLightType::SPOT);

		gLightDir->SetDiffuse(Float3(1, 0, 0));
		gLightPoint->SetDiffuse(Float3(0, 1, 0));
		gLightSpot->SetDiffuse(Float3(0, 0, 1));

		gLightDir->SetVisible(true);
		gLightPoint->SetVisible(false);
		gLightSpot->SetVisible(false);

		// Create Billboard
		gBillboard = new Billboard;
		gBillboard->SetSize(Float2(1, 1));
		gBillboard->SetTexture("flare.png");
		gBillboard->GetMaterial()->blendMode = eBlendMode::ADD;
	}
			U1 Resource::CreateHardwareBuff( MeshHDBuffer* pOutBuff,CalCoreMesh* pInMesh ){
				std::vector<ABuff>	vertexData;
				std::vector<UInt>								indexData;
	
				SInt	iMeshCount	=	pInMesh->getCoreSubmeshCount();
				SInt	iVertexCount	=	0;
				SInt	iIndexCount		=	0;
				//首先计算顶点数 和 索引数
				for(SInt i=0;i<iMeshCount;i++){
					CalCoreSubmesh*	pSubMesh	=	pInMesh->getCoreSubmesh(i);
					iVertexCount	+=	pSubMesh->getVectorVertex().size();
					iIndexCount		+=	pSubMesh->getFaceCount()*3;
				}
				if(	iVertexCount	==	0	||
					iIndexCount		==	0)
				{
					return	false;
				}
				//分配空间
				vertexData.resize(iVertexCount);
				indexData.resize(iIndexCount);
	
				SInt	iV	=	0;
				SInt	iI	=	0;
	
				
				for(SInt i=0;i<iMeshCount;i++){
					CalCoreSubmesh*	pSubMesh							=	pInMesh->getCoreSubmesh(i);
					//计算切线
					pSubMesh->enableTangents(0,true);
	
					std::vector<CalCoreSubmesh::Vertex>&							lstVector	=	pSubMesh->getVectorVertex();
					std::vector<std::vector<CalCoreSubmesh::TangentSpace> >&		lstTangent	=	pSubMesh->getVectorVectorTangentSpace();
					std::vector<std::vector<CalCoreSubmesh::TextureCoordinate> >&	lstTexcoord	=	pSubMesh->getVectorVectorTextureCoordinate();
					std::vector<CalCoreSubmesh::Face>&								lstFace		=	pSubMesh->getVectorFace();
					
					SInt iFaceCount	=	lstFace.size();
					//索引缓冲
					for(SInt	j=0;j<iFaceCount;j++){
						indexData[iI]	=	lstFace[j].vertexId[0]	+	iV;
						indexData[iI+1]	=	lstFace[j].vertexId[1]	+	iV;
						indexData[iI+2]	=	lstFace[j].vertexId[2]	+	iV;
						iI	+=	3;
					}
					
					//顶点缓冲数据
					for(SInt j=0;j<iVertexCount;j++){
						CalCoreSubmesh::Vertex&				ver		=	lstVector[j];
						CalCoreSubmesh::TangentSpace		tang	=	lstTangent[0][j];
						CalCoreSubmesh::TextureCoordinate	uv		=	lstTexcoord[0][j];
		
						//位置
						vertexData[iV].Position		=	Float3(ver.position.x,ver.position.y,ver.position.z);


						SInt	iBoneCount	=	ver.vectorInfluence.size();
						for(SInt	k=0;k<4;k++){
							if(k<iBoneCount){
 								vertexData[iV].BoneWeight[k]	=	ver.vectorInfluence[k].weight*255.0f;
								//如果只传递自己需要的矩阵 
								//骨骼索引变成0~n
 								vertexData[iV].BoneIndex[k]		=	ver.vectorInfluence[k].boneId;
							}else{
								vertexData[iV].BoneWeight[k]	=	0;
								vertexData[iV].BoneIndex[k]		=	0;

							}

						}

						//法线
						vertexData[iV].Normal		=	Float3(ver.normal.x,ver.normal.y,ver.normal.z);
						//UV
						vertexData[iV].UV.x		=	uv.u;
						vertexData[iV].UV.y		=	uv.v;
						//切线
						vertexData[iV].Tangent.x		=	tang.tangent.x;
						vertexData[iV].Tangent.y		=	tang.tangent.y;
						vertexData[iV].Tangent.z		=	tang.tangent.z;
	
	
	
						iV++;
					}
					AString	strName	=	m_strProductName	+	pInMesh->getName();
					Render::Vertex::IDeclare::Info	dInfo;
					dInfo.SetPNTT_Animation();
 					pOutBuff->pVertexDeclare	=	Render::System::GetSingleton()->CreateProduct<Render::Vertex::IDeclare>("PNTTANIM",&dInfo);
					Render::Buffer::Info		vInfo;
					vInfo.SetVertexBuffer(iVertexCount,52);
					vInfo.InitData			=	&vertexData[0];
					pOutBuff->pVertexBuff		=	Render::System::GetSingleton()->CreateProduct<Render::Buffer>(strName+"VB",&vInfo);

					vInfo.SetIndexBuffer32(iFaceCount*3);
					vInfo.InitData			=	&indexData[0];
					pOutBuff->pIndexBuff		=	Render::System::GetSingleton()->CreateProduct<Render::Buffer>(strName+"IB",&vInfo);

				}
				return	true;
			}
Example #15
0
Float3<T> Float3<T>::operator -() const
{
	return Float3(-this->x, -this->y, -this->z);
}
Example #16
0
Float3<T> Float3<T>::operator -(const Float3 &v) const
{
	return Float3(this->x - v.x, this->y - v.y, this->z - v.z);
}
Example #17
0
Float3<T> Float3<T>::operator +(const Float3 &v) const
{
	return Float3(this->x + v.x, this->y + v.y, this->z + v.z);
}
Example #18
0
Float3<T> Float3<T>::scaledBy(const Float3 &v) const
{
	return Float3(this->x * v.x, this->y * v.y, this->z * v.z);
}
Example #19
0
//----------------------------------------------------------------------------
void CubeMaps::CreateScene ()
{
    // Create the root of the scene.
    mScene = new0 Node();
    mWireState = new0 WireState();
    mRenderer->SetOverrideWireState(mWireState);

    // Create the walls of the cube room.  Each of the six texture images is
    // RGBA 64-by-64.
    Node* room = new0 Node();
    mScene->AttachChild(room);

    // Index buffer shared by the room walls.
    IndexBuffer* ibuffer = new0 IndexBuffer(6, sizeof(int));
    int* indices = (int*)ibuffer->GetData();
    indices[0] = 0;  indices[1] = 1;  indices[2] = 3;
    indices[3] = 0;  indices[4] = 3;  indices[5] = 2;

    // The vertex format shared by the room walls.
    VertexFormat* vformat = VertexFormat::Create(2,
        VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
        VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0);
    int vstride = vformat->GetStride();
    VertexBufferAccessor vba;

    // The texture effect shared by the room walls.
    Texture2DEffect* effect = new0 Texture2DEffect(Shader::SF_LINEAR);

    VertexBuffer* vbuffer;
    TriMesh* wall;
    std::string textureName;

    // +x wall
    vbuffer = new0 VertexBuffer(4, vstride);
    vba.ApplyTo(vformat, vbuffer);
    vba.Position<Float3>(0) = Float3(+1.0f, -1.0f, -1.0f);
    vba.Position<Float3>(1) = Float3(+1.0f, -1.0f, +1.0f);
    vba.Position<Float3>(2) = Float3(+1.0f, +1.0f, -1.0f);
    vba.Position<Float3>(3) = Float3(+1.0f, +1.0f, +1.0f);
    vba.TCoord<Float2>(0, 0) = Float2(0.0f, 0.0f);
    vba.TCoord<Float2>(0, 1) = Float2(1.0f, 0.0f);
    vba.TCoord<Float2>(0, 2) = Float2(0.0f, 1.0f);
    vba.TCoord<Float2>(0, 3) = Float2(1.0f, 1.0f);
    wall = new0 TriMesh(vformat, vbuffer, ibuffer);
    room->AttachChild(wall);
    textureName = Environment::GetPathR("XpFace.wmtf");
    Texture2D* xpTexture = Texture2D::LoadWMTF(textureName);
    wall->SetEffectInstance(effect->CreateInstance(xpTexture));

    // -x wall
    vbuffer = new0 VertexBuffer(4, vstride);
    vba.ApplyTo(vformat, vbuffer);
    vba.Position<Float3>(0) = Float3(-1.0f, -1.0f, +1.0f);
    vba.Position<Float3>(1) = Float3(-1.0f, -1.0f, -1.0f);
    vba.Position<Float3>(2) = Float3(-1.0f, +1.0f, +1.0f);
    vba.Position<Float3>(3) = Float3(-1.0f, +1.0f, -1.0f);
    vba.TCoord<Float2>(0, 0) = Float2(0.0f, 0.0f);
    vba.TCoord<Float2>(0, 1) = Float2(1.0f, 0.0f);
    vba.TCoord<Float2>(0, 2) = Float2(0.0f, 1.0f);
    vba.TCoord<Float2>(0, 3) = Float2(1.0f, 1.0f);
    wall = new0 TriMesh(vformat, vbuffer, ibuffer);
    room->AttachChild(wall);
    textureName = Environment::GetPathR("XmFace.wmtf");
    Texture2D* xmTexture = Texture2D::LoadWMTF(textureName);
    wall->SetEffectInstance(effect->CreateInstance(xmTexture));

    // +y wall
    vbuffer = new0 VertexBuffer(4, vstride);
    vba.ApplyTo(vformat, vbuffer);
    vba.Position<Float3>(0) = Float3(+1.0f, +1.0f, +1.0f);
    vba.Position<Float3>(1) = Float3(-1.0f, +1.0f, +1.0f);
    vba.Position<Float3>(2) = Float3(+1.0f, +1.0f, -1.0f);
    vba.Position<Float3>(3) = Float3(-1.0f, +1.0f, -1.0f);
    vba.TCoord<Float2>(0, 0) = Float2(0.0f, 0.0f);
    vba.TCoord<Float2>(0, 1) = Float2(1.0f, 0.0f);
    vba.TCoord<Float2>(0, 2) = Float2(0.0f, 1.0f);
    vba.TCoord<Float2>(0, 3) = Float2(1.0f, 1.0f);
    wall = new0 TriMesh(vformat, vbuffer, ibuffer);
    room->AttachChild(wall);
    textureName = Environment::GetPathR("YpFace.wmtf");
    Texture2D* ypTexture = Texture2D::LoadWMTF(textureName);
    wall->SetEffectInstance(effect->CreateInstance(ypTexture));

    // -y wall
    vbuffer = new0 VertexBuffer(4, vstride);
    vba.ApplyTo(vformat, vbuffer);
    vba.Position<Float3>(0) = Float3(+1.0f, -1.0f, -1.0f);
    vba.Position<Float3>(1) = Float3(-1.0f, -1.0f, -1.0f);
    vba.Position<Float3>(2) = Float3(+1.0f, -1.0f, +1.0f);
    vba.Position<Float3>(3) = Float3(-1.0f, -1.0f, +1.0f);
    vba.TCoord<Float2>(0, 0) = Float2(0.0f, 0.0f);
    vba.TCoord<Float2>(0, 1) = Float2(1.0f, 0.0f);
    vba.TCoord<Float2>(0, 2) = Float2(0.0f, 1.0f);
    vba.TCoord<Float2>(0, 3) = Float2(1.0f, 1.0f);
    wall = new0 TriMesh(vformat, vbuffer, ibuffer);
    room->AttachChild(wall);
    textureName = Environment::GetPathR("YmFace.wmtf");
    Texture2D* ymTexture = Texture2D::LoadWMTF(textureName);
    wall->SetEffectInstance(effect->CreateInstance(ymTexture));

    // +z wall
    vbuffer = new0 VertexBuffer(4, vstride);
    vba.ApplyTo(vformat, vbuffer);
    vba.Position<Float3>(0) = Float3(+1.0f, -1.0f, +1.0f);
    vba.Position<Float3>(1) = Float3(-1.0f, -1.0f, +1.0f);
    vba.Position<Float3>(2) = Float3(+1.0f, +1.0f, +1.0f);
    vba.Position<Float3>(3) = Float3(-1.0f, +1.0f, +1.0f);
    vba.TCoord<Float2>(0, 0) = Float2(0.0f, 0.0f);
    vba.TCoord<Float2>(0, 1) = Float2(1.0f, 0.0f);
    vba.TCoord<Float2>(0, 2) = Float2(0.0f, 1.0f);
    vba.TCoord<Float2>(0, 3) = Float2(1.0f, 1.0f);
    wall = new0 TriMesh(vformat, vbuffer, ibuffer);
    room->AttachChild(wall);
    textureName = Environment::GetPathR("ZpFace.wmtf");
    Texture2D* zpTexture = Texture2D::LoadWMTF(textureName);
    wall->SetEffectInstance(effect->CreateInstance(zpTexture));

    // -z wall
    vbuffer = new0 VertexBuffer(4, vstride);
    vba.ApplyTo(vformat, vbuffer);
    vba.Position<Float3>(0) = Float3(-1.0f, -1.0f, -1.0f);
    vba.Position<Float3>(1) = Float3(+1.0f, -1.0f, -1.0f);
    vba.Position<Float3>(2) = Float3(-1.0f, +1.0f, -1.0f);
    vba.Position<Float3>(3) = Float3(+1.0f, +1.0f, -1.0f);
    vba.TCoord<Float2>(0, 0) = Float2(0.0f, 0.0f);
    vba.TCoord<Float2>(0, 1) = Float2(1.0f, 0.0f);
    vba.TCoord<Float2>(0, 2) = Float2(0.0f, 1.0f);
    vba.TCoord<Float2>(0, 3) = Float2(1.0f, 1.0f);
    wall = new0 TriMesh(vformat, vbuffer, ibuffer);
    room->AttachChild(wall);
    textureName = Environment::GetPathR("ZmFace.wmtf");
    Texture2D* zmTexture = Texture2D::LoadWMTF(textureName);
    wall->SetEffectInstance(effect->CreateInstance(zmTexture));

    // A sphere to reflect the environment via a cube map.  The colors will
    // be used to modulate the cube map texture.
    vformat = VertexFormat::Create(3,
        VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
        VertexFormat::AU_NORMAL, VertexFormat::AT_FLOAT3, 0,
        VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT3, 0);
    vstride = vformat->GetStride();

    mSphere = StandardMesh(vformat).Sphere(64, 64, 0.125f);
    room->AttachChild(mSphere);

    // Generate random vertex colors for the sphere.  The StandardMesh class
    // produces a sphere with duplicated vertices along a longitude line.
    // This allows texture coordinates to be assigned in a manner that treats
    // the sphere as if it were a rectangle mesh.  For vertex colors, we want
    // the duplicated vertices to have the same color, so a hash table is used
    // to look up vertex colors for the duplicates.
    vba.ApplyTo(mSphere);
    std::map<Float3,Float3> dataMap;
    for (int i = 0; i < vba.GetNumVertices(); ++i)
    {
        Float3& position = vba.Position<Float3>(i);
        Float3& color = vba.Color<Float3>(0, i);
        std::map<Float3,Float3>::iterator iter = dataMap.find(position);
        if (iter != dataMap.end())
        {
            color = iter->second;
        }
        else
        {
            color[0] = 0.0f;
            color[1] = Mathf::IntervalRandom(0.5f, 0.75f);
            color[2] = Mathf::IntervalRandom(0.75f, 1.0f);
            dataMap.insert(std::make_pair(position, color));
        }
    }

    // Create the cube map and attach it to the sphere.
    std::string effectFile = Environment::GetPathR("CubeMap.wmfx");
    CubeMapEffect* cubeMapEffect = new0 CubeMapEffect(effectFile);

    ShaderFloat* reflectivity = new0 ShaderFloat(1);
    (*reflectivity)[0] = 0.5f;

    std::string cubeName = Environment::GetPathR("CubeMap.wmtf");
    TextureCube* cubeTexture = TextureCube::LoadWMTF(cubeName);
    cubeTexture->GenerateMipmaps();
    mCubeMapInstance = cubeMapEffect->CreateInstance(cubeTexture,
        reflectivity, false);

    mSphere->SetEffectInstance(mCubeMapInstance);

    // Allow culling to be disabled on the sphere so when you move inside
    // the sphere, you can see the previously hidden facets and verify that
    // the cube image for those facets is correctly oriented.
    mSphereCullState = cubeMapEffect->GetCullState(0, 0);
}
void PhysicsComponent::onEvent(Event* e)
{
	EventType type = e->getType();
	switch(type)
	{
	case EVENT_ATTRIBUTE_UPDATED: //Removes physics objects when the corresponding physics attribute is removed
	{
		Event_AttributeUpdated* attributeUpdated = static_cast<Event_AttributeUpdated*>(e);
		int attributeIndex = attributeUpdated->index;
		if(attributeUpdated->attributeEnum == ATTRIBUTE_PHYSICS)
		{
			if(attributeUpdated->isDeleted)
			{
				if(attributeIndex < physicsObjects_->size() && physicsObjects_->at(attributeIndex) != nullptr)
				{
  					dynamicsWorld_->removeRigidBody(physicsObjects_->at(attributeIndex));
					delete physicsObjects_->at(attributeIndex);
					physicsObjects_->at(attributeIndex) = nullptr;
				}
				else
				{
					DEBUGPRINT("Mismatch when synchronizing deletion of physics objects with physics attributes");
				}
			}
			else if(attributeUpdated->isCreated)
			{
			}
			else
			{
				itrPhysics.at(attributeIndex)->reloadDataIntoBulletPhysics = true;
			}
		}
		/*else if(attributeUpdated->attributeEnum == ATTRIBUTE_CAMERA)
		{
			if(attributeUpdated->isDeleted)
			{
  				dynamicsWorld_->removeRigidBody(frustumPhysicsObjects_->at(attributeIndex));
				delete frustumPhysicsObjects_->at(attributeIndex);
				frustumPhysicsObjects_->at(attributeIndex) = nullptr;
			}
		}*/
		break;
	}
	case EVENT_MODIFY_PHYSICS_OBJECT:
	{
		Event_ModifyPhysicsObject* modifyPhysicsObject = static_cast<Event_ModifyPhysicsObject*>(e);

		int physicsAttributeIndex = modifyPhysicsObject->ptr_physics.index();
		if(physicsAttributeIndex < physicsObjects_->size() && physicsAttributeIndex > -1)
		{
			PhysicsObject* physicsObject = physicsObjects_->at(physicsAttributeIndex);
			if(physicsObject != NULL)
			{
				//Cast void pointer sent in Event_ModifyPhysicsObject to its expected data type and modify physics object accordingly
				switch(modifyPhysicsObject->modifyWhatDataInPhysicsObjectData)
				{
				case XKILL_Enums::ModifyPhysicsObjectData::GRAVITY:
					{
						Float3* gravity = static_cast<Float3*>(modifyPhysicsObject->data);
						
						physicsObject->setGravity(btVector3(gravity->x, gravity->y, gravity->z));
						break;
					}
				case XKILL_Enums::ModifyPhysicsObjectData::VELOCITY:
					{
						Float3* velocity = static_cast<Float3*>(modifyPhysicsObject->data);
						
						physicsObject->setLinearVelocity(btVector3(velocity->x, velocity->y, velocity->z));
						break;
					}
				case XKILL_Enums::ModifyPhysicsObjectData::VELOCITYPERCENTAGE:
					{
						Float3* velocityPercentage = static_cast<Float3*>(modifyPhysicsObject->data);
						
						btVector3 currentLinearVelocity = physicsObject->getLinearVelocity();
						physicsObject->setLinearVelocity(btVector3(currentLinearVelocity.x()*velocityPercentage->x, currentLinearVelocity.y()*velocityPercentage->y, currentLinearVelocity.z()*velocityPercentage->z));
						break;
					}
				case XKILL_Enums::ModifyPhysicsObjectData::FLAG_STATIC:
					{
						bool* staticPhysicsObject = static_cast<bool*>(modifyPhysicsObject->data);
						
						if(*staticPhysicsObject == true)
						{
							physicsObject->setCollisionFlags(physicsObject->getCollisionFlags() | btCollisionObject::CF_STATIC_OBJECT);
						}
						else if(*staticPhysicsObject == false)
						{
							physicsObject->setCollisionFlags(physicsObject->getCollisionFlags() & ~ btCollisionObject::CF_STATIC_OBJECT);
						}
						break;
					}
				case XKILL_Enums::ModifyPhysicsObjectData::COLLISIONFILTERMASK:
					{
						/*In order to modify "collisionFilterMask", a physics objects needs to be removed from the Bullet Physics dynamics world and then readded using "addRigidBody", where "collisionFilterMask" is passed as argument.
						Write physics object data to physics attribute, modify "collisionFilterMask", and set the "reloadDataIntoBulletPhysics" flag, and this class will handle the removal and addition of the physics object.*/
						
						short* collisionFilterMaskFromEvent = static_cast<short*>(modifyPhysicsObject->data);

						AttributePtr<Attribute_Physics> ptr_physics = itrPhysics.at(physicsAttributeIndex);
						physicsObject->writeNonSynchronizedPhysicsObjectDataToPhysicsAttribute();
						ptr_physics->collisionFilterMask = *collisionFilterMaskFromEvent;
						ptr_physics->reloadDataIntoBulletPhysics = true;
					}
					break;
				case XKILL_Enums::ModifyPhysicsObjectData::GIVE_IMPULSE:
					{
						Float3* impulseVector = static_cast<Float3*>(modifyPhysicsObject->data);

						btVector3 impulse = convert(*impulseVector);
						physicsObject->applyCentralImpulse(impulse);
						break;
					}
				case XKILL_Enums::ModifyPhysicsObjectData::IF_TRUE_RECALCULATE_LOCAL_INERTIA_ELSE_SET_TO_ZERO:
					{
						bool* recalculateLocalInertia = static_cast<bool*>(modifyPhysicsObject->data);

						btVector3 localInertia = btVector3(0.0f, 0.0f, 0.0f);
						btScalar mass = itrPhysics.at(physicsAttributeIndex)->mass;
						if(*recalculateLocalInertia == true)
						{
							btCollisionShape* collisionShape = physicsObject->getCollisionShape();
							collisionShape->calculateLocalInertia(mass, localInertia);

							physicsObject->setMassProps(mass, localInertia);
							physicsObject->updateInertiaTensor();
						}
						else
						{
							physicsObject->setMassProps(mass, localInertia);
						}

						break;
					}
				}
			}
			else
			{
				ERROR_MESSAGEBOX("Invalid physics attribute id when handling event of type EVENT_MODIFY_PHYSICS_OBJECT, error 1");
			}
		}
		else
		{
			ERROR_MESSAGEBOX("Invalid physics attribute id when handling event of type EVENT_MODIFY_PHYSICS_OBJECT, error 2");
		}
		break;
	}
	case EVENT_CLOSEST_HIT_RAY_CAST:
		{
			Event_ClosestHitRayCast* event_ClosestRayCast = static_cast<Event_ClosestHitRayCast*>(e);
			handleEvent_ClosestRayCast(event_ClosestRayCast);
		break;
		}
	case EVENT_ALL_HITS_RAY_CAST:
		{
			Event_AllHitsRayCast* event_AllHitsRayCast = static_cast<Event_AllHitsRayCast*>(e);
			handleEvent_AllHitsRayCast(event_AllHitsRayCast);
		break;
		}
	case EVENT_LOAD_LEVEL_BULLET:
		CollisionShapes::Instance()->loadCollisionShapes();
		break;
	case EVENT_UNLOAD_LEVEL:
		CollisionShapes::Instance()->unloadCollisionShapes();
		break;
	case EVENT_NULL_PROCESS_STOPPED_EXECUTING:
		{
			//Reset apart-fallen world
			while(itrPhysics.hasNext())
			{
				AttributePtr<Attribute_Physics> ptr_physics = itrPhysics.getNext();
				if(ptr_physics->collisionFilterGroup == XKILL_Enums::PhysicsAttributeType::PROP)
				{
					if(physicsObjects_->at(ptr_physics.index()) != nullptr)
					{
						PropPhysicsObject* propPhysicsObject = static_cast<PropPhysicsObject*>(physicsObjects_->at(ptr_physics.index()));
						
						ptr_physics->collisionFilterGroup = XKILL_Enums::PhysicsAttributeType::WORLD;
						ptr_physics->collisionFilterMask = XKILL_Enums::PhysicsAttributeType::PLAYER | XKILL_Enums::PhysicsAttributeType::PROJECTILE |
							XKILL_Enums::PhysicsAttributeType::FRUSTUM | XKILL_Enums::PhysicsAttributeType::PICKUPABLE |
							XKILL_Enums::PhysicsAttributeType::RAY | XKILL_Enums::PhysicsAttributeType::PROP;

						ptr_physics->ptr_spatial->ptr_position->position = Float3(propPhysicsObject->worldOrigin_.x(),propPhysicsObject->worldOrigin_.y(),propPhysicsObject->worldOrigin_.z());
					
						propPhysicsObject->setCollisionFlags(propPhysicsObject->getCollisionFlags() | btCollisionObject::CF_STATIC_OBJECT);//check, might not be needed

						ptr_physics->gravity = Float3(0.0f, 0.0f, 0.0f);
						ptr_physics->linearVelocity = Float3(0.0f, 0.0f, 0.0f);
						ptr_physics->mass = 0;
						ptr_physics->collisionResponse = true;

						SEND_EVENT(&Event_ReloadPhysicsAttributeDataIntoBulletPhysics(ptr_physics.index()));
					}
				}
			}
		break;
		}
	case EVENT_RELOAD_PHYSICS_ATTRIBUTE_DATA_INTO_BULLET_PHYSICS:
		{
			Event_ReloadPhysicsAttributeDataIntoBulletPhysics* event_ReloadPhysicsAttributeDataIntoBulletPhysics = static_cast<Event_ReloadPhysicsAttributeDataIntoBulletPhysics*>(e);
			int physicsAttributeId = event_ReloadPhysicsAttributeDataIntoBulletPhysics->physicsAttributeId;
			AttributePtr<Attribute_Physics> ptr_physics = itrPhysics.at(physicsAttributeId);

			ptr_physics->reloadDataIntoBulletPhysics = true;
			synchronizeWithAttributes(ptr_physics, physicsAttributeId);

			break;
		}
	}
}
Example #21
0
//----------------------------------------------------------------------------
void GlossMaps::CreateScene ()
{
    mScene = new0 Node();
    mTrnNode = new0 Node();
    mScene->AttachChild(mTrnNode);

    // Create vertex and index buffers to be shared by two meshes.
    VertexFormat* vformat = VertexFormat::Create(3,
        VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
        VertexFormat::AU_NORMAL, VertexFormat::AT_FLOAT3, 0,
        VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0);
    int vstride = vformat->GetStride();

    VertexBuffer* vbuffer = new0 VertexBuffer(4, vstride);
    VertexBufferAccessor vba(vformat, vbuffer);
    Float3 yVector(0.0f, 1.0f, 0.0f);
    vba.Position<Float3>(0) = Float3(-0.5f, 0.0f, -0.5f);
    vba.Position<Float3>(1) = Float3(-0.5f, 0.0f,  0.5f);
    vba.Position<Float3>(2) = Float3( 0.5f, 0.0f,  0.5f);
    vba.Position<Float3>(3) = Float3( 0.5f, 0.0f, -0.5f);
    vba.Normal<Float3>(0) = yVector;
    vba.Normal<Float3>(1) = yVector;
    vba.Normal<Float3>(2) = yVector;
    vba.Normal<Float3>(3) = yVector;
    vba.TCoord<Float2>(0, 0) = Float2(1.0f, 0.0f);
    vba.TCoord<Float2>(0, 1) = Float2(1.0f, 1.0f);
    vba.TCoord<Float2>(0, 2) = Float2(0.0f, 1.0f);
    vba.TCoord<Float2>(0, 3) = Float2(0.0f, 0.0f);

    IndexBuffer* ibuffer = new0 IndexBuffer(6, sizeof(int));
    int* indices = (int*)ibuffer->GetData();
    indices[0] = 0;  indices[1] = 1;  indices[2] = 3;
    indices[3] = 3;  indices[4] = 1;  indices[5] = 2;

    // The light and material are used by both the gloss and non-gloss
    // objects.
    Light* light = new0 Light(Light::LT_DIRECTIONAL);
    light->Ambient = Float4(0.1f, 0.1f, 0.1f, 1.0f);
    light->Diffuse = Float4(0.6f, 0.6f, 0.6f, 1.0f);
    light->Specular = Float4(1.0f, 1.0f, 1.0f, 1.0f);
    light->DVector = AVector(0.0f, -1.0f, 0.0f);

    Material* material = new0 Material();
    material->Ambient = Float4(0.2f, 0.2f, 0.2f, 1.0f);
    material->Diffuse = Float4(0.7f, 0.7f, 0.7f, 1.0f);
    material->Specular = Float4(1.0f, 1.0f, 1.0f, 25.0f);

    // Create a non-gloss-mapped square.
    TriMesh* squareNoGloss = new0 TriMesh(vformat, vbuffer, ibuffer);
    squareNoGloss->LocalTransform.SetRotate(HMatrix(AVector::UNIT_X,
        -0.25f*Mathf::PI));
    squareNoGloss->LocalTransform.SetTranslate(APoint(1.0f, -1.0f, 0.0f));
    squareNoGloss->SetEffectInstance(
        LightDirPerVerEffect::CreateUniqueInstance(light, material));
    mTrnNode->AttachChild(squareNoGloss);

    // Create a gloss-mapped square.
    TriMesh* squareGloss = new0 TriMesh(vformat, vbuffer, ibuffer);
    squareGloss->LocalTransform.SetRotate(HMatrix(AVector::UNIT_X,
        -0.25f*Mathf::PI));
    squareGloss->LocalTransform.SetTranslate(APoint(-1.0f, -1.0f, 0.0f));
    mTrnNode->AttachChild(squareGloss);

    std::string effectFile = Environment::GetPathR("GlossMap.wmfx");
    GlossMapEffect* effect = new0 GlossMapEffect(effectFile);

    std::string baseName = Environment::GetPathR("Magic.wmtf");
    Texture2D* baseTexture = Texture2D::LoadWMTF(baseName);
    squareGloss->SetEffectInstance(effect->CreateInstance(baseTexture,
        light, material));
}
Example #22
0
//----------------------------------------------------------------------------
void Fluids3D::CreateScene ()
{
    // Get fluid solver parameters.
    const int bound0M1 = mSmoke->GetIMax();
    const int bound1M1 = mSmoke->GetJMax();
    const int bound2M1 = mSmoke->GetKMax();
    const int bound0 = bound0M1 + 1;
    const int bound1 = bound1M1 + 1;
    const int bound2 = bound2M1 + 1;
    const int quantity = bound0*bound1*bound2;
    const float* x = mSmoke->GetX();
    const float* y = mSmoke->GetY();
    const float* z = mSmoke->GetZ();

#ifdef USE_PARTICLES
    // Create the vertex format.
    VertexFormat* vformat = VertexFormat::Create(3,
        VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
        VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0,
        VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT4, 0);
#else
    VertexFormat* vformat = VertexFormat::Create(2,
        VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
        VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT4, 0);
#endif

    // Create the vertex buffer for the cube.
#ifdef USE_PARTICLES
    const int numVertices = 4*quantity;
#else
    const int numVertices = quantity;
#endif

    int vstride = vformat->GetStride();
    VertexBuffer* vbuffer = new0 VertexBuffer(numVertices, vstride);

    int i0, i1, i2, index;

#ifdef USE_PARTICLES
    const float delta = mSmoke->GetDx();
    Float4* posSize = new1<Float4>(quantity);
    for (i2 = 0, index = 0; i2 < bound2; ++i2)
    {
        for (i1 = 0; i1 < bound1; ++i1)
        {
            for (i0 = 0; i0 < bound0; ++i0, ++index)
            {
                posSize[index] = Float4(x[i0], y[i1], z[i2], delta);
            }
        }
    }

    mCube = new0 Particles(vformat, vbuffer, 4, posSize, 1.0f);
    UpdateVertexBuffer();

    IndexBuffer* ibuffer = mCube->GetIndexBuffer();
#else
    VertexBufferAccessor vba(vformat, vbuffer);
    for (i2 = 0, index = 0; i2 < bound2; ++i2)
    {
        for (i1 = 0; i1 < bound1; ++i1)
        {
            for (i0 = 0; i0 < bound0; ++i0, ++index)
            {
                vba.Position<Float3>(index) = Float3(x[i0], y[i1], z[i2]);
            }
        }
    }

    // Create the index buffer for the cube.
    const int numIndices =
        6*bound0M1*bound1M1*bound2 +
        6*bound0M1*bound1*bound2M1 +
        6*bound0*bound1M1*bound2M1;

    IndexBuffer* ibuffer = new0 IndexBuffer(numIndices, sizeof(int));
    int* indices = (int*)ibuffer->GetData();

    const int bound01 = bound0*bound1;
    int j0, j1, j2, j3;
    for (i2 = 0; i2 < bound2; ++i2)
    {
        for (i1 = 0; i1 < bound1M1; ++i1)
        {
            for (i0 = 0; i0 < bound0M1; ++i0)
            {
                j0 = i0 + bound0*(i1 + bound1*i2);
                j1 = j0 + 1;
                j2 = j1 + bound0;
                j3 = j2 - 1;
                *indices++ = j0;
                *indices++ = j1;
                *indices++ = j2;
                *indices++ = j0;
                *indices++ = j2;
                *indices++ = j3;
            }
        }
    }

    for (i1 = 0; i1 < bound1; ++i1)
    {
        for (i2 = 0; i2 < bound2M1; ++i2)
        {
            for (i0 = 0; i0 < bound0M1; ++i0)
            {
                j0 = i0 + bound0*(i1 + bound1*i2);
                j1 = j0 + 1;
                j2 = j1 + bound01;
                j3 = j2 - 1;
                *indices++ = j0;
                *indices++ = j1;
                *indices++ = j2;
                *indices++ = j0;
                *indices++ = j2;
                *indices++ = j3;
            }
        }
    }

    for (i0 = 0; i0 < bound0; ++i0)
    {
        for (i1 = 0; i1 < bound1M1; ++i1)
        {
            for (i2 = 0; i2 < bound2M1; ++i2)
            {
                j0 = i0 + bound0*(i1 + bound1*i2);
                j1 = j0 + bound0;
                j2 = j1 + bound01;
                j3 = j2 - bound0;
                *indices++ = j0;
                *indices++ = j1;
                *indices++ = j2;
                *indices++ = j0;
                *indices++ = j2;
                *indices++ = j3;
            }
        }
    }

    mCube = new0 TriMesh(vformat, vbuffer, ibuffer);
    UpdateVertexBuffer();
#endif

    mNumIndices = ibuffer->GetNumElements();
    mIndices = new1<int>(mNumIndices);
    memcpy(mIndices, ibuffer->GetData(), mNumIndices*sizeof(int));

    // Create the cube effect.
#ifdef USE_PARTICLES
    std::string path = Environment::GetPathR("Disk.wmtf");
    Texture2D* texture = Texture2D::LoadWMTF(path);
    VisualEffectInstance* instance =
        VertexColor4TextureEffect::CreateUniqueInstance(texture,
        Shader::SF_NEAREST, Shader::SC_CLAMP_EDGE, Shader::SC_CLAMP_EDGE);
#else
    VertexColor4Effect* effect = new0 VertexColor4Effect();
    VisualEffectInstance* instance = effect->CreateInstance();
#endif

    const VisualPass* pass = instance->GetPass(0);
    AlphaState* astate = pass->GetAlphaState();
    astate->BlendEnabled = true;

    CullState* cstate = pass->GetCullState();
    cstate->Enabled = false;

    DepthState* dstate = pass->GetDepthState();
    dstate->Enabled = false;
    dstate->Writable = false;

    mCube->SetEffectInstance(instance);

    mScene = new0 Node();
    mScene->AttachChild(mCube);
}
Example #23
0
	void MeshGroup::_genMesh(const Array<Mesh *> & arr, int first, int last, bool hasLightingColor)
	{
		MeshSourcePtr source = arr[0]->GetSource();
		Mesh * mesh = new Mesh;

		for (int i = 0; i < source->GetMeshBufferCount(); ++i)
		{
			SubMesh * submesh = mesh->NewSubMesh();
			VertexBufferPtr srcVB = source->GetMeshBuffer(i)->GetRenderOp()->vertexBuffers[0];
			IndexBufferPtr srcIB = source->GetMeshBuffer(i)->GetRenderOp()->indexBuffer;
			int p_offset = source->GetMeshBuffer(i)->GetRenderOp()->vertexDeclarations[0].GetElementOffset(eVertexSemantic::POSITION);
			int n_offset = source->GetMeshBuffer(i)->GetRenderOp()->vertexDeclarations[0].GetElementOffset(eVertexSemantic::NORMAL);
			int stride = srcVB->GetStride();

			VertexBufferPtr vb = HWBufferManager::Instance()->NewVertexBuffer(stride, srcVB->GetCount() * (last - first));
			
			const char * v_src = (const char *)srcVB->Lock(eLockFlag::READ);
			char * v_dest = (char *)vb->Lock(eLockFlag::WRITE);
			for (int j = first; j < last; ++j)
			{
				const Mat4 & worldTM = arr[j]->GetWorldTM();
				bool hasScale = arr[j]->GetWorldScale() != Float3(1, 1, 1);

				for (int k = 0; k < srcVB->GetCount(); ++k)
				{
					memcpy(v_dest, v_src, stride);

					Float3 * position = (Float3 *)(v_dest + p_offset);
					position->TransformA(worldTM);
					
					if (n_offset != -1)
					{
						Float3 * normal = (Float3 *)(v_dest + n_offset);
						normal->TransformN(worldTM);

						if (hasScale)
						{
							normal->Normalize();
						}
					}

					v_dest += stride;
					v_src += stride;
				}
			}
			vb->Unlock();
			srcVB->Unlock();

			IndexBufferPtr ib = HWBufferManager::Instance()->NewIndexBuffer(srcIB->GetCount() * (last - first));

			int startVertex = 0;
			const short * i_src = (const short *)srcIB->Lock(eLockFlag::READ);
			char * i_dest = (char *)ib->Lock(eLockFlag::WRITE);
			for (int j = first; j < last; ++j)
			{
				for (int k = 0; k < srcIB->GetCount(); ++k)
				{
					*i_dest++ = (*i_src++) + startVertex;
				}

				startVertex += srcVB->GetCount();
			}
			ib->Unlock();
			srcIB->Unlock();
			
			submesh->GetRenderOp()->vertexDeclarations[0] = source->GetMeshBuffer(i)->GetRenderOp()->vertexDeclarations[0];
			submesh->GetRenderOp()->vertexBuffers[0] = vb;
			submesh->GetRenderOp()->indexBuffer = ib;
			submesh->GetRenderOp()->primCount = ib->GetCount() / 3;
			submesh->GetRenderOp()->primType = ePrimType::TRIANGLE_LIST;

			if (hasLightingColor)
			{
				int count = submesh->GetRenderOp()->vertexBuffers[0]->GetCount();
				submesh->GetRenderOp()->vertexDeclarations[LIGHTING_COLOR_STREAM].AddElement(eVertexSemantic::LIGHTING_COLOR, eVertexType::UBYTE4);
				submesh->GetRenderOp()->vertexBuffers[LIGHTING_COLOR_STREAM] = HWBufferManager::Instance()->NewVertexBuffer(4, count);

				Array<Rgba32> lightColors;
				Rgba32 * data = (Rgba32 *)submesh->GetRenderOp()->vertexBuffers[LIGHTING_COLOR_STREAM]->Lock(eLockFlag::WRITE);
				for (int j = first; j < last; ++j)
				{
					arr[j]->GetLightingColor(lightColors);
					d_assert (lightColors.Size() > 0);

					memcpy(data, &lightColors[0], 4 * count);

					startVertex += count;
					lightColors.Clear();
				}
				submesh->GetRenderOp()->vertexBuffers[LIGHTING_COLOR_STREAM]->Unlock();
			}

			*submesh->GetMaterial() = *source->GetMeshBuffer(i)->GetMaterial();

			submesh->SetMeshShader(source->GetMeshBuffer(i)->GetShader());
		}

		mesh->SetSLMode(hasLightingColor ? eStaticLightingMode::LIGHTING_COLOR : eStaticLightingMode::NONE);

		mMeshes.PushBack(mesh);
	}
Example #24
0
TiledDatasetView::TiledDatasetView() :
centerDataSpace( Float3( -1.0f, -1.0f, -1.0f ) ),
extentDataSpace( Float3( -1.0f, -1.0f, -1.0f ) ),
widthNumPixels ( -1 )
{
}
Example #25
0
Float3 Float4x4::TranslatePart() const {
	return Float3( m_value[0][3], m_value[1][3], m_value[2][3] );
}
Example #26
0
			const Float3 Quaternion::Axis() const
			{
				const float s = pow( 1 - w * w, 0.5f );
				return Float3( x/s, y/s, z/s );
			}
//----------------------------------------------------------------------------
void ReflectionsAndShadows::CreatePlanes ()
{
    VertexFormat* vformat = VertexFormat::Create(2,
        VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
        VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0);
    int vstride = vformat->GetStride();

    // Create the floor mesh.
    VertexBuffer* vbuffer = new0 VertexBuffer(4, vstride);
    VertexBufferAccessor floor(vformat, vbuffer);

    float xValue = 128.0f;
    float yValue = 256.0f;
    float zValue = 0.0f;
    floor.Position<Float3>(0) = Float3(-xValue, -yValue, zValue);
    floor.Position<Float3>(1) = Float3(+xValue, -yValue, zValue);
    floor.Position<Float3>(2) = Float3(+xValue, +yValue, zValue);
    floor.Position<Float3>(3) = Float3(-xValue, +yValue, zValue);
    floor.TCoord<Float2>(0, 0) = Float2(0.0f, 0.0f);
    floor.TCoord<Float2>(0, 1) = Float2(1.0f, 0.0f);
    floor.TCoord<Float2>(0, 2) = Float2(1.0f, 1.0f);
    floor.TCoord<Float2>(0, 3) = Float2(0.0f, 1.0f);

    IndexBuffer* ibuffer = new0 IndexBuffer(6, sizeof(int));
    int* indices = (int*)ibuffer->GetData();
    indices[0] = 0;  indices[1] = 1;  indices[2] = 2;
    indices[3] = 0;  indices[4] = 2;  indices[5] = 3;

    mPlane0 = new0 TriMesh(vformat, vbuffer, ibuffer);

    Texture2DEffect* effect = new0 Texture2DEffect(Shader::SF_LINEAR_LINEAR,
        Shader::SC_REPEAT, Shader::SC_REPEAT);
    std::string path = Environment::GetPathR("Sand.wmtf");
    Texture2D* texture = Texture2D::LoadWMTF(path);
    mPlane0->SetEffectInstance(effect->CreateInstance(texture));

    mScene->AttachChild(mPlane0);

    // Create the wall mesh.
    vbuffer = new0 VertexBuffer(4, vstride);
    VertexBufferAccessor wall(vformat, vbuffer);

    xValue = -128.0f;
    yValue = 256.0f;
    zValue = 128.0f;
    wall.Position<Float3>(0) = Float3(xValue, -yValue, 0.0f);
    wall.Position<Float3>(1) = Float3(xValue, +yValue, 0.0f);
    wall.Position<Float3>(2) = Float3(xValue, +yValue, zValue);
    wall.Position<Float3>(3) = Float3(xValue, -yValue, zValue);
    wall.TCoord<Float2>(0, 0) = Float2(0.0f, 0.0f);
    wall.TCoord<Float2>(0, 1) = Float2(1.0f, 0.0f);
    wall.TCoord<Float2>(0, 2) = Float2(1.0f, 1.0f);
    wall.TCoord<Float2>(0, 3) = Float2(0.0f, 1.0f);

    mPlane1 = new0 TriMesh(vformat, vbuffer, ibuffer);

    path = Environment::GetPathR("Stone.wmtf");
    texture = Texture2D::LoadWMTF(path);
    mPlane1->SetEffectInstance(effect->CreateInstance(texture));

    mScene->AttachChild(mPlane1);
}
Example #28
0
Float3<T> Float3<T>::scaledBy(T m) const
{
	return Float3(this->x * m, this->y * m, this->z * m);
}
Example #29
0
void ImGui::render()
{
    for (auto command : commands)
    {
        switch (command.type)
        {
        case Command::Rectangle:
        {
            decltype(Command::rect) rect = command.rect;

            gfxApi->pushState();

            gfxApi->setBlendingEnabled(true);
            gfxApi->setBlendFactors(GfxSrcAlpha,
                                    GfxSrcAlpha,
                                    GfxOneMinusSrcAlpha,
                                    GfxOneMinusSrcAlpha);
            gfxApi->setDepthFunction(GfxAlways);

            gfxApi->begin(compiledVertex, nullptr, nullptr, nullptr, compiledFragment);
            gfxApi->setMesh(mesh);

            float w = gfxApi->getViewportWidth();
            float h = gfxApi->getViewportHeight();
            float fleft = rect.left / w * 2.0f - 1.0f;
            float fright = rect.right / w * 2.0f - 1.0f;
            float ftop = rect.top / h * 2.0f - 1.0f;
            float fbottom = rect.bottom / h * 2.0f - 1.0f;

            gfxApi->uniform(compiledVertex, "offset", Float2(fleft, fbottom));
            gfxApi->uniform(compiledVertex, "size", Float2(fright-fleft, ftop-fbottom));
            gfxApi->uniform(compiledVertex, "gradientStart", rect.gradientStart);
            gfxApi->uniform(compiledVertex, "gradientSize", rect.gradientSize);
            gfxApi->uniform(compiledVertex, "gradientPower", rect.gradientPower);
            gfxApi->uniform(compiledVertex, "color", Float3(rect.colorR, rect.colorG, rect.colorB));
            gfxApi->uniform(compiledFragment, "topRight", Float2(rect.right, rect.top));
            gfxApi->uniform(compiledFragment, "bottomLeft", Float2(rect.left, rect.bottom));
            gfxApi->uniform(compiledFragment, "brightness", rect.brightness);
            gfxApi->uniform(compiledFragment, "topLeftCornerRoundness", rect.topLeftCornerRoundness);
            gfxApi->uniform(compiledFragment, "topRightCornerRoundness", rect.topRightCornerRoundness);
            gfxApi->uniform(compiledFragment, "bottomLeftCornerRoundness", rect.bottomLeftCornerRoundness);
            gfxApi->uniform(compiledFragment, "bottomRightCornerRoundness", rect.bottomRightCornerRoundness);

            gfxApi->draw();
            gfxApi->end();

            gfxApi->popState();
            break;
        }
        case Command::Text:
        {
            float w = gfxApi->getViewportWidth();
            float h = gfxApi->getViewportHeight();
            float fleft = command.text.left / w * 2.0f - 1.0f;
            float fbottom = command.text.bottom / h * 2.0f - 1.0f;

            font->render(command.text.size,
                         Float2(fleft, fbottom),
                         command.textStr.getData(),
                         nullptr,
                         Float3(command.text.colorR,
                                command.text.colorG,
                                command.text.colorB));
            break;
        }
        case Command::PushScissor:
        {
            gfxApi->pushState();

            gfxApi->setScissorEnabled(true);
            gfxApi->setScissor(command.scissor.left,
                               command.scissor.bottom,
                               command.scissor.right-command.scissor.left,
                               command.scissor.top-command.scissor.bottom);
            break;
        }
        case Command::PopScissor:
        {
            gfxApi->popState();
            break;
        }
        default:
        {
            SDL_assert(false);
            break;
        }
        }
    }

    commands.clear();
}
Example #30
0
//----------------------------------------------------------------------------
void VolumeTextures::CreateScene ()
{
	mScene = new0 Node();
	mAlphaState = new0 AlphaState();
	mAlphaState->BlendEnabled = true;
	mRenderer->SetOverrideAlphaState(mAlphaState);
	mCullState = new0 CullState();
	mCullState->Enabled = false;
	mRenderer->SetOverrideCullState(mCullState);

	// Create the grid of square meshes.
	const int numSlices = 64;
	const int numSamples = 32;

	// The vertex format that is shared by all square meshes.
	VertexFormat* vformat = VertexFormat::Create(2,
	                        VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
	                        VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT3, 0);
	int vstride = vformat->GetStride();

	// The index buffer that is shared by all square meshes.
	int numIndices = 6*(numSamples-1)*(numSamples-1);
	IndexBuffer* ibuffer = new0 IndexBuffer(numIndices, sizeof(int));
	int* indices = (int*)ibuffer->GetData();
	for (int i1 = 0; i1 < numSamples - 1; ++i1)
	{
		for (int i0 = 0; i0 < numSamples - 1; ++i0)
		{
			int v0 = i0 + numSamples * i1;
			int v1 = v0 + 1;
			int v2 = v1 + numSamples;
			int v3 = v0 + numSamples;
			*indices++ = v0;
			*indices++ = v1;
			*indices++ = v2;
			*indices++ = v0;
			*indices++ = v2;
			*indices++ = v3;
		}
	}

	// Create the volume texture.  Three Gaussian distributions are used for
	// the RGB color channels.  The alpha channel is constant.
	const int bound = 64;
	Texture3D* texture = new0 Texture3D(Texture::TF_A8R8G8B8, bound, bound,
	                                    bound, 1);
	unsigned char* data = (unsigned char*)texture->GetData(0);
	const float mult = 1.0f/(bound - 1.0f);
	const float rParam = 0.01f;
	const float gParam = 0.01f;
	const float bParam = 0.01f;
	const float extreme = 8.0f;
	APoint rCenter( 0.5f*extreme,  0.0f,         0.0f);
	APoint gCenter(-0.5f*extreme, -0.5f*extreme, 0.0f);
	APoint bCenter(-0.5f*extreme, +0.5f*extreme, 0.0f);
	unsigned char commonAlpha = 12;
	APoint point;
	for (int z = 0; z < bound; ++z)
	{
		point[2] = -extreme + 2.0f*extreme*mult*z;
		for (int y = 0; y < bound; ++y)
		{
			point[1] = -extreme + 2.0f*extreme*mult*y;
			for (int x = 0; x < bound; ++x)
			{
				point[0] = -extreme + 2.0f*extreme*mult*x;

				AVector diff = point - rCenter;
				float sqrLength = diff.SquaredLength();
				float rGauss = 1.0f - rParam*sqrLength;
				if (rGauss < 0.0f)
				{
					rGauss = 0.0f;
				}

				diff = point - gCenter;
				sqrLength = diff.SquaredLength();
				float gGauss = 1.0f - gParam*sqrLength;
				if (gGauss < 0.0f)
				{
					gGauss = 0.0f;
				}

				diff = point - bCenter;
				sqrLength = diff.SquaredLength();
				float bGauss = 1.0f - bParam*sqrLength;
				if (bGauss < 0.0f)
				{
					bGauss = 0.0f;
				}

				*data++ = (unsigned char)(255.0f*bGauss);
				*data++ = (unsigned char)(255.0f*gGauss);
				*data++ = (unsigned char)(255.0f*rGauss);
				*data++ = commonAlpha;
			}
		}
	}

	// The volume texture effect that is shared by all square meshes.
	std::string effectFile = Environment::GetPathR("VolumeTextures.wmfx");
	VolumeTextureEffect* effect = new0 VolumeTextureEffect(effectFile);
	VisualEffectInstance* instance = effect->CreateInstance(texture);

	// The grid of squares.
	const int numVertices = numSamples*numSamples;
	float inv = 1.0f/(numSamples - 1.0f);
	VertexBufferAccessor vba;
	for (int slice = 0; slice < numSlices; ++slice)
	{
		VertexBuffer* vbuffer = new0 VertexBuffer(numVertices, vstride);
		vba.ApplyTo(vformat, vbuffer);

		float w = slice/(numSlices - 1.0f);
		float z = 2.0f*w - 1.0f;
		for (int i1 = 0, i = 0; i1 < numSamples; ++i1)
		{
			float v = i1*inv;
			float y = 2.0f*v - 1.0f;
			for (int i0 = 0; i0 < numSamples; ++i0, ++i)
			{
				float u = i0*inv;
				float x = 2.0f*u - 1.0f;
				vba.Position<Float3>(i) = Float3(x, y, z);
				vba.TCoord<Float3>(0, i) = Float3(u, v, w);
			}
		}

		TriMesh* mesh = new0 TriMesh(vformat, vbuffer, ibuffer);
		mesh->SetEffectInstance(instance);
		mScene->AttachChild(mesh);
	}
}