Jump to content
  • Advertisement
Sign in to follow this  
John117

Draw Texture with right Aspect Ratio

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello Everybody,

I have a problem, everytime if I want to draw a Texture in a quad as an post processing effect (Texture Preview Window), the texture always draw stretch, not in the uniform aspect ratio like I wish :(

Do you guys have any solution proposals?

Greets

John

 

Pic of my Problem:

01.jpg

Share this post


Link to post
Share on other sites
Advertisement

Aspect ratio is usually fixed by a simple multiplication of the vertex with the projection matrix (which can be orthographic or projection).

Share this post


Link to post
Share on other sites

Hello felipefsdev,

In my Shader I don't use matrices!? :(

Here my code:

// Attributes ---------------------------------------------------------------------------- Attributes
Texture2D DiffuseTexture;

// TrilinearSampler ---------------------------------------------------------------------------- TrilinearSampler
SamplerState TrilinearSampler
{
  Filter = MIN_MAG_MIP_LINEAR;
  AddressU = WRAP;
  AddressV = WRAP;
  AddressW = WRAP;
  MipLODBias = 0.0f;
  MaxAnisotropy = 0;
  BorderColor = float4(0.0f, 0.0f, 0.0f, 0.0f);
};

// VertexShader struct ---------------------------------------------------------------------------- VertexShader struct
struct Vertex
{
 float3 Position : POSITION;
 float2 UV       : TEXCOORD0;
};

// PixelShader struct ---------------------------------------------------------------------------- PixelShader struct
struct Pixel
{
 float4 Position : SV_POSITION;
 float2 UV       : TEXCOORD0;
};

// Vertex Shader ---------------------------------------------------------------------------- Vertex Shader
Pixel vertexShader(Vertex input)
{
 Pixel result = (Pixel) 0;
 result.Position = float4(input.Position, 1.0f);
 result.UV = input.UV;
 
 return result;
}

// Pixel Shader ---------------------------------------------------------------------------- PixelShader
float4 pixelShader(Pixel input) : SV_Target0
{
 float3 diffuse = DiffuseTexture.Sample(TrilinearSampler, input.UV);
 
 return float4(diffuse, 1.0f);
}

technique11 Main
{
 pass p0
 {
  SetVertexShader(CompileShader(vs_5_0, vertexShader()));
  SetHullShader(NULL);
  SetDomainShader(NULL);
  SetGeometryShader(NULL);
  SetPixelShader(CompileShader(ps_5_0, pixelShader()));
 }
}
Edited by John117

Share this post


Link to post
Share on other sites

I never used this tool you are using, so I don't know about the details, but shaders are usually similar on how they operate. I found on google that there's a "matrix node" for the tool you are using:

http://steamcommunity.com/games/314720

NEW NODES:

- Added a new matrix node (the Mx icon under Types) with which you can create typical matrices like view, projection, scale and more. Should help when creating effects like Shadow Mapping.

It looks like you want a projection orthographic matrix (which has the width and height of the view, and the far and near points where vertex are visible). So, you projection matrix will be multiplied against each vertex position.

Edited by felipefsdev

Share this post


Link to post
Share on other sites

Hello,

if I write this in my Shader:

Pixel vertexShader(Vertex input)
{
 Pixel result = (Pixel) 0;
 result.Position = mul(float4(input.Position.xyz, 1.0f), Projection);
 result.UV = input.UV;
 
 return result;
}

And that's my Matrix

Matrix.OrthoLH(renderTargetWidth, renderTargetHeight, 0.1f, 1500.0f)

I see nothing anymore :(

 

Greets

John

Edited by John117

Share this post


Link to post
Share on other sites

I imagine that your texture quad is rendered at the Z position 0.0, so it's not covered by the near/far value of the ortho projection:

Matrix.OrthoLH(renderTargetWidth, renderTargetHeight, 0.1f, 1500.0f)

Check one of these:

Matrix.OrthoLH(renderTargetWidth, renderTargetHeight, -1.0f, 1.0f)

Matrix.OrthoLH(renderTargetWidth, renderTargetHeight, 1.0f, -1.0f)

The `vertexShader` seems to be without issues.

Share this post


Link to post
Share on other sites

Hello,

with your code, I see a little pixel far away, only if I maipulate the width and height, I can make it bigger, but than I see only the stretch result again :(

What's the way the Picturebox Control does it make it to draw it in the right aspect ratio, if I change the view style to zoom or If i open an image in a Picture View App, how do these tools draw the imgage, that the aspect ratio is everytime correct?

Greets

John

Share this post


Link to post
Share on other sites

I tried a manipulation of the Viewport too, but i come not forward with my code :(

                float r1 = texture.Size.Width / texture.Size.Height; // The aspect ratio you want your viewport to have
                float r2 = this.renderTarget.Width / this.renderTarget.Height;

                float x = 0;
                float y = 0;
                float w = texture.Size.Width;
                float h = texture.Size.Height;

                if (r1 > r2)
                {
                    // Viewport is wider than screen, fit on X
                    w = texture.Size.Width;
                    h = w / r1;
                    //y = h / 2;
                }
                else
                {
                    // Screen is wider than viewport, fit on Y
                    h = texture.Size.Height;
                    w = h / r1;
                    //x = w / 2;
                }

                // Quad Render Target
                // Set Viewport
                this.deviceEngine.SetViewport(x, y, w, h, 0.0f, 1.0f);

Greets

John

Edited by John117

Share this post


Link to post
Share on other sites

I also tried with a manipulation of the world matirx with this code:

                float r1 = texture.Size.Width / (float)this.renderTarget.Width;
                float r2 = texture.Size.Height / (float)this.renderTarget.Height;

                if (r1 > r2)
                {
                    // Viewport is wider than screen, fit on X
                    world.M22 = 1.0f;
                }
                else
                {
                    // Screen is wider than viewport, fit on Y
                }

 

But there is the same problem, any ideas to solve it? :(

Greets

John

Share this post


Link to post
Share on other sites

Please check your input vertex positions and texture coordinates. It seems like you are drawing a full screen quad/triangle with the vertex positions in normalized device coordinates, and the texture coordinates are likely (0,0) at the min and (1,1) at the max corner. This will result in the image being stretched. 

If that is the case here you have to options. Modify the vertex positions to reduce the size of the full screen effect. As already mentioned this could be done through a matrix or you can hard code it. The other option you have is to change the texture coordinate to account for the aspect ratio.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!