Solved

Specular component

Posted on 2001-06-09
4
277 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 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