• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
hawksprite

Textured object is not appearing

7 posts in this topic

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
[code]

[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[i]);
stream.Write(vertexColors[i]);
stream.Write(textureCoords[i]);
}

// 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);
}
}
}
[/code]

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
[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() ) );
}
}
[/code]
0

Share this post


Link to post
Share on other sites
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:
[code]

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

TextureDrawer.TextureDevice.Rasterizer.State = RasterizerState.FromDescription(TextureDrawer.TextureDevice, rsd);
[/code] Edited by hawksprite
0

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.
0

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:

[code]

[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();
}
[/code]
0

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  
Followers 0