Sign in to follow this  

MDX (2.0) - DrawIndexedPrimitives

This topic is 4106 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

Hi, I'm using MDX (2.0) and trying to draw simple primitives (a rotating cube) but whatever I try *nothing* appears - just a black screen. Here's my code snippets: Declarations:
    Protected buf As VertexBuffer, ibuf As IndexBuffer

Render:
        If buf <> Nothing Then
            Dim xangle As Single = Environment.TickCount / 500.0F
            Dim yangle As Single = Environment.TickCount / 800.0F
            Dim xrot As Matrix = Matrix.RotationX(xangle)
            Dim yrot As Matrix = Matrix.RotationY(yangle)

            _graphics.Transform.World = Matrix.Multiply(xrot, yrot)
            _graphics.VertexFormat = PositionColored.Format
            _graphics.SetStreamSource(0, buf, 0)
            _graphics.Indices = ibuf
            _graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 0, 12)

            Exit Sub
        End If

Buffer creation:
        Dim i As Integer

        buf = VertexBuffer.CreateGeneric(Of PositionTextured)( _
            _graphics, _
            8, _
            Usage.WriteOnly, _
            CustomVertex.PositionColored.Format, _
            Pool.Default, _
            Nothing)

        Dim verts As Generic.GraphicsBuffer(Of PositionColored) = buf.Lock(Of PositionColored)(0, 0, LockFlags.None)

        i = 0

        verts(i) = New CustomVertex.PositionColored(-0.5F, 0.5F, -0.5F, Color.Red.ToArgb())
        i += 1
        verts(i) = New CustomVertex.PositionColored(0.5F, 0.5F, -0.5F, Color.Orange.ToArgb())
        i += 1
        verts(i) = New CustomVertex.PositionColored(-0.5F, -0.5F, -0.5F, Color.Yellow.ToArgb())
        i += 1
        verts(i) = New CustomVertex.PositionColored(0.5F, -0.5F, -0.5F, Color.Green.ToArgb())
        i += 1
        verts(i) = New CustomVertex.PositionColored(-0.5F, 0.5F, 0.5F, Color.Blue.ToArgb())
        i += 1
        verts(i) = New CustomVertex.PositionColored(0.5F, 0.5F, 0.5F, Color.Violet.ToArgb())
        i += 1
        verts(i) = New CustomVertex.PositionColored(-0.5F, -0.5F, 0.5F, Color.Red.ToArgb())
        i += 1
        verts(i) = New CustomVertex.PositionColored(0.5F, -0.5F, 0.5F, Color.Orange.ToArgb())

        buf.Unlock()

        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

        ibuf = IndexBuffer.CreateGeneric(Of PositionColored)( _
            _graphics, _
            36, _
            Usage.None, _
            Pool.Default, _
            False, _
            Nothing)

        Dim arr As Generic.GraphicsBuffer(Of Short) = buf.Lock(Of Short)(0, 0, LockFlags.None)

        i = 0

        ' Front face
        arr(i) = 0 : i += 1 : arr(i) = 3 : i += 1 : arr(i) = 1 : i += 1
        arr(i) = 0 : i += 1 : arr(i) = 2 : i += 1 : arr(i) = 3 : i += 1

        ' Top face
        arr(i) = 0 : i += 1 : arr(i) = 1 : i += 1 : arr(i) = 4 : i += 1
        arr(i) = 1 : i += 1 : arr(i) = 5 : i += 1 : arr(i) = 4 : i += 1

        ' Left face
        arr(i) = 0 : i += 1 : arr(i) = 4 : i += 1 : arr(i) = 2 : i += 1
        arr(i) = 2 : i += 1 : arr(i) = 4 : i += 1 : arr(i) = 6 : i += 1

        ' Right face
        arr(i) = 1 : i += 1 : arr(i) = 7 : i += 1 : arr(i) = 5 : i += 1
        arr(i) = 1 : i += 1 : arr(i) = 3 : i += 1 : arr(i) = 7 : i += 1

        ' Back face
        arr(i) = 4 : i += 1 : arr(i) = 5 : i += 1 : arr(i) = 6 : i += 1
        arr(i) = 5 : i += 1 : arr(i) = 7 : i += 1 : arr(i) = 6 : i += 1

        ' Bottom face
        arr(i) = 2 : i += 1 : arr(i) = 6 : i += 1 : arr(i) = 3 : i += 1
        arr(i) = 3 : i += 1 : arr(i) = 6 : i += 1 : arr(i) = 7 : i += 1

        buf.Unlock()

