Dark Bit Factory & Gravity

PROGRAMMING => C / C++ /C# => Topic started by: va!n on November 15, 2008

Title: DX9 Questions
Post by: va!n on November 15, 2008
DX9 - Drawing 2D Text:

Using this framework i have really a problem (even with searching on google and using MSDN), how to Draw 2D Text to the DX screen  ::)

Code: [Select]
void WinMainCRTStartup()
{             
HWND hWnd = ( CreateWindowEx(WS_EX_TOPMOST,"edit",0,0,0,0,0,0,0,0,0,0) );

//DirectX9 Initializations
IDirect3DDevice9* g_pD3DDevice;
IDirect3D9*       g_pD3D = Direct3DCreate9( D3D_SDK_VERSION );

//Create Device
g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
    D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                &d3dpp, &g_pD3DDevice );

ShowCursor(FALSE); // g_pD3DDevice->ShowCursor(FALSE);

do
{

g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(100,100,100), 1.0f, 0 );
g_pD3DDevice->BeginScene();
// Rendering of scene objects happens here



// End the scene
g_pD3DDevice->EndScene();
g_pD3DDevice->Present( NULL, NULL, NULL, NULL );

} while ( !GetAsyncKeyState(VK_ESCAPE) );
 
}


I have tried some things to print 2D Text like "Hello World" to the DX screen. As far as i have read their are different ways to draw text on a DX screen... There is a GDI (slow?) GDI solution and direct DX solutions. I am a bit confused what way is the best (even for 1k/4k) and how to add it to the source.

ID3DXFont      // The interface for creating our font?
ID3DXFont::DrawText
D3DXCreateFont
D3DXCreateFontIndirect

I have looked to some sources on the web but i dont get managed to draw any 2D text to the screen :(
Btw, where can i find a pack full of 1k and 4k DX9 intros with sources to learn from? Does someone here as the zip with some nice releases and sources available by a forum member here in the past? ^^ thx!

Title: Re: [VS2008] DX9 Questions
Post by: va!n on November 16, 2008
Ok, after some hours i finally managed it to draw some text to the screen but i think something is somewhere wrong with the code... it compiles without erros but with warnings and crinkler dont like the OBJ file when the "D3DXCreateFont" command is active :P

Code: [Select]
void WinMainCRTStartup()
{             
HWND hWnd = ( CreateWindowEx(WS_EX_TOPMOST,"edit",0,0,0,0,0,0,0,0,0,0) );

//DirectX9 Initializations
IDirect3DDevice9* g_pD3DDevice;
IDirect3D9*       g_pD3D = Direct3DCreate9( D3D_SDK_VERSION );
    LPD3DXFONT   lpFont;

//Create Device
g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
    D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                &d3dpp, &g_pD3DDevice );


D3DXCreateFont( g_pD3DDevice, // D3D device
96, // Height
0, // Width
FW_DONTCARE, // Weight
0, // MipLevels, 0 = autogen mipmaps
FALSE, // Italic
ANSI_CHARSET, // CharSet
OUT_DEFAULT_PRECIS, // OutputPrecision
ANTIALIASED_QUALITY, // Quality == DEFAULT_QUALITY
DEFAULT_PITCH | FF_DONTCARE, // PitchAndFamily
"Arial", // pFaceName
&lpFont); // ppFont */

ShowCursor(FALSE); // g_pD3DDevice->ShowCursor(FALSE);

RECT rt ={0,0,640,480};

do
{

g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,100), 1.0f, 0 );
g_pD3DDevice->BeginScene();
// Rendering of scene objects happens here

lpFont->DrawText(NULL, "Hello World", -1, &rt, DT_WORDBREAK | DT_NOCLIP | DT_CENTER | DT_VCENTER, 0xFFFFFFFF);

// End the scene
g_pD3DDevice->EndScene();
g_pD3DDevice->Present( NULL, NULL, NULL, NULL );

} while ( !GetAsyncKeyState(VK_ESCAPE) );
 
}

