Sign in to follow this  
Kilickaya

Untitled

Recommended Posts

Kilickaya    110

When my program executes the line "Direct3D objD3D = new Direct3D ()", a red rectangle as big as the screen is drawn and a red line from up-left of the screen to right-down of it and another red line from up-right of the screen to left-down of it. With what functions is that appearance got rid of?

Thanks in advance

Share this post


Link to post
Share on other sites
Burnt_Fyr    1665
Without source code this is impossible to even begin to help with. Try posting the constructor of the Direct3D class, and anything else that you feel might be relevant.

Share this post


Link to post
Share on other sites
Kilickaya    110
Quote:
Original post by Burnt_Fyr
Without source code this is impossible to even begin to help with. Try posting the constructor of the Direct3D class, and anything else that you feel might be relevant.


---------------------------
This is my source code. It should draw a triangle. If you inform me what else is wrong with my whole code, I be glad.

private void Form1_Paint (object sender, PaintEventArgs e)
{
Direct3D objDirect3D = new Direct3D ();
PresentParameters pp = new PresentParameters ();
pp.BackBufferHeight = 1080;
pp.BackBufferWidth = 1920;
Device dvc = new Device (objDirect3D, 0, DeviceType.Hardware, this.Handle, CreateFlags.HardwareVertexProcessing, pp);
VertexBuffer VrtxBuf = new VertexBuffer (dvc, 3 * 3 * 4, Usage.WriteOnly, VertexFormat.None, Pool.Managed);
DataStream VrtxStrm = VrtxBuf.Lock (0, 0, LockFlags.None);
float [] VrtxData = { 50f, 0f, 0f, 0f, 50f, 0f, 0f, 25f, 0f };
VrtxStrm.WriteRange (VrtxData);
VrtxBuf.Unlock ();
dvc.Clear (ClearFlags.All, Color.Black, 1f, 0);
dvc.BeginScene ();
dvc.SetStreamSource (0, VrtxBuf, 0, 12);
dvc.DrawPrimitives (PrimitiveType.TriangleList, 0, 1);
dvc.EndScene ();
dvc.Present ();
VrtxStrm.Dispose ();
VrtxBuf.Dispose ();
dvc.Dispose ();
objDirect3D.Dispose ();
}

Share this post


Link to post
Share on other sites
SuperVGA    1132
Quote:
Original post by Kilickaya
When my program executes the line "Direct3D objD3D = new Direct3D ()", a red rectangle as big as the screen is drawn


-I don't suppose that line alone is guilty of the drawn primitives.

VrtxData and DrawPrimitives() on the other hand, may have something to do with it.

What do you want the code to do, what would you consider "correct" behaviour?

Share this post


Link to post
Share on other sites
Kilickaya    110
Quote:
Original post by SuperVGA
Quote:
Original post by Kilickaya
When my program executes the line "Direct3D objD3D = new Direct3D ()", a red rectangle as big as the screen is drawn


-I don't suppose that line alone is guilty of the drawn primitives.

VrtxData and DrawPrimitives() on the other hand, may have something to do with it.

What do you want the code to do, what would you consider "correct" behaviour?


---------------------------------------
My code should draw only a triangle. Even if I write only the first line which is "Direct3D objD3D = new Direct3D ()" the red rectangle and the two lines are drawn.

Share this post


Link to post
Share on other sites
unbird    8338
First of all: IMHO it is not a good idea to create Direct3D and the device in your form's paint handler. You rather create it at your application start or at least in your form's constructor. Otherwise you create your device every time your form redraws. Similar can be said for the vertex buffer: create it once and only change it when your geometry changes, in the paint handler you just do the rendering. Where did you get that code from ?

Then: I can't quite remember if MDX automatically sets the FVF (device.VertexFormat) if you hook up the stream. Rather set it manually. Anyway: You use a VertexFormat.None which is AFAIR only valid if you setup your own vertex declaration, which you don't either. Guessing from 3 * 3 * 4, your stream stride and your float array, you have a vertex format of position only, so the FVF is VertexFormat.Position. Actually MDX provides some handy structs for some FVF combinations to start with, so in your case you could use CustomVertex.PositionOnly.

Furthermore you don't set any transformation states and other - relevant - render states.

Share this post


Link to post
Share on other sites
Programmer16    2321
The big red rectangle is how the system handles exceptions thrown during the paint event.

Are you using MDX or SlimDX?

As stated, you don't want to create the Direct3D object and Device each paint event. Create them once in your Load event and then use them when needed. Also, make sure to Dispose them in your Closing event.

Now, the bigger thing is: why are you using Direct3D for this? Or more importantly, why are you using Direct3D in this fashion? If you're using it for simple 2D, GDI would probably be more than sufficient (which is pretty much how you're using Direct3D right now anyway.) If you're using it for anything else, you're going to want it running in a tight loop, not only when the window get's repainted (which is rarely.)

Also, for future reference, when posting source code please use the source tags [ source ] and [ /source ] (without spaces of course.)

Share this post


Link to post
Share on other sites
Kilickaya    110
Quote:
Original post by unbird
First of all: IMHO it is not a good idea to create Direct3D and the device in your form's paint handler. You rather create it at your application start or at least in your form's constructor. Otherwise you create your device every time your form redraws. Similar can be said for the vertex buffer: create it once and only change it when your geometry changes, in the paint handler you just do the rendering. Where did you get that code from ?

Then: I can't quite remember if MDX automatically sets the FVF (device.VertexFormat) if you hook up the stream. Rather set it manually. Anyway: You use a VertexFormat.None which is AFAIR only valid if you setup your own vertex declaration, which you don't either. Guessing from 3 * 3 * 4, your stream stride and your float array, you have a vertex format of position only, so the FVF is VertexFormat.Position. Actually MDX provides some handy structs for some FVF combinations to start with, so in your case you could use CustomVertex.PositionOnly.