Camera setup (on start and when device is reset):
        ' Setup camera.
        _graphics.Transform.View = Matrix.LookAtLeftHanded( _
            New Vector3(0, 0, -50), New Vector3(0, 0, 0), New Vector3(0, 1, 0))
        _graphics.Transform.Projection = Matrix.PerspectiveFieldOfViewLeftHanded( _
            Math.PI / 50.0F, _graphicsManager.Resolution.Width / _graphicsManager.Resolution.Height, 1.0F, 100.0F)

Thanks in advance for any help...

Share this post


Link to post
Share on other sites
Quote:
Original post by alex_myrpg
Hi, I'm using MDX (2.0) and trying to draw simple primitives (a rotating cube) but whatever I try *nothing* appears - just a black screen.

What happens if you clear to something other than black? Have you disabled lighting?

Also, what happens if you render the first 8 verts without an index buffer, do you get something on screen? If not, this probably isn't because of the DrawIndexedPrimitive call, but rather something else...

Are you clearing the z-buffer?

Lastly, I'd recommend using the 4 parameter overload of SetStreamSource, and not the 3 parameter one.

Hope this helps.

Share this post


Link to post
Share on other sites
FYI: MDX 2.0 is now considered deprecated. It is very likely it will be removed from the DirectX SDK in the future. Also MS has never released an runtime environment for it. In order to obtain the runtime, end users must install the SDK. You should seriously consider porting your code to MDX 1.1 or the XNA Framework (what MDX 2.0 has become).

Share this post


Link to post
Share on other sites
Yes, I will indeed port it to XNA in time. But that's still in beta too I believe, and anyway supposably it's fairly similar to MDX 2.0. To the other two posters, thank you very much for you replies - I will try them asap. :)

Share this post


Link to post
Share on other sites
I'm sorry , but I don't know much bout Visual basic ( I think it's visual basic , is it ? ) . I could only write the app you're tryin to program in c# mdx code , and you could try to find the error ... If it helps you ...

Share this post


Link to post
Share on other sites
Posting your complete Render method would help. If that is all there is to it then you are not clearing the backbuffer, beginning the scene, ending the scene or calling present.

I know next to zero VB, but I think your Render Sub should look a little like this ...
_graphics.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, Color.White, 1.0F, 0)

_graphics.BeginScene()

If buf <> Nothing Then
Dim xangle As Single = Environment.TickCount / 500.0F
Dim yangle As Single = Environment.TickCount / 800.0F
Dim xrot As Matrix = Matrix.RotationX(xangle)
Dim yrot As Matrix = Matrix.RotationY(yangle)

_graphics.Transform.World = Matrix.Multiply(xrot, yrot)
_graphics.VertexFormat = PositionColored.Format
_graphics.SetStreamSource(0, buf, 0)
_graphics.Indices = ibuf
_graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 0, 12)

End If

_graphics.EndScene()
_graphics.Present()



If you are already doing those things then I would start to look at your Projection matrix (as mentioned earlier in this thread) and your World transforms i.e. see if your cube renders without spinning it (Transform.World = Matrix.Identity). Your camera position in the LookAt matrix is also set fairly far back considering the cube is of unit size.

If all your are seeing is black and your clear colour is white (as above) then I think you should work on that before trying to render anything [smile]

Hope this helps,
ViLiO

Share this post


Link to post
Share on other sites
Hi, thank you for your replies. In fact I know C# pretty well too...not quite as well as VB.NET but well enough - so if it would help, I could translate the code maybe... Anyway yes of course I am doing the Clear, begin scene etc., I'm not new to DirectX at all, but I've only used it for 2D stuff before, never 3D. Still, I will try the Matrix.Identity and also clearing the ZBuffer maybe. Thanks

Share this post


Link to post
Share on other sites
This is XNA code that I used to draw a cube. use what you need...

