Translating Transformed Vertices

This is a discussion on Translating Transformed Vertices within the Game Programming forums, part of the General Programming Boards category; This is slower than using vertex buffers but maybe it won't be significant for you. Not when your vertices are ...

  1. #16
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    This is slower than using vertex buffers but maybe it won't be significant for you.
    Not when your vertices are lower than about 100. If the number is less than 100 then internally Direct3D turns DrawPrimitiveUP into a DrawPrimitive call. In fact gamedev and beyond3d both recommend using DrawPrimitiveUP on batches of less than 100.

    Using DrawPrimitiveUP on small batches is faster than continually locking/unlocking vertex buffers.


    Ideally you want to use a huge indexed vertex buffer and simply give your object a certain range of those indices to use - same for textures. Specify a huge texture and index into it with your u,v coords for the correct texture.

    This is akin to the old days when they used tiled bitmaps to represent all animation frames and simply indexed into the bitmap to extract the correct image.

    Incidentally try this instead of what you are using.

    Code:
     
    class Object2D
    {
      ID3DXSprite *m_pSprite;
      IDirect3DTexture9 *m_pTexture;
      IDirect3DDevice9 *m_pDevice;
     
      D3DXVECTOR3 m_pPos;
      float m_fScale;
      public:
    	  Object2D(void):m_pSprite(NULL),m_pTexture(NULL),m_pDevice(NULL),m_pPos(D3DXVECTOR3(0.0f,0.0f,0.0f)) {}
    	  ~Object2D(void)
    	  {
    		 SAFE_RELEASE(m_pTexture);
    		 SAFE_RELEASE(m_pSprite);
    	  }
    	  
    	  void Create(IDirect3DDevice9 *_device,float _scale,D3DXVECTOR3 _pos,std::string _file)
    	 {
    	   m_pDevice=_device;
    	   m_fScale=_scale;
    	   m_pPos=_pos;
       
    	   //Load texture
    	   D3DXCreateTextureFromFile(m_pDevice,_file.c_str(),&m_pTexture);
     
    	   //Create Sprite interface
    	   D3DXCreateSprite(m_pDevice,&m_pSprite);
    	}
     
    	void UpdateAndDraw(void)
    	{
    	   D3DXMATRIX trans;
    	   D3DXMATRIX scale;
    	   D3DXMATRIX world;
    	  
    	   //Translation
    	   D3DXMatrixTranslation(&trans,m_pPos.x,m_pPos.y,m_pPos.z);
     
    	   //Scaling
    	   D3DXMatrixScaling(&scale,m_fScale,m_fScale,1.0f);
     
    	   //Concatenate
    	   world=scale*trans;
     
    	   //Set transform
    	   m_pSprite->SetTransform(&world);
     
    	   //Draw sprite
    	   m_pSprite->Begin(D3DXSPRITE_ALPHABLEND);
     
    	   m_pSprite->Draw(Texture,NULL,NULL,NULL,0xFFFFFFFF);
     
    	   m_pSprite->End();
    	 }
    };
    ID3DXSprite is much easier than creating your own class that pretty much does the same thing. I'm changing all the billboarding code and sprite code in both of my games to use ID3DXSprite.
    Last edited by VirtualAce; 12-04-2004 at 03:06 PM.

  2. #17
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Okay, well here's a gist of the game:
    If anyone's ever played Evolve (or Evolite), it's an oldschool game from way back in the day, you'll get what's happening.

    Each player sets conditions for their organisms. (I.e., if two friends are beside you, go straight. If one enemy and one friend are beside you, turn left. And so forth.) Organisms multiply from eating food, or die if surrounded by too many enemies. (Something like the Game of Life, but not quite). Otherwise, the organisms are lemmingly travelling across the screen in their direction.

    Now, each organism is just a square (4 vertices, width/height of 8 pixels.) With no overlap, the maximum amount of organisms that can fit in the screen are 20480.

    1280 x 1024 = 1310720 Pixels
    8 x 8 = 64 Pixels per Organism, 1310720 / 64 = 20480 Organisms

    (The reason for 2 Million before was a resolution of 1600x1200 and one pixel per organism. But that would be impractical and very ugly.)

    Though this will never be the case, I have to work with some number, and 20480 works. Since each organism has it's own velocity, I'll be needing to render each one every frame.

    This Sprite method is looking promising, though I don't quite get what float _scale should be passed, nor where the actual size of the sprite gets declared. (And does UpdateAndDraw() fall inside of the D3DDevice->BeginScene() and D3DDevice->EndScene() methods? I'm hoping so, if I've learned anything about DX).




    Thanks alot so far for the pointers so far Mr. Wizard and Bubba. I've been using DirectX for a long time, yet I still feel that I'm very near the beginning of it. There's so much to it that it's tough to wrap my head around everything the first time. (Or second, or third, or tenth).

  3. #18
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    Scale is used to scale the final vertices. When you pass in the IDirect3DTexture9 interface to ID3DXSprite it auto-computes the size of the quad based on the size of your texture.

    D3DSURFACE_DESC _pDesc;

    Texture->GetLevelDesc(0,&_pDesc);

    float Width=(float)_pDesc.Width;
    float Height=(float)_pDesc.Height;

    Incidentally you must retrieve these values in order to correctly rotate sprites. ID3DXSprite assumes all sprites start in the upper left corner so you must offset their position by -Width*0.5f and -Height*0.5f.

    These values can also be used for Bounding box computation on your sprite.

    You must call ID3DXSprite::Begin(DWORD flags) prior to drawing any sprites.
    You must draw between a Begin() and End() block.
    You must clal ID3DXSprite::End() after you are done or prior to IDirect3DDevice9::EndScene().

  4. #19
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Hm, I'm getting an obscene amount of linker errors when trying to compile.

    Includes:
    Code:
    #include <D3D9.h>
    #include <D3DX9CORE.h>
    
    #pragma comment(lib, "D3D9.lib")
    #pragma comment(lib, "D3DX9.lib")
    Is there something that I'm missing?

    And I'm still a little unsure about the scaling. Does that refer to the scaling of my screen? (I.e. Pass Width/Height) Or the scaling of the Texture in relation to the Screen (I.e. 1 to 1)?

    EDIT: Since I'm linking the D3DX9.lib, I decided it a good idea o include D3DX9.h, but still, many many linking errors.

  5. #20
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    If you are compiling under MSVC for DirectX 9.0 Summer 2004 SDK + -> you MUST have 2 things.

    1. The d3dx9.lib library file - it's in the summer 2004 extras.
    2. BASETSD.H - this has not been included with the most recent DirectX SDKs. It defines several standard data types for MSVC. MS ASSumes we are developing using their super uber cool .NET neato frito garbage and so have dropped support for MSVC 6. But most people are NOT developing on .NET because it doesn't offer anything new for games and the upgrade doesn't really relate as far as graphics and games are concerned.

    BASETSD.h is available on this site in the build errors thread, or you can get it inside of the Platform SDK.

  6. #21
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    My SDK Version is 9.0 (October 2004) Release.

    The D3DX9.lib is in the "\Lib\" Folder.

    BASETSD.h was not in the "\Include\" folder, but after creating a new Text Doc, pasting the code, and resaving as basetsd.h, I am still getting the same errors.

    An example of a few:
    Code:
    Linking...
    D3DX9.lib(cshaderprogram.obj) : error LNK2001: unresolved external symbol ___security_cookie
    D3DX9.lib(cpslegacyprogram.obj) : error LNK2001: unresolved external symbol ___security_cookie
    D3DX9.lib(cpsprogram.obj) : error LNK2001: unresolved external symbol ___security_cookie
    D3DX9.lib(cvsprogram.obj) : error LNK2001: unresolved external symbol ___security_cookie
    D3DX9.lib(cfxlprogram.obj) : error LNK2001: unresolved external symbol ___security_cookie

  7. #22
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    And you are positive you are linking with the MSVC 6 version of d3dx9.lib?

    Those are all D3DX library errors. Double check which library you are linking to. Double check that you are not pointing MSVC to another library prior to the one you want. In other words make the DirectX library directory the FIRST one on the list - otherwise it wont find the files correctly. Same for includes.

  8. #23
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Hm, I could've sworn I did it properly (I followed DrunkenHyena's setup instructions as best I could).

    Tools->Options->Directories Tab

    In the "Include files" dropdown I get these in the "Directories Listbox":
    C:\Program Files\Microsoft DirectX 9.0 SDK (October 2004)\Include
    C:\DXSDK\include
    C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE
    C:\Program Files\Microsoft Visual Studio\VC98\MFC\INCLUDE
    C:\Program Files\Microsoft Visual Studio\VC98\ATL\INCLUDE

    And in the "Library files" dropdown I have these listed as Directories:
    C:\Program Files\Microsoft DirectX 9.0 SDK (October 2004)\Lib
    C:\DXSDK\Lib
    C:\Program Files\Microsoft Visual Studio\VC98\LIB
    C:\Program Files\Microsoft Visual Studio\VC98\MFC\LIB

    Should I remove the DXSDK (They're the DX8 SDK Information Files), but my understanding was that it just searched form the top of the list down until it found what it needed.


    It's not a problem that I'm Including/Linking the same files in two seperate headers is it?
    My cD3D.h header has:
    Code:
    #include <Windows.h>
    #include <D3D9.h>
    #include "cORGANISM.h"
    
    #pragma comment(lib, "D3D9.lib")
    And my cORGANISM.h header has:
    Code:
    #include <D3D9.h>
    #include <D3DX9.h>
    #include <D3DX9CORE.h>
    
    #pragma comment(lib, "D3D9.lib")
    #pragma comment(lib, "D3DX9.lib")
    Thanks for all the suggestions so far though. I'm sure that basetsd.h would've come into play in the near future too.

  9. #24
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    If you include d3dx9.h and link with d3dx9.lib you DO NOT need to include d3dx9core.h AFAIK. I believe that d3dx9.h acts as a do-it-all header and included the appropriate DirectX headers for those functions for which it makes use of. Now of course if you want DirectInput, DirectMusic, etc, you must include the correct header. But not for use of the D3DX library. The whole thing should be in d3dx9.h

  10. #25
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Okie dokes, well I've removed the D3DX9CORE.h file.

    Without the:
    #pragma comment(lib, "D3DX9.lib")

    I get:
    Code:
    Linking...
    cORGANISM.obj : error LNK2001: unresolved external symbol _D3DXCreateSprite@8
    cORGANISM.obj : error LNK2001: unresolved external symbol _D3DXCreateTextureFromFileA@12
    cORGANISM.obj : error LNK2001: unresolved external symbol _D3DXMatrixScaling@16
    cORGANISM.obj : error LNK2001: unresolved external symbol _D3DXMatrixTranslation@16
    cORGANISM.obj : error LNK2001: unresolved external symbol _D3DXMatrixMultiply@12
    Debug/Evolve.exe : fatal error LNK1120: 5 unresolved externals
    But MSDN tells me that D3DX9.lib is the import library for all of those. So I toss it in, and that's where I get, instead, 67 errors.

    I'm running out of ideas. Maybe I have a faulty D3DX9.lib file? I don't know.

    I'll just keep messing around with it, and if I stumble on the answer, I'll let you know so's the next person that hits this ridiculous problem doesn't have a heart attack as well.

  11. #26
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    You are simply not linking with d3dx9.lib. That can be the only thing leading to those errors. Either MSVC 6 is not finding it correctly or you are missing something when you are specifying where it is. Basically the headers are now ok and you have no errors relating to headers that I can see. Your problem is d3dx9.lib.

    You may also want to try and re-create your entire project in another folder. Sometimes projects get fragged under MSVC. You may also want to try and do a clean, and rebuild entire project. Something's amiss somewhere.

    Please post screenshots of your build environment, the code that is failing or the code that uses the functions that are not being linked in correctly, the project window for both includes, source, and for dependencies - this will show all external dependencies like the d3dx9.lib library functions. Also it would be helpful for you to post a screenshot of the class view and anything else you may think relevant to the problem.

    This is really the only way I can help short of taking control of your system via remote connection on MSN messenger and diagnosing/troubleshooting it myself.

    Also if you have the teamspeak client, I can fire up my teamspeak server and we can discuss and troubleshoot the problem together while on remote connection. Of course you will need speakers and a microphone and the client. Google for teamspeak RC2 client and it will find it. I won't spam the link here.
    Last edited by VirtualAce; 12-09-2004 at 08:17 PM.

  12. #27
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Okay, well I've put together all that I think you mentioned and a few extras that, just in case, might come in handy.

    I was restricted by the size of the attachments so I included the source files.

    I doubt it's working like it should. I was hoping to troubleshoot as I went, but, well, I can't without it actually running.

    Each time before I run, I re-build all. I also re-created the project in a seperate directory, but the same errors kept coming up.

    I really appreciate you taking all this time, I just have no clue where to go from here. (If it ends up to be a missing semi-colon, I will cry.)

    (And I think I'll invest in a microphone in the near future).
    Attached Files Attached Files

  13. #28
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    The code compiled fine on my machine with 0 errors and 0 warnings.

  14. #29
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    Here is the link information:

    Note that even when I left out d3d9.lib, dxguid.lib, and winmm.lib it still compiled fine. However it does absolutely nothing but attempt to fire up Direct3D and then quickly return to the desktop.

  15. #30
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    Here is the include info:

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vertices and Indices
    By beene in forum Game Programming
    Replies: 15
    Last Post: 05-07-2007, 03:08 AM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  4. Translating Java to C. Mergesorting linked list.
    By Mikro in forum C Programming
    Replies: 10
    Last Post: 06-22-2004, 11:34 AM
  5. winding vertices
    By confuted in forum Game Programming
    Replies: 0
    Last Post: 07-21-2002, 09:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21