Jump to content
  • Advertisement
Sign in to follow this  
hawksprite

Textured object is not appearing

This topic is 2265 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been trying to work on a method for drawing 2D objects, such as GUI's and what not in DX10 through SlimDX but they just won't appear.

Here's the class


[StructLayout(LayoutKind.Sequential)]
struct Vertex
{
public Vector4 PositionRhw;
public Vector4 Color;
public Vector2 UV;
}

public static class TextureDrawer
{
public static Device TextureDevice;
}

public class T2D
{
public Texture2D Texture;

Effect effect;
EffectTechnique technique;
EffectPass pass;
InputLayout layout;
Buffer vertices;
List<Vector4> vertexPositions = new List<Vector4>();
List<Vector2> textureCoords = new List<Vector2>();
List<Vector4> vertexColors = new List<Vector4>();

public T2D(string FileName, SlimDX.DXGI.SwapChain swapChain)
{
Texture = Texture2D.FromFile(TextureDrawer.TextureDevice, FileName);

/*
// Setup the spriteInstances
spriteInstances = new SpriteInstance[1];
spriteInstances[0] = new SpriteInstance(new ShaderResourceView(TextureDrawer.TextureDevice, Texture), new Vector2(10, 10), new Vector2(10, 10));
*/
effect = Effect.FromFile(TextureDrawer.TextureDevice, "SimpleTexturedQuad.fx", "fx_4_0", SlimDX.D3DCompiler.ShaderFlags.None, SlimDX.D3DCompiler.EffectFlags.None);
technique = effect.GetTechniqueByIndex(0);
pass = technique.GetPassByIndex(0);

EffectResourceVariable shaderTetxure = effect.GetVariableByName("texture2d").AsResource();
ShaderResourceView textureView = new ShaderResourceView(TextureDrawer.TextureDevice, Texture);
shaderTetxure.SetResource(textureView);

InputElement[] inputElements = new InputElement[]
{
new InputElement("POSITION", 0, SlimDX.DXGI.Format.R32G32B32A32_Float, 0, 0),
new InputElement("COLOR", 0, SlimDX.DXGI.Format.R32G32B32A32_Float, 16, 0),
new InputElement("TEXCOORD", 0, SlimDX.DXGI.Format.R32G32_Float, 32, 0)
};

layout = new InputLayout(TextureDrawer.TextureDevice, inputElements, pass.Description.Signature);

vertexPositions.Add(new Vector4(-0.5f, -0.5f, 0.5f, 1.0f));
vertexPositions.Add(new Vector4(-0.5f, 0.5f, 0.5f, 1.0f));
vertexPositions.Add(new Vector4(0.5f, -0.5f, 0.5f, 1.0f));
vertexPositions.Add(new Vector4(0.5f, 0.5f, 0.5f, 1.0f));

textureCoords.Add(new Vector2(0.0f, 1.0f));
textureCoords.Add(new Vector2(0.0f, 0.0f));
textureCoords.Add(new Vector2(1.0f, 1.0f));
textureCoords.Add(new Vector2(1.0f, 0.0f));

vertexColors.Add(new Vector4(1.0f, 0.0f, 0.0f, 1.0f));
vertexColors.Add(new Vector4(0.0f, 1.0f, 0.0f, 1.0f));
vertexColors.Add(new Vector4(0.0f, 0.0f, 1.0f, 1.0f));
vertexColors.Add(new Vector4(1.0f, 0.0f, 1.0f, 1.0f));

DataStream stream = new DataStream(vertexPositions.Count * Marshal.SizeOf(typeof(Vertex)), true, true);
for (int i = 0; i < vertexPositions.Count; i++)
{
stream.Write(vertexPositions);
stream.Write(vertexColors);
stream.Write(textureCoords);
}

// Important: when specifying initial buffer data like this, the buffer will
// read from the current DataStream position; we must rewind the stream to
// the start of the data we just wrote.
stream.Position = 0;

BufferDescription bufferDescription = new BufferDescription();
bufferDescription.BindFlags = BindFlags.VertexBuffer;
bufferDescription.CpuAccessFlags = CpuAccessFlags.None;
bufferDescription.OptionFlags = ResourceOptionFlags.None;
bufferDescription.SizeInBytes = vertexPositions.Count * Marshal.SizeOf(typeof(Vertex));
bufferDescription.Usage = ResourceUsage.Default;

vertices = new Buffer(TextureDrawer.TextureDevice, stream, bufferDescription);

stream.Close();
}

public void Draw()
{
TextureDrawer.TextureDevice.InputAssembler.SetInputLayout(layout);
TextureDrawer.TextureDevice.InputAssembler.SetPrimitiveTopology(PrimitiveTopology.TriangleStrip);
TextureDrawer.TextureDevice.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertices, Marshal.SizeOf(typeof(Vertex)), 0));

for (int p = 0; p < technique.Description.PassCount; p++)
{
pass.Apply();

TextureDrawer.TextureDevice.Draw(vertexPositions.Count, 0);
}
}
}


This is my second day working with SlimDX so I don't know as much as I'd like and it's probably something simple.

Shader code


Texture2D texture2d;

matrix worldMatrix;
matrix viewMatrix;
matrix projectionMatrix;


SamplerState linearSampler
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};

struct VS_IN
{
float4 position : POSITION;
float4 color : COLOR;
float2 UV: TEXCOORD0;
};

struct PS_IN
{
float4 position : SV_POSITION;
float4 color : COLOR;
float2 UV: TEXCOORD0;
};


