Sign in to follow this  
skowronkow

[SlimDX]

Recommended Posts

Hi! I have little problem with texturing in SlimDX. I create simple triangle and i want to add texture to it. Could someone give me a simple example how can i get it? (I've read some tuts for MDX 2.0 so i would be appreciated to see WORKING slimDX's examples) Thanks for Yor help

Share this post


Link to post
Share on other sites
OK, so lets try to modify MiniTri example from D3D9 folder in your SlimDX examples to get our triangle textured.

I guess, firstly we need to modify our custom vertices type.



[StructLayout(LayoutKind.Sequential)]
struct Vertex
{
public Vector4 PositionRhw;
public int Color;
public float Tu;
public float Tv;
}


Is it correct? (Color ofcourse its not important here)

Then i load texture form png, lets called it tex, and modify our VertexBuffer to:


Vertices = new VertexBuffer(Device, 3*Marshal.SizeOf(typeof(Vertex)), Usage.WriteOnly,VertexFormat.None,Pool.Managed);

DataStream stream = Vertices.Lock(0, 0, LockFlags.None);
Vertex[] vertexData = new Vertex[3];
vertexData[0].PositionRhw = new Vector4(400.0f, 100.0f, 0.5f, 1.0f);
vertexData[0].Color = Color.Red.ToArgb();
vertexData[0].Tu = 0.0f;
vertexData[0].Tv = 0.0f;
vertexData[1].PositionRhw = new Vector4(650.0f, 500.0f, 0.5f, 1.0f);
vertexData[1].Color = Color.Blue.ToArgb();
vertexData[1].Tu = 1.0f;
vertexData[1].Tv = 0.0f;
vertexData[2].PositionRhw = new Vector4(150.0f, 500.0f, 0.5f, 1.0f);
vertexData[2].Color = Color.Green.ToArgb();
vertexData[2].Tu = 1.0f;
vertexData[2].Tv = 1.0f;
stream.WriteRange(vertexData);
Vertices.Unlock();


and the last modification is in render method:


Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);

Device.BeginScene();
Device.SetTexture(0, tex);
Device.SetStreamSource(0, Vertices, 0, Marshal.SizeOf(typeof(Vertex)));

Device.VertexFormat = VertexFormat.PositionRhw | VertexFormat.Diffuse | VertexFormat.Texture0;

Device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);
Device.EndScene();
Device.Present();


And what i get is the same triangle with a little bit more darked colors..

What i missed?

Share this post


Link to post
Share on other sites
First thing I noticed...from the docs:

Texture1
Vertex format contains 1 texture coordinate set.

Texture0
Vertex format contains no texture coordinate sets.

You don't want Texture0.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this