首页 > 其他分享 >HLSL 研究学习之七章一节

HLSL 研究学习之七章一节

时间:2023-04-03 11:38:07浏览次数:36  
标签:一节 pd3dDevice 1.0 HLSL 0.0 七章 --------------------------------------------------


                                 HLSL 研究学习之七章一节

    注:所有代码均由红孩儿编写或以Microsoft DX9 SDK Sample 代码修改而成,转载请注明.

                                           (7_1)3D三角形纹理与色彩混合(用原来色彩)

 

 

 

                                                                     

HLSL 研究学习之七章一节_winapi

 

hlsl.fx:

 

// -------------------------------------------------------------
// hlsl.fx:(7_1)3D三角形纹理与色彩混合(用原来色彩)
// -------------------------------------------------------------
// 全局变量
// -------------------------------------------------------------
float4x4 matWorldViewProj; 
float4x4 matWorld; 
float4 vecLightDir;
texture g_MeshTexture;  
// -------------------------------------------------------------
// 纹理过滤方式
// -------------------------------------------------------------
sampler TextureSampler = 
sampler_state
{
    Texture = <g_MeshTexture>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};// -------------------------------------------------------------
// 输出顶点信息结构
// -------------------------------------------------------------
struct VS_OUTPUT
{
    float4 Pos  :       POSITION;
    float4 Diffuse    : COLOR0;
    float2 TexUV :      TEXCOORD0; 
};// -------------------------------------------------------------
// 顶点渲染指令段
// -------------------------------------------------------------
VS_OUTPUT VS(float4 Pos : POSITION,float4 Dif : COLOR0, float2 TexUV: TEXCOORD0)
{
    VS_OUTPUT Out;      
    Out.Pos = mul(Pos, matWorldViewProj); // transform Position
    Out.TexUV=TexUV;
    Out.Diffuse = Dif;
    return Out;
}// -------------------------------------------------------------
// 像素渲染指令段
// -------------------------------------------------------------struct PS_OUTPUT
{
    float4 RGBColor : COLOR0;  // Pixel color    
};PS_OUTPUT PS(VS_OUTPUT In) : COLOR
{
    PS_OUTPUT Output;
    Output.RGBColor= tex2D(TextureSampler, In.TexUV) * In.Diffuse;
    return Output;
}
// -------------------------------------------------------------
// 管道效果指令段
// -------------------------------------------------------------
technique TShader
{
    pass P0
    {
        // 编译相应的指令段
        VertexShader = compile vs_1_1 VS();
        PixelShader  = compile ps_1_4 PS();
    }
} 
C++代码:
//-----------------------------------------------------------------------------
// HLSL学习笔记:(7_1)3D三角形纹理与色彩混合(用原来色彩)
//-----------------------------------------------------------------------------
#include <Windows.h>
#include <mmsystem.h>
#include <d3dx9.h>
#include <strsafe.h> 
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9             g_pD3D       = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9       g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DVERTEXBUFFER9 g_pVB        = NULL; // Buffer to hold vertices
LPD3DXEFFECT            g_pEffect    = NULL;
LPDIRECT3DTEXTURE9      g_pTexture   = NULL; // Our texture
D3DXVECTOR4             g_LightDir= D3DXVECTOR4( 0.0f, 0.0f, 1.0f, 1.0f );
// A structure for our custom vertex type
struct CUSTOMVERTEX
{
    FLOAT x, y, z;      // The untransformed, 3D position for the vertex
    DWORD color;        // The vertex color
 FLOAT tu, tv; 
};// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1) 
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
    // Create the D3D object.
    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        return E_FAIL;    // Set up the structure used to create the D3DDevice
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;    // Create the D3DDevice
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }    // Turn off culling, so we see the front and back of the triangle
    g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );    // Turn off D3D lighting, since we are providing our own vertex colors
    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE ); //创建HLSH
 if( FAILED(D3DXCreateEffectFromFile( g_pd3dDevice, "hlsl.fx", NULL, NULL, 
  D3DXSHADER_DEBUG, NULL, &g_pEffect, NULL ) ) )
 {
        return E_FAIL;
 }
 if( g_pEffect != NULL ) g_pEffect->OnResetDevice();
   
 //设定效果:因为只有一个,所以初始时就设了~
 D3DXHANDLE hTechnique = g_pEffect->GetTechniqueByName( "TShader" ); 
 g_pEffect->SetTechnique( hTechnique );    return S_OK;
} 
//-----------------------------------------------------------------------------
// Name: InitGeometry()
// Desc: Creates the scene geometry
//-----------------------------------------------------------------------------
HRESULT InitGeometry()
{
    // Initialize three vertices for rendering a triangle
    CUSTOMVERTEX g_Vertices[] =
    {
        { -1.0f,-1.0f, 0.0f, 0x00ff00ff,0.0f,1.0f },
        {  1.0f,-1.0f, 0.0f, 0xff0000ff,1.0f,1.0f },
        {  0.0f, 1.0f, 0.0f, 0xff00ffff,0.5f,0.0f },
    };    // Create the vertex buffer.
    if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
    {
        return E_FAIL;
    }    // Fill the vertex buffer.
    VOID* pVertices;
    if( FAILED( g_pVB->Lock( 0, sizeof(g_Vertices), (void**)&pVertices, 0 ) ) )
        return E_FAIL;
    memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );
    g_pVB->Unlock(); if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "banana.bmp", &g_pTexture ) ) )
    {
  MessageBox(NULL, "Could not find banana.bmp", "Textures.exe", MB_OK);
  return E_FAIL;
    } g_pEffect->SetTexture( "g_MeshTexture", g_pTexture );
    return S_OK;
} 
//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
    if( g_pTexture != NULL )
        g_pTexture->Release();    if( g_pEffect != NULL )        
        g_pEffect->Release();    if( g_pVB != NULL )
        g_pVB->Release();    if( g_pd3dDevice != NULL )
        g_pd3dDevice->Release();    if( g_pD3D != NULL )
        g_pD3D->Release();
} 
//-----------------------------------------------------------------------------
// Name: SetupMatrices()
// Desc: Sets up the world, view, and projection transform matrices.
//-----------------------------------------------------------------------------
VOID SetupMatrices()
{
    // For our world matrix, we will just rotate the object about the y-axis.
    D3DXMATRIXA16 matWorld;    UINT  iTime  = timeGetTime() % 1000;
    FLOAT fAngle = iTime * (2.0f * D3DX_PI) / 1000.0f;
    D3DXMatrixRotationY( &matWorld, fAngle );    D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f );
    D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
    D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
    D3DXMATRIXA16 matView;
    D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );    D3DXMATRIXA16 matProj;
    D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f ); //设定VS矩阵
 D3DXMATRIX mWorldViewProj = matWorld * matView * matProj;
 g_pEffect->SetMatrix( "matWorldViewProj", &mWorldViewProj );}
 
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
    // Clear the backbuffer to a black color
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );    // Begin the scene
    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    {
        // Setup the world, view, and projection matrices
        SetupMatrices();
  //按VS,PS渲染
  if( g_pEffect != NULL )
  {
   UINT nPasses=0;
   UINT iPass=0;
   if(SUCCEEDED(g_pEffect->Begin( &nPasses, 0 )))
   {
    for(iPass = 0; iPass < nPasses; iPass ++ )
    {
     g_pEffect->BeginPass( iPass );
    }
    g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );
    g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
    g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 1 );
    g_pEffect->EndPass();
    g_pEffect->End();
   }
  }
        // End the scene
        g_pd3dDevice->EndScene();
    }    // Present the backbuffer contents to the display
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
} 
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
        case WM_DESTROY:
            Cleanup();
            PostQuitMessage( 0 );
            return 0;
    }    return DefWindowProc( hWnd, msg, wParam, lParam );
} 
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
    // Register the window class
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                      "D3D Tutorial", NULL };
    RegisterClassEx( &wc );    // Create the application's window
    HWND hWnd = CreateWindow( "D3D Tutorial", "HLSL学习7_1",
                              WS_OVERLAPPEDWINDOW, 100, 100, 256, 256,
                              GetDesktopWindow(), NULL, wc.hInstance, NULL );    // Initialize Direct3D
    if( SUCCEEDED( InitD3D( hWnd ) ) )
    {
        // Create the scene geometry
        if( SUCCEEDED( InitGeometry() ) )
        {
            // Show the window
            ShowWindow( hWnd, SW_SHOWDEFAULT );
            UpdateWindow( hWnd );            // Enter the message loop
            MSG msg;
            ZeroMemory( &msg, sizeof(msg) );
            while( msg.message!=WM_QUIT )
            {
                if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
                {
                    TranslateMessage( &msg );
                    DispatchMessage( &msg );
                }
                else
    {
     Render();
    }
            }
        }
    }    UnregisterClass( "D3D Tutorial", wc.hInstance );
    return 0;
}

 

