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

HLSL 研究学习之六章六节

时间:2023-04-03 11:37:15浏览次数:40  
标签:pd3dDevice 1.0 六章 六节 0.0 -------------------------------------------------------


                                                                        HLSL 研究学习之六章六节

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

                                            (6_6)3D三角形双纹理与色彩混合(fx中单一色,不用PS)

 

                                                      

HLSL 研究学习之六章六节_application

 

hlsl.fx:

// -------------------------------------------------------------
//hlsl.fx
// -------------------------------------------------------------
// 全局变量
// -------------------------------------------------------------
float4x4 matWorldViewProj; 
texture g_Texture;  
string  g_TexFile="5.bmp";
texture g_Texture2;  
string  g_TexFile2="leaf.png";
// -------------------------------------------------------------
// 输出顶点信息结构
// -------------------------------------------------------------
struct VS_OUTPUT
{
    float4 Pos  :POSITION;
    float4 Diffuse :COLOR0;
    float2 TexUV1 :TEXCOORD0; 
    float2 TexUV2 :TEXCOORD1; 
};// -------------------------------------------------------------
// 顶点渲染指令段
// -------------------------------------------------------------
VS_OUTPUT VS(float4 Pos : POSITION,float2 TexUV1: TEXCOORD0,float2 TexUV2: TEXCOORD1)
{
    VS_OUTPUT Out;      
    Out.Pos    = mul(Pos, matWorldViewProj); // transform Position
    Out.TexUV1 = TexUV1;
    Out.TexUV2 = TexUV2;
    float4 diffuse = { 1.0f, 0.0f, 1.0f, 1.0f};
    Out.Diffuse.rgb = diffuse;
    Out.Diffuse.a = 1.0f; 
    return Out;
}// -------------------------------------------------------------
// 管道效果指令段
// -------------------------------------------------------------
technique TShader
{
    pass P0
    {
       Texture[0] = <g_Texture>;   //指定第一层纹理
       ColorOp[0] = Modulate;    //指定第一层色彩的操作方式
       ColorArg1[0] = Texture;   //指定第一层色彩操作方式的第一方为纹理
       ColorArg2[0] = Diffuse;   //指定第一层色彩操作方式的第二方为顶点色彩
       AlphaOp[0] = Modulate;    //指定第一层Alpha操作方式
       AlphaArg1[0] = Texture;   //指定第一层Alpha操作方式的第一方为从纹理中取得
       AlphaArg2[0] = Diffuse;   //指定第一层Alpha操作方式的第二方为从顶点色彩中取得
       MinFilter[0] = Linear;    //设置第一层纹理纹理过滤方式
       MagFilter[0] = Linear;    //设置第一层纹理纹理过滤方式
       MipFilter[0] = Linear;    //设置第一层纹理纹理过滤方式
       TexCoordIndex[0]=0;       //设置第一层纹理的坐标寻址
       
       Texture[1] = <g_Texture2>;  //指定第二层纹理
       ColorOp[1] = Modulate;    //指定第二层色彩的操作方式
       ColorArg1[1] = Texture;   //指定第二层色彩操作方式的第一方为纹理
       ColorArg2[1] = Current;   //指定第二层色彩操作方式的第二方为顶点色彩
       AlphaOp[1]   = Modulate;  //指定第二层Alpha操作方式
       AlphaArg1[1] = Texture;   //指定第二层Alpha操作方式的第一方为从纹理中取得
       AlphaArg2[1] = Diffuse;   //指定第二层Alpha操作方式的第二方为从顶点色彩中取得
       MinFilter[1] = Linear;    //设置第二层纹理纹理过滤方式
       MagFilter[1] = Linear;    //设置第二层纹理纹理过滤方式
       MipFilter[1] = Linear;    //设置第二层纹理纹理过滤方
       TexCoordIndex[1]=1;       //设置第二层纹理的坐标寻址
        // 编译相应的指令段
        VertexShader = compile vs_1_1 VS();
    }
} 
C++代码:
 
