Пример #1
0
CollisionParticleEmitter::CollisionParticleEmitter(DAVA::Entity *entity, btCollisionWorld *word, DAVA::ParticleEmitter *emitter)
	: CollisionBaseObject(entity, word)
{
	if(NULL != emitter && NULL != word)
	{
		DAVA::Matrix4 curEntityTransform = entity->GetWorldTransform();
		DAVA::Vector3 pos = curEntityTransform.GetTranslationVector();

		btObject = new btCollisionObject();
		btShape = new btBoxShape(btVector3(boxHalfSize, boxHalfSize, boxHalfSize));

		btObject->setCollisionShape(btShape);

		btTransform trans;
		trans.setIdentity();
		trans.setOrigin(btVector3(pos.x, pos.y, pos.z));
		btObject->setWorldTransform(trans);

		btWord->addCollisionObject(btObject);

		boundingBox = DAVA::AABBox3(DAVA::Vector3(), boxHalfSize * 2);
	}
}
CollisionRenderObject::CollisionRenderObject(DAVA::Entity *entity, btCollisionWorld *word, DAVA::RenderObject *renderObject)
    : CollisionBaseObject(entity, word)
    , btTriangles(NULL)
    , btShape(NULL)
{
    if(NULL != renderObject && NULL != word)
    {
        bool anyPolygonAdded = false;
        DAVA::Matrix4 curEntityTransform = entity->GetWorldTransform();

        DAVA::AABBox3 commonBox;

        int maxVertexCount = 0;
        int bestLodIndex = 0;
        int curSwitchIndex = renderObject->GetSwitchIndex();

        // search for best lod index
        for(DAVA::uint32 i = 0; i < renderObject->GetRenderBatchCount(); ++i)
        {
            int batchLodIndex;
            int batchSwitchIndex;

            DAVA::RenderBatch* batch = renderObject->GetRenderBatch(i, batchLodIndex, batchSwitchIndex);
            int vertexCount = batch->GetPolygonGroup()->GetVertexCount();
            if(vertexCount > maxVertexCount && curSwitchIndex == batchSwitchIndex)
            {
                bestLodIndex = batchLodIndex;
                maxVertexCount = vertexCount;
            }
        }

        for(DAVA::uint32 i = 0; i < renderObject->GetRenderBatchCount(); ++i)
        {
            int batchLodIndex;
            int batchSwitchIndex;

            DAVA::RenderBatch* batch = renderObject->GetRenderBatch(i, batchLodIndex, batchSwitchIndex);
            if(batchLodIndex == bestLodIndex && batchSwitchIndex == curSwitchIndex)
            {
                DAVA::PolygonGroup* pg = batch->GetPolygonGroup();

                if(NULL != pg)
                {
                    // is this the first polygon in cycle
                    if(!anyPolygonAdded)
                    {
                        anyPolygonAdded = true;
                        btTriangles = new btTriangleMesh();
                    }

                    for(int i = 0; i < pg->indexCount; i += 3 )
                    {
                        DAVA::uint16 index0 = pg->indexArray[i];
                        DAVA::uint16 index1 = pg->indexArray[i+1];
                        DAVA::uint16 index2 = pg->indexArray[i+2];

                        DAVA::Vector3 v;
                        pg->GetCoord(index0, v);
                        v = v * curEntityTransform;
                        btVector3 vertex0(v.x, v.y, v.z);

                        pg->GetCoord(index1, v);
                        v = v * curEntityTransform;
                        btVector3 vertex1(v.x, v.y, v.z);

                        pg->GetCoord(index2, v);
                        v = v * curEntityTransform;
                        btVector3 vertex2(v.x, v.y, v.z);

                        btTriangles->addTriangle(vertex0, vertex1, vertex2, false);
                    }

                    // save original bbox
                    boundingBox.AddAABBox(pg->GetBoundingBox());
                }
            }
        }

        if(anyPolygonAdded)
        {
            // increase bbox a a little bit
            boundingBox.AddPoint(boundingBox.min - DAVA::Vector3(0.5f, 0.5f, 0.5f));
            boundingBox.AddPoint(boundingBox.max + DAVA::Vector3(0.5f, 0.5f, 0.5f));

            DAVA::Vector3 pos = curEntityTransform.GetTranslationVector();

            btObject = new btCollisionObject();
            btShape = new btBvhTriangleMeshShape(btTriangles, true, true);

            btObject->setCollisionShape(btShape);
            btWord->addCollisionObject(btObject);
        }
    }
}
void ModificationWidget::ApplyScaleValues(ST_Axis axis)
{
	DAVA::float32 scaleValue = 1.0f;

	switch (axis)
	{
	case ST_AXIS_X:
		scaleValue = xAxisModify->value();
		break;
	case ST_AXIS_Y:
		scaleValue = yAxisModify->value();
		break;
	case ST_AXIS_Z:
		scaleValue = zAxisModify->value();
		break;
	default:
		break;
	}

	if(NULL != curScene)
	{
		EntityGroup selection = curScene->selectionSystem->GetSelection();

		if(selection.Size() > 1)
		{
			curScene->BeginBatch("Multiple transform");
		}

		for (size_t i = 0; i < selection.Size(); ++i)
		{
			DAVA::Entity *entity = selection.GetEntity(i);
			DAVA::Matrix4 origMatrix = entity->GetLocalTransform();

			DAVA::Vector3 pos, scale, rotate;
			if(origMatrix.Decomposition(pos, scale, rotate))
			{
				DAVA::Matrix4 newMatrix;
				DAVA::Matrix4 scaleMatrix;
				DAVA::Matrix4 transformMatrix;

				DAVA::Matrix4 moveToZeroPos;
				DAVA::Matrix4 moveFromZeroPos;

				moveToZeroPos.CreateTranslation(-origMatrix.GetTranslationVector());
				moveFromZeroPos.CreateTranslation(origMatrix.GetTranslationVector());

				DAVA::float32 newEntityScale;
				if(pivotMode == PivotAbsolute)
				{
					if(0 != scale.x)
					{
						newEntityScale = scaleValue / scale.x;
					}
					else
					{
						newEntityScale = 0;
					}
				}
				else
				{
					newEntityScale = scaleValue;
				}

				scaleMatrix.CreateScale(DAVA::Vector3(newEntityScale, newEntityScale, newEntityScale));
				newMatrix = origMatrix * moveToZeroPos * scaleMatrix * moveFromZeroPos;
				newMatrix.SetTranslationVector(origMatrix.GetTranslationVector());

				curScene->Exec(new TransformCommand(entity,	origMatrix, newMatrix));
			}
		}

		if(selection.Size() > 1)
		{
			curScene->EndBatch();
		}
	}
}
void ModificationWidget::ApplyRotateValues(ST_Axis axis)
{
	DAVA::float32 x = DAVA::DegToRad(xAxisModify->value());
	DAVA::float32 y = DAVA::DegToRad(yAxisModify->value());
	DAVA::float32 z = DAVA::DegToRad(zAxisModify->value());

	if(NULL != curScene)
	{
		EntityGroup selection = curScene->selectionSystem->GetSelection();

		if(selection.Size() > 1)
		{
			curScene->BeginBatch("Multiple transform");
		}

		for (size_t i = 0; i < selection.Size(); ++i)
		{
			DAVA::Entity *entity = selection.GetEntity(i);
			DAVA::Matrix4 origMatrix = entity->GetLocalTransform();

			DAVA::Vector3 pos, scale, rotate;
			if(origMatrix.Decomposition(pos, scale, rotate))
			{
				DAVA::Matrix4 newMatrix;
				DAVA::Matrix4 rotationMatrix;
				DAVA::Matrix4 transformMatrix;

				DAVA::Matrix4 moveToZeroPos;
				DAVA::Matrix4 moveFromZeroPos;

				moveToZeroPos.CreateTranslation(-origMatrix.GetTranslationVector());
				moveFromZeroPos.CreateTranslation(origMatrix.GetTranslationVector());

				if(pivotMode == PivotAbsolute)
				{
					switch (axis)
					{
					case ST_AXIS_X:
						rotationMatrix.CreateRotation(DAVA::Vector3(1, 0, 0), x - rotate.x);
						break;
					case ST_AXIS_Y:
						rotationMatrix.CreateRotation(DAVA::Vector3(0, 1, 0), y - rotate.y);
						break;
					case ST_AXIS_Z:
						rotationMatrix.CreateRotation(DAVA::Vector3(0, 0, 1), z - rotate.z);
						break;
					default:
						break;
					}
				}
				else
				{
					switch (axis)
					{
					case ST_AXIS_X:
						rotationMatrix.CreateRotation(DAVA::Vector3(1, 0, 0), x);
						break;
					case ST_AXIS_Y:
						rotationMatrix.CreateRotation(DAVA::Vector3(0, 1, 0), y);
						break;
					case ST_AXIS_Z:
						rotationMatrix.CreateRotation(DAVA::Vector3(0, 0, 1), z);
						break;
					default:
						break;
					}
				}

				newMatrix = origMatrix * moveToZeroPos * rotationMatrix * moveFromZeroPos;
				newMatrix.SetTranslationVector(origMatrix.GetTranslationVector());

				curScene->Exec(new TransformCommand(entity,	origMatrix, newMatrix));
			}
		}

		if(selection.Size() > 1)
		{
			curScene->EndBatch();
		}
	}
}
void ModificationWidget::ApplyMoveValues(ST_Axis axis)
{
	DAVA::float32 x = xAxisModify->value();
	DAVA::float32 y = yAxisModify->value();
	DAVA::float32 z = zAxisModify->value();

	if(NULL != curScene)
	{
		EntityGroup selection = curScene->selectionSystem->GetSelection();

		if(selection.Size() > 1)
		{
			curScene->BeginBatch("Multiple transform");
		}

		for (size_t i = 0; i < selection.Size(); ++i)
		{
			DAVA::Entity *entity = selection.GetEntity(i);
			DAVA::Matrix4 origMatrix = entity->GetLocalTransform();
			DAVA::Vector3 origPos = origMatrix.GetTranslationVector();
			DAVA::Vector3 newPos = origPos;

			if(pivotMode == PivotAbsolute)
			{
				switch (axis)
				{
				case ST_AXIS_X:
					newPos.x = x;
					break;
				case ST_AXIS_Y:
					newPos.y = y;
					break;
				case ST_AXIS_Z:
					newPos.z = z;
					break;
				default:
					break;
				}
			}
			else
			{
				switch (axis)
				{
				case ST_AXIS_X:
					newPos.x += x;
					break;
				case ST_AXIS_Y:
					newPos.y += y;
					break;
				case ST_AXIS_Z:
					newPos.z += z;
					break;
				default:
					break;
				}
			}

			DAVA::Matrix4 newMatrix = origMatrix;
			newMatrix.SetTranslationVector(newPos);

			curScene->Exec(new TransformCommand(entity,	origMatrix, newMatrix));
		}

		if(selection.Size() > 1)
		{
			curScene->EndBatch();
		}
	}
}
void ModificationWidget::ReloadValues()
{
	if(modifMode == ST_MODIF_SCALE)
	{
		xLabel->setText("Scale:");

		yLabel->setVisible(false);
		zLabel->setVisible(false);
		yAxisModify->setVisible(false);
		zAxisModify->setVisible(false);
	}
	else
	{
		xLabel->setText("X:");

		yLabel->setVisible(true);
		zLabel->setVisible(true);
		yAxisModify->setVisible(true);
		zAxisModify->setVisible(true);
	}

	if(NULL != curScene)
	{
		EntityGroup selection = curScene->selectionSystem->GetSelection();
		if(selection.Size() > 0 && (modifMode == ST_MODIF_MOVE || modifMode == ST_MODIF_ROTATE || modifMode == ST_MODIF_SCALE))
		{
			xAxisModify->setEnabled(true);
			yAxisModify->setEnabled(true);
			zAxisModify->setEnabled(true);

			xAxisModify->showButtons(true);
			yAxisModify->showButtons(true);
			zAxisModify->showButtons(true);

			if(selection.Size() > 1)
			{
				groupMode = true;

				if(pivotMode == PivotRelative)
				{
					xAxisModify->setValue(0);
					yAxisModify->setValue(0);
					zAxisModify->setValue(0);
				}
				else
				{
					xAxisModify->showButtons(false);
					yAxisModify->showButtons(false);
					zAxisModify->showButtons(false);

					xAxisModify->clear();
					yAxisModify->clear();
					zAxisModify->clear();
				}
			}
			else
			{
				groupMode = false;

				if(pivotMode == PivotRelative)
				{
					xAxisModify->setValue(0);
					yAxisModify->setValue(0);
					zAxisModify->setValue(0);
				}
				else
				{
					DAVA::Entity *singleEntity = selection.GetEntity(0);
					if(NULL != singleEntity)
					{

						DAVA::float32 x = 0;
						DAVA::float32 y = 0;
						DAVA::float32 z = 0;

						DAVA::Matrix4 localMatrix = singleEntity->GetLocalTransform();
						switch (modifMode)
						{
						case ST_MODIF_MOVE:
							{
								DAVA::Vector3 translation = localMatrix.GetTranslationVector();
								x = translation.x;
								y = translation.y;
								z = translation.z;
							}
							break;
						case ST_MODIF_ROTATE:
							{
								DAVA::Vector3 pos, scale, rotate;
								if(localMatrix.Decomposition(pos, scale, rotate))
								{
									x = DAVA::RadToDeg(rotate.x);
									y = DAVA::RadToDeg(rotate.y);
									z = DAVA::RadToDeg(rotate.z);
								}
							}
							break;
						case ST_MODIF_SCALE:
							{
								DAVA::Vector3 pos, scale, rotate;
								if(localMatrix.Decomposition(pos, scale, rotate))
								{
									x = scale.x;
									y = scale.y;
									z = scale.z;
								}
							}
							break;
						default:
							break;
						}

						xAxisModify->setValue(x);
						yAxisModify->setValue(y);
						zAxisModify->setValue(z);
					}
				}
			}
		}
		else
		{
			xAxisModify->showButtons(true);
			yAxisModify->showButtons(true);
			zAxisModify->showButtons(true);

			xAxisModify->setEnabled(false);
			yAxisModify->setEnabled(false);
			zAxisModify->setEnabled(false);

			xAxisModify->clear();
			yAxisModify->clear();
			zAxisModify->clear();
		}
	}
}