How to change the atributes in D3DXCreateFont() when wanting the same size like in Office "Arial 48" for example? Heigh and Width to 48 seems not to be the same.
Title: Re: [VS2008] DX9 Questions
Post by: Jim on November 16, 2008
From the documentation, so untested:
Code: [Select]
height = -MulDiv(PointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72)(Yes, that's a negative height!) Set width to 0.
On my PC, GetDeviceCaps(hDC, LOGPIXELSY) returns 96, so for a 48 point font, this would give
-(48 * 96)/72=-64

Not sure if you'll ever be able to use crinkler with d3dx.  What's the error message?

Jim
Title: Re: [VS2008] DX9 Questions
Post by: va!n on November 16, 2008
@Jim:
thanks for the fontsize algo...
crinkler gives me following error message:

Code: [Select]
D:\_Coding_CPP\Crinkler>crinkler /SUBSYSTEM:WINDOWS  /COMPMODE:SLOW /CRINKLER  /
ENTRY:WinMainCRTStartup main.obj d3d9.lib kernel32.lib User32.Lib d3dx9.lib
Crinkler 1.1 (Jan 12 2008) (c) 2005-2008 Aske Simon Christensen & Rune Stubbe

Target: out.exe
Subsystem type: WINDOWS
Compression mode: SLOW
Hash size: 100 MB
Hash tries: 20
Order tries: 0
Report: NONE
Transforms: NONE
Replace DLLs: NONE
Range DLLs: NONE

loading:
  main.obj  d3d9.lib  kernel32.lib  User32.Lib
  d3dx9.lib
 : fatal error: LNK   0: could Not find symbol '_D3DXCreateFontA@48'

i am not sure if its my fault or if there is any other and possible more size-optimized and crinkler-friendly way to draw 2D Text on a DX9 screen. Not sure, if there is something missing like  "something->" in the line where i use D3DXCreateFont()
Title: Re: [VS2008] DX9 Questions
Post by: Jim on November 16, 2008
Try going to
Project->Properties->Configuration Properties->General
Check that
Character Set=Use Multi-Byte Character Set
Title: Re: [VS2008] DX9 Questions
Post by: va!n on November 16, 2008
@Jim:
I have tried to change it to unicode... but no way to get it work with crinkler... :(
I am not sure how other ppl are doing 2D text in 1k/4k DX9 prods.
Title: Re: [VS2008] DX9 Questions
Post by: Jim on November 16, 2008
Why would you change it to unicode?   ???
Title: Re: [VS2008] DX9 Questions
Post by: Rbz on November 16, 2008
Seems to me that you have an old d3dx9.lib or a broken one  ???

I've successfully compiled it using crinkler, final file size is 728 bytes, using VC2005.

Title: Re: [VS2008] DX9 Questions
Post by: va!n on November 16, 2008
@Jim:
ah sorry.. thought its the same as unicode... now tried to set multi-byte char...  still compiles as release version without prob... but when trying to use the created OBJ with crinkler i get always the same error as before...   : fatal error: LNK   0: could Not find symbol '_D3DXCreateFontA@48'
Title: Re: [VS2008] DX9 Questions
Post by: va!n on November 16, 2008
@rbz:
thanks for the info... btw its still strange... i have installed the latest DX9 SDK (Nov 2008) and using VS2008... i will try to uninstall the SDK package and install it new... maybe it will fix my prob...
Title: Re: [VS2008] DX9 Questions
Post by: Rbz on November 16, 2008
Btw, make sure you have providing the correct dx9 lib files for crinkler, you can use Libpath argument:

Eg.:
/LIBPATH:"C:\Program Files\Microsoft DirectX SDK (February 2007)\Lib\x86"
Title: Re: [VS2008] DX9 Questions
Post by: Jim on November 16, 2008
Check your paths in VC++
Go to:
Tools->Options->Projects and Solutions->VC++ Directories
Choose 'Library Files' from the top right drop down list.

Make sure the path to your DirectX SDK install folder
(eg. C:\Program Files\Microsoft DirectX SDK (February 2007)\Include)
 is before any that begin with 'VCInstallDir'.

You can rearrange them using the up and down arrows.

Do the same for 'Include Files'.

Jim


Title: Re: [VS2008] DX9 Questions
Post by: va!n on November 17, 2008
Quote
/LIBPATH:"C:\Program Files\Microsoft DirectX SDK (February 2007)\Lib\x86"

@rbz:
thanks for the info, i didnt found out how to get it really work... I added this switch to my *.bat file and deleted all *.lib files i copied by hand into the folder place of my compiled *.obj and crinkler.exe

In the path of SDK are only DX related libs.... but now the system libs like Kernel32 and so on... Is there a way to add two paths for libs? One for the DX libs in the SDK folder and one for the libs comming with VS for win32 system?

Else i just have to copy again all needed libs by hand into an own folder where i use crinkler.
Title: Re: [VS2008] DX9 Questions
Post by: Jim on November 17, 2008
If you fix it in the UI as I explained, you don't need to fiddle with crinkler options at all.  DON'T copy any SDK libs into your own folder, you'll get in a terrible mess and we won't be able to help!
The problem you have is that the Platform SDK that comes with VS2005/08 has an old version of DirectX SDK in it (with a statically linkable d3dx.lib instead of the new export library).  If you use the static version crinkler won't work and your exe will be 100s Kb.  You need to make VS look at the newly installed SDK first by moving the directory up in the list, above the original ones.

Jim
Title: Re: [VS2008] DX9 Questions
Post by: va!n on November 17, 2008
@Jim:
Thanks! Have not saw your previous posting... i will give it a try... (however i have not added crinkler to VS2008. I just copy the created *.obj to a temp folder and then running crinkler by hand (a batch file i created).

@all:
sorry but i still have one more DX9 related question:

I would like to have a fullscreen background gradient like following picture, which i created with following code. So whats the best (tiny) way to add this? Theoretical i think it should be possible by creating/using "vertices and define for each corner a color" or creating/using a big texture? In praxis i have no idea how to do it atm... i am complete new to DX9 with CPP... thanks in advance

Code: [Select]
     For x = 0 To 640-1
         For y = 0 To 480-1
           SetPixel ( x ,y, RGB( x/3.0, 127, y/2.0 ) )
         Next
     Next


Title: Re: [VS2008] DX9 Questions
Post by: hellfire on November 17, 2008
Quote
fullscreen background gradient [...] using vertices and define for each corner a color or creating a big texture?
Both ways yield identical results (although textures are more flexible).
If you're heading for a really small filesize you should integrate it with the rest of the code as good as possible
So if you've got texturing enabled anyways and no need for vertex-colour elsewhere, it's probably best to use a texture (and the other way round).
For the moment I would recommend to fuck the filesize and learn the basics first.
Title: Re: [VS2008] DX9 Questions
Post by: va!n on November 18, 2008
@hellfire:
thanks for your reply... i dont need any textures nor vertices for the rest of the intro... so i will try to use vertices and define a color for each corner... hope google will help me how to use/work with vertices and how to color the corners to get the gradient background fx. (its all like experience coding and trying how things are working)

You are right and i absolutly agree with you, when saying i should learn the basics first! I have a very simple 1k intro idea since a long time and since i am trying to learn CPP/DX9 coding, i am trying to start with very basic stuff (Init/Open Screen, Draw 2D Text, Vertices for GradientBG) and try to fit this into a 1k framework. Btw, do have any good reference (url/book) for easy learning und understanding how to code DX9/CPP stuff like for Intro/DemoScene? Thx




Title: Re: [VS2008] DX9 Questions
Post by: Jim on November 18, 2008
Quote
Thanks! Have not saw your previous posting... i will give it a try... (however i have not added crinkler to VS2008. I just copy the created *.obj to a temp folder and then running crinkler by hand (a batch file i created).
The easiest way to use crinkler with VS is to get the crinkler exe, rename it to link.exe and put it in the folder along with your .vcproj file.
Then go in to
Project->Properties->Configuration Properties->Linker->Command Line
and add
/CRINKLER
to the Additional Options box.

You might also need to set
C/C++->Optimization->Whole Program Optimization to 'No'
Linker->Optimization->Link Time Code Generation to 'Default'
otherwise crinkler will barf.
That way, no need for any batch files or anything, crinkler gets run as part of the ordinary build process.  If you remove the /CRINKLER option, the crinkler exe will just call the original Microsoft linker and you get a normal uncrinkled exe.


For drawing gouraud rectangles with the minimum of fuss, I suggest using the DrawPrimitiveUP() function.
You can then do something like
Code: [Select]
MYVERTEX vertices[4];
d3d_device->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, vertices, sizeof(MYVERTEX))
That will draw a quad (two triangles in a fan) from the 4 vertices.
You might define MYVERTEX like this
Code: [Select]
typedef struct
{
  float x,y,z,w;
  D3DCOLOR rgb;
} MYVERTEX;
Then you need to tell Direct3D what your vertex looks like - you've got a post-transformed coordinate (you want to use 2d screen coordinates, and not have d3d do any rotations, right?) and a diffuse colour, so, once, before you draw anything
Code: [Select]
d3d_device->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
You probably want to set the z to be in front of your clipping plane, and w to be 1/z.
Your 4 rgb values will be the 4 colours at the corners of your rectangle.  There are some D3D macros you might want to use.

Jim
Title: Re: DX9 Questions
Post by: va!n on November 19, 2008
sorry guys...
now i have installed VS2005 (without uninstalling the VS2008 and DX9 SDK November version)...
When i try to compile rbz 1k DX9 framework i get following error, that the include file can not be found...

Quote
Befehlszeilen     Die temporäre Datei "d:\_Coding_CPP\1k_DX9_TUM\Debug\RSP00000334282508.rsp" wird erstellt. Inhalt:
[
/Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_VC80_UPGRADE=0x0600" /D "_MBCS" /Gm /EHsc /RTC1 /MTd /Fp".\Debug/Tiny_DX9.pch" /Fo".\Debug/" /Fd".\Debug/" /W3 /c /ZI /TP .\Main.cpp
]Erstellen der Befehlszeile "cl.exe @d:\_Coding_CPP\1k_DX9_TUM\Debug\RSP00000334282508.rsp /nologo /errorReport:prompt" Ausgabefenster     Kompilieren...
Main.cpp
d:\_coding_cpp\1k_dx9_TUM\main.cpp(13) : fatal error C1083: Datei (Include) kann nicht geöffnet werden: "windows.h": No such file or directory
 Ergebnisse     Das Buildprotokoll wurde unter "file://d:\_Coding_CPP\1k_DX9_TUM\Debug\BuildLog.htm" gespeichert.
Tiny_DX9 - 1 Fehler, 0 Warnung(en)

So, why does the compiler dont find his own (header) file(s)? Is it just a settings problem in VS2005 or the project file?
Title: Re: DX9 Questions
Post by: hellfire on November 19, 2008
Have a look at this (http://msdn.microsoft.com/en-us/library/ms235626(VS.80).aspx).
Title: Re: DX9 Questions
Post by: Jim on November 19, 2008
Or this http://dbfinteractive.com/forum/index.php?topic=1243.0 (http://dbfinteractive.com/forum/index.php?topic=1243.0)
VS2005 Express doesn't come with the Platform SDK (windows.h etc) and it doesn't come configured to build Win32 applications by default.
You should be able to point it at the VS2008 Express Platform SDK folder using the instructions in our primer though.

VS2005 is practically identical to VS2008, I don't know why you would need both.  2008 is mostly for .NET3.0, which you're not using.

Jim

Title: Re: DX9 Questions
Post by: va!n on November 19, 2008
@hellfire:
thanks. i will take a look

@Jim:
thanks. i will give it a try...
Quote
VS2005 is practically identical to VS2008, I don't know why you would need both.  2008 is mostly for .NET3.0, which you're not using.

Normaly i would use and work with the latest official version (VS2008) because i thought it creates better optimized output. But as i heard, compiling rbz 1k DX9 framework with VS2005 results in a smaller exesize as my results with VS2008 !? Thats the reason i will try it out with VS2005. I now d/l and installed "WIndows SDK 6.1 (Windows Server 2008), because d/l for Windows 2003 seems no longer to exist at MS. Hope i will get VS2005 work...

Title: Re: DX9 Questions
Post by: Jim on November 20, 2008
I think you are barking up the wrong tree.  You are chasing a few tiny bytes saving long before you have any idea what you are doing. ::)
Get 2008 working.  It's a gifthorse.

Jim

Title: Re: DX9 Questions
Post by: Jim on November 20, 2008
I should add...the rules of optimisation
rule 1: don't optimise
rule 2: don't optimise yet

Title: Re: DX9 Questions
Post by: va!n on November 21, 2008
@Jim:
Yes, you may be right. I am trying to add step by step some very simple/basic DX9 stuff to the 1k intro idea i have... but when trying to add new stuff / code i try to get it work without a really look to its exe size! When i get it work/implented i look if there is something i can recode or optimize a little bit.

On the other hand atm i have tried to get work VC2005 and VC2008 and i have to say, compiling the 1k DX9 Framework with VS2006 seems to saves some bytes (even when crinkled) as far as i see... i will do some tests again... I think before trying to add much stuff as possible into a 1k, i will try to get the empty framework tiny as possible... i am not really sure but i think i found a way to get rbz framework some bytes smaller as his original version. (i have to check and test it again).

Btw, next days i will go to a bookstore and check out what good DX9 related books for beginners are available... but due fact of some money issues i will check the web for good urls/turorials too and then i will try learning some very smple DX9 basics step by step...

Actually i try to check out and understand how to use/work with vertices (for creating gradient background)... but as i read on pouet, a square is not needed (hint by mentor)... atm i have no idea what i means... another thing i will try to get work, how to generate a texture (big array) and using this data (texture pixel data) for the background texture...

Its all just playing a bit around with CPP and DX9... just experience... My goal is still to release hopefully the 1k intro at TUM 2008... (this would be my own little xmas present too ;)
Title: Re: DX9 Questions
Post by: va!n on November 23, 2008
does someone has experience how to generate a texture and draw this in realtime to the texture? This is what i comed up yet but i dont really know how to put my pixeldatas direct on the texture. I thought about writing pixel datas to an array (size 512*512) and copy this data to the texture. But as i heared, the best way doing this, is LOCK the texture, drawing stuff in realtime and UNLOCK texture...

Code: [Select]
g_pD3DDevice->CreateTexture( 512, 512, 0, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &g_pTexture, NULL );

D3DLOCKED_RECT lr;
g_pTexture->LockRect(0, &lr, 0, 0);

  // Here code To draw on texture

g_pTexture->UnlockRect(0);


This is my untested code for creating the texture:

Code: [Select]

for (int x=0; x<512; x++)
{
for (int y=0; y<512; y++)
{
TheLockedTextureObject = D3DCOLOR_XRGB( x/3.0, 127, y/2.0) ;
}
}

Title: Re: DX9 Questions
Post by: va!n on November 23, 2008
i have tried following (found on web a similar source and tried to modify/fit for own use by trying to do a XOR texture for testing)... but i cant see the texture...

Code: [Select]
D3DLOCKED_RECT lr;
g_pTexture->LockRect(0, &lr, 0, 0);
D3DCOLOR* pixel = (D3DCOLOR*)lr.pBits;
int texcount = 256*256;

While (texcount--)
{
      For(int x = 0; x < 256; x++)
    {
      For(int y = 0; y < 256; y++)
      {   
          int c = x ^ y;
          *pixel++ == D3DCOLOR_XRGB(c,c,c);
      }
    }

}
g_pTexture->UnlockRect(0);

The mainloop (cutted down):

Code: [Select]
do
{
    g_pD3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,128,255), 1.0f, 0 );
  g_pD3DDevice->BeginScene();
 
  g_pD3DDevice->SetTexture( 0, g_pTexture );        // cant see it :(

    g_pD3DDevice->EndScene();
    g_pD3DDevice->Present( NULL, NULL, NULL, NULL );

} While ( !GetAsyncKeyState(VK_ESCAPE) );
Title: Re: DX9 Questions
Post by: hellfire on November 23, 2008
Code: [Select]
g_pD3DDevice->BeginScene();
g_pD3DDevice->SetTexture( 0, g_pTexture );        // cant see it :(
g_pD3DDevice->EndScene();
Well, you need to draw something...

Code: [Select]
D3DCOLOR* pixel = (D3DCOLOR*)lr.pBits;
...
*pixel++ == D3DCOLOR_XRGB(c,c,c);
This depends on your texture-format; here you're assuming RGBA 8:8:8:8 which will probably the case most of the time.
You need to look at lr.Pitch (http://msdn.microsoft.com/en-us/library/bb172570(VS.85).aspx).

Title: Re: DX9 Questions
Post by: va!n on November 23, 2008
@hellfire:
thanks. not really sure about the pitch atm... everytime when i try to change something on following line or just compile/run this line:

Code: [Select]
          *pixel++ == D3DCOLOR_XRGB(c,c,c);

my XP system complete hangs and i have to reboot... really frustrated...
Title: Re: DX9 Questions
Post by: Jim on November 23, 2008
Quote
Code: [Select]
      *pixel++ == D3DCOLOR_XRGB(c,c,c);
Just leave that bit for the time being.  The first version you posted is probably OK.
<edit>Are you really using == instead of =  ?  They're very different things in C</edit>

You need to work on the drawing.
If you read the documentation :P for SetTexture() you will see that all it does is tell D3D where it can get the texture from.  It doesn't draw anything.  You need to do two more things.
1. Enable Texturing on the pipeline - IIRC that's SetRenderState()
2. Draw a quad that has some UVs on it.  You can do that with the DrawPrimitiveUP, but you will need to change the vertex and FVF to include UV coordinates.

Jim
Title: Re: DX9 Questions
Post by: Rbz on November 23, 2008
Why not try OpenGL ? 
Directx is a bit smaller if you work with pixel shaders, since it's not your case, IMO you can do a lot better with OpenGL.

Btw, did you have tried iq framework?  It comes with examples and might help you.
http://rgba.scenesp.org/iq/demoscene/codes/isystem1k4k/isystem1k4k.htm



Title: Re: DX9 Questions
Post by: va!n on November 23, 2008
@rbz:
i have tried OGL using PB in the past... but since using DX for small sizes like 1k/4k results in smaller exe size and DX features some more latest technology functions, i will try to learn and use how to work with DX9...

Btw, i have looked at IQs 1k framework... but he dont draw direct on a texture using DX... he uses shader for the gradient background and this is, what i dont want... first i want learn the basics like how to create my wanted gradient background at runtime (in realtime) on a texture and how to display this...
Title: Re: DX9 Questions
Post by: va!n on November 26, 2008
Does someone know a good DX9 book for beginners or url, where it is easy to understand how to use/work with DX9 while showing the basics of DX9 like:

- How to create a gradient background (quad by using vertices) and coloring each corner   (no 3D yet, just pure 2D)
- How to generate and display textures  (no 3D yet, just pure 2D)

I am just asking, because i still have problems with creating the gradient background with vertices (seems i did something wrong or forgot). thx
Title: Re: DX9 Questions
Post by: Jim on November 26, 2008
Why not post the code for your best attempt?
There might be some sample code out there that can help, but you're going to face the problem that you're trying to crack C++, DirectX, Windows and 1Kb exes all at the same time.  Noone, except the folks here, are going to be able to help you with that.  ;D

Jim
Title: Re: DX9 Questions
Post by: hellfire on November 26, 2008
I assume you've already had a look into iq's direct3d-framework,
so you're able to create a window and initialize direct3d,
essentially ending up in a pointer to the direct3d-device-interface:
Code: [Select]
IDirect3DDevice9 *dev;
Now you want to draw "something" - let's say it's a cube.
First of all you have to define vertices. Each vertex will consist of a position and a colour:
Code: [Select]
struct Vertex
{
   float x,y,z;
   unsigned int col;
};

Now define a cube.
Usually one would need 8 vertices, but since all further attributes (colours, normals, texture-coordinates, etc) are stored along with the vertex-position, we'll define 4 individual vertices per side:
Code: [Select]
Vertex cubevdata[] = {
 {-1.0f, -1.0f, -1.0f, 0xff0000}, // top
 {-1.0f, -1.0f,  1.0f, 0xff0000},
 { 1.0f, -1.0f,  1.0f, 0x00ff00},
 { 1.0f, -1.0f, -1.0f, 0x00ff00},

 { 1.0f,  1.0f, -1.0f, 0x0000ff}, // bottom
 { 1.0f,  1.0f,  1.0f, 0x0000ff},
 {-1.0f,  1.0f,  1.0f, 0xff0000},
 {-1.0f,  1.0f, -1.0f, 0xff0000},

 {-1.0f,  1.0f, -1.0f, 0x00ff00}, // left
 {-1.0f,  1.0f,  1.0f, 0x00ff00},
 {-1.0f, -1.0f,  1.0f, 0x0000ff},
 {-1.0f, -1.0f, -1.0f, 0x0000ff},

 { 1.0f, -1.0f, -1.0f, 0xffff00}, // right
 { 1.0f, -1.0f,  1.0f, 0xffff00},
 { 1.0f,  1.0f,  1.0f, 0x00ffff},
 { 1.0f,  1.0f, -1.0f, 0x00ffff},

 { 1.0f, -1.0f, -1.0f, 0x00ff00}, // front
 { 1.0f,  1.0f, -1.0f, 0x00ff00},
 {-1.0f,  1.0f, -1.0f, 0xff00ff},
 {-1.0f, -1.0f, -1.0f, 0xff00ff},

 {-1.0f, -1.0f,  1.0f, 0xff00ff}, // back
 {-1.0f,  1.0f,  1.0f, 0xff00ff},
 { 1.0f,  1.0f,  1.0f, 0xffff00},
 { 1.0f, -1.0f,  1.0f, 0xffff00},
};
(the same as in iq's code just with an additional colour).

Now Direct3D doesn't handle quads, so you have to define two triangles for each side.
Each triangle is made up from three indices into the vertexbuffer (above):
Code: [Select]
int cubeidata[] = {
    0, 1, 2,
    0, 2, 3,
    4, 5, 6,
    4, 6, 7,
    8, 9,10,
    8,10,11,
   12,13,14,
   12,14,15,
   16,17,18,
   16,18,19,
   20,21,22,
   20,22,23 };
(again shamelessly copied from iq's source)

Now you have to copy that stuff to the video-card.
Create a vertexbuffer with 6(sides) * 4(vertices per side) * sizeof(Vertex) bytes, lock the buffer, copy your stuff into it and unlock again:
Code: [Select]
   IDirect3DVertexBuffer9 *vBuffer;
   res= dev->CreateVertexBuffer( 6*4*sizeof(Vertex), D3DUSAGE_WRITEONLY, 0, D3DPOOL_DEFAULT, &vBuffer, NULL );
   Vertex *vtx;
   vBuffer->Lock(0,0,(void**)&vtx,0);
   memcpy(vtx, cubevdata, 6*4*sizeof(Vertex) );
   vBuffer->Unlock();

Same for the indices:
Code: [Select]
   IDirect3DIndexBuffer9 *iBuffer;
   res= dev->CreateIndexBuffer( 12*3*sizeof(int), D3DUSAGE_WRITEONLY, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &iBuffer, NULL );
   unsigned int *idx;
   iBuffer->Lock(0,0,(void**)&idx,0);
   memcpy(idx, cubeidata, 12*3*sizeof(int) );
   iBuffer->Unlock();
iq used a d3dxmesh here which handles most of it for you thus saving code-size.

Before you can draw anything, you need to set some matrices.
1. Projection:
Code: [Select]
    D3DXMATRIX proj;
    D3DXMatrixPerspectiveFovRH(&proj, 1, 4.0/3.0, 1.0, 100.0);
    dev->SetTransform( D3DTS_PROJECTION, (const D3DMATRIX*)&proj);
(see D3DXMatrixPerspectiveFovRH (http://msdn.microsoft.com/en-us/library/bb205351(VS.85).aspx) for details)

2. Camera:
Code: [Select]
   D3DXMATRIX cam;
   D3DXVECTOR3 pos(0,0,-10);
   D3DXVECTOR3 target(0,0,0);
   D3DXVECTOR3 up(0,1,0);
   D3DXMatrixLookAtRH(&cam, &pos, &target, &up);
   dev->SetTransform( D3DTS_VIEW, (const D3DMATRIX*)&cam );
(see D3DXMatrixLookAtRH (http://msdn.microsoft.com/en-us/library/bb205343(VS.85).aspx) for details)

3. Model:
Code: [Select]
    D3DXMATRIX rot;
    D3DXMatrixRotationYawPitchRoll( &rot, time, time, time );
    dev->SetTransform( D3DTS_WORLD, (const D3DMATRIX*)&rot );
(D3DXMatrixRotationYawPitchRoll (http://msdn.microsoft.com/en-us/library/bb205361.aspx) rotates the object around three axes; "time" is some slowly increasing float)

Don't forget to disable lighting:
Code: [Select]
    dev->SetRenderState( D3DRS_LIGHTING, false );
Now draw the object:
Code: [Select]
    dev->SetIndices( iBuffer ); // set index-buffer
    dev->SetStreamSource( 0, vBuffer, 0, sizeof(Vertex) ); // set vertex-buffer
    dev->SetFVF(D3DFVF_XYZ | D3DFVF_DIFFUSE); // set vertex-format (xyz and colour)
    dev->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, 6*4, 0, 6*2 ); // draw 12 triangles (indexing a maximum of 24 vertices)

Now try to extend the vertex-structure (and fvf (http://msdn.microsoft.com/en-us/library/bb174433(VS.85).aspx)) to hold texture-coordinates and enable your map.
Title: Re: DX9 Questions
Post by: AnimalMother on November 26, 2008
I was looking for some DirectX tutorials too and found some interesting sites:

http://www.toymaker.info/
http://www.mvps.org/directx/
http://www.32bits.co.uk/

Really good resources there  ;)
Title: Re: DX9 Questions
Post by: Rbz on November 28, 2008
About Crinkler, you can read Mentor's explanation (http://www.dbfinteractive.com/forum/index.php/topic,3149.msg43551.html#msg43551) about Crinkler command line options that might save you some bytes for your production.

Title: Re: DX9 Questions
Post by: va!n on November 28, 2008
@hellfire:
Thanks, i will study the source (wihtout PS) and your comments very soon... atm i am trying to add the vertex example you can found in the DX SDK, but i miss something... i have even problems when trying to use rand() for randomize values... everytime compiler errors... possible i have to change my source / codingstyle...

@AnimalMother :
Thanks for the links.

@rbz:
thanks for the link to mentors posting... havent notived it before...  (cool to have this great guy here in our community  8) )


Title: Re: DX9 Questions
Post by: Jim on November 28, 2008
If you're using crinkler, you almost certainly don't have the C runtime library hanging around so you won't be able to use rand().

Jim

Title: Re: DX9 Questions
Post by: va!n on November 28, 2008
@jim:
thanks for reply...
while developing i use the original VC2005 without manually inbuild crinkler... so the VC2005 installation is unchanged and crinkler will not be touched... btw, i had something simular error some days ago and it was my fault, because i create an array inside "void WinMainCRTStartup()" which causes a compiler error too and costs a lot time to find and fix this. After i put this line outsite the "void WinMainCRTStartup()", the source compiled fine and exe worked fine...  so i think its somewhere my fault in the dirty source :P  (dirty, because i am trying/experience some things)
Title: Re: DX9 Questions
Post by: AnimalMother on December 04, 2008
I bought this used book a week ago -> http://www.amazon.de/gp/product/3827269555

If you wish I can give you the examples provided on cd. The sources are commented in german but I think this should be no problem for you  ;)
Title: Re: DX9 Questions
Post by: va!n on December 05, 2008
@AnimalMother:
Thanks, check ya PM ;)