# what's wrong with my vs code

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

## Recommended Posts

When the D3DRS_LOCALVIEWER render state is set to FALSE, the system uses the following formula to calculate the reflection vector in environment map. R=2 *Nz *N-I In the preceding formula, R is the reflection vector being computed, N is the camera-space vertex normal. The Nz element is the world-space z of the vertex normal, and I is the vector (0,0,1) of an infinitely distant viewpoint This is my code for this render
//vertex shader for EM

vs_2_0

//constant
//c0-c3 camera space transposed
//c4-c7 word * view *proj
//c8-c11 world space

def c20, 2.0f, 1.0f, 0.5f, 0.0f
def c21, 0.0f, 0.0f, 1.0f, 0.0f

dcl_position  v0
dcl_normal    v1

m4x4 oPos, v0, c4         //transform vertor

mov  r2,v1                //normal
m3x3 r3.xyz, r2, c0        //transform normal to camera space
nrm r2.xyz, r3             //r2 : normal cameral N

mov  r3,v1
m3x3 r4.xyz,r3,c8
nrm  r3.xyz,r4
mov  r1.x, r3.z            //r1.x : Nz

//calculat

mov  r8, c21

mul  r4.x,r1.x,c20.x
mov  r5.w,  c20.y
nrm  r7,r5

mov oT0.xyz,-r7


##### Share on other sites
The problem is you're writing in assembly language! Use HLSL!

##### Share on other sites
What leads you to the conclusion that something is wrong with your vs code? Do you get an error? Does the code cause a crash? Does it do something other than you intended? Does it do nothing at all?

"What's wrong with my code?" is just a little too vague for any real analysis of a problem.

##### Share on other sites
Quote:
 Original post by JoemanThe problem is you're writing in assembly language! Use HLSL!

You're hurting my and ntnet's feelings here [grin]

##### Share on other sites
according to the d3d,the last code be :
mov oT0.xyz,r7
but i use it, the reflection direction is reverse. The reflection image is at the reverse side on the sphere.
i don't know why?
Thanks!

##### Share on other sites
Instead of wasting a constant for 2.0, just do

It's still one instruction and doesn't use precious constant space. Also, why do you move constants to R registers before using them? You can operate on them while still in C registers, saving precious GPU clock cycles and instruction slots.

Perhaps cube env maps need a transform, like sphere env maps do, so that they don't come out upside down. Perhaps they don't. If the direction is backwards, as you say, why not change the last line to not invert the result... ie: use r7, not -r7.

Doesn't LOCALVIEWER affect the specular calculation, not reflection map calculation? For a ref map, you still want a full CSRV calculation, which isn't what you're doing... you want "v = i - 2 * dot(i, n) * n", where i is the light direction (incident ray), and n is the normal.

##### Share on other sites
Quote:
 Original post by NamethatnobodyelsetookInstead of wasting a constant for 2.0, just do add result, source, sourceIt's still one instruction and doesn't use precious constant space. Also, why do you move constants to R registers before using them? You can operate on them while still in C registers, saving precious GPU clock cycles and instruction slots.Perhaps cube env maps need a transform, like sphere env maps do, so that they don't come out upside down. Perhaps they don't. If the direction is backwards, as you say, why not change the last line to not invert the result... ie: use r7, not -r7.Doesn't LOCALVIEWER affect the specular calculation, not reflection map calculation? For a ref map, you still want a full CSRV calculation, which isn't what you're doing... you want "v = i - 2 * dot(i, n) * n", where i is the light direction (incident ray), and n is the normal.

I want to get the reflection image like this : assume the viwer is at infinate, so i use the other formular: R=2NzN-I
when i use this formular , i should set mov oT0.xyz,r7. But when i use this ,the direction is invert. Why, Is there wrong in D3D SDk?

##### Share on other sites
Quote:
Original post by Anonymous Poster
Quote:
 Original post by NamethatnobodyelsetookInstead of wasting a constant for 2.0, just do add result, source, sourceIt's still one instruction and doesn't use precious constant space. Also, why do you move constants to R registers before using them? You can operate on them while still in C registers, saving precious GPU clock cycles and instruction slots.Perhaps cube env maps need a transform, like sphere env maps do, so that they don't come out upside down. Perhaps they don't. If the direction is backwards, as you say, why not change the last line to not invert the result... ie: use r7, not -r7.Doesn't LOCALVIEWER affect the specular calculation, not reflection map calculation? For a ref map, you still want a full CSRV calculation, which isn't what you're doing... you want "v = i - 2 * dot(i, n) * n", where i is the light direction (incident ray), and n is the normal.

I want to get the reflection image like this : assume the viwer is at infinate, so i use the other formular: R=2NzN-I
when i use this formular , i should set mov oT0.xyz,r7. But when i use this ,the direction is invert. Why, Is there wrong in D3D SDk?

sorry for make it top in the forums

##### Share on other sites
First, lets figure out what you're trying to do... Are you trying to generate spheremapping coordinates? Cube mapping coordinates?

I posted the wrong CSRV formula above... it should be:
// CSRV: 2*[(N*E)/(N*N)]*N - E// r7 = CameraSpaceNormal// r5 = EyeVector (cameraspaceposition, normalized)char *ShaderSection_CalcCSRV =  "dp3 r0.y, r7.xyz, r7.xyz\n"  "dp3 r0.x, r7.xyz, r5.xyz\n"  "rcp r0.y, r0.y\n"  "add r1.xyz, r7.xyz, r7.xyz\n"  "mul r1.xyz, r1.xyz, r0.xxx\n"  "mad r1.xyz, r1.xyz, r0.yyy, -r5.xyz\n"// Normalize CSRV  "dp3 r2.w, r1.xyz, r1.xyz\n"  "rsq r2.w, r2.w\n"  "mul r2.xyz, r1.xyz, r2.www\n"

That can be used for cube mapping.

For spheremapping, use your normal in camera space. Just bias it, (u = normx*0.5 + 0.5, v = normy*-0.5 + 0.5)

I'm not sure where your formula comes from, and LOCALVIEWER has nothing to do with reflection map generation. If you want help with whatever method you're attempting to use, you MUST give more detail...

If you're copying the SDKs terminology of "Diffuse reflectance model" and "Specular reflectance model", you need to tell us... You mention LOCALVIEWER which makes me think specular, and then you mention reflection environment map, which makes me think it's not specular...

##### Share on other sites
Quote:
 Original post by NamethatnobodyelsetookFirst, lets figure out what you're trying to do... Are you trying to generate spheremapping coordinates? Cube mapping coordinates?I posted the wrong CSRV formula above... it should be:// CSRV: 2*[(N*E)/(N*N)]*N - E// r7 = CameraSpaceNormal// r5 = EyeVector (cameraspaceposition, normalized)char *ShaderSection_CalcCSRV = "dp3 r0.y, r7.xyz, r7.xyz\n" "dp3 r0.x, r7.xyz, r5.xyz\n" "rcp r0.y, r0.y\n" "add r1.xyz, r7.xyz, r7.xyz\n" "mul r1.xyz, r1.xyz, r0.xxx\n" "mad r1.xyz, r1.xyz, r0.yyy, -r5.xyz\n"// Normalize CSRV "dp3 r2.w, r1.xyz, r1.xyz\n" "rsq r2.w, r2.w\n" "mul r2.xyz, r1.xyz, r2.www\n"That can be used for cube mapping.For spheremapping, use your normal in camera space. Just bias it, (u = normx*0.5 + 0.5, v = normy*-0.5 + 0.5)I'm not sure where your formula comes from, and LOCALVIEWER has nothing to do with reflection map generation. If you want help with whatever method you're attempting to use, you MUST give more detail...If you're copying the SDKs terminology of "Diffuse reflectance model" and "Specular reflectance model", you need to tell us... You mention LOCALVIEWER which makes me think specular, and then you mention reflection environment map, which makes me think it's not specular...

Thanks!
The formula comes from d3d sdk. You can find it from the cubic environment mapping in sdk.
I use a cubic environment.The flag of LOCALVIEWER is used to determine which formula is used in calculating the reflector vetex. This can be found in sdk mentioned above.
I don't know where is wrong?
I used the first formula R=2(E dot N)N-E ,the result is correct.
Why it is wrong when i used R=2NzN-I

##### Share on other sites

Sorry for making it topper!-:)
It bothers me for so long time.