Solved

Specular component

Posted on 2001-06-09
4
258 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
Comment Utility
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
Comment Utility
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
Comment Utility
Will you explain why 16 bit?
0
 
LVL 2

Expert Comment

by:cybermike3d
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

15 Experts available now in Live!

Get 1:1 Help Now