HLSL研究学习之三_hlsl 红孩儿-程序员宅基地

技术标签: output  null  application  winapi  DirectX 3D 学习  float  structure  

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

 

                                                  (3_1)3D中3D(用Fx中单一色)

 

 

 

 

//(3_1)3D三角形(fx中单一色)
// -------------------------------------------------------------
// 全局变量
// -------------------------------------------------------------
float4x4 matWorldViewProj; 

// -------------------------------------------------------------
//顶点输出信息
// -------------------------------------------------------------
struct VS_OUTPUT
{
   float4 Pos: POSITION;
};

// -------------------------------------------------------------
// 顶点渲染指令段
// -------------------------------------------------------------

VS_OUTPUT VS( float4 Pos: POSITION )
{
   VS_OUTPUT Out = (VS_OUTPUT) 0;
   Out.Pos = mul(Pos, matWorldViewProj); // 通过矩阵来设置顶点位置
   return Out;
}

// -------------------------------------------------------------
// 像素渲染指令段
// -------------------------------------------------------------
float4 PS() : COLOR
{
   return float4(1.0,  1.0,  0.0, 0.0);//顶点色彩设黄色
}


// -------------------------------------------------------------
// 效果指令段
// -------------------------------------------------------------
technique TShader
{
    pass P0
    {
        VertexShader = compile vs_1_1 VS();
        PixelShader  = compile ps_1_1 PS();
    }
}

 

C++代码:

 

//-----------------------------------------------------------------------------
//(3_1)3D三角形(fx中单一色)
//-----------------------------------------------------------------------------
#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;
// 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
};

// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)

 


//-----------------------------------------------------------------------------
// 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, },
        {  1.0f,-1.0f, 0.0f, 0xff0000ff, },
        {  0.0f, 1.0f, 0.0f, 0xffffffff, },
    };

    // 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();

    return S_OK;
}

 


//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
    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();

        // Render the vertex buffer contents

  //按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;
}

 

 

 

 

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/honghaier/article/details/3188453

智能推荐

盲图像超分辨率重建 ( CVPR,2022) (Pytorch)(附代码)_盲超分辨率重建-程序员宅基地

文章浏览阅读1.1w次,点赞33次,收藏141次。基于噪声和核函数的精细退化盲图像超分辨率重建,这篇测评文章用心写了一个下午,个人觉得作为一个初学者入门教程,再合适不过了,敬请查阅_盲超分辨率重建

Executors创建的4种线程池的使用-程序员宅基地

文章浏览阅读74次。为什么80%的码农都做不了架构师?>>> ..._private executor 建立的线程池

android 使用shell模拟触屏_Android随笔之——用shell脚本模拟用户按键、触摸操作...-程序员宅基地

文章浏览阅读720次。之前写过两篇关于Android中模拟用户操作的博客(其实用一篇是转载的),现在就来讲讲用shell脚本来模拟用户按键操作。本次的目标是用shell脚本打开微信并在其搜索框中搜索相关内容。本文的模拟功能主要是用adb的input命令来实现,如果你adb的环境变量配置正确的话,在cmd中输入adb shell input就可以看见input的用法了。usage: input ...input te..._androd shell操作ui

Java之集合常见的笔试题_空题hashmap<integer,string> hh=new hashmap<integer, -程序员宅基地

文章浏览阅读961次,点赞2次,收藏4次。一、请说明Map接口和Collection接口的区别简答如下: 1)都是集合类的接口,但是Collection是存储一组数据的,比如Set,List,Queue这些接口都是继承Collection的。而Map是按键值对存储的,有key和value,关注点在key的使用 2)Collection中存储了一组对象,而Map存储关键字/值对。 在Map对象中,每一个关键字最多有一个关联的值。 ..._空题hashmap hh=new hashmap();hh.put(202

NFS实现系统启动时延迟挂载-程序员宅基地

文章浏览阅读1.2k次。nfs客户端1. vi /etc/fstab 加入一下选项_netdev10.10.10.1:/vol1/fs1 /data nfs defaults,_netdev 0 02. service netfs start; chkconfig netfs onthat's it.Now your mount boot process should look somethin..._fstab 延迟挂载

一文读懂微服务编排利器—Zeebe-程序员宅基地

文章浏览阅读1.8k次,点赞3次,收藏18次。导语 | 微服务架构的一大核心是把大的复杂的业务系统拆分成高内聚的微服务,每个服务负责相对独立的逻辑。服务拆分的好处无需赘述,但是要实现业务价值,不是看单个服务的能力,而是要协调所有服务保..._zeebe 失败任务重复创建job新实例

随便推点

Oracle11g数据库详细安装步骤_oracle database 11g软件没有典型安装-程序员宅基地

文章浏览阅读1.9k次,点赞4次,收藏28次。一,Oracle数据库安装包下载1.从Oracle官网下载所需的数据库安装包【下载需要注册登录Oracle账户】http://www.oracle.com/technetwork/database/enterprise-edition/downloads/index.html2.此处有下载好的Oracle Database 11g Release 2 百度云网盘 提取码:ze2z;【此..._oracle database 11g软件没有典型安装

docker、docker-compose环境变量以及传参_docker-compose env参数传递-程序员宅基地

文章浏览阅读5.6k次。docker中传参可用ARG接收参数#构建镜像docker build -f /manage/src/main/docker/Dockerfile -t manage:1.0 --build-arg server_name= --build-arg server_version=1.0 /manage/targetdockerFile# 该镜像需要依赖的基础镜像FROM java:8#传参ARG server_nameARG server_version#环境变量ENV jar_docker-compose env参数传递

Linq 基本语法_linq语法介绍-程序员宅基地

文章浏览阅读1.2k次。以下都是转载内容1.简单的linq语法 //1 var ss = from r in db.Am_recProScheme select r; //2 var ss1 = db.Am_recProScheme; //3 _linq语法介绍

springcloudgateway+oauth2实现权限控制_springcloudoauth2权限控制-程序员宅基地

文章浏览阅读1.2w次,点赞7次,收藏34次。文章目录鉴权服务基础配置鉴权配置网关服务基础配置网关过滤器配置权限校验过滤器配置接口调试前端适配鉴权服务OAuth 2.0 的四种方式OAuth2实现分析基础配置新建house-oauth模块,依赖oauth2。 <dependencies> <dependency> <groupId>com.babyjuan</groupId> <artifactId>house-common</art_springcloudoauth2权限控制

广州大学c语言期末考试题,广州大学C语言程序的设计期末考试卷含答案(2)-程序员宅基地

文章浏览阅读243次。广州大学C语言程序的设计期末考试卷含答案(2) (9页) 本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦!14.9 积分. . . . .院、系领导审批并签名 A 卷参考答案广州大学 2009-2010 学年第 1 学期考试卷课程 程序设计 1 考试形式(闭卷,考试)学院 数学与信息 系 专业 信安,计 班级 学号 姓名_ 题次一二三四五六七八九十总分评卷人..._广州大学c语言题库

画出lightGBM的特征重要性并排序_lightgbm特征重要性排序-程序员宅基地

文章浏览阅读4.2k次。df = pd.DataFrame()df['feature name'] = data_df.drop(["label"], axis=1).columns #data_df为特征数据集df['importance'] = clf.feature_importance()df.sort_values(by='importance',inplace=True, ascending=False)df2 = df.head(50)df2.plot.barh(x = 'feature name',._lightgbm特征重要性排序

推荐文章

热门文章

相关标签