Rotating object

Started by
1 comment, last by Toastmastern 7 years, 6 months ago
Hello,

I have a planet that I needs to rotate around it's own Y-axis. I read up some and the way to go seems to be to let
the shader do the work. I have a rotation matrix:

	mPlanetRotationMatrix = XMMATRIX(cosf(radiansToRotate), 0.0f, sinf(radiansToRotate), 0.0f,
							  0.0f, 1.0f, 0.0f, 0.0f,
							  -sinf(radiansToRotate), 0.0f, cosf(radiansToRotate), 0.0f,
							  0.0f, 0.0f, 0.0f, 1.0f);
I then multiply my viewMatrix I get from the camera with this rotationMatrix:

	mCamera->GetViewMatrix(viewMatrix);

	planetViewMatrix = XMMatrixMultiply(mPlanetRotationMatrix, viewMatrix);
I then send it to the ColorShader:

        result = shaderManager->RenderColorShader(direct3D->GetDeviceContext(), mTerrain->GetIndexCount(), worldMatrix, planetViewMatrix, projectionMatrix, tessellationAmount, textureManager->GetTexture(0));
The view matrix(in this class it is called viewMatrix again) is updated every frame in the code:

	result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, tessellationAmount, texture);
	if (!result)
	{
		return false;
	}

	RenderShader(deviceContext, indexCount);
Here is the SetShaderParameters code:

	HRESULT result;
	D3D11_MAPPED_SUBRESOURCE mappedResource;
	MatrixBufferType *dataPtr;
	unsigned int bufferNumber;
	TessellationBufferType *dataPtr2;

	worldMatrix = XMMatrixTranspose(worldMatrix);
	viewMatrix = XMMatrixTranspose(viewMatrix);
	projectionMatrix = XMMatrixTranspose(projectionMatrix);

	result = deviceContext->Map(mMatrixBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
	if (FAILED(result))
	{
		return false;
	}

	dataPtr = (MatrixBufferType*)mappedResource.pData;

	dataPtr->world = worldMatrix;
	dataPtr->view = viewMatrix;
	dataPtr->projection = projectionMatrix;

	deviceContext->Unmap(mMatrixBuffer, 0);

	bufferNumber = 0;

	deviceContext->DSSetConstantBuffers(bufferNumber, 1, &mMatrixBuffer);

	result = deviceContext->Map(mTessellationBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
	if (FAILED(result))
	{
		return false;
	}

	dataPtr2 = (TessellationBufferType*)mappedResource.pData;

	dataPtr2->tessellationAmount = tessellationAmount;
	dataPtr2->padding = XMFLOAT3(0.0f, 0.0f, 0.0f);

	deviceContext->Unmap(mTessellationBuffer, 0);

	bufferNumber = 0;

	deviceContext->HSSetConstantBuffers(bufferNumber, 1, &mTessellationBuffer);

	deviceContext->DSSetShaderResources(0, 1, &texture);

	return true;
Is there anything obvious that I am missing?

Thanks in advance
Toastmastern
Advertisement
This is going to sound silly, but have you made sure radiansToRotate is changing to a different value every frame?

This is going to sound silly, but have you made sure radiansToRotate is changing to a different value every frame?

It is, what I found was that the radiansToRotate was always so small and I forgotten that it would work this way, I had to add radiansToRotate to the old radiansToRotate all the time to increase it. I tried a different approach before and that one had me only use the difference every frame and not the total amount. Now I only have 1 bug left that I will sort out tonight(something with my gameTime class that gets the difference wrong when the second mark goes from 59 to 0 or something like that)

//Toastmastern

This topic is closed to new replies.

Advertisement