Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8628261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:29:32+00:00 2026-06-12T08:29:32+00:00

I am learning DirectX effect and I know that all the fixed pipline render

  • 0

I am learning DirectX effect and I know that all the fixed pipline render state can be set in an .fx file, when I want to set a point light in an .fx file, how to compute the final pixel color? what is the light[0] come from? below is the code of the .fx file, I don’t know how to write the pixel shader part(BasicPS) to compute the final color.

    // World, View and Projection Matrix 
    uniform extern float4x4 gWVP;  

    // Output Vertex structure 
    struct OutputVS 
    { 
          float4 posH : POSITION0; 
    }; 

    OutputVS BasicVS(float4 posL : POSITION0) 
    { 

          // Zero out our output. 
          OutputVS outVS = (OutputVS)0; 

          // Transform to homogeneous clip space. 
          outVS.posH = mul(posL, gWVP); 

          // Done--return the output. 
          return outVS; 
    } 

    float4 BasicPS() : COLOR 
    { 
        //What to write here? 
    } 

    technique Tech1 
    { 
        pass p0 
        { 
            vertexShader = compile vs_2_0 BasicVS(); 
            pixelShader  = compile ps_2_0 BasicPS(); 

            Lighting = True; 
            LightEnable[0] = True; 
            LightType[0] = POINT; 

            // Light color is white 
            LightAmbient[0] = {0.6f, 0.6f, 0.6f, 0.0f}; 
            LightDiffuse[0] = {1.0f, 1.0f, 1.0f, 0.0f}; 
            LightSpecular[0] = {0.6f, 0.6f, 0.6f, 0.0f}; 

            // Light position 
            LightPosition[0] = {10.0f, 10.0f, -5.0f}; 

            // Light range 
            LightRange[0] = 1000.0f; 

            // Light falloff 
            LightFalloff[0] = 1.0f; 

            // Attenuations 
            LightAttenuation0[0] = 1.0f; 
            LightAttenuation1[0] = 0.0f; 
            LightAttenuation2[0] = 0.0f; 
        } 
    } 


