• Welcome to Dark Bit Factory & Gravity.
 

DX9 Questions

Started by va!n, November 15, 2008

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

Jim

Or this 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

Challenge Trophies Won:

va!n

@hellfire:
thanks. i will take a look

@Jim:
thanks. i will give it a try...
QuoteVS2005 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...

- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

Jim

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

Challenge Trophies Won:

Jim

I should add...the rules of optimisation
rule 1: don't optimise
rule 2: don't optimise yet

Challenge Trophies Won:

va!n

@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 ;)
- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

va!n

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...


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:



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


- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

va!n

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...


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):


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) );

- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

hellfire

g_pD3DDevice->BeginScene();
g_pD3DDevice->SetTexture( 0, g_pTexture );        // cant see it :(
g_pD3DDevice->EndScene();

Well, you need to draw something...

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.

Challenge Trophies Won:

va!n

@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:


          *pixel++ == D3DCOLOR_XRGB(c,c,c);


my XP system complete hangs and i have to reboot... really frustrated...
- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

Jim

Quote      *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
Challenge Trophies Won:

Rbz

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



Challenge Trophies Won:

va!n

@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...
- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

va!n

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
- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

Jim

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
Challenge Trophies Won:

hellfire

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:
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:
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:
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):
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:
   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:
   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:
    D3DXMATRIX proj;
    D3DXMatrixPerspectiveFovRH(&proj, 1, 4.0/3.0, 1.0, 100.0);
    dev->SetTransform( D3DTS_PROJECTION, (const D3DMATRIX*)&proj);

(see D3DXMatrixPerspectiveFovRH for details)

2. Camera:
   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 for details)

3. Model:
    D3DXMATRIX rot;
    D3DXMatrixRotationYawPitchRoll( &rot, time, time, time );
    dev->SetTransform( D3DTS_WORLD, (const D3DMATRIX*)&rot );

(D3DXMatrixRotationYawPitchRoll rotates the object around three axes; "time" is some slowly increasing float)

Don't forget to disable lighting:
    dev->SetRenderState( D3DRS_LIGHTING, false );

Now draw the object:
    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) to hold texture-coordinates and enable your map.
Challenge Trophies Won:

AnimalMother

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  ;)

Rbz

About Crinkler, you can read Mentor's explanation about Crinkler command line options that might save you some bytes for your production.

Challenge Trophies Won:

va!n

@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) )


- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

Jim

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

Challenge Trophies Won:

va!n

@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)
- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won: