# HLSL - Converting these 2d coordinates.

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

## Recommended Posts

Hi guys!

This may seem extremely simple, yet it isn't for me, and therefore I'm asking for your help on this topic. I'd like to convert coordinates from 0 to 1 that start at the top left corner, and end in the bottom right corner. Unfortunately I can't just insert these values into the NDC coordinates in my vertex shader, as that's not how they work, description from msdn: (http://msdn.microsoft.com/en-us/library/windows/desktop/cc308049(v=vs.85).aspx)

The reason for this is because I'm starting on my 2d simple gui, nothing fancy. So I simply have a structure called Rectangle, defining x0, y0, x1, y1, where 0,0 is the top left cornor, and 1,1 is the bottom right, but I'd need to convert these into the NDC space coordinates, how?

Maybe I should know how to do this, maybe not, either way, how could this be done? Any input would be appreciated.

Thank you, as usual.

-MIGI0027

##### Share on other sites
x0 = x0 * 2.0f - 1.0f;
x1 = x1 * 2.0f - 1.0f;
y0 = y0 * -2.0f + 1.0f;
y1 = y1 * -2.0f + 1.0f;

L. Spiro

[EDIT]
Fixed to flip vertically.
[/EDIT] Edited by L. Spiro

##### Share on other sites

Thank you.

When using the coordinates: 0, 0, .5f, .5f, and transforming them using your method, the result isn't as expected.

I would have expected some thing going from the top left to the middle of the screen. My implementation:

// Creating the vertices
Surface->SetArea(&CDK::Rectangle::ToScreen(CDK::Rectangle(0, 0, .5f, .5f));

// ::ToScreen, I do realize that this can be optimized, but that's not the goal for now.
CDK::Rectangle& CDK::Rectangle::ToScreen(Rectangle& a)
{
a.x0 = a.x0 * 2.0f - 1.0f;
a.x1 = a.x1 * 2.0f - 1.0f;
a.y0 = a.y0 * 2.0f - 1.0f;
a.y1 = a.y1 * 2.0f - 1.0f;

return a;
}


Edit:

...
Rectangle(float _x0, float _y0, float _x1, float _y1)
{
x0 = _x0;
x1 = _x1;
y0 = _y0;
y1 = _y1;
}

Edited by Migi0027

##### Share on other sites
You didn’t give much context.
Are you using a world-view-projection matrix?  Or projection matrix at all?

You shouldn’t be.  The purpose of the projection matrix you create with D3DXMatrixOrthoLH() and friends is to put coordinates into normalized device coordinates.

In other words, the vertex shader should look like:
OUT = IN;
I explained this in fair detail here.

Your parallax occlusion is looking nice.

[EDIT]
Your description of what you wanted to happen was hidden between imagery and code.
If your error is that it went to the lower-left instead of the upper-left, then the fix is simple:
CDK::Rectangle& CDK::Rectangle::ToScreen(Rectangle& a)
{
a.x0 = a.x0 * 2.0f - 1.0f;
a.x1 = a.x1 * 2.0f - 1.0f;
a.y0 = a.y0 * -2.0f + 1.0f;
a.y1 = a.y1 * -2.0f + 1.0f;

return a;
}
[/EDIT]

L. Spiro Edited by L. Spiro

##### Share on other sites

Sorry for the confusion here, my vertex shader isn't anything fancy as it's user customization. (Notice the #REGION things, they are replaced by the engine).

Maybe I should, but no, there's no projection matrix involved here. I'm simply sending a structure containing x, y, tx, ty, (t -> texture).

So to convert those coordinates I'd have to use the ortho matrix? (D3DXMatrixOrthoLH)

#REGION_INPUTS
SamplerState ss;

struct VS_Output
{
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD0;
};

VS_Output VShader(float2 position : POSITION, float2 tex : TEXCOORD)
{
VS_Output Output;
Output.Tex = tex; // Just pass it
Output.Pos = float4(position.xy, 0, 1);

#REGION_VERTEX

return Output;
}

{
float4 color;

#REGION_PIXEL

return color;
}


PS. The post you sent me isn't read yet, doing it after this post is written.

And thank you.

##### Share on other sites

I provided [EDIT] tags in both posts to show the solution to your problem.

L. Spiro

##### Share on other sites

Thank you, it seems to work.

-MIGI0027

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 9
• 11
• 15
• 21
• 26