Search code examples
c#3dxnatexturing

Pretransformed textured polygons


I'm only starting with 3D so I have a question about drawing pretransformed stuff onto the screen.

I was able to figure out how to draw colored polygons in pretransformed form by using some tutorials, but I just can't understand how to texture them... Is that even possible? If so how?

What I have now:

private void TestVertices()
{
 vertices = new VertexPositionColor[3];
 vertices[0].Position = new Vector3(-0.5f, -0.5f, 0f);
 vertices[0].Color = Color.Red;
 vertices[1].Position = new Vector3(0, 0.5f, 0f);
 vertices[1].Color = Color.Green;
 vertices[2].Position = new Vector3(0.5f, -0.5f, 0f);
 vertices[2].Color = Color.Blue;
}

and

protected override void Draw(GameTime gameTime)
{
     device.Clear(Color.DarkSlateBlue);
     effect.CurrentTechnique = effect.Techniques["Pretransformed"];
     foreach (EffectPass pass in effect.CurrentTechnique.Passes)
     {
         pass.Apply();
         device.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 1, VertexPositionColor.VertexDeclaration);
     }
     base.Draw(gameTime);
}

Solution

  • Instead of VertexPositionColor, use VertexPositionColorTexture for your vertices, and set the TextureCoordinate member to a value between 0 and 1 on each axis.

    When drawing, set GraphicsDevice.Texture[0] to the texture you want to use.

    Ensure that your pixel shader does something like this:

    // in the header:
    sampler myTexture : register(s0);
    
    // in your shader function:
    float4 outputColor = input.Color * tex2D(myTexture, input.TexCoord);
    

    If you are using BasicEffect, the equivalent is to use VertexPositionColorTexture, set the BasicEffect.Texture to your texture, and set BasicEffect.TextureEnabled = true.