PS_IN VS( VS_IN vertexShaderIn )
{
PS_IN vertexShaderOut = (PS_IN)0;

vertexShaderOut.position = vertexShaderIn.position;
vertexShaderOut.color = vertexShaderIn.color;
vertexShaderOut.UV = vertexShaderIn.UV;

vertexShaderOut.position = mul(vertexShaderIn.position, worldMatrix);
vertexShaderOut.position = mul(vertexShaderOut.position, viewMatrix);
vertexShaderOut.position = mul(vertexShaderOut.position, projectionMatrix);

return vertexShaderOut;
}

float4 PS( PS_IN pixelShaderIn ) : SV_Target
{
float4 finalColor = texture2d.Sample( linearSampler, pixelShaderIn.UV );
finalColor = float4(1,1,1,1);
return finalColor;
}

technique10 Render
{
pass P0
{
SetGeometryShader( 0 );
SetVertexShader( CompileShader( vs_4_0, VS() ) );
SetPixelShader( CompileShader( ps_4_0, PS() ) );
}
}

Share this post


Link to post
Share on other sites
Advertisement
No I haven't.

Would that be the device's Rasterizer? If so what should I set it to.

Edit:
I believe I set the state now but no changes to the cull mode made the object appear:


RasterizerStateDescription rsd = new RasterizerStateDescription();
rsd.FillMode = SlimDX.Direct3D10.FillMode.Solid;
rsd.CullMode = CullMode.Front;

TextureDrawer.TextureDevice.Rasterizer.State = RasterizerState.FromDescription(TextureDrawer.TextureDevice, rsd);
Edited by hawksprite

Share this post


Link to post
Share on other sites
I've got a triangle to appear. Being newer to SlimDX my code is just a horrible mess of example snipets and crappy code holding it all together.

But now i've hit another wall. I can make one triangle but im not 100% sure how to turn it into a more rectangular state then send the information needed to the shader to sample it as a texture.

Share this post


Link to post
Share on other sites
I have it working now i'll post the code below as a guideline for anybody having issues with this:



[STAThread]
static void Main()
{
var form = new RenderForm("Legends Among Men");
var desc = new SwapChainDescription()
{
BufferCount = 1,
ModeDescription = new ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new Rational(60, 1), Format.R8G8B8A8_UNorm),
IsWindowed = true,
OutputHandle = form.Handle,
SampleDescription = new SampleDescription(1, 0),
SwapEffect = SwapEffect.Discard,
Usage = Usage.RenderTargetOutput
};

Device device;
SwapChain swapChain;
Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, desc, out device, out swapChain);

device.Factory.SetWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);

Texture2D backBuffer = Texture2D.FromSwapChain<Texture2D>(swapChain, 0);
var renderView = new RenderTargetView(device, backBuffer);
var bytecode = ShaderBytecode.CompileFromFile("MiniTri.fx", "fx_5_0", ShaderFlags.None, EffectFlags.None);
var effect = new Effect(device, bytecode);
var technique = effect.GetTechniqueByIndex(0);
var pass = technique.GetPassByIndex(0);
var layout = new InputLayout(device, pass.Description.Signature, new[] {
new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0)
});



device.ImmediateContext.OutputMerger.SetTargets(renderView);
device.ImmediateContext.Rasterizer.SetViewports(new Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height, 0.0f, 1.0f));

var textureView = new ShaderResourceView(device, Texture2D.FromFile(device, "picture2.png"));

var stream = new DataStream(4 * 32, true, true);
stream.WriteRange(new[] {
new Vector4(-0.5f, -0.5f, 0.5f, 1.0f), new Vector4(0.0f, 1.0f,0,0),
new Vector4(-0.5f, 0.5f, 0.5f, 1.0f), new Vector4(0.0f, 0.0f,0,0),
new Vector4(0.5f, -0.5f, 0.5f, 1.0f), new Vector4(1.0f, 1.0f,0,0),
new Vector4(0.5f, 0.5f, 0.5f, 1.0f), new Vector4(1.0f, 0.0f,0,0)
});
stream.Position = 0;

var vertices = new SlimDX.Direct3D11.Buffer(device, stream, new BufferDescription()
{
BindFlags = BindFlags.VertexBuffer,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.None,
SizeInBytes = 4 * 32,
Usage = ResourceUsage.Default
});
stream.Dispose();

var view = Matrix.Translation(Vector3.Zero);
var proj = Matrix.Translation(Vector3.Zero);
var world = Matrix.Translation(Vector3.Zero);

float zz = 0;

device.ImmediateContext.InputAssembler.InputLayout = layout;
device.ImmediateContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleStrip;
device.ImmediateContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertices, 32, 0));

effect.GetVariableByName("model_texture").AsResource().SetResource(textureView);
effect.GetVariableByName("proj").AsMatrix().SetMatrix(proj);
effect.GetVariableByName("view").AsMatrix().SetMatrix(view);

MessagePump.Run(form, () =>
{
device.ImmediateContext.ClearRenderTargetView(renderView, Color.Black);

zz += 0.001f;
world = Matrix.Translation(new Vector3(zz, 0, 0));

effect.GetVariableByName("world").AsMatrix().SetMatrix(world);

for (int i = 0; i < technique.Description.PassCount; ++i)
{
pass.Apply(device.ImmediateContext);
device.ImmediateContext.Draw(4, 0);
}

swapChain.Present(1, PresentFlags.None);
});

bytecode.Dispose();
vertices.Dispose();
layout.Dispose();
effect.Dispose();
renderView.Dispose();
backBuffer.Dispose();
device.Dispose();
swapChain.Dispose();
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!