btw, make sure lighting is working correctly and turn culling off.



using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
//using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Components;
using Microsoft.Xna.Framework.Graphics;
//using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using ODE;
using ODE.Geoms;


namespace ODEComponents
{
public partial class ODEBox : Microsoft.Xna.Framework.GameComponent
{
private IGraphicsDeviceService gds;
private Helpers.ICameraService camService;

private VertexDeclaration vertexDecl;
//private IndexBuffer indexBuffer;


private VertexPositionNormalTexture[] cubeVerts = new VertexPositionNormalTexture[8];
private int[] cubeIndices = new int[36];

private Effect effect;

//EffectParams
private EffectParameter WorldITParam;
private EffectParameter WorldParam;
private EffectParameter WorldViewProjParam;
private EffectParameter ViewIParam;
private EffectParameter SpecColorParam;
private EffectParameter DiffColorParam;
private EffectParameter SubColorParam;
private EffectParameter LightPosParam;
private EffectParameter RollOffParam;
private EffectParameter ColorTextureParam;

Box box;
Body body;

public ODEBox()
{
InitializeComponent();

}

public override void Start()
{
box = new Box(1, 1, 1);

body = new Body(ODE_NET2.world);

body.Mass = Mass.BoxTotal(1f, 1, 1, 1);

box.RigidBody = this.body;

//test
System.Random RandNum = new System.Random();

this.Position = new Vector3(RandNum.Next(-10, 10), RandNum.Next(10, 100), RandNum.Next(-10, 10));

gds = this.Game.GameServices.GetService<IGraphicsDeviceService>();
camService = this.Game.GameServices.GetService<Helpers.ICameraService>();

ODE_NET2.space.Add(box);

CompiledEffect compiledEffect = Effect.CompileEffectFromFile(@".\Media\velvety.fx", null, null, CompilerOptions.None, TargetPlatform.Windows);
effect = new Effect(gds.GraphicsDevice, compiledEffect.GetShaderCode(), CompilerOptions.None, null);

effect.CurrentTechnique = effect.Techniques["UntexturedPS"];

//Console.WriteLine(compiledEffect.ErrorsAndWarnings);


WorldITParam = effect.Parameters["WorldIT"];
WorldParam = effect.Parameters["World"];
WorldViewProjParam = effect.Parameters["WorldViewProj"];
ViewIParam = effect.Parameters["ViewI"];
SpecColorParam = effect.Parameters["SpecColor"];
DiffColorParam = effect.Parameters["DiffColor"];
LightPosParam = effect.Parameters["LightPos"];
SubColorParam = effect.Parameters["SubColor"];
RollOffParam = effect.Parameters["RollOff"];
ColorTextureParam = effect.Parameters["ColorTexture"];

LightPosParam.SetValue(new Vector3(0.0f, 100.0f, 0.0f));
DiffColorParam.SetValue(new Vector3(0.0f, 0.0f, 0.0f));
SubColorParam.SetValue(new Vector3(1.0f, 1.0f, 1.0f));
ColorTextureParam.SetValue(Texture2D.FromFile(gds.GraphicsDevice, @".\Media\default_color.dds"));
RollOffParam.SetValue(0.2f);
SpecColorParam.SetValue(new Vector3(RandNum.Next(0, 100) / 100f, RandNum.Next(0, 100) / 100f, RandNum.Next(0, 100) / 100f));

cubeVerts[0] = new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, 0.5f), Vector3.Backward, new Vector2(0.0f, 0.0f)); //front-left-top
cubeVerts[1] = new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, 0.5f), Vector3.Backward, new Vector2(0.0f, 1.0f)); //front-left-bottom
cubeVerts[2] = new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, 0.5f), Vector3.Backward, new Vector2(1.0f, 0.0f)); //front-right-top
cubeVerts[3] = new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, 0.5f), Vector3.Backward, new Vector2(1.0f, 1.0f)); //front-right-bottm

