Texture coordinates?!

How I should define texture coordinates in XNA. There seems to be something wicked going on in following code:

...
vertexList = new VertexPositionTextureDevil;
vertexList[0] = new VertexPositionTexture(new Vector3(-1.0f, 1.0f, 0), new Vector2(0.0f, 0.0f)); // Color.Red
vertexList[1] = new VertexPositionTexture(new Vector3(-1.0f, -1.0f, 0), new Vector2(0.0f, 1.0f)); // Color.Yellow
vertexList[2] = new VertexPositionTexture(new Vector3(1.0f, 1.0f, 0), new Vector2(1.0f, 0.0f)); // Color.Green

vertexList[3] = new VertexPositionTexture(new Vector3(1.0f, 1.0f, 0), new Vector2(1.0f, 0.0f)); // Color.Green
vertexList[4] = new VertexPositionTexture(new Vector3(-1.0f, -1.0f, 0), new Vector2(0.0f, 1.0f)); // Color.Yellow
vertexList[5] = new VertexPositionTexture(new Vector3(1.0f, -1.0f, 0), new Vector2(1.0f, 1.0f)); // Color.Red

vertexDeclaration = new VertexDeclaration(graphics.GraphicsDevice, VertexPositionTexture.VertexElements);
...

Only with vertex colors everything is fine, but when try to use textures everything is messed up. So how I should define texture coordinates with XNA

EDIT: Whole point is to generate screen aligned quad..



Answer this question

Texture coordinates?!

  • kperson

    Looking at this I don't see anything wrong with your co-ordinates. Have you checked the texture itself to make sure it isn't corrupted and is being loaded into XNA in the correct format

    At first glance it looks as though the texture is not loaded correctly. Can you show us the shader you are using


  • Jim Wooley

    I'm using custom shader and it should be fine because I've used it with normal DX.

    http://img133.imageshack.us/img133/146/jokubugaahy7.jpg <-- something strange with texture coords..

  • BrianMcCashin

     Shawn Hargreaves wrote:
    You aren't setting any vertex shader here: is that set up elsewhere in your code

    If you aren't specifically setting a vertex shader, this rendering will just be inheriting whatever was previously set. My guess would be the previous vertex shader isn't passing through texture coordinates.


    I defined vertex shader it's still not working. Now it will just throw an exeception (InvalidCallException, ErrorCode=-2147467259) when DrawUserPrimitives is called. Is there any sample/tutorial how simple texture screen aligned should be done with XNA


    EDIT: My mistake BeginScene() was commented out  Big thanks to Shawn!

  • Kevin Hoffman

    You aren't setting any vertex shader here: is that set up elsewhere in your code

    If you aren't specifically setting a vertex shader, this rendering will just be inheriting whatever was previously set. My guess would be the previous vertex shader isn't passing through texture coordinates.


  • BJ Custard

    Texture should be fine because I've tried various textures.

    Shader code:

    // GLOBALS
    //
    uniform float waveFreq = 0.0;
    uniform float waveAmp = 0.0;
    uniform float time = 0.0;

    texture SCENE
    <
    string name = "SCENE";
    >;

    uniform sampler2D tex0;


    // STRUCTRS
    //
    struct PS_IN
    {
    float4 t0 : TEXCOORD0;
    };

    struct PS_OUT
    {
    float4 c0 : COLOR;
    };


    // PS ENTRY POINT
    //
    PS_OUT main(PS_IN ps_in)
    {
    float PI = 3.14159;

    // point
    float4 point = ps_in.t0 - float4(0.5, 0.5, 0.0, 0.1);

    // radius
    float radius = sqrt(point.x * point.x + point.y * point.y);

    // angles
    float cosAngle = point.x / radius;
    float sinAngle = point.y / radius;

    // calculate wave angle
    float waveAngle = (radius - time) * waveFreq;
    waveAngle = fmod(waveAngle, 2 * PI);

    // calculate offset
    float offset = 1 - cos(waveAngle - PI);
    offset *= waveAmp;

    // new radius
    float newRadius = radius + offset;

    // new point
    float4 newPoint = float4(newRadius * cosAngle + 0.5, newRadius * sinAngle + 0.5, 0.0, 0.1);

    PS_OUT ps_out;
    ps_out.c0 = tex2D(tex0, newPoint);
    ps_out.c0 += float4(time, 0.0, 0.0, 0.0);

    return ps_out;
    }

    // PASSES
    //
    technique ripple
    {
    pass
    {
    Sampler[0] = sampler_state
    {
    Texture = (SCENE);
    };

    CullMode = None;
    MultiSampleAntiAlias = TRUE;
    AlphaBlendEnable = TRUE;

    PixelShader = compile ps_2_0 main();
    }
    }

  • Irfon Subhan

    Texture coordinates will normally range from 0,0 (the top left of the texture) to 1,1 (the bottom right), but this depends on what shader you are using to render the polygon.

    Are you using BasicEffect for this, or a custom shader

    What exactly is the problem with your output A bit more detail about how the image is looking wrong would be useful to help diagnose this.


  • Texture coordinates?!