Hello I'm going to be quick here. I don't now were I should post this since I sort of think it fits in in general programming, game programming and Directx and Xna. I'm working on my own vector class. And I want to be able to use it with the DirectX::XMVECTOR. So I created an assignment operator overloading function that looks like this:
Vector3& Vector3::operator=(const DirectX::XMVECTOR& vector)
{
this->X = DirectX::XMVectorGetX(vector);
this->Y = DirectX::XMVectorGetY(vector);
this->Z = DirectX::XMVectorGetZ(vector);
return *this;
}
I then tested it out with following code:
OUTPUT:
5
Ok so I get the correct output here, the thing is that now none of my geometry is showing up. An output of the camera lookUp vector (just tested with a random vector in my camera class) shows that it's increasing in value every frame.
It all worked when I had this code in my assignment function:
Vector3 Vector3::operator=(const DirectX::XMVECTOR& vector)
{
return Vector3(DirectX::XMVectorGetX(vector), DirectX::XMVectorGetY(vector), DirectX::XMVectorGetZ(vector));
}
But now I get this output:
10
As you see, wrong output, but the geometry shows up, and my lookAt vector gets the correct output
Neither did it work when I had this as my code in my assignment function:
Vector3 Vector3::operator=(const DirectX::XMVECTOR& vector)
{
this->X = DirectX::XMVectorGetX(vector);
this->Y = DirectX::XMVectorGetY(vector);
this->Z = DirectX::XMVectorGetZ(vector);
return *this;
}
With this code I get the same output in the console:
5
But No geometry showing up. I'm I not returning the same thing in the functions that doesn't return a reference?
This doesn't make any sense to me. The only reason to why I noticed this is because I read that you should always return the reference when you overload the assignment operator, so that you can do this:
Vector3 a, b, c;
a = b = c;
English isn't my first language so there cn be some miscommunication (spelling?) going on. I would love it if someone could be kind to explain what i'm acually doing when i'm returning an reference. I get sort of confused since when you do this:
Vector3 vec = Vector3(3, 5, 7);
Vector3* vecPtr = &vec;
I assign the adress of vec to vecPtr, but since none of a, b or c is an pointer I get confused and doesn't really have an idea of whats happening. And is it correct to say that in this code:
void func(int& number)
{
number = 5;
}
int main()
{
int variable = 0;
func(variable);
return 0;
}
i'm changing the value of "variable" since I'm "sending" it into the function func()? Otherwise I have got it all wrong.
I thought that I show you my camera code to if someone should ask for it, maybe it's there I've f*cked up.
void Camera::render(Vector3 objectWorldPosition, Vector3 objectRotation, Vector3 objectScale, DirectX::XMFLOAT4X4& outputWorldMatrix)
{
//create and set matrices
DirectX::XMMATRIX worldMatrix;
DirectX::XMMATRIX objectRotationMatrix = DirectX::XMMatrixRotationRollPitchYawFromVector(objectRotation);
DirectX::XMMATRIX objectScalingMatrix = DirectX::XMMatrixScalingFromVector(objectScale);
DirectX::XMMATRIX objectWorldPositionMatrix = DirectX::XMMatrixTranslationFromVector(objectWorldPosition);
DirectX::XMMATRIX cameraRotationMatrix = DirectX::XMMatrixRotationRollPitchYawFromVector(this->rotation);
//OutputDebugStringA((std::to_string(this->lookAt.X) + " ," + std::to_string(this->lookAt.Y) + " ," + std::to_string(this->lookAt.Z)).c_str());
//calculate new lookAt and up- vectors with camera rotation
this->lookAt = DirectX::XMVector3TransformCoord(this->lookAt, cameraRotationMatrix);
this->up = DirectX::XMVector3TransformCoord(this->up, cameraRotationMatrix);
this->lookAt = DirectX::XMVectorAdd(this->position, this->lookAt);
//create worldmatrix
worldMatrix = DirectX::XMMatrixMultiply(objectRotationMatrix, objectScalingMatrix);
worldMatrix = DirectX::XMMatrixMultiply(worldMatrix, objectWorldPositionMatrix);
//create matrices
DirectX::XMStoreFloat4x4(&this->viewMatrix, DirectX::XMMatrixLookToLH(this->position, this->lookAt, this->up));
//transpose the matrices before sending them to the vertex shader
DirectX::XMStoreFloat4x4(&this->viewMatrix, DirectX::XMMatrixTranspose(DirectX::XMLoadFloat4x4(&this->viewMatrix)));
worldMatrix = DirectX::XMMatrixTranspose(worldMatrix);
//output world matrix
DirectX::XMStoreFloat4x4(&outputWorldMatrix, worldMatrix);
}
Thank you for your taking your time to read