0
\$\begingroup\$

I am trying to adjust the following code to render a quad on a 2D texture at a specific location and specific size. Currently this code will render a fullscreen quad, which makes sense since the POSITION semantic is being set to the fullscreen size.

My issue, is that I don't know how to adjust the POSITION semantics in the declaration below to use the specified location and size of a texture.

private short[] indexData = new short[] { 0, 1, 2, 2, 3, 0 };

private VertexPositionTexture[] CreateTriangles(Vector2 location, 
                                                float width, float height)
{

    // how do I get these values, and where do I set them below?
    float topleft = ??;
    float topRight = ??;
    float bottomLeft = ??;
    float bottomRight = ??;

    VertexPositionTexture[] vertices = new VertexPositionTexture[]
    {
        new VertexPositionTexture(new Vector3(1f, -1f, 0f), 
                                  new Vector2(1, 1)),
        new VertexPositionTexture(new Vector3(-1f, -1f, 0), 
                                  new Vector2(0, 1)),
        new VertexPositionTexture(new Vector3(-1f, 1f, 0), 
                                  new Vector2(0, 0)),
        new VertexPositionTexture(new Vector3(1f, 1f, 0), 
                                  new Vector2(1, 0)) 
    };

    return vertices;
}

All I'm trying to do is render a quad overtop of a texture at a specific location. If I use the code as-is with my texture is will stretch the texture to the entire size of the screen (again, makes sense because of how the POSITION semantic is being set).

\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

In the code here :

new VertexPositionTexture(new Vector3(1f, -1f, 0f), 
                                  new Vector2(1, 1)),
        new VertexPositionTexture(new Vector3(-1f, -1f, 0), 
                                  new Vector2(0, 1)),
        new VertexPositionTexture(new Vector3(-1f, 1f, 0), 
                                  new Vector2(0, 0)),
        new VertexPositionTexture(new Vector3(1f, 1f, 0), 
                                  new Vector2(1, 0)) 

You try to adjust the Quad Size, by defining 4 points. Depends on whether you try to define them in clockwise or counterclock wise, you can set the positions , topleft, tobRight, BottomLeft, BottomRight instead of those 4 points.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks, I know I needed to change those points (that's what I was asking). I just didn't know how, but I figured it out anyway. \$\endgroup\$
    – test
    Commented Nov 20, 2014 at 19:33
0
\$\begingroup\$

I figured it out. Hopefully this can help someone else.

private short[] indexData = new short[] { 0, 1, 2, 2, 3, 0 };

private VertexPositionTexture[] CreateTriangles(Vector2 location, 
                                                int width, int height)
{
    /* this is what the index data tells us
     * 
     * 2  ________________ 3
     *   |\               |
     *   |  \             |
     *   |    \           |
     *   |      \         |
     *   |        \       |
     *   |          \     |
     *   |            \   |
     *   |              \ |
     *   |________________|
     * 1                   0
     * 
     */

    float bbw = this.gfx.PresentationParameters.BackBufferWidth;
    float bbh = this.gfx.PresentationParameters.BackBufferHeight;

    float left = -(((bbw / 2f) - location.X) / (bbw / 2f));
    float top = (((bbh / 2f) - location.Y) / (bbh / 2f));
    float right = -(((bbw / 2f) - (location.X + width)) / (bbw / 2f));
    float bottom = (((bbh / 2f) - (location.Y + height)) / (bbh / 2f));

    // this is how the triangle points are specified
    VertexPositionTexture[] vertices = new VertexPositionTexture[]
    {

        // point 0
        new VertexPositionTexture(new Vector3(right, bottom, 0f), 
                                  new Vector2(1, 1)),

        // point 1
        new VertexPositionTexture(new Vector3(left, bottom, 0f), 
                                  new Vector2(0, 1)),

        // point 2
        new VertexPositionTexture(new Vector3(left, top, 0f), 
                                  new Vector2(0, 0)),

        // point 3
        new VertexPositionTexture(new Vector3(right, top, 0f), 
                                  new Vector2(1, 0)) 

    };

    return vertices;
}
\$\endgroup\$
1
  • \$\begingroup\$ Is this is an answer please mark it as answer. \$\endgroup\$
    – user52551
    Commented Feb 19, 2015 at 19:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .