Thread: How to Antialias Direct3D?

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    73

    Post How to Antialias Direct3D?

    A quick question of how to go about forcing antialias in direct3D..

    Just getting started and just playing with some simple 3D animation, lighting, shading, other effects, etc... but I noticed my scene is loaded with jagged edges. Such as when drawing in wireframe mode...Tried to do a few searches, but haven't found a solution to my problem.. I just want to antialias my Direct3D app.. Not sure what I have to set though:

    D3DRS_MULTISAMPLEANTIALIAS ??
    D3DRS_MULTISAMPLEMASK ??

    are these flags that I need to be looking into? I'm a bit confused.

    (yes, I know I can manually force my graphics card to force antialias in all direct3D apps, but I don't want to do that..)

    thnx for any help

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    There is an AntiAlias sample that ships with the DirectX 9 sdk. Source is included.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    73
    yep.. i noticed... looked at the program that shipped with SDK..

    sample.. yes...
    tutorial... no...

    have u looked at that sample?
    the sample uses so much extra code just to illustrate antialiasing in Direct3D in a variety of situations. As a result it loads meshes, drawing cylinders, full GUI with buttons/listboxes, text, and loading code full of callback functions that I'm having a hard time finding out the necessary steps to achieve antialias. I've literally gone through each function and find snippets on retrieving a desired level of Multisampling determined by the GUI selections, but still can't piece together what i need. Nobody can tell me the steps i need to follow to atleast give me a push in the right direction?

    - Do i need to query my hardware for capabilities?
    - Set multisampling flags before creating device?

    What do i need, eh?

    I've tried setting MultiSampling flags before creating a device but all i get is immediate crashes so i'm really confused...

    I just want to antialias a simple 3D cube that I've drawn and that I rotate with keyboard buttons.. not wishing to achieve anything more. Its gotta be something simple.. like 3 lines of code I'm thinking..

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Okay, here is one of the methods to do it:

    In the D3DPRESENT_PARAMETERS structure, set the MutliSampleType member to D3DMULTISAMPLE_NONMASKABLE. Then set the MultiSampleQuality member to your desired anti-aliased quality. You need to check the caps to make sure this is supported and to see how many quality levels are supported by the device.

    The call you should look into is

    IDirect3D9::CheckDeviceMultiSampleType

    Then you specify the MutliSampleType and it will fill in the number of quality levels supported or return an error code if it's not supported.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    73
    Well.. gave it my best shot... hopefully i used it semi-correctly. (Feel free to scream at me if I used it wrong and am an idiot).
    Seems to produce better image quality. So I'm guessing it works.. thnx for the help..

    Oh yeah.. what exactly is MultiSampleQuality?? My device returns 4 Does that refer to 4x or something else?
    If I don't subtract 1 from this number before setting I get a crash.

    Code:
    extern IDirect3D9 *g_pD3D9;
    extern IDirect3DDevice9 * g_pD3DDevice9;
    
    bool InitD3D(HWND hWnd)
    {
      g_pD3D9 = Direct3DCreate9(D3D_SDK_VERSION);
    	
      if(g_pD3D9 == NULL)
        return false;
    
      D3DDISPLAYMODE d3ddm;
    
      g_pD3D9->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,
    							   &d3ddm);
    
      D3DPRESENT_PARAMETERS d3dpp;
    
      memset(&d3dpp, 0, sizeof(d3dpp));
      d3dpp.BackBufferWidth = g_nScreenWidth;
      d3dpp.BackBufferHeight = g_nScreenHeight;
      d3dpp.BackBufferFormat = d3ddm.Format;
      d3dpp.BackBufferCount = 1;
      d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
      d3dpp.EnableAutoDepthStencil = true;
      d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
      d3dpp.hDeviceWindow = hWnd;
      d3dpp.Windowed = true;
    	
      DWORD total;
    	  
    if(SUCCEEDED(g_pD3D9->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT,		  							              D3DDEVTYPE_HAL,
    									      d3ddm.Format,
    									      true,
                                                         D3DMULTISAMPLE_NONMASKABLE,
    									      &total)))
      {
        d3dpp.MultiSampleType = D3DMULTISAMPLE_NONMASKABLE;
        d3dpp.MultiSampleQuality = total - 1;
      }
      
    d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
    d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
    
      if(FAILED(g_pD3D9->CreateDevice(D3DADAPTER_DEFAULT, 
    							 D3DDEVTYPE_HAL, 
    							 hWnd,
                                       D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                                             &d3dpp, 
    							 &g_pD3DDevice9)))
         return false;
    				
      return true;
    }
    Last edited by Deo; 06-01-2005 at 10:11 PM.

  6. #6
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by Deo
    I've literally gone through each function and find snippets on retrieving a desired level of Multisampling determined by the GUI selections, but still can't piece together what i need. Nobody can tell me the steps i need to follow to atleast give me a push in the right direction?
    Try harder! Just keep trying soldier! I remember when I used to play FFIX. You see I was at the end of the game, but I really wanted to get the gold chocobo. Well I let my friend do all of the work to solve the puzzle, and then I felt bad for myself because I wish I could've had felt the accomplishment, not my friend.

    In conclusion, just keep trying by getting rid of all of the *stuff*. Not only will this help you with antialiasing, but also all of that other code they threw into the example with. I have faith in you!

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Quote Originally Posted by Deo
    Well.. gave it my best shot... hopefully i used it semi-correctly. (Feel free to scream at me if I used it wrong and am an idiot).
    Seems to produce better image quality. So I'm guessing it works.. thnx for the help..

    Oh yeah.. what exactly is MultiSampleQuality?? My device returns 4 Does that refer to 4x or something else?
    If I don't subtract 1 from this number before setting I get a crash.
    Yes, the number of quality levels are 0-based as it states in the SDK docs. So you have 0, 1, 2, and 3 available to you. Looks like you are doing it correctly to me.

    You can read about how MSAA works here:

    http://www.3dcenter.de/artikel/multi...g/index3_e.php
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why use Direct3D?
    By m3rk in forum Tech Board
    Replies: 42
    Last Post: 05-22-2009, 09:08 AM
  2. Need help with getting DirectX 9.0 initilization
    By DarkMortar in forum Windows Programming
    Replies: 7
    Last Post: 05-09-2006, 08:58 PM
  3. Creating a 2D GUI using Direct3D
    By codec in forum Game Programming
    Replies: 8
    Last Post: 09-23-2004, 11:57 AM
  4. Problem with Direct3D
    By frenchfry164 in forum Tech Board
    Replies: 1
    Last Post: 11-27-2003, 04:28 PM
  5. Direct3D vs. OpenGL
    By PorkyChop in forum Game Programming
    Replies: 22
    Last Post: 12-08-2002, 12:41 AM