• Create Account

## HLSL - Converting these 2d coordinates.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1Migi0027 (肉コーダ)  Members

Posted 20 November 2013 - 02:21 PM

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

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/cuboidzone.wordpress.com/

### #2L. Spiro  Members

Posted 20 November 2013 - 03:35 PM

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, 20 November 2013 - 04:13 PM.

### #3Migi0027 (肉コーダ)  Members

Posted 20 November 2013 - 03:58 PM

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, 20 November 2013 - 03:59 PM.

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/cuboidzone.wordpress.com/

### #4L. Spiro  Members

Posted 20 November 2013 - 04:03 PM

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.
Since you were asking I had assumed you weren’t using this.

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, 20 November 2013 - 04:11 PM.

### #5Migi0027 (肉コーダ)  Members

Posted 20 November 2013 - 04:11 PM

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 PShader(VS_Output input) : SV_TARGET
{
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.

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/cuboidzone.wordpress.com/

### #6L. Spiro  Members

Posted 20 November 2013 - 04:14 PM

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

L. Spiro

### #7Migi0027 (肉コーダ)  Members

Posted 20 November 2013 - 04:17 PM

Thank you, it seems to work.

-MIGI0027

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/cuboidzone.wordpress.com/

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.