Furthermore you don't set any transformation states and other - relevant - render states.

------------------------------------------
Would you please write the correct appearance of my code upwards? Namely, how should it be seen in my Form class (when its parts are seperated into the methods of my Form class)?


Share this post


Link to post
Share on other sites
unbird    8338
Oops, my bad, I think it's SlimDX, since he's using DataStream (in MDX it's called GraphicsStream). So forget about my hint for CustomVertex. You could use Vector3 if you stay with position only. But as soon as your vertex's has more properties, go for self-defined structs.

Admittedly, Programmer16 is right: For doing simple 2D in a paint handler, i.e. non-realtime, GDI+ is far more appropriate. And before I spoon-feed a "solution" you rather tell us where you want to go after painting triangles.

Share this post


Link to post
Share on other sites
Kilickaya    110
[quote]Original post by Programmer16

Now, the bigger thing is: why are you using Direct3D for this? Or more importantly, why are you using Direct3D in this fashion? If you're using it for simple 2D, GDI would probably be more than sufficient (which is pretty much how you're using Direct3D right now anyway.) If you're using it for anything else, you're going to want it running in a tight loop, not only when the window get's repainted (which is rarely.)

---------------------------------------
My goal is not 2-D drawing. I'm learning Direct3D and that simple triangle is only a start.

Share this post


Link to post
Share on other sites
Kilickaya    110
Quote:
Original post by Programmer16

Also, for future reference, when posting source code please use the source tags [ source ] and [ /source ] (without spaces of course.)

---------------------------
Where will I write
 and 
?

Share this post


Link to post
Share on other sites
unbird    8338
Ok, I still have some questions:

- Have you localized some errors by stepping through with the debugger (i.e. crashes) ?
- Have you setup the DirectX debug runtimes. This will give you more detailed error messages. In C# you will probably need to use DebugView to grab the messages.

First I'd take a look at your device setup. I doubt it works, or at least I doubt the device is created the way you want. Take a look at the presentation parameters, you only set the backbuffer size.

Share this post


Link to post
Share on other sites
Programmer16    2321

private void Form1_Paint(object sender, PaintEventArgs e)
{
/* 1*/ Direct3D objDirect3D = new Direct3D ();
/* 2*/ PresentParameters pp = new PresentParameters ();
/* 3*/ pp.BackBufferHeight = 1080;
/* 4*/ pp.BackBufferWidth = 1920;
/* 5*/ Device dvc = new Device (objDirect3D, 0, DeviceType.Hardware, this.Handle, CreateFlags.HardwareVertexProcessing, pp);
/* 6*/ VertexBuffer VrtxBuf = new VertexBuffer (dvc, 3 * 3 * 4, Usage.WriteOnly, VertexFormat.None, Pool.Managed);
/* 7*/ DataStream VrtxStrm = VrtxBuf.Lock (0, 0, LockFlags.None);
/* 8*/ float [] VrtxData = { 50f, 0f, 0f, 0f, 50f, 0f, 0f, 25f, 0f };
/* 9*/ VrtxStrm.WriteRange (VrtxData);
/*10*/ VrtxBuf.Unlock ();
/*11*/ dvc.Clear (ClearFlags.All, Color.Black, 1f, 0);
/*12*/ dvc.BeginScene ();
/*13*/ dvc.SetStreamSource (0, VrtxBuf, 0, 12);
/*14*/ dvc.DrawPrimitives (PrimitiveType.TriangleList, 0, 1);
/*15*/ dvc.EndScene ();
/*16*/ dvc.Present ();
/*17*/ VrtxStrm.Dispose ();
/*18*/ VrtxBuf.Dispose ();
/*19*/ dvc.Dispose ();
/*20*/ objDirect3D.Dispose ();
}


I've added numbers to the above to make it easier to reference.

1) Try to use more descriptive names; pp and dvc really don't cut it. It may not seem apparent now, but once you get into a project and have to skim through a 500 line method to find out what pp is, you'll be sorry. More importantly, it makes it easier on those that have to read your code (like us), so we don't have to try to figure what is what. This is just my opinion though and can be ignored.

2) On line 6, VertexFormat.None tells the device that you're not supplying any data to it (what you're looking for is VertexFormat.Position.)

3) On line 8, you're supplying your vertices' positions in counter-clockwise, which is Direct3D's default culling mode (meaning they won't get rendered.)

4) On line 11, you tell the device to clear everything (via ClearFlags.All) when you've only supplied a target (you didn't setup a stencil or a z-buffer when you created the device.)

5) On line 17, you dispose your vertex stream, which is handled automatically (I don't believe this is a problem though.)

6) You don't set any render-states. You need to at least set RenderStates.Lighting to false so that you can see something (with lighting enabled, everything rendered will be black and you have a black background.)

7) As mentioned, you don't set a projection matrix, so your vertices aren't going to get transformed correctly (and thus, more than likely, won't show up on screen.)

What you need to do is read up on Direct3D's fixed-function rendering pipeline (which will help with points 2, 3, 4, and 6), the transformation pipeline (which will help with point 8), and the documents for whichever SDK you're using. Alternatively, since it seems you're using SlimDX, you could look through the samples supplied with the SDK.

As unbird stated, the only way to really figure out what is going wrong (after you've fixed the problems that were already mentioned) is to step through the program and read the debug output.

HTH!

Quote:
Original post by unbird
... Take a look at the presentation parameters, you only set the backbuffer size.
I thought the same thing, but SlimDX sets default values for everything.

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