Search Home Members Contacts
About Us
Products
Downloads
Community
Support
Pages: [1]
  Print  
Author Topic: Can't get a vector from actor to camera.  (Read 1289 times)
MiloDC
Community Member
*
Posts: 129


WWW
« on: December 04, 2007, 02:15:22 PM »

In my vertex shader, I'm trying to get a unit vector that points to my camera from the center of an actor's bounding sphere.  (I pass the value of the sphere's center using shdDefault.SetEffectParamFloatArray().)

In RenderMonkey, all I do is this, and it works fine:

Code:
float3 viewPos : VIEWPOSITION;
float3 boundingCenter;
.
.
.
float3 myVector = normalize(boundingCenter - viewPos);


My problem is that I am unable to convert viewPos into world coordinates (which boundingCenter uses).

Anyone know how I can get this working?  With what do I need to multiply viewPos?
« Last Edit: December 04, 2007, 03:16:32 PM by MiloDC » Logged

sybixsus
Customers
Community Member
*****
Posts: 1088


WWW
« Reply #1 on: December 04, 2007, 02:30:45 PM »

To convert into world position, you have to multiply by the world matrix. TV passes the World Matrix automatically. So something like this ( untested ) should work :

Code:
float4x4 wMatrix:World;
float3 boundingCenter;
float3 viewPos : VIEWPOSITION;

float3 worldPos = mul(viewPos,wMatrix);
float3 myVector = normalize(boundingCenter - worldPos);
Logged
ZaPPZion
Community Member
*
Posts: 341


« Reply #2 on: December 04, 2007, 02:32:14 PM »

you're not using vectors, you're using floating point numbers.
To get a vector pointing to your camera, you have to do some operation like this:
TV_3DVECTOR posActor = new TV_3DVECTOR();
TV_3DVECTOR posCamera = new TV_3DVECTOR();
now get the information of the actor's position, put that in posActor.x, posActor.y, posActor.z
same for the camera, but put the x, y, z coordinates in posCamera
then:
TV_3DVECTOR myVector = normalize(posCamera-posActor);
that'll be the solution to your problem i guess
Logged
MiloDC
Community Member
*
Posts: 129


WWW
« Reply #3 on: December 04, 2007, 02:51:21 PM »

To convert into world position, you have to multiply by the world matrix. TV passes the World Matrix automatically. So something like this ( untested ) should work :

Code:
float4x4 wMatrix:World;
float3 boundingCenter;
float3 viewPos : VIEWPOSITION;

float3 worldPos = mul(viewPos,wMatrix);
float3 myVector = normalize(boundingCenter - worldPos);

No, I've already tried that.  It doesn't work.

In fact, I've tried multiplying viewPos with all of TV3D's matrix semantics.  None of them work.

I think there might be more than one multiplication involved.




you're not using vectors, you're using floating point numbers.

That doesn't matter.  An array of 3 floats is the same as a 3-axis vector.  They're completely interchangeable, mathematically speaking.


To get a vector pointing to your camera, you have to do some operation like this:
TV_3DVECTOR posActor = new TV_3DVECTOR();
TV_3DVECTOR posCamera = new TV_3DVECTOR();
now get the information of the actor's position, put that in posActor.x, posActor.y, posActor.z
same for the camera, but put the x, y, z coordinates in posCamera
then:
TV_3DVECTOR myVector = normalize(posCamera-posActor);
that'll be the solution to your problem i guess

Um, I'm doing this in the shader, not in my TV3D C# code.
Logged

sybixsus
Customers
Community Member
*****
Posts: 1088


WWW
« Reply #4 on: December 04, 2007, 02:53:36 PM »

Why is bounding center a float? Shouldn't it be a float3 like the others?

And what exactly are you passing to ViewPos? If it's a local position ( which I assumed from your description ) then multiplying it by the world matrix will work. If it's not, then we'd need to know what it is.

Or heck, just pass the world position of the camera directly and cut out all the shader matrix multiplication.
« Last Edit: December 04, 2007, 02:55:48 PM by sybixsus » Logged
ZaPPZion
Community Member
*
Posts: 341


« Reply #5 on: December 04, 2007, 03:08:33 PM »

oops, didnt read that as a float array of 3, my bad;)
anyway, i think the center of the boundingsphere should be float3 as well then:)
Logged
MiloDC
Community Member
*
Posts: 129


WWW
« Reply #6 on: December 04, 2007, 03:25:11 PM »

Sorry guys, boundingCenter is a float3 in my shader code, I just mistyped it here (I've edited my original post to reflect the correction).

And what exactly are you passing to ViewPos? If it's a local position ( which I assumed from your description ) then multiplying it by the world matrix will work. If it's not, then we'd need to know what it is.

viewPos is just the float3 VIEWPOSITION semantic, i.e. presumably the position of the camera.  (Though I have no idea whether it's in world space or some other coordinate system.  The semantics WIKI doesn't supply that information.)

Or heck, just pass the world position of the camera directly and cut out all the shader matrix multiplication.

That's a fine idea, but I'd prefer to do all the calculation on the GPU.  (Faster that way.)
Logged

sybixsus
Customers
Community Member
*****
Posts: 1088


WWW
« Reply #7 on: December 04, 2007, 03:39:24 PM »

Are you sure it's faster to multiply the matrix over and over again for every single vertex than it is to do it once per frame and be done with it?

Anyway, I don't know the semantic, as I've not used it personally, so if it isn't supplying the position in either world or local position, I really have no idea. I'm sure one of the more experienced HLSL programmers will do.
Logged
MiloDC
Community Member
*
Posts: 129


WWW
« Reply #8 on: December 04, 2007, 03:59:13 PM »

Are you sure it's faster to multiply the matrix over and over again for every single vertex than it is to do it once per frame and be done with it?

Very good point, sybixsus.

Yeah, I think I'll just calculate the numbers before the shader executes.  Thanks.
Logged

jviper
Community Member
*
Posts: 1366

Discipline in training


« Reply #9 on: December 05, 2007, 09:44:21 AM »

That ViewPosition semantic is to the position of your camera (the camera that is in use when you are rendering the mesh with the shader applied to it) in world space. You shouldn't have to do anything to that to get your viewposition. I'd think if anything it's your bounding center that needs to be multiplied by some view transformation matrix, depending on how you calculated it.
Logged

JAbstract.....Don't just imagine, make it happen!
MiloDC
Community Member
*
Posts: 129


WWW
« Reply #10 on: March 03, 2008, 11:24:22 AM »

Just thought I should mention that the original problem that I had was my fault.  I forget what I did wrong, but in any case, TV3D was not to blame at all.
Logged

Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.3 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks