x


How to get the global position of a vertex in a Cg vertex shader?

I'm trying to write a Shader with Cg that calculates a vertex's uv-texcoord out of its position in 3d space. I got some results but the shader obviously uses the positions of the vertices relative to the local coordinate system of the mesh, because every GameObject I applied this shader to gets texture coordinates accordingly to its rotation. And that's the way it should behave normally, of course. However, I wan't to compute the global - the world vertex coordinates. Is there any way to retrieve them in Cg? Maybe through some glstate.matrix multiplications? I don't quite get these yet.

more ▼

asked Jun 07 '10 at 10:32 PM

Sejster gravatar image

Sejster
59 3 3 6

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Something like this should work (see more in the sparse docs).

// v.vertex is the input vertex in the vertex shader
float3 worldPos = mul (_Object2World, v.vertex).xyz;
more ▼

answered Jun 08 '10 at 08:43 AM

Aras gravatar image

Aras ♦♦
1.6k 7 26

That's exactly what I wanted. Thank you very much!

Jun 08 '10 at 11:00 PM Sejster

@Sejster You should mark the answer as accepted.

Jun 09 '10 at 02:26 PM ericksson
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1649
x236
x137
x88

asked: Jun 07 '10 at 10:32 PM

Seen: 2509 times

Last Updated: Jun 07 '10 at 10:32 PM