0
\$\begingroup\$

I'm using SharpDX Toolkit and load a png file with SharpDX.Toolkit.Graphics.Texture.Load(GraphicsDevice, Stream)
If I apply the texture to another texture it's drawn upside down. The texture is:original
and is drawn as this:it's drawn
Loading is:

Texture image;
FileInfo file = new FileInfo(layer.Id.ToString() + ".png");
if (!file.Exists)
    return;
image = Texture.Load(GraphicsDevice, file.OpenRead());
layer.SetData(image.GetData<byte>());

the SetData Method:

private Texture2D map;
public void SetData(byte[] data) {
    map.SetData(data);
}

VertexBuffer declaration:

vertexBuffer = SharpDX.Toolkit.Graphics.Buffer.Vertex.New<VertexPositionTexture>(GraphicsDevice, new[] {
    new VertexPositionTexture(new Vector3(0, 0, 0), new Vector2(0, 0)),
    new VertexPositionTexture(new Vector3(0, Size.Height, 0), new Vector2(0, 1)),
    new VertexPositionTexture(new Vector3(Size.Width, Size.Height, 0), new Vector2(1, 1)),
    new VertexPositionTexture(new Vector3(Size.Width, 0, 0), new Vector2(1, 0))
});

and the WVP:

wvp = Matrix.Scaling(zoom) *
    Matrix.LookAtLH(new Vector3(camPosition, 0), new Vector3(camPosition, 1), Vector3.Up) *
    Matrix.OrthoLH(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 1);

Any idea why SharpDX draws the image upside down and how I can fix it?

\$\endgroup\$
3
\$\begingroup\$

I think you are probably misunderstanding the coordinate system in which you are working. Higher numbers for X and Y are not necessarily up and right on the screen. So you're most likely seeing an upside down quad, or your camera is behind the thing you're looking at and you're seeing its back (a double-sided quad will show the mirror image of your texture if you look at it from the back).

Turn your quad upside down (by changing the order of vertices in the vertex buffer declaration), or change your camera position.

\$\endgroup\$
1
  • \$\begingroup\$ I take this as "answer" because I changed the position of the LookAt (target -z and "Up" ist Vector3.Down) because I had problems with mouse input but didn't changed vertex order and texture coordinates. \$\endgroup\$ – AliveDevil Oct 11 '13 at 20:26
1
\$\begingroup\$

Perhaps your texture mapping is upside down? Try modifying your texture coordinates in vertex buffer.

vertexBuffer = SharpDX.Toolkit.Graphics.Buffer.Vertex.New<VertexPositionTexture>(GraphicsDevice, new[] {
    new VertexPositionTexture(new Vector3(0, 0, 0), new Vector2(0, 1)),
    new VertexPositionTexture(new Vector3(0, Size.Height, 0), new Vector2(0, 0)),
    new VertexPositionTexture(new Vector3(Size.Width, Size.Height, 0), new Vector2(1, 0)),
    new VertexPositionTexture(new Vector3(Size.Width, 0, 0), new Vector2(1, 1))
});
\$\endgroup\$
1
\$\begingroup\$

It's important to understand the difference between the spacial coordinates being passed in for the vertex, and the texture coordinates being passed in as the second argument.

The spacial coordiantes work as expected, like any plot you've ever seen. However, for textures, the most common scheme is to put (0,0) in the upper left corner, and (1,1) or (Xres,Yres) in the lower right corner.

Try drawing a plot of where you have applied which coordinate, and you'll see that you put (0,1) and (1,1) at the top of the quad, and not the bottom.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.