Solved

Specular component

Posted on 2001-06-09
4
267 Views
Last Modified: 2013-12-26
I am trying to implement some specular highlights on a circle in dx8 but the code does not work. I am writing the important part of code.

//global variables
LPDIRECT3D8             g_pD3D       = NULL; LPDIRECT3DDEVICE8       g_pd3dDevice = NULL; LPDIRECT3DVERTEXBUFFER8 g_pVB        = NULL;

struct CUSTOMVERTEX
{
    D3DXVECTOR3 position;
    D3DXVECTOR3 normal;  
};

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL)

HRESULT InitD3D( HWND hWnd )
{
   
    if( NULL == ( g_pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) )
        return E_FAIL;

 
    D3DDISPLAYMODE d3ddm;
    if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
        return E_FAIL;

    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = d3ddm.Format;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

 
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF , hWnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }
 
    // Turn off culling
    g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );

    // Turn on the zbuffer
    g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );

    return S_OK;
}

HRESULT InitGeometry()
{
    // Create the vertex buffer.
    if( FAILED( g_pd3dDevice->CreateVertexBuffer( 50*sizeof(CUSTOMVERTEX),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, &g_pVB ) ) )
    {
        return E_FAIL;
    }

    CUSTOMVERTEX* pVertices;
    if( FAILED( g_pVB->Lock( 0, 0, (BYTE**)&pVertices, 0 ) ) )
        return E_FAIL;

pVertices[0].position = D3DXVECTOR3( 0.0f,3.0f, 0.0f );
pVertices[0].normal   = D3DXVECTOR3( 0.0f, 3.0f, 0.0f );
 
  for( DWORD i=1; i<49; i++ )
    {
        FLOAT theta = (2*D3DX_PI* i)/(49-1);

    pVertices[i].position = D3DXVECTOR3( 3*sinf(theta), 3*cosf(theta),0.0f );

        pVertices[i+0].normal   = D3DXVECTOR3( 3*sinf(theta), 3*cosf(theta) , 0.0f);
        }
    g_pVB->Unlock();

    return S_OK;
}

VOID SetupMatrices()
{
    D3DXMATRIX matWorld;
    D3DXMatrixIdentity( &matWorld );
   
   D3DXMatrixTranslation(&matWorld,1.0,0.0f,0.0f);
    g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

   
    D3DXMATRIX matView;
D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3( 0.0f, 0.0f,-50.0f ),
                                  &D3DXVECTOR3( 0.0f, 0.0f, 0.0f ),
                                  &D3DXVECTOR3( 0.0f, 1.0f, 0.0f ) );
    g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );

    D3DXMATRIX matProj;
    D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
    g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}

VOID SetupMaterial()
{
    D3DMATERIAL8 mtrl;
    ZeroMemory( &mtrl, sizeof(D3DMATERIAL8) );
      
    mtrl.Diffuse.r = mtrl.Ambient.r = 1.0f;
    mtrl.Diffuse.g = mtrl.Ambient.g = 1.0f;
    mtrl.Diffuse.b = mtrl.Ambient.b = 0.0f;
    mtrl.Diffuse.a = mtrl.Ambient.a = 1.0f;

    mtrl.Specular.r=0.0f;
      mtrl.Specular.g=1.0f;
      mtrl.Specular.b=0.0f;
      mtrl.Specular.a=1.0f;

      mtrl.Power=50.0f;

    g_pd3dDevice->SetMaterial( &mtrl );

        // Finally, turn on some ambient light.
      g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x00202020 );
   
}

VOID Render()
{
    // Clear the backbuffer and the zbuffer
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
                         D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );

    // Begin the scene
    g_pd3dDevice->BeginScene();

    // Setup materials
    SetupMaterial();

    // Setup the world, view, and projection matrices
    SetupMatrices();

    // Render the vertex buffer contents
    g_pd3dDevice->SetStreamSource( 0, g_pVB, sizeof(CUSTOMVERTEX) );
    g_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
    g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 47 );

    // End the scene
    g_pd3dDevice->EndScene();

    // Present the backbuffer contents to the display
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}


A circle is drawn but there are no specular highlifgts. Also if I want to set a desired shade of red color how can I set that if I know its RGB value.And what color format is best for games 8,16 or 24 bit.  
0
Comment
Question by:Sana060101
  • 3
4 Comments
 
LVL 2

Expert Comment

by:cybermike3d
ID: 6179631
Welcome into the lions den. There are a very narrow set of parameters within which specularity will work. If you a scale setting other than 1, specularity will not work.

16 Bit DEFINATELY

I have made a program ... unfortunately it is in VB, but it allows you to do the testing. Maybe u should do the same for C and then maybe you'll get somewhere.

Take a look at http://www.cybermike3d.co.za/1jet1.jpg 

There are the setting guidelines to get specular lighting and material fx working

Does this help ?
0
 
LVL 2

Accepted Solution

by:
cybermike3d earned 40 total points
ID: 6179640
Lighting is wrong ... Specularity only seems to work with directional ... And very badly with point, and not at all with Spot.
0
 

Author Comment

by:Sana060101
ID: 6216568
Will you explain why 16 bit?
0
 
LVL 2

Expert Comment

by:cybermike3d
ID: 6216683
On certain cards (riva) the program is slower and also does not render properly, than if u use 32 bit. If u r transfering image data, 32 bits is twice as much data.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now