the cpp file of this program as below

    #include <d3dx9.h>
    #include <MMSystem.h>

    LPDIRECT3D9             g_pD3D              = NULL ; // Used to create the D3DDevice
    LPDIRECT3DDEVICE9       g_pd3dDevice        = NULL ; // Our rendering device
    ID3DXMesh*              g_pTeapotMesh       = NULL ; // Hold the teapot
    ID3DXEffect*            g_pEffect           = NULL ; // Effect interface
    bool                    g_bActive           = true ; // Is window active?

    // Handles for effects
    D3DXHANDLE g_hWVP;      // world, view and projection matrix handle.
    D3DXHANDLE g_hTech;     // Technique handle

    #define SAFE_RELEASE(P) if(P){ P->Release(); P = NULL;}

    HRESULT BuildEffect()
    {
        char* effectFile = "./PointLight.fx";

        ID3DXBuffer* errors = 0;

        // Create Effect from file
        HRESULT hr = D3DXCreateEffectFromFile(
            g_pd3dDevice,
            effectFile, 
            NULL,
            NULL,
            D3DXSHADER_DEBUG,
            NULL,
            &g_pEffect,
            &errors);
        if( errors )
            MessageBox(0, (char*)errors->GetBufferPointer(), 0, 0);

        g_hWVP  = g_pEffect->GetParameterByName(0, "gWVP");
        g_hTech = g_pEffect->GetTechniqueByName("Tech1");

        return D3D_OK;
    }

    HRESULT SetupPointLight()
    {
        D3DLIGHT9 pointLight;
        ZeroMemory(&pointLight, sizeof(pointLight));

        // Set light
        g_pd3dDevice->SetLight(0, &pointLight);

        return D3D_OK;
    }

    HRESULT SetupMaterial()
    {
    D3DMATERIAL9 material;
    ZeroMemory(&material, sizeof(material));

    D3DXCOLOR red = D3DCOLOR_XRGB(255, 0, 0);
    material.Ambient = red;
    material.Diffuse = red;
    material.Specular = red;
    material.Emissive = D3DXCOLOR(0, 0, 0, 0);
    material.Power = 2.0f; // only affect specular color

    g_pd3dDevice->SetMaterial(&material);

    return D3D_OK;
    }

    HRESULT InitD3D( HWND hWnd )
    {
        // Create the D3D object, which is needed to create the D3DDevice.
        if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        {
            MessageBoxA(NULL, "Create D3D9 object failed!", "Error", 0) ;
            return E_FAIL;
        }

        D3DPRESENT_PARAMETERS d3dpp; 
        ZeroMemory( &d3dpp, sizeof(d3dpp) );

        d3dpp.Windowed = TRUE; // use window mode, not full screen
        d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
        d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

        // Create device
        if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
            D3DCREATE_SOFTWARE_VERTEXPROCESSING,
            &d3dpp, &g_pd3dDevice ) ) )
        {
            MessageBoxA(NULL, "Create D3D9 device failed!", "Error", 0) ;
            return E_FAIL;
        }

        // Create teapot
        D3DXCreateTeapot(g_pd3dDevice, &g_pTeapotMesh, NULL) ;

        // Build effect
        BuildEffect();

        return S_OK;
    }

    void DrawTeapot()
    {
        // translate model to origin
        D3DXMATRIX world ;
        D3DXMatrixTranslation(&world, 0.0f, 0.0f, 0.0f) ;
        g_pd3dDevice->SetTransform(D3DTS_WORLD, &world) ;

        // set view
        D3DXVECTOR3 eyePt(0.0f, 0.0f, -10.0f) ;
        D3DXVECTOR3 upVec(0.0f, 1.0f, 0.0f) ;
        D3DXVECTOR3 lookCenter(0.0f, 0.0f, 0.0f) ;

        D3DXMATRIX view ;
        D3DXMatrixLookAtLH(&view, &eyePt, &lookCenter, &upVec) ;
        g_pd3dDevice->SetTransform(D3DTS_VIEW, &view) ;

        // set projection
        D3DXMATRIX proj ;
        D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI / 4, 1.0f, 1.0f, 1000.0f) ;
        g_pd3dDevice->SetTransform(D3DTS_PROJECTION, &proj) ;

        D3DXMATRIX worldviewproj = world * view * proj;

        // Set matrix
        g_pEffect->SetMatrix(g_hWVP, &worldviewproj);

        // Set technique
        g_pEffect->SetTechnique(g_hTech);

        // Render pass
        UINT numPass = 0;
        g_pEffect->Begin(&numPass, 0);
        g_pEffect->BeginPass(0);
        g_pTeapotMesh->DrawSubset(0);
        g_pEffect->EndPass();
        g_pEffect->End();
    }

    VOID Cleanup()
    {
        SAFE_RELEASE(g_pTeapotMesh) ;
        SAFE_RELEASE(g_pd3dDevice) ;
        SAFE_RELEASE(g_pD3D) ;
    }

    void Render(float timeDelta)
    {
        if (!g_bActive)
        {
            Sleep(50) ;
        }

        SetupMaterial();

        SetupPointLight();

        // Clear the back-buffer to a RED color
        g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );

        // Begin the scene
        if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
        {
            // Draw teapot 
            DrawTeapot();

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

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

    LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
    {
        switch( msg )
        {
        case WM_KEYDOWN:
            {
                switch( wParam )
                {
                case VK_ESCAPE:
                    SendMessage( hWnd, WM_CLOSE, 0, 0 );
                    break ;
                default:
                    break ;
                }
            }
            return 0 ;

        case WM_SIZE:
            if(wParam == SIZE_MAXHIDE || wParam == SIZE_MINIMIZED)
                g_bActive = false;
            else
                g_bActive = true;
            return 0;

        case WM_ACTIVATEAPP:
            if(wParam == TRUE)
                g_bActive = true ;
            else
                g_bActive = false ;
            return 0 ;

        case WM_DESTROY:
            Cleanup();
            PostQuitMessage( 0 );
            return 0;
        }

        return DefWindowProc( hWnd, msg, wParam, lParam );
    }

    INT WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR szCmdLine, int iCmdShow)
    {
        WNDCLASSEX winClass ;

        winClass.lpszClassName = "Teapot";
        winClass.cbSize        = sizeof(WNDCLASSEX);
        winClass.style         = CS_HREDRAW | CS_VREDRAW;
        winClass.lpfnWndProc   = MsgProc;
        winClass.hInstance     = hInstance;
        winClass.hIcon         = NULL ;
        winClass.hIconSm       = NULL ;
        winClass.hCursor       = LoadCursor(NULL, IDC_ARROW) ;
        winClass.hbrBackground = NULL ;
        winClass.lpszMenuName  = NULL ;
        winClass.cbClsExtra    = 0;
        winClass.cbWndExtra    = 0;

        RegisterClassEx (&winClass) ;  

        HWND hWnd = CreateWindowEx(NULL,  
            winClass.lpszClassName,     // window class name
            "Teapot",                   // window caption
            WS_OVERLAPPEDWINDOW,        // window style
            32,                         // initial x position
            32,                         // initial y position
            600,                        // initial window width
            600,                        // initial window height
            NULL,                       // parent window handle
            NULL,                       // window menu handle
            hInstance,                  // program instance handle
            NULL) ;                     // creation parameters

        // Create window failed
        if(hWnd == NULL)
        {
            MessageBoxA(hWnd, "Create Window failed!", "Error", 0) ;
            return -1 ;
        }

        // Initialize Direct3D
        if( SUCCEEDED(InitD3D(hWnd)))
        { 
            // Show the window
            ShowWindow( hWnd, SW_SHOWDEFAULT );
            UpdateWindow( hWnd );

            MSG msg ; 
            ZeroMemory( &msg, sizeof(msg) );
            PeekMessage( &msg, NULL, 0U, 0U, PM_NOREMOVE );

            // Get last time
            static DWORD lastTime = timeGetTime();

            while (msg.message != WM_QUIT)  
            {
                if(PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) != 0)
                {
                    TranslateMessage (&msg) ;
                    DispatchMessage (&msg) ;
                }
                else // Render the game if there is no message to process
                {
                    // Get current time
                    DWORD currTime  = timeGetTime();

                    // Calculate time elapsed
                    float timeDelta = (currTime - lastTime) * 0.001f;

                    // Render
                    Render(timeDelta) ;

                    // Update last time to current time for next loop
                    lastTime = currTime;
                }
            }
        }

        UnregisterClass(winClass.lpszClassName, hInstance) ;
        return 0;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:29:33+00:00Added an answer on June 12, 2026 at 8:29 am

    Finally, I figure it out myself, the vertex shader and pixel shader cannot omitted, If you don’t want to use shader to compute light, you can specify them as null

    pixelShader = null;
    pixelShader = null;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am learning inter-process communication. I want to create server that can listen to
Just curious to know if anyone has ever used gametutorials.com products for learning directX.
I want to start learning DirectX and I do have an extremely strong grasp
I am learning DirectX 11 right now, and was trying out one of the
I'm developing a game for windows for learning purposes (I'm learning DirectX). I would
Learning a lot in my few years of programming that the best projects are
Learning xml, Can anyone help me? I have following XML code: **<book lang=en>name of
I am from .net C# background and I want to learn DirectX. I have
what is the best book for learning graphics in c# with directx
I started learning DirectX 11 with C++. It's hard, but I think I'm doing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.