PxVec3 CameraComponent::worldToScreenPoint(const PxVec3& position) const
{
	PxVec4 clip = m_camera.getViewProjection().transform(PxVec4(position, 1.0));
	PxVec3 ndc(clip.x / clip.w, clip.y / clip.w, clip.z / clip.w);
	PxVec2 screenDim((PxReal)getCoreEngine()->getViewport()->getScreenWidth(), (PxReal)getCoreEngine()->getViewport()->getScreenHeight());
	return PxVec3((screenDim.x / 2.0f) * ndc.x + (screenDim.x / 2.0f), screenDim.y - ((screenDim.y / 2.0f) * ndc.y + (screenDim.y / 2.0f)), clip.z);
}
void GUIOnCamera::onStart()
{
	m_mainCamera = getGameObjectByName("camera");
	m_mainCameraComponent = m_mainCamera->getGameComponent<CameraComponent>();

	m_top = getGameObjectByName("top");
	m_bottom = getGameObjectByName("bottom");
	m_right = getGameObjectByName("right");
	m_left = getGameObjectByName("left");

	m_topRight = getGameObjectByName("top right");
	m_topLeft = getGameObjectByName("top left");
	m_bottomRight = getGameObjectByName("bottom right");
	m_bottomLeft = getGameObjectByName("bottom left");

	PxVec3 screen = m_mainCameraComponent->worldToScreenPoint(*m_bottom->getTransform()->getPosition());

	m_image = new GUIImage(PxVec4(0.0f, 0.0f, 0.2f, 0.2f), PxVec4(screen, 0.0f), "Textures/MegaFox.png");
	instantiate((new GameObject("GUI test image"))->addGUIComponent(m_image));
}
Range<PxVec4> ClothImpl<SwCloth>::getParticleAccelerations()
{
	if(mCloth.mParticleAccelerations.empty())
	{
		uint32_t n = mCloth.mCurParticles.size();
		mCloth.mParticleAccelerations.resize(n, PxVec4(0.0f));
	}

	mCloth.wakeUp();

	PxVec4* data = &mCloth.mParticleAccelerations.front();
	return Range<PxVec4>(data, data + mCloth.mParticleAccelerations.size());
}
void ClothImpl<SwCloth>::setVirtualParticles(Range<const uint32_t[4]> indices, Range<const PxVec3> weights)
{
	mCloth.mNumVirtualParticles = 0;

	// shuffle indices to form independent SIMD sets
	uint16_t numParticles = uint16_t(mCloth.mCurParticles.size());
	TripletScheduler scheduler(indices);
	scheduler.simd(numParticles, 4);

	// convert indices to byte offset
	Vec4us dummy(numParticles, uint16_t(numParticles+1), uint16_t(numParticles+2), 0); 
	Vector<uint32_t>::Type::ConstIterator sIt = scheduler.mSetSizes.begin();
	Vector<uint32_t>::Type::ConstIterator sEnd = scheduler.mSetSizes.end();
	TripletScheduler::ConstTripletIter tIt = scheduler.mTriplets.begin(), tLast;
	mCloth.mVirtualParticleIndices.resize(0);
	mCloth.mVirtualParticleIndices.reserve(indices.size() + 3 * uint32_t(sEnd - sIt));
	for(; sIt != sEnd; ++sIt)
	{
		uint32_t setSize = *sIt;
		for(tLast = tIt + setSize; tIt != tLast; ++tIt, ++mCloth.mNumVirtualParticles)
			mCloth.mVirtualParticleIndices.pushBack(Vec4us(*tIt));
		mCloth.mVirtualParticleIndices.resize(
			(mCloth.mVirtualParticleIndices.size() + 3) & ~3, dummy);
	}
	Vector<Vec4us>::Type(mCloth.mVirtualParticleIndices.begin(), 
		mCloth.mVirtualParticleIndices.end()).swap(mCloth.mVirtualParticleIndices);

	// precompute 1/dot(w,w)
	Vec4fAlignedVector().swap(mCloth.mVirtualParticleWeights);
	mCloth.mVirtualParticleWeights.reserve(weights.size());
	for(; !weights.empty(); weights.popFront())
	{
		PxVec3 w = reinterpret_cast<const PxVec3&>(weights.front());
		PxReal scale = 1 / w.magnitudeSquared();
		mCloth.mVirtualParticleWeights.pushBack(PxVec4(w.x, w.y, w.z, scale));
	}

	mCloth.notifyChanged();
}
// Transform Vec4 (tangent) by PxMat33, ignoring tangent.w
PX_INLINE void transform_FLOAT4_by_PxMat33(FLOAT4_TYPE& dst, const FLOAT4_TYPE& src, const PxMat33& m)
{
	const PxVec4 source = (const PxVec4&)src;
	(PxVec4&)dst = PxVec4(m * source.getXYZ(), PxSign(m.getDeterminant()) * source.w);
}
bool ApexVertexBuffer::mergeBinormalsIntoTangents()
{
	const uint32_t numBuffers = mFormat.getBufferCount();

	int32_t normalBufferIndex = -1;
	int32_t tangentBufferIndex = -1;
	int32_t binormalBufferIndex = -1;
	for (uint32_t i = 0; i < numBuffers; i++)
	{
		const RenderVertexSemantic::Enum semantic  = mFormat.getBufferSemantic(i);
		const RenderDataFormat::Enum format = mFormat.getBufferFormat(i);
		if (semantic == RenderVertexSemantic::NORMAL && format == RenderDataFormat::FLOAT3)
		{
			normalBufferIndex = (int32_t)i;
		}
		else if (semantic == RenderVertexSemantic::TANGENT && format == RenderDataFormat::FLOAT3)
		{
			tangentBufferIndex = (int32_t)i;
		}
		else if (semantic == RenderVertexSemantic::BINORMAL && format == RenderDataFormat::FLOAT3)
		{
			binormalBufferIndex = (int32_t)i;
		}
	}

	if (normalBufferIndex != -1 && tangentBufferIndex != -1 && binormalBufferIndex != -1)
	{
		// PH: This gets dirty. modifying the parameterized object directly
		BufferF32x3* normalsBuffer = static_cast<BufferF32x3*>(mParams->buffers.buf[normalBufferIndex]);
		BufferF32x3* oldTangentsBuffer = static_cast<BufferF32x3*>(mParams->buffers.buf[tangentBufferIndex]);
		BufferF32x3* oldBinormalsBuffer = static_cast<BufferF32x3*>(mParams->buffers.buf[binormalBufferIndex]);
		BufferF32x4* newTangentsBuffer = static_cast<BufferF32x4*>(GetInternalApexSDK()->getParameterizedTraits()->createNvParameterized("BufferF32x4"));

		if (normalsBuffer != NULL && oldTangentsBuffer != NULL && oldBinormalsBuffer != NULL && newTangentsBuffer != NULL)
		{
			const uint32_t numElements = (uint32_t)oldTangentsBuffer->data.arraySizes[0];

			PX_ASSERT(oldTangentsBuffer->data.arraySizes[0] == oldBinormalsBuffer->data.arraySizes[0]);
			{
				// resize the array
				NvParameterized::Handle handle(*newTangentsBuffer, "data");
				PX_ASSERT(handle.isValid());
				handle.resizeArray((int32_t)numElements);
			}
			PX_ASSERT(oldTangentsBuffer->data.arraySizes[0] == newTangentsBuffer->data.arraySizes[0]);

			const PxVec3* normals = normalsBuffer->data.buf;
			const PxVec3* oldTangents = oldTangentsBuffer->data.buf;
			const PxVec3* oldBinormals = oldBinormalsBuffer->data.buf;
			PxVec4* newTangents = (PxVec4*)newTangentsBuffer->data.buf;

			for (uint32_t i = 0; i < numElements; i++)
			{
				const float binormal = PxSign(normals[i].cross(oldTangents[i]).dot(oldBinormals[i]));
				newTangents[i] = PxVec4(oldTangents[i], binormal);
			}

			// Ok, real dirty now
			mParams->buffers.buf[(uint32_t)tangentBufferIndex] = newTangentsBuffer;
			for (uint32_t i = (uint32_t)binormalBufferIndex + 1; i < numBuffers; i++)
			{
				mParams->buffers.buf[i - 1] = mParams->buffers.buf[i];
			}
			mParams->buffers.buf[numBuffers - 1] = NULL;
			{
				NvParameterized::Handle handle(*mParams, "buffers");
				PX_ASSERT(handle.isValid());
				handle.resizeArray((int32_t)numBuffers - 1);
			}
			oldTangentsBuffer->destroy();
			oldBinormalsBuffer->destroy();

			// and make same change to the format too
			VertexFormatParameters* format = static_cast<VertexFormatParameters*>(mParams->vertexFormat);
			PX_ASSERT(format->bufferFormats.buf[tangentBufferIndex].semantic == RenderVertexSemantic::TANGENT);
			PX_ASSERT(format->bufferFormats.buf[tangentBufferIndex].format == RenderDataFormat::FLOAT3);
			format->bufferFormats.buf[tangentBufferIndex].format = RenderDataFormat::FLOAT4;

			VertexFormatParametersNS::BufferFormat_Type binormalBuffer = format->bufferFormats.buf[binormalBufferIndex];
			for (uint32_t i = (uint32_t)binormalBufferIndex + 1; i < numBuffers; i++)
			{
				format->bufferFormats.buf[i - 1] = format->bufferFormats.buf[i];
			}

			// swap it to the last such that it gets released properly
			format->bufferFormats.buf[numBuffers - 1] = binormalBuffer;
			{
				NvParameterized::Handle handle(*format, "bufferFormats");
				PX_ASSERT(handle.isValid());
				handle.resizeArray((int32_t)numBuffers - 1);
			}

			return true;
		}
	}
	return false;
}
Beispiel #7
0
void JF::Component::ColisionBox::Reset()
{
	// 1) 
	auto* pTransform	= GetOwner()->GetComponent<JF::Component::Transform>();
	XMFLOAT4X4& rMatrix	= pTransform->GetTransformMatrix();

	assert(pTransform != nullptr);

	// 2) 
	m_pShape = m_pPXDevice->GetPhysics()->createShape(
		PxBoxGeometry(m_Size.x, m_Size.y, m_Size.z),
		*m_pPXDevice->GetMaterial());
	
	// 3)
	PxTransform localTm( PxMat44(
		PxVec4(rMatrix._11, rMatrix._12, rMatrix._13, rMatrix._14),
		PxVec4(rMatrix._21, rMatrix._22, rMatrix._23, rMatrix._24),
		PxVec4(rMatrix._31, rMatrix._32, rMatrix._33, rMatrix._34),
		PxVec4(rMatrix._41, rMatrix._42, rMatrix._43, rMatrix._44)
	));

	// 4)
	m_pRigidBody = m_pPXDevice->GetPhysics()->createRigidDynamic(localTm);
	m_pRigidBody->attachShape(*m_pShape);
	PxRigidBodyExt::updateMassAndInertia(*m_pRigidBody, 10.0f);
	m_pRigidBody->setLinearVelocity(PxVec3(0, 10, 2.0f));
	m_pPXDevice->GetScene()->addActor(*m_pRigidBody);

	// 5)
	float cX = 0.0f;
	float cY = 0.0f;
	float cZ = 0.0f;

	float eX = 1.0f;
	float eY = 1.0f;
	float eZ = 1.0f;

	// 정점 
	JF::Vertex::PosColor  vertices[] =
	{
		// 1, 1, 1	(0)
		{ XMFLOAT3(cX + eX, cY + eY, cZ + eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) },
		// 1, 1, -1	(1)
		{ XMFLOAT3(cX + eX, cY + eY, cZ + -eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) },
		// -1, 1, 1	(2)
		{ XMFLOAT3(cX + -eX, cY + eY, cZ + eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) },
		// -1, 1, -1 (3)
		{ XMFLOAT3(cX + -eX, cY + eY, cZ + -eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) },
		// 1, -1, 1 (4)
		{ XMFLOAT3(cX + eX, cY + -eY, cZ + eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) },
		// 1, -1, -1 (5)
		{ XMFLOAT3(cX + eX, cY + -eY, cZ + -eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) },
		// -1, -1, 1 (6)
		{ XMFLOAT3(cX + -eX, cY + -eY, cZ + eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) },
		// -1, -1, -1 (7)
		{ XMFLOAT3(cX + -eX, cY + -eY, cZ + -eZ), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f) }
	};
	m_VertexSize = ARRAYSIZE(vertices);

	// 인덱스
	UINT indices[] =
	{
		// Top
		0,1, 1,3, 3,2, 0,2,
		// Bottom
		4,5, 5,7, 7,6, 6,4,
		// Top - Bottom
		0,4, 1,5, 2,6, 3,7
	};
	m_indexCnt = ARRAYSIZE(indices);

	// Create 
	gRENDERER->CreateVertexBuffer(&vertices[0], sizeof(JF::Vertex::PosColor) * m_VertexSize, &m_VertBuff);
	gRENDERER->CreateIndexBuffer(&indices[0], sizeof(UINT) * m_indexCnt, &m_IndexBuff);
}