Jump to content
  • Advertisement
Sign in to follow this  
jdub

Triangle won't render (D3D9)

This topic is 2695 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'm trying to draw a triangle transformed in world space and nothing shows up when I run the application. P.S. Code is slimdx but it should look very similar to what's called in directx

Camera Code(Called before main loop starts):


static void setCamera(Device device, int width, int height)
{
Matrix projection = Matrix.PerspectiveFovLH((float)(Math.PI / 4.0), (float)width / (float)height, 1.0f, 50.0f);
Matrix view = Matrix.LookAtLH( Vector3.Zero, new Vector3(0.0f, 0.0f, 1.0f), new Vector3(0.0f, 1.0f, 0.0f) );

device.SetTransform(TransformState.Projection, projection);
device.SetTransform(TransformState.View, view);
}



MessagePump/Main Loop Code:


VertexBuffer vBuffer = new VertexBuffer(device, 3 * 20, Usage.None, VertexFormat.None, Pool.Managed);
vBuffer.Lock(0, 0, LockFlags.None).WriteRange<Vertex>(vData);

SlimDX.Windows.MessagePump.Run(form, () =>
{
device.BeginScene();
device.Clear(ClearFlags.Target, new Color4(Color.Teal), 1.0f, 0);

device.VertexDeclaration = vertexDeclaration;
device.SetStreamSource(0, vBuffer, 0, 20);
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1);

device.EndScene();
device.Present();
});



Triangle Creation Code:


Vertex[] vData = new Vertex[]
{
new Vertex(){Position = new Vector3(-100.0f , -100.0f , 5.0f), Color = 100},
new Vertex(){Position = new Vector3(0.0f , 100.0f , 5.0f), Color = 50},
new Vertex(){Position = new Vector3(100.0f , 100.0f , 5.0f), Color = 100},
};

Share this post


Link to post
Share on other sites
Advertisement
Hello,

The problem might be because you are not unlocking your vertex buffer after writing to it (thus, locking it). When you are trying to render its content on the screen, the buffer is still locked and inaccessible by the renderer.

You should check the Slim DX library for an unlock() function which you would call directly after writing the content in the vertex buffer.

Also, just as a side note, when you create the instance of your vertex buffer, the "3 * 20" part is the size of the buffer, right? Are you sure that your vertex structure is 20 bytes of length? You should consider using 3 * sizeof(Vertex) because you'll be sure about the size of the structure and if you want to extend it, then you won't to go back into your code to change the size of the vertex buffer.

Share this post


Link to post
Share on other sites
Thanks for that. I'm baffled why the code even ran. However, my triangle is still not showing >:(


I'm absolutely sure that my Vertex is 20 bytes large

Share this post


Link to post
Share on other sites
Ah yes I think I got it; it didn't come to my mind at first mind. Your vertex format is set to "none". I don't know how Slim DX works, but it shouldn't be put to "none". There should be at least two flags set here: one for your vertex coordinate data and one for your color component. In DirectX, common flags for such a Vertex structure are D3DFVF_XYZRHW and D3DFVF_DIFFUSE.

Try playing around with vertex format. You need to set one to tell the vertex buffer you are creating which type of vertex will reside in it.

You might also have to use a function from Slim DX that tells the renderer to render the vertices using the FVF (flexible vertex format) you used right after setting the stream source. It should be called "setFVF" or something along the lines of that.

Share this post


Link to post
Share on other sites
I think I do the same thing with this code:



VertexElement[] vertexElements = new VertexElement[]
{
new VertexElement(0,0,DeclarationType.Float4,DeclarationMethod.Default, DeclarationUsage.Position, 0),
new VertexElement(0,16, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0), // specular right?
VertexElement.VertexDeclarationEnd
};

VertexDeclaration vertexDeclaration = new VertexDeclaration(device, vertexElements);

/code]


Also, I added: device.SetRenderState(RenderState.Lighting, false); before I render the triangle because I thought that because I hadn't defined any lights.... maybe it would not appear in the scene. Still nothing though

Share this post


Link to post
Share on other sites
It's fine to create a vertex buffer with VertexFormat.None when later using a proper vertex declaration. You either work with FVF or a declaration, not both.

But your struct and the declaration don't match. Your position is a Vector3, your declaration has a Float4 instead. You can avoid using magic numbers (20) by getting the struct size (== vertex stride) with Marshal.SizeOf(Type). You can even check the offsets with Marshal.OffsetOf.

[strike]Then, since you use a DeclarationUsage.Position, you also need to set proper transformations with device.SetTransform (world, view and projection matrices)[/strike]. Sorry, misread, you actually do already, except for the World Transform, but the default might be fine (identity).

Or if you want to use pretransformed vertices (like Nevadas suggested with D3DFVF_XYZRHW), use DeclarationUsage.PositionTransformed and a Vector4.

The D3DX helper class comes with a couple of functions to ease working with formats, e.g. you can check the stride with GetDeclarationVertexSize.

Edit: For debugging disable cull mode, z test and blending. If that still does not show anything, start up PIX to see what's happening.

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!