Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


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.

  • You cannot reply to this topic
6 replies to this topic

#1 Migi0027 (肉コーダ)   Crossbones+   -  Reputation: 3345

Like
0Likes
Like

Posted 20 November 2013 - 02:21 PM

Hi guys! wink.png

 

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)

 

IC534071.png

 

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. happy.png

-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/


Sponsor:

#2 L. Spiro   Crossbones+   -  Reputation: 19269

Like
2Likes
Like

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.


#3 Migi0027 (肉コーダ)   Crossbones+   -  Reputation: 3345

Like
0Likes
Like

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.

 

2ur1rpk.png

 

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/


#4 L. Spiro   Crossbones+   -  Reputation: 19269

Like
2Likes
Like

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.


#5 Migi0027 (肉コーダ)   Crossbones+   -  Reputation: 3345

Like
0Likes
Like

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. smile.png


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/


#6 L. Spiro   Crossbones+   -  Reputation: 19269

Like
1Likes
Like

Posted 20 November 2013 - 04:14 PM

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

 

 

L. Spiro



#7 Migi0027 (肉コーダ)   Crossbones+   -  Reputation: 3345

Like
0Likes
Like

Posted 20 November 2013 - 04:17 PM

Thank you, it seems to work. smile.png

 

-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.



PARTNERS