//-----------------------------------------------------------------------------
//HLSL学习
//-----------------------------------------------------------------------------
#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
LPDIRECT3DTEXTURE9      g_pTexture2   = 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; 
 FLOAT tu2, tv2; 
};// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX2) 
//-----------------------------------------------------------------------------
// 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, 0xffff0000,0.0f,1.0f,0.0f,1.0f},
        {  1.0f,-1.0f, 0.0f, 0xff0000ff,1.0f,1.0f,1.0f,1.0f},
        {  0.0f, 1.0f, 0.0f, 0xffffffff,0.5f,0.0f,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(); const char* t_FileName;
 g_pEffect->GetString("g_TexFile",&t_FileName); if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, t_FileName, &g_pTexture ) ) )
    {
  MessageBox(NULL, "Could not find banana.bmp", "Textures.exe", MB_OK);
  return E_FAIL;
    } g_pEffect->SetTexture( "g_Texture", g_pTexture );
 const char* t_FileName2;
 g_pEffect->GetString("g_TexFile2",&t_FileName2); if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, t_FileName2, &g_pTexture2 ) ) )
    {
  MessageBox(NULL, "Could not find banana.bmp", "Textures.exe", MB_OK);
  return E_FAIL;
    } g_pEffect->SetTexture( "g_Texture2", g_pTexture2 );
    return S_OK;
} 
//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
    if( g_pTexture != NULL )
        g_pTexture->Release();    if( g_pTexture2 != NULL )
        g_pTexture2->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学习",
                              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,六章,六节,0.0,-------------------------------------------------------
From: https://blog.51cto.com/u_15941225/6165860

相关文章

  • 6 第六章 字典
    一个简单的字典alien_0={'color':'green','points':5}print(alien_0['color'])print(alien_0['points'])使用字典#在Python中,字典用放在花括号{}中的一系列......
  • 第六节 使用ISM Web组态软件的自定义数据
    文章目录前言一、ISM的自定义数据支持哪些运算二、使用说明1.取位运算2.复杂数据运算3.查看自定义数据结果总结前言自定义数据是数据模型的额外补充,比如有的数据模型里面......
  • 第六章.Hive组件安装配置
    第六章.Hive组件安装配置1.1.实验目的完成本实验,您应该能够:掌握Hive组件安装配置掌握Hive组件格式化和启动1.2.实验要求熟悉Hive组件安装配置了解Hive组......
  • 第六章 应用层
    第六章应用层应用层协议=传输层协议+端口号常见的应用层协议和端口号HTTP(超文本传输协议)TCP的80端口是万维网(WWW)客户程序与服务器程序之间交互协议。......
  • 第六章 Calico架构图的详解图
    1,Calico网络模型主要工作组件:1.Felix:运行在每一台Host的agent进程,主要负责网络接口管理和监听、路由、ARP管理、ACL管理和同步、状态上报等。保证跨主机容器网络互......
  • 动手学深度学习v2——第六章predict_ch6
    在QA环节,有位同学问了第六章的predict函数在哪,书中没有给出,使用predict_ch3稍作更改可得。defpredict_ch6(net,test_iter,device,n=6):#@save"""预测标签(定义......
  • 第六章 贪心一
    第六章贪心一区间问题区间选点给定 N 个闭区间 [ai,bi],请你在数轴上选择尽量少的点,使得每个区间内至少包含一个选出的点。输出选择的点的最小数量。位于区间端点......
  • 第六节:Git常用指令汇总和常见使用场景剖析
    一.        二.        三.         !作       者:Yaopengfei(姚鹏飞)博客地址:http://www.cnblog......
  • 第五第六章
    第5章散列函数定义:一类将任意长度的输入位(或字节)串转换为固定长度的输出的函数数字签名应用:消息长,公钥运算代价大,牺牲部分安全性(碰撞)取散列做签名混淆!数据结构中的访问......
  • 第六节:pnpm剖析(简介、硬/软链接、实操、配置、原理)
    一.pnpm简介1.官网的给出的定义 官网:https://pnpm.io/zh/ 快速的,节省磁盘空间的包管理工具优势: 当使用npm或Yarn时,如果你有100个项目,并且所有项目都有......