Managed DX - Porting from DX8 (C++ 2 C#)

Started by
6 comments, last by leehairy 18 years, 6 months ago
I am porting an "old" DirectX application and am having trouble trying to some direct replacements in managed DX. The first one is dealing with assigning color to a light!

/* old code */
D3DLIGHT       myLight
D3DCOLORVALUE  Diffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
myLight.Diffuse = Diffuse;
/* new code */
Light myLight
myLight.Diffuse  = System.Drawing.Color.fromRGBA(1.0f, 1.0f, 1.0f, 1.0f);
myLight.Ambient  = System.Drawing.Color.fromRGBA(1.0f, 1.0f, 1.0f, 1.0f);
myLight.Specular = System.Drawing.Color.fromRGBA(1.0f, 1.0f, 1.0f, 1.0f); 

// arghhhh.... System.Drawing.Color.fromRGBA  takes ints not floats!
All the examples i have seen for setting a MDX color use the System.Drawing.Color, the closest of which i can use is Color.FromRGBA(). Unfortunately this does not take values as floats. The second one, and by far the most confusing to me, is using SetTextureStage(). In MDX the 3rd argument is a float value? What on earth do i use? I just cannot seem to figure what these constants would be!

/* old code */
myDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
myDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
myDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
myDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
myDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1 );
/* new code */      

device.SetTextureStageState(0, TextureStageStates.ColorArgument1, TextureArgument.TextureColor);
device.SetTextureStageState(0, TextureStageStates.ColorOperation, TextureOperation.Modulate);
device.SetTextureStageState(0, TextureStageStates.ColorArgument2, TextureArgument.Diffuse);
device.SetTextureStageState(0, TextureStageStates.AlphaArgument1, TextureOperation.TextureColor);
device.SetTextureStageState(0, TextureStageStates.AlphaOperation, TextureOperation.SelectArg1);

// arghhhh.... SetTextureStageState  takes a float value not these MDX constants!
Any help whatsoever is most appreciated..
Advertisement
Well, I can give a suggestion for the first one, anyway :)

Quote:
/* new code */
Light myLight
myLight.Diffuse = System.Drawing.Color.fromRGBA(1.0f, 1.0f, 1.0f, 1.0f);
myLight.Ambient = System.Drawing.Color.fromRGBA(1.0f, 1.0f, 1.0f, 1.0f);
myLight.Specular = System.Drawing.Color.fromRGBA(1.0f, 1.0f, 1.0f, 1.0f);


This is just an issue of scaling your numbers. In the float scale, 0.0 is no intensity and 1.0 is full. On the integer scale, 0 is no intensity and 255 is full intensity. Soooo...

float float_color_element
int int_color_element

int_color_element = (int) (255 * float_color_element);

In your example:
/* new code */Light myLightmyLight.Diffuse  = System.Drawing.Color.fromRGBA((int) (255 * 1.0f), (int) (255 * 1.0f), (int) (255 * 1.0f), (int) (255 * 1.0f)); ...


Simply just convert each of your color elements into integers at the point where you set your color settings. Hope that helps!
"Game Programming" in an of itself does not exist. We learn to program and then use that knowledge to make games.
Instruo -
Thanks for the reply. Your right, in fact this is what i have already done for Colors. It just seems a messy solution.

Thanks anyways..

While i am on the subject of porting issues. This code sample is also making my head scratch.. i cannot see where it must be wrong..;(

/* old code */D3DXMATRIX     yawMatrix;D3DXQUATERNION yawQuaternian;D3DXMatrixRotationY(&yawMatrix, angle);D3DXQuaternionRotationMatrix(&yawQuaternian, &yawMatrix);      /* replacement new code */Matrix          yawMatrix;Quaternion      yawQuaternian; yawMatrix.RotateY(angle);        yawQuaternian.RotateMatrix(yawMatrix);


During compilation i get a "warning CS0649"
'yawQuaternian' is never assigned to, and will always have its default value

What am i doing wrong here?... i am obviosuly missing something fundemental!

Didnt the line
yawQuaternian.RotateMatrix(yawMatrix);
just set it??
Hi there leehairy,
How are you doing?

The Problem
Porting c++ to C# with focus on DirectX.

The Solution
SetTextureStageState
For the equivalent of SetTextureStageState you can use Device.TextureState[0]
i.e.
Device.TextureState[0].ColorOperation = TextureOperation.Modulate;

I hope this helps.

Okay, so I'm thinking of porting some of my games wrote in c++ to c#, i know both langages and can convert between the two, but is there any advantage of porting from my original c++ to c#? Thanks in advance.
~Mark Schadler
Armadon,

Your a ** STAR **

Thanks a million!!!!!!!!!!
Quote:Original post by Stiffler
Okay, so I'm thinking of porting some of my games wrote in c++ to c#, i know both langages and can convert between the two, but is there any advantage of porting from my original c++ to c#? Thanks in advance.


Stiffler, I dont really see any advantage at all. I am just doing it for a learning excercise and a bit of fun.

INMO at this stage, MDX and .NET are not the ideal combination -- (at the moment)

1) Documentation on MSDN is pretty limited { for Msoft .. its actually v.bad }
2) References/Tutorials/Examples on internet limited
3) Even for a small game - user has to install .NET, which in my case is 10x bigger than my application.
4) No major performance advantage **or disadvantage.
5) Subject to change.
6) Some of the API names are odd to say least.. eg to load a cubemap, there is no cubemap.fromfile() as there is with other classes.. I had to use TextureLoader.CubeMapFromFile().. took me a while to find this one!!

** NOTE not interested in debate about speed etc..lets just say for the purposes of this article they are equal.

After saying all these things and using C# for a few days now. I actually have to recommend it.
1) Points 1-6 above - will all be addressed in the near future
2) Its so easy to figure out
3) Almost code-&-forget.. never too happy about "new"ing everything, but the amount of time/effort you seem to save over not worrying about "free"ing is great.







This topic is closed to new replies.

Advertisement