cubeVerts[4] = new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, -0.5f), Vector3.Forward, new Vector2(0.0f, 0.0f)); //back-left-top
cubeVerts[5] = new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, -0.5f), Vector3.Forward, new Vector2(0.0f, 1.0f)); //back-left-bottom
cubeVerts[6] = new VertexPositionNormalTexture(new Vector3(0.5f, 0.5f, -0.5f), Vector3.Forward, new Vector2(1.0f, 0.0f)); //back-right-top
cubeVerts[7] = new VertexPositionNormalTexture(new Vector3(0.5f, -0.5f, -0.5f), Vector3.Forward, new Vector2(1.0f, 1.0f)); //back-right-bottom

// Indices will improve performance by avoiding redundent vertices
cubeIndices[0] = 0; //front
cubeIndices[1] = 1;
cubeIndices[2] = 2;
cubeIndices[3] = 1;
cubeIndices[4] = 3;
cubeIndices[5] = 2;
cubeIndices[6] = 4; //back
cubeIndices[7] = 5;
cubeIndices[8] = 6;
cubeIndices[9] = 5;
cubeIndices[10] = 7;
cubeIndices[11] = 6;
cubeIndices[12] = 0; //top
cubeIndices[13] = 4;
cubeIndices[14] = 6;
cubeIndices[15] = 0;
cubeIndices[16] = 2;
cubeIndices[17] = 6;
cubeIndices[18] = 0; //left
cubeIndices[19] = 4;
cubeIndices[20] = 5;
cubeIndices[21] = 0;
cubeIndices[22] = 5;
cubeIndices[23] = 1;
cubeIndices[24] = 1; //bottom
cubeIndices[25] = 5;
cubeIndices[26] = 7;
cubeIndices[27] = 1;
cubeIndices[28] = 3;
cubeIndices[29] = 7;
cubeIndices[30] = 3; //right
cubeIndices[31] = 7;
cubeIndices[32] = 6;
cubeIndices[33] = 3;
cubeIndices[34] = 6;
cubeIndices[35] = 2;



// Vertex declaration
vertexDecl = new VertexDeclaration(gds.GraphicsDevice, VertexPositionNormalTexture.VertexElements);
gds.GraphicsDevice.VertexDeclaration = vertexDecl;

//gds.GraphicsDevice.RenderState.FillMode = FillMode.WireFrame;

}

// ODE knows where this box is at all times.
public Matrix mWorld
{
get
{
return (box.Rotation * Matrix.CreateTranslation(box.Position));
}

}

public Vector3 Position
{
get
{
return box.Position;
}
set
{
box.Position = value;
}
}


public override void Update()
{
//Matrix mWorld = Matrix.Identity;
// SetValues
WorldITParam.SetValue(Matrix.Transpose(Matrix.Invert(mWorld)));
WorldParam.SetValue(mWorld);
WorldViewProjParam.SetValue(mWorld * camService.View * camService.Projection);
ViewIParam.SetValue(Matrix.Invert(camService.View));



//Console.WriteLine("updating box");
}

public override void Draw()
{

effect.Begin( EffectStateOptions.Default );
foreach( EffectPass pass in effect.CurrentTechnique.Passes )
{
pass.Begin();

effect.CommitChanges();
// Draw Cube
gds.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, 0, 36, 12, cubeIndices, cubeVerts);
//gds.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, 12, cubeVerts);


pass.End();
}
effect.End();

}

}
}

Share this post


Link to post
Share on other sites
One thing I see is when you are creating your indexbuffer, you are setting the Use16BitIndices to False, but you are using Short which IS 16-bit. Try setting that flag to True.

Share this post


Link to post
Share on other sites
Quote:
Original post by alex_myrpg
Nope, nothing works. :( Really stuck here... I guess I'll port it to XNA first since there's virtually ZERO support for MDX 2.0.


As I have mentioned before, MDX 2.0 is now deprecated. In fact it is set to expire coming up, and will no longer function. Yep, I know, that's a pretty shitty thing for them to be doing. But I guess it makes sure prerelease software is no longer used.

From David Wellers Blog:
"A recent post on the MSDN Forum raised an issue that users of Managed DirectX Beta 2.0 (MDX 2.0) may or may not be aware of: The MDX 2.0 beta libraries will cease to function after October 5th, 2006. That's 15 days from now."

Continue Reading His Post Here

[Edited by - Billr17 on September 20, 2006 5:52:20 PM]

Share this post


Link to post
Share on other sites

This topic is 4106 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.

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