Sign in to follow this  

D3DXVECTOR3 assignment question (newbie)

This topic is 4732 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm fairly new to both C++ and DX9, but I don't think I've missed anything obvious. I'm learning here and am creating a Camera class in C++. The class has several D3DXVECTOR3 member variables. I've found that I can't do: myVectorVar = [1.0f, 0.0f, 0.0f]; There appear to be no assignment operator overloads for the D3DXVECTOR3 that takes three floats. Am I missing something obvious here? Is there an easy way to add this functionality? Is there a better way of doing this? Thanks all!

Share this post


Link to post
Share on other sites
you can initialize your vector when you create it

D3DXVECTOR3 myVec(1.0f, 0.0f, 0.0f);


however if you want to reasign values to your vector variable, then you have to do it the old way


myVec.x = 1.0f;
myVec.y = 0.0f;
myVec.z = 0.0f;


or create a temporary object just to reasign values to your vector

myVec = D3DXVECTOR3(1.0f, 0.0f, 0.0f);

Share this post


Link to post
Share on other sites
Quote:
Original post by mike25025
myVectorVar = D3DXVECTOR3(1.0f, 0.0f, 0.0f);


That works.

Just to ensure that I understand (I'm self-taught in C/C++ and have gaps in my understanding)....

This expression creates a temporary, unnamed D3DXVECTOR3 struct using the assignment operator (float, float, float), then assigns it to myVectorVar using the assignment operator that accepts a D3DXVECTOR3, then discards the unnamed temp D3DXVECTOR3 struct. Right? (At least this is what happens conceptually, right?)

Thank you for your help!

Share this post


Link to post
Share on other sites
Quote:
Original post by NogginBoink
Quote:
Original post by mike25025
myVectorVar = D3DXVECTOR3(1.0f, 0.0f, 0.0f);


That works.

Just to ensure that I understand (I'm self-taught in C/C++ and have gaps in my understanding)....

This expression creates a temporary, unnamed D3DXVECTOR3 struct using the assignment operator (float, float, float), then assigns it to myVectorVar using the assignment operator that accepts a D3DXVECTOR3, then discards the unnamed temp D3DXVECTOR3 struct. Right? (At least this is what happens conceptually, right?)

Thank you for your help!


yes

i dont know if it is slower than setting the x,y,and z values directly

Share this post


Link to post
Share on other sites
I don't know if it's slower, either. At this point in my programming career, I'm much more concerned that I actually understand things.

I suspect, however, that it's not any slower. Microsoft's compiler is pretty darn smart.

So, I did a little test.

Here's the source code:


void CCamera::Test()
{
lookAt = D3DXVECTOR3(1.0f, 2.0f, 3.0f);
location.x = 4.0f;
location.y = 5.0f;
location.z = 6.0f;
}



When compiled, here's the disassembly:


lookAt = D3DVECTOR3(1.0f, 2.0f, 3.0f);
00412B23 push 40400000h
00412B28 push 40000000h
00412B2D push 3F800000h
00412B32 lea ecx,[ebp-0DCh]
00412B3D call D3DXVECTOR3::D3DXVECTOR3 (4115E6h)
00412B3D mov ecx, dword ptr [this]
00412B40 add ecx, 0Ch
00412B43 mov edx, dword ptr [eax]
00412B45 mov dword ptr [ecx], edx
00412B47 mov edx, dword ptr [eax+4]
00412B4A mov dword ptr [ecx+4], edx
00412B4D mov eax, dword ptr [eax+8]
00412B50 mov dword ptr [ecx+8], eax
location.x = 4.0f;
00412B53 mov eax, dword ptr [this]
00412B56 mov dword ptr [eax], 40800000h
location.y = 5.0f;
00412B5C mov eax, dword ptr [this]
00412B5F mov dword ptr [eax+4], 40A00000h
location.z = 6.0f;
00412B66 mov eax, dword ptr [this]
00412B69 mov dword ptr [eax+8], 40C00000h



(There may be typos in the above assembly; Visual Studio didn't let me copy/paste from the disassembly window so I had to type it in by hand.)

Obviously mike's method is slower.

Okay, so now what if I go from debug build to release build and turn on optimizations? Well, let's find out...

...as it turns out, both versions are more optimized than the debug build, but the second method (using the location assignment) of assigning values to the D3DXVECTOR3 struct is still faster than the first (using the lookat assignment). In fact, the second method compiles to three "mov dword ptr [ecx+xxx], immediate" instructions, which would be very fast. The first method, when optimized, is ten instructions (as opposed to three), and I can't say that I fully understand the disassembly to explain what it's actually doing.

I'm sorry, but I don't feel like manually typing in the disassembly of the optomized version.

Share this post


Link to post
Share on other sites
In further tests to try to optimize both speed and ease of typing my code, I defined a macro:

#define MAKEVECTOR3 (a, xx, yy, zz) a.x = xx; a.y = yy; a.z = zz;

And tried to create a new D3DXVECTOR3 in code with

MAKEVECTOR3 (upVector, 1.0f, 2.0f, 3.0f);

Interestingly, this version compiles into five assembly instructions. So it's not as fast as individually assigning each member of the vector, but it sure is quicker to type it this way.


00415A0 mov edx, 40A00000h
00415A5 mov eax, 40C00000h
00415AA mov dword ptr [ecx + 18h], 40800000h
00415B1 mov dword ptr [ecx + 1Ch], edx
00415B4 mov dword ptr [ecx + 20h], eax




Looking at the disassembly of the macro version, it appears that Microsoft has simply missed an opportunity to optimize the code. Two of the instructions are clearly unnecessary in this version. Of course, I'm not entirely certain that more instructions necessarily means more execution time. Given the multiple pipelines of the Pentium processors, this version may in fact be faster.

This was compiled with Visual Studio 2003.

Share this post


Link to post
Share on other sites

This topic is 4732 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this