GLS_1985

Hello

Please I am new in directx and I have a big problem from my first time ,when trying to draw a simple triangle on a form using directx...

I wrote this down in C++ Clr (using directx manage),which i took it from a book, it is simple, and I fully understand each part of it :-

PresentParameters^ p=gcnew PresentParameters();
p->Windowed::set(true);
p->SwapEffect::set(SwapEffect::Discard);
Device ^device=gcnew Device(0,DeviceType::Hardware,this,CreateFlags::SoftwareVertexProcessing,p);



array<CustomVertex::TransformedColored^,1> ^verts=gcnew array<CustomVertex::TransformedColored^,1>(3);
verts[0]=gcnew CustomVertex::TransformedColored(Vector4(this->Width/2.0f,50.0f,0.5f,1.0f),1);
verts[1]=gcnew CustomVertex::TransformedColored(Vector4(this->Width-this->Width/5.0f,this->Height-this->Height/5.0f,0.5f,1.0f),1);
verts[2]=gcnew CustomVertex::TransformedColored(Vector4(this->Width/5.0f,this->Height-this->Height/5.0f,0.5f,1.0f),1);

verts[0]->Color=Color::Aqua::get().ToArgb();
verts[1]->Color=Color::Black::get().ToArgb();
verts[2]->Color=Color::Purple::get().ToArgb();

device->BeginScene();
device->SetStreamSource(0, verts, 0);
device->VertexFormat=CustomVertex::TransformedColored::Format;
device->DrawUserPrimitives(PrimitiveType::TriangleList,0,verts);
device->EndScene();
device->Present();

But when i run it it alwyes give me the same Exception for device->DrawUserPrimitives(PrimitiveType::TriangleList,0,verts); line:-

//
An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.DirectX.dll

Additional information: Operation is not valid due to the current state of the object.

//

So please any information that could help me ,

I have Visual Stiudio 2005 team Edition
directx SDK Dec 2006
Microsoft Manage DirectX ver:- 1.0.2902.0

***Ah Please Could any Body tell me how to get Microsoft Manage DirectX ver:- 2.0, because Vs 2005 have a problem with my current ver...


Thank you very mush



Re: Game Technologies: Graphics Please Help

Bad Habit

Firstly you want see if the device is OK. Comment out all the stuff in between BeginScene() and EndScene(), so youre left with begin, end and present. If that works then uncomment VertexFormat and PrawUserPrimitive() and give that a try. Then uncomment SetStreamSource(), which is the most likely culprit since its for vertex buffers.

If you want to go down the managed DirectX road then switch to C#, its much nicer to use plus you have all the XNA stuff to play with. Otherwise use unmanaged DX, you can actually mix unmanaged C++ with managed C++.





Re: Game Technologies: Graphics Please Help

GLS_1985

Thank you for your replay

But device->SetStreamSource line was just a wrong in copy code I delete it :) ,
any way i try device->BeginScene(); device->EndScene(); alone and run ,it work without any problem...

Then I try to uncomment VertexFormat and PrawUserPrimitive() and debug then run it give me the same problem without any change .

could the problem be from my video card or something like that...

my video card is
Intel Graphics Media Accelerator (GMA) 900


Please my university project depend on that

Thank you very mush




Re: Game Technologies: Graphics Please Help

Bad Habit

A few things to try:

Set the presentation parameters like:

presentParams->Windowed = true;
presentParams->SwapEffect = SwapEffect::Discard;


be careful about using the scope resolution operator :: its not an object member access operator like . and ->. Read up on your C++.

You can also try declaring the vertex array as, and set up like:

array<CustomVertex::TransformedColored, 1>^ vertices;

vertices[0].Position = Vector4(width / 2.0f, 50.0f, 0.5f, 1.0f);
vertices[0].Color = System::Drawing::Color::Aqua.ToArgb();

Lastly:

device->DrawUserPrimitives(PrimitiveType::TriangleList,0,verts);

should have 1 as its second parameter to indicate you want to draw one primitive or triangle.


You should also consider getting a card if you want to go down this road, you can pick up something like an fx5200 for quite cheap which has the ability to do shader 2 stuff. Shop around for the best deal.





Re: Game Technologies: Graphics Please Help

GLS_1985

Hello

Thank you for continue supporting me...

For sorry I try every thing you mention it to me, but no positive result appear; the same problem at the same line...

But now I have something new that could help in solving this problem :-

When the Exception message appear I click break and go to the Auto box, Choose there the device object and extract it tree to see it member, there i found that an object called DepthStencilSurface with type Microsoft::DirectX::Direct3D::Surface^ have an exception, it message :-
<error: an exception of type: Microsoft::DirectX::Direct3D::NotFoundException^ occurred>

