• 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
Opt7ons

DirectX 9 Instancing (with SharpDX)

1 post in this topic

I am having some trouble getting instancing to work with [b]DX9[/b] and [b]Sharpdx[/b].

My test triangle [b]appears only in the first frame [/b]and then it [b]disappears [/b]or fails to render for every loop [b]after the first[/b].

Here is what I have currently:


[CODE]
[StructLayout(LayoutKind.Sequential)]
public struct VertexPositionColor
{
public Vector4 Position;
public int Color;

public VertexPositionColor(Vector4 position, int color)
{
this.Position = position;
this.Color = color;
}
}

public class Program
{
public static void Main(String[] args)
{
RenderForm form = new RenderForm("SharpDXTest Window");
Direct3D d3d = new Direct3D();
Device device = new Device(d3d, 0, DeviceType.Hardware, form.Handle, CreateFlags.HardwareVertexProcessing,
new PresentParameters(form.ClientSize.Width, form.ClientSize.Height));

device.SetRenderState(RenderState.Lighting, false);
device.SetRenderState<Cull>(RenderState.CullMode, Cull.None);

var effect = Effect.FromFile(device, "MiniCube.fx", ShaderFlags.None);
var technique = effect.GetTechnique(0);
var pass = effect.GetPass(technique, 0);

// Prepare the vertex declaration
var vertexElems = new[] {
new VertexElement(0, 0, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.Position, 0),
new VertexElement(0, 12, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0),
new VertexElement(1, 0, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 1),
new VertexElement(1, 16, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 2),
new VertexElement(1, 32, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 3),
new VertexElement(1, 48, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.TextureCoordinate, 4),
VertexElement.VertexDeclarationEnd
};
var vertexDecl = new VertexDeclaration(device, vertexElems);

// Prepare matrices
var view = Matrix.LookAtRH(new Vector3(0, 0, 10), new Vector3(0, 0, 0), Vector3.UnitY);
var proj = Matrix.OrthoRH(form.ClientSize.Width, form.ClientSize.Height, 0.1f, 1000000000.0f);
device.SetRenderState(RenderState.Lighting, false);
device.SetRenderState<Cull>(RenderState.CullMode, Cull.None);
effect.SetValue("proj", proj);
effect.SetValue("view", view);

VertexBuffer vertices = new VertexBuffer(device, 3 * 20, Usage.WriteOnly, VertexFormat.None, Pool.Managed);
vertices.Lock(0, 0, LockFlags.None).WriteRange(new[] {
new VertexPositionColor() {Position = new Vector4(0, 0, 1.0f, 1), Color = Color.Red.ToArgb()},
new VertexPositionColor() {Position = new Vector4(110, 110, 1.0f, 1), Color = Color.Red.ToArgb()},
new VertexPositionColor() {Position = new Vector4(110, 0, 1.0f, 1), Color = Color.Red.ToArgb()},
});
vertices.Unlock();

Matrix mat = Matrix.Identity;
//mat.Transpose();
VertexBuffer instances = new VertexBuffer(device, 1 * 64, Usage.WriteOnly, VertexFormat.None, Pool.Managed);
instances.Lock(0, 0, LockFlags.None).WriteRange(new[] {
mat
});
instances.Unlock();

IndexBuffer indices = new IndexBuffer(device, 6, Usage.WriteOnly, Pool.Managed, true);
indices.Lock(0, 0, LockFlags.None).WriteRange(new short[] {
(short)0, (short)1, (short)2,
});
indices.Unlock();

var world = Matrix.Translation(100, 100, 0);

effect.Technique = technique;
effect.SetValue("proj", proj);
effect.SetValue("view", view);
effect.SetValue("world", world);

RenderLoop.Run(form, () =>
{
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, (Color4)Color.CornflowerBlue, 1.0f, 0);

device.BeginScene();

effect.Begin(0);
effect.BeginPass(0);

effect.SetValue("proj", proj);
effect.SetValue("view", view);
effect.SetValue("world", world);
device.SetStreamSourceFrequency(0, 3, StreamSource.IndexedData);
device.SetStreamSource(0, vertices, 0, 20);
device.SetStreamSourceFrequency(1, 1, StreamSource.InstanceData);
device.SetStreamSource(1, instances, 0, 64);

device.VertexDeclaration = vertexDecl;
device.Indices = indices;

device.DrawIndexedPrimitive(PrimitiveType.TriangleList, 0, 0, 3, 0, 1);

device.ResetStreamSourceFrequency(0);
device.ResetStreamSourceFrequency(1);

effect.EndPass();

effect.End();

device.EndScene();
device.Present();
});
}[/CODE]


and the shader:
[CODE]

float4x4 world;
float4x4 proj;
float4x4 view;

sampler2D mySampler
{
MinFilter = Point;
MagFilter = Point;
};

struct VS_IN
{
float4 pos : POSITION0;
float4 tex : COLOR0;
float4 W0 : TEXCOORD0;
float4 W1 : TEXCOORD1;
float4 W2 : TEXCOORD2;
float4 W3 : TEXCOORD3;
};

struct PS_IN
{
float4 tex : COLOR0;
float4 pos : POSITION0;
};

PS_IN VS( VS_IN input )
{
//float4x4 wor = float4x4(input.W0, input.W1, input.W2, float4(input.W3.xyz, 1.0f));
//float4x4 objectworld = mul(wor, world);
float4x4 worldViewProj = mul(world, mul(view, proj));

PS_IN output = (PS_IN)0;

output.pos = mul(input.pos, worldViewProj);
output.tex = input.tex;

return output;
}

float4 PS( PS_IN input ) : COLOR
{
float4 output = input.tex;
output.a=1;
output.r=1;
//output = tex2D(mySampler, input.tex);
return output;
}

technique Main
{
pass P0
{
VertexShader = compile vs_3_0 VS();
PixelShader = compile ps_3_0 PS();
}
}
[/CODE]

Any help appreciated [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img].
0

Share this post


Link to post
Share on other sites

I think your problem is in VertexDeclaration.

When you are creating new VertexElement, second parameter in it's constructor is short offset - you set its wrong here

 new VertexElement(0, 0, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.Position, 0),
		 new VertexElement(0, 12, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0),

 

 

float4 = 4 float * 4 bites = 16

and you make offset 12 in Color declaration

1

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