标签:一节,pd3dDevice,1.0,HLSL,0.0,七章,--------------------------------------------------
From: https://blog.51cto.com/u_15941225/6165859

相关文章

  • HLSL 研究学习之六章六节
                                                                           HLSL研究学习之六章六节    注:所有代码均由红孩儿编写或以MicrosoftDX9SDKSample代码修改而成,转载请注明.    ......
  • 第七章 工程化 - 实例体验 - 基于 vue框架 开发一个完整的组件库 一
    基于vue框架开发一个完整的组件库来体验前端工程化的魅力对一个组件库的开发来全面认识熟悉前端工程化的配置,系统的理解工程化中各个工具所起到的作用,并且将其最终可......
  • 第十七章 空闲空间管理
    1.随着free的次数增加,空闲列表的元素个数增加,因为不会合并,每次分配内存时,开销也增大,内存碎片增加2.采用最差匹配策略,空闲列表的元素个数增加了。因为每次选择与请求......
  • C++ primer 第七章记录
    1:void可以指向任何类型的对象,但是不能直接操作所指向的对象*2:常量指针可以指向非常量对象doubledval=3.14;constdouble*cptr=&dval;合法,constdouble*只......
  • 第二部分 第七章 创建并管理类和对象
    1、class关键字定义类。类的主体包含方法和字段。classCirle{intradius;doubleArea(){returnMath.PI*radius*radius;}}2、创建类实例使用关键字newCirclec......
  • 第七章:KVM虚拟机克隆
    第七章:KVM虚拟机克隆1.简介虚拟机的克隆⽅式分为完整克隆与链接克隆2.KVM完整克隆格式:virt-clone--auto-clone-o虚拟机名称-n克隆名称[root@wjl~]#virt-clo......
  • python数据分析与挖掘实战第七章
    #代码7-1数据探索importpandasaspddatafile='data3/air_data.csv'#航空原始数据,第一行为属性标签resultfile='data3/explore.csv'#数据探索结果表data=......
  • 第七章 财政收入影响因素分析及预测
    importpandasaspddatafile=r"C:\Users\ying\Desktop\air_data.csv"resultfile=r"C:\Users\ying\Desktop\explore.xlsx"data=pd.read_csv(datafile,encoding='utf-8')e......
  • 数据分析第七章
    航空公司客户价值分析数据探索importpandasaspddatafile=r"D:\py_project\a_三下\air_data.csv"resultfile=r'D:\py_project\a_三下\explore.csv'data=pd.......
  • 第七章 --- 航空预测
    1.数据描述与探索 importmatplotlib.pyplotaspltimportnumpyasnpimportpandasaspd#对数据进行基本的探索#返回缺失值个数以及最大最小值importpandasa......