Could this be the reason for the problem If yes please could you give me some instruction that could help me using DepthStencilSurface object to pass the Exception...

Thank you




Re: Game Technologies: Graphics Please Help

Bad Habit

That could be happening if you're clearing the depth buffer but you haven't created one when setting the presentation parameters.

So you can set a depth buffer like:

presentParams->Windowed = true;
presentParams->SwapEffect = SwapEffect::Discard;
presentParams->EnableAutoDepthStencil = true;
presentParams->AutoDepthStencilFormat = DepthFormat::D16;

...

device->Clear(ClearFlags::Target | ClearFlags::ZBuffer, System::Drawing::Color::Blue, 1.0f, 0);

or don't set the depth buffer and clear like:

device->Clear(ClearFlags::Target, System::Drawing::Color::Blue, 1.0f, 0);

Here's an example of the code I played with, which worked for me but you will need the platform SDK installed to use:

// Graphics3D.h

#include "Windows.h"


using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace Microsoft::DirectX;
using namespace Microsoft::DirectX::Direct3D;
using namespace Microsoft::DirectX::Diagnostics;

public ref class Graphics3D
{
public:
Graphics3D()
{
}

~Graphics3D()
{
if(device)
delete device;
}

bool Init(System::Windows::Forms::Form^ frm)
{
try
{
PresentParameters^ presentParams = gcnew PresentParameters();
presentParams->Windowed = true;
presentParams->SwapEffect = SwapEffect::Discard;
presentParams->EnableAutoDepthStencil = true;
presentParams->AutoDepthStencilFormat = DepthFormat::D16;
device = gcnew Device(0, DeviceType::Hardware, frm,
CreateFlags::SoftwareVertexProcessing,
presentParams);

// Create vertices
float width = (float)frm->Width;
float height = (float)frm->Height;
vertices = gcnew array<CustomVertex::TransformedColored, 1>(3);
vertices[0].Position = Vector4(width / 2.0f, 50.0f, 0.5f, 1.0f);
vertices[0].Color = System::Drawing::Color::Aqua.ToArgb();
vertices[1].Position = Vector4(width - width / 5.0f, height - height / 5.0f, 0.5f, 1.0f);
vertices[1].Color = System::Drawing::Color::Black.ToArgb();
vertices[2].Position = Vector4(width / 5.0f, height - height / 5.0f, 0.5f, 1.0f);
vertices[2].Color = System::Drawing::Color::Purple.ToArgb();

return true;
}
catch(DirectXException^)
{
return false;
}
}

bool Render()
{
if(!device)
return false;

//Clear the backbuffer to a blue color
device->Clear(ClearFlags::Target | ClearFlags::ZBuffer, System::Drawing::Color::Blue, 1.0f, 0);
//Begin the scene
device->BeginScene();

// Rendering of scene objects can happen here
device->VertexFormat = CustomVertex::TransformedColored::Format;
device->DrawUserPrimitives(PrimitiveType::TriangleList, 1, vertices);

//End the scene
device->EndScene();
device->Present();

return true;
}

void OnApplicationIdle(System::Object^ sender, System::EventArgs^ e)
{
MSG m;
while(!PeekMessage(&m, NULL, 0, 0, 0))
{
if(!Render())
return;
}
}

private:
Microsoft::DirectX::Direct3D::Device^ device;
array<CustomVertex::TransformedColored, 1>^ vertices;
};

// TestApp.cpp

#include "stdafx.h"
#include "Form1.h"
#include "Graphics3D.h"

using namespace TestDrawUserPrimitives;


[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);

Form1^ form = gcnew Form1();
Graphics3D^ graphics = gcnew Graphics3D();

form->Show();
if(!graphics->Init(form))
{
MessageBox::Show("Error creating device");
return 1;
}

Application::Idle += gcnew System::EventHandler(graphics, &Graphics3D::OnApplicationIdle);

// Create the main window and run it
Application::Run(form);


return 0;
}






Re: Game Technologies: Graphics Please Help

GLS_1985

Hello

Thank you very mush it work , i update my code using yours and it draw that triangle...

I think the problem was in :-
presentParams->EnableAutoDepthStencil = true;
presentParams->AutoDepthStencilFormat = DepthFormat::D16;
device->Clear(ClearFlags::Target | ClearFlags::ZBuffer, System::Drawing::Color::Blue, 1.0f, 0);

Now i could continue reading directX book...

Thank you very mush for helping me
You are really a good programmer