走完DXUT三步曲,下面就可以正式开始学习shader了,这里主要讲解如何搭建Shader的框架:

这个实例先用CG,后面我会尝试用HLSL试试

当我把这个例子写出来的时候小兴奋了把,DXUT太强大了,还有微软的文档及sample和NVIDIA的文档及sample真是不可多得的财富,从此坚定了我紧跟微软和英伟达脚步的决心,哈哈!

废话不多说了,直接上源码:

//--------------------------------------------------------------------------------------
// File: control_color_by_using_CG_DXUT(june_2010)_D3D9.cpp
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "DXUT.h"
#include "DXUTgui.h"//CDXUTDialogResourceManager需要
#include "resource.h"#pragma comment(lib, "cg.lib")
#pragma comment(lib, "cgd3d9.lib")#define IDC_SLIDER_RED          1
#define IDC_SLIDER_GREEN        2
#define IDC_SLIDER_BLUE         3#define IDC_STATIC_RED         4
#define IDC_STATIC_GREEN        5
#define IDC_STATIC_BLUE         6// Global variable
static CGcontext myCgContext;//环境,中间层
static CGprofile myCgVertexProfile;//格式
static CGprogram myCgVertexProgram;//目标文件static IDirect3DVertexBuffer9* myVertexBuffer = NULL;static const WCHAR* myProgramNameW = L"MyProgram";//程序名字,输出错误时用
static const char   *myProgramName = "MyProgram",*myVertexProgramFileName = "myVertex.cg",//shader源文件*myVertexProgramEntryFunctionName = "myVertexEntry"; //顶点shader的入口函数名
static CGparameter myCgVertexParam_r,myCgVertexParam_g,myCgVertexParam_b;int r,g,b;CDXUTDialogResourceManager g_dlg_resource_manager;
CDXUTDialog g_control_dlg;static void checkForCgError(const char *situation)
{char buffer[4096];CGerror error;const char *string = cgGetLastErrorString(&error);//error  A pointer to a CGerror variable for returning the last error code.if (error != CG_NO_ERROR) {if (error == CG_COMPILER_ERROR) {sprintf(buffer,"Program: %s\n""Situation: %s\n""Error: %s\n\n""Cg compiler output...\n",myProgramName, situation, string);OutputDebugStringA(buffer);OutputDebugStringA(cgGetLastListing(myCgContext));/*Each Cg context maintains a null-terminated string containing warning and error messages generated by the Cg compiler,state managers and the like. cgGetLastListing allows applications and custom state managers to query the listing text. cgGetLastListing returns the current listing string for the given CGcontext. When a Cg runtime error occurs, applications can use the listing text from the appropriate context to provide the user with detailed information about the error. */sprintf(buffer,"Program: %s\n""Situation: %s\n""Error: %s\n\n""Check debug output for Cg compiler output...",myProgramName, situation, string);MessageBoxA(0, buffer,"Cg compilation error", MB_OK | MB_ICONSTOP | MB_TASKMODAL);} else {sprintf(buffer,"Program: %s\n""Situation: %s\n""Error: %s",myProgramName, situation, string);MessageBoxA(0, buffer,"Cg runtime error", MB_OK | MB_ICONSTOP | MB_TASKMODAL);}exit(1);}
}//--------------------------------------------------------------------------------------
// Rejects any D3D9 devices that aren't acceptable to the app by returning false
//--------------------------------------------------------------------------------------
bool CALLBACK IsD3D9DeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, D3DFORMAT BackBufferFormat,bool bWindowed, void* pUserContext )
{// Typically want to skip back buffer formats that don't support alpha blendingIDirect3D9* pD3D = DXUTGetD3D9Object();if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType,AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING,D3DRTYPE_TEXTURE, BackBufferFormat ) ) )return false;return true;
}//--------------------------------------------------------------------------------------
// Before a device is created, modify the device settings as needed
//--------------------------------------------------------------------------------------
bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, void* pUserContext )
{return true;
}//--------------------------------------------------------------------------------------
// Create any D3D9 resources that will live through a device reset (D3DPOOL_MANAGED)
// and aren't tied to the back buffer size
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnD3D9CreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc,void* pUserContext )
{HRESULT hr;V_RETURN(g_dlg_resource_manager.OnD3D9CreateDevice(pd3dDevice));return S_OK;
}static void createCgPrograms()
{const char** profileOpts;myCgVertexProfile = cgD3D9GetLatestVertexProfile();//返回最新版本checkForCgError("getting latest profile");profileOpts = cgD3D9GetOptimalOptions(myCgVertexProfile);//得到该版本对应的最佳选项checkForCgError("getting latest profile option");myCgVertexProgram =cgCreateProgramFromFile(myCgContext,              /* Cg runtime context */CG_SOURCE,                /* Program in human-readable form */myVertexProgramFileName,  /* Name of file containing program */myCgVertexProfile,        /* Profile: OpenGL ARB vertex program */myVertexProgramEntryFunctionName,      /* Entry function name */profileOpts);             /* Pass optimal compiler options */checkForCgError("creating vertex program from file");      myCgVertexParam_r =cgGetNamedParameter(myCgVertexProgram, "r");checkForCgError("could not get r parameter");myCgVertexParam_g =cgGetNamedParameter(myCgVertexProgram, "g");checkForCgError("could not get g parameter");myCgVertexParam_b =cgGetNamedParameter(myCgVertexProgram, "b");checkForCgError("could not get b parameter");}
struct MY_V3F
{FLOAT x, y, z;
};
static HRESULT initVertexBuffer(IDirect3DDevice9* pDev)
{/* Initialize three vertices for rendering a triangle. */static const MY_V3F triangleVertices[] = {{ -0.8f,  0.8f, 0.0f },{  0.0f,  0.8f, 0.0f },{ -0.4f,  0.0f, 0.0f }};if (FAILED(pDev->CreateVertexBuffer(sizeof(triangleVertices),0, D3DFVF_XYZ,D3DPOOL_DEFAULT,&myVertexBuffer, NULL))) {return E_FAIL;}void* pVertices;if (FAILED(myVertexBuffer->Lock(0, 0, /* map entire buffer */&pVertices, 0))) {return E_FAIL;}memcpy(pVertices, triangleVertices, sizeof(triangleVertices));myVertexBuffer->Unlock();return S_OK;
}//--------------------------------------------------------------------------------------
// Create any D3D9 resources that won't live through a device reset (D3DPOOL_DEFAULT)
// or that are tied to the back buffer size
//--------------------------------------------------------------------------------------
HRESULT CALLBACK OnD3D9ResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc,void* pUserContext )
{HRESULT hr;V_RETURN(g_dlg_resource_manager.OnD3D9ResetDevice());g_control_dlg.SetLocation(pBackBufferSurfaceDesc->Width - 170, pBackBufferSurfaceDesc->Height - 350);g_control_dlg.SetSize(170, 300);// setup view matrixD3DXMATRIX mat_view;D3DXVECTOR3 eye(0.0f, 0.0f, -5.0f);D3DXVECTOR3 at(0.0f, 0.0f, 0.0f);D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);D3DXMatrixLookAtLH(&mat_view, &eye, &at, &up);pd3dDevice->SetTransform(D3DTS_VIEW, &mat_view);// set projection matrixD3DXMATRIX mat_proj;float aspect = (float)pBackBufferSurfaceDesc->Width / pBackBufferSurfaceDesc->Height;D3DXMatrixPerspectiveFovLH(&mat_proj, D3DX_PI/4, aspect, 1.0f, 100.0f);pd3dDevice->SetTransform(D3DTS_PROJECTION, &mat_proj);cgD3D9SetDevice(pd3dDevice);checkForCgError("setting Direct3D device");static int firstTime = 1;if (firstTime) {/* Cg runtime resources such as CGprogram and CGparameter handlessurvive a device reset so we just need to compile a Cg programjust once.  We do however need to unload Cg programs withcgD3DUnloadProgram upon when a Direct3D device is lost and loadCg programs every Direct3D device reset with cgD3D9UnloadProgram. */createCgPrograms();firstTime = 0;}/* false below means "no parameter shadowing" */cgD3D9LoadProgram(myCgVertexProgram, false, 0);checkForCgError("loading vertex program");return initVertexBuffer(pd3dDevice);
}//--------------------------------------------------------------------------------------
// Handle updates to the scene.  This is called regardless of which D3D API is used
//--------------------------------------------------------------------------------------
void CALLBACK OnFrameMove( double fTime, float fElapsedTime, void* pUserContext )
{
}//--------------------------------------------------------------------------------------
// Render the scene using the D3D9 device
//--------------------------------------------------------------------------------------
void CALLBACK OnD3D9FrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
{HRESULT hr;// Clear the render target and the zbuffer V( pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB( 0, 45, 50, 170 ), 1.0f, 0 ) );// Render the sceneif( SUCCEEDED( pd3dDevice->BeginScene() ) ){V(g_control_dlg.OnRender(fElapsedTime));cgD3D9BindProgram(myCgVertexProgram);checkForCgError("binding vertex program");/* Render the triangle. */pd3dDevice->SetStreamSource(0, myVertexBuffer, 0, sizeof(MY_V3F));pd3dDevice->SetFVF(D3DFVF_XYZ);cgSetParameter1f(myCgVertexParam_r, (float)r/255);cgSetParameter1f(myCgVertexParam_g, (float)g/255);cgSetParameter1f(myCgVertexParam_b, (float)b/255);cgUpdateProgramParameters(myCgVertexProgram);pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);V( pd3dDevice->EndScene() );}
}//--------------------------------------------------------------------------------------
// Handle messages to the application
//--------------------------------------------------------------------------------------
LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam,bool* pbNoFurtherProcessing, void* pUserContext )
{*pbNoFurtherProcessing = g_dlg_resource_manager.MsgProc(hWnd, uMsg, wParam, lParam);if(*pbNoFurtherProcessing)return 0;*pbNoFurtherProcessing = g_control_dlg.MsgProc(hWnd, uMsg, wParam, lParam);if(*pbNoFurtherProcessing)return 0;return 0;
}//--------------------------------------------------------------------------------------
// Release D3D9 resources created in the OnD3D9ResetDevice callback
//--------------------------------------------------------------------------------------
void CALLBACK OnD3D9LostDevice( void* pUserContext )
{g_dlg_resource_manager.OnD3D9LostDevice();myVertexBuffer->Release();cgD3D9SetDevice(NULL);
}//--------------------------------------------------------------------------------------
// Release D3D9 resources created in the OnD3D9CreateDevice callback
//--------------------------------------------------------------------------------------
void CALLBACK OnD3D9DestroyDevice( void* pUserContext )
{g_dlg_resource_manager.OnD3D9DestroyDevice();
}
void CALLBACK OnGUIEvent(UINT event, int control_id, CDXUTControl* control, void* user_context)
{switch(control_id){case IDC_SLIDER_RED:{r = g_control_dlg.GetSlider(IDC_SLIDER_RED)->GetValue();CDXUTStatic* pStatic = g_control_dlg.GetStatic(IDC_STATIC_RED);if(pStatic){WCHAR wszText[128];swprintf_s( wszText, 128, L"R:%3d", r );pStatic->SetText( wszText );}break;}case IDC_SLIDER_GREEN:{g = g_control_dlg.GetSlider(IDC_SLIDER_GREEN)->GetValue();CDXUTStatic* pStatic = g_control_dlg.GetStatic(IDC_STATIC_GREEN);if(pStatic){WCHAR wszText[128];swprintf_s( wszText, 128, L"G:%3d", g );pStatic->SetText( wszText );}break;}case IDC_SLIDER_BLUE:b = g_control_dlg.GetSlider(IDC_SLIDER_BLUE)->GetValue();CDXUTStatic* pStatic = g_control_dlg.GetStatic(IDC_STATIC_BLUE);if(pStatic){WCHAR wszText[128];swprintf_s( wszText, 128, L"B:%3d", b );pStatic->SetText( wszText );}break;}
}
void InitDialogs()
{g_control_dlg.Init(&g_dlg_resource_manager);int  y = 10,  height = 22;g_control_dlg.SetCallback(OnGUIEvent);g_control_dlg.AddStatic(IDC_STATIC_RED,L"R:100",10, y+24, 30, height);g_control_dlg.AddSlider(IDC_SLIDER_RED, 50, y += 24, 100, height);g_control_dlg.GetSlider(IDC_SLIDER_RED)->SetRange(0, 255);g_control_dlg.GetSlider(IDC_SLIDER_RED)->SetValue(100);r = 100;g_control_dlg.AddStatic(IDC_STATIC_GREEN,L"G:100",10, y+24, 30, height);g_control_dlg.AddSlider(IDC_SLIDER_GREEN, 50, y += 24, 100, height);g_control_dlg.GetSlider(IDC_SLIDER_GREEN)->SetRange(0, 255);g_control_dlg.GetSlider(IDC_SLIDER_GREEN)->SetValue(100);g = 100;g_control_dlg.AddStatic(IDC_STATIC_BLUE,L"B:100",10, y+24, 30, height);g_control_dlg.AddSlider(IDC_SLIDER_BLUE, 50, y += 24, 100, height);g_control_dlg.GetSlider(IDC_SLIDER_BLUE)->SetRange(0, 255);g_control_dlg.GetSlider(IDC_SLIDER_BLUE)->SetValue(100);b = 100;}
//--------------------------------------------------------------------------------------
// Initialize everything and go into a render loop
//--------------------------------------------------------------------------------------
INT WINAPI wWinMain( HINSTANCE, HINSTANCE, LPWSTR, int )
{// Enable run-time memory check for debug builds.
#if defined(DEBUG) | defined(_DEBUG)_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endifmyCgContext = cgCreateContext();//创建环境checkForCgError("creating context");cgSetParameterSettingMode(myCgContext, CG_DEFERRED_PARAMETER_SETTING);/*CG_IMMEDIATE_PARAMETER_SETTING Non-erroneous cgSetParameter commands immediately update the corresponding 3D API parameter. This is the default mode. CG_DEFERRED_PARAMETER_SETTING Non-erroneous cgSetParameter commands record the updated parameter value but do not immediately update the corresponding 3D API parameter.These updates will happen during the next program bind. The updates can be explicitly forced to occur by using cgUpdateProgramParameters or cgUpdatePassParameters. */// Set the callback functionsDXUTSetCallbackD3D9DeviceAcceptable( IsD3D9DeviceAcceptable );DXUTSetCallbackD3D9DeviceCreated( OnD3D9CreateDevice );DXUTSetCallbackD3D9DeviceReset( OnD3D9ResetDevice );DXUTSetCallbackD3D9FrameRender( OnD3D9FrameRender );DXUTSetCallbackD3D9DeviceLost( OnD3D9LostDevice );DXUTSetCallbackD3D9DeviceDestroyed( OnD3D9DestroyDevice );DXUTSetCallbackDeviceChanging( ModifyDeviceSettings );DXUTSetCallbackMsgProc( MsgProc );DXUTSetCallbackFrameMove( OnFrameMove );// TODO: Perform any application-level initialization hereInitDialogs();// Initialize DXUT and create the desired Win32 window and Direct3D device for the applicationDXUTInit( true, true ); // Parse the command line and show msgboxesDXUTSetHotkeyHandling( true, true, true );  // handle the default hotkeysDXUTSetCursorSettings( true, true ); // Show the cursor and clip it when in full screenDXUTCreateWindow( L"control_color_by_using_CG_DXUT(june_2010)_D3D9" );DXUTCreateDevice( true, 400, 400 );// Start the render loopDXUTMainLoop();// TODO: Perform any application-level cleanup herecgDestroyProgram(myCgVertexProgram);checkForCgError("destroying vertex program");cgDestroyContext(myCgContext);checkForCgError("destroying context");return DXUTGetExitCode();
}

对应的cg文件

/*--------------------------------------------------------------------------myVertex.cg -- the vertex shader controling the color of the triangle  (c) Seamanj.2013/7/10
--------------------------------------------------------------------------*/
struct Output {float4 position : POSITION;float3 color    : COLOR;
};Output myVertexEntry(float2 position : POSITION, uniform float r, uniform float g, uniform float b)
{   Output OUT;OUT.position = float4(position,0,1);OUT.color = float3(r,g,b);return OUT;
}

最终的运行效果:

可以通过三个滚动条改变三角形的颜色

OK,睡觉,明天任务搞个HLSL版本出来,另外<<Pro OGRE 3D Programming>>第4章搞定,OGRE的学习正式提上日程,争取月底OGRE毕业,到时候专心研究Hydrax源码.不然太对不起老何了!

DXUT实战1:CG+D3D9+DXUT(june_2010)相关推荐

  1. DXUT实战3:HLSL(withEffect)+D3D9+DXUT(june_2010) . .

    好了,这是DXUT的最后一个实战,接下来我可能会学习下NVDIA FX Composer 2.5以及Shader Debugger,另外准备每周至少一个shader吧,今天任务把<<Pro ...

  2. DXUT实战2:HLSL(withoutEffect)+D3D9+DXUT(june_2010) .

    不好意思,昨天<<Pro OGRE 3D Programming>>第4章没有搞定,本来想晚上搞出来,一起写博客的,但是最终还是睡觉去了,看来晚上真心不适合学新知识,还是写博客 ...

  3. 一天搞定DXUT三步曲之一:DXUT框架

    当初学GL的时候, 一直想写那个HDR的SHADER程序,苦于没有框架,再加上GL的例子太少,最后果断踏上了DX这条不归路.花了一天时间(准确的说是第一天的上午和第二天的下午)把DXUT的框架学习了, ...

  4. baidu luaplus luabind

    luaplus 介绍LuaPlus: 好用的Lua For C++扩展 - 沐枫小筑(C++) - C... LuaPlus是Lua的C++增强,也就是说,LuaPlus本身就是在Lua的源码上进行增 ...

  5. DXUT扩展之摄像机

    最近需要用DXUT写一个球形环境映射,需要用到摄像机,然而原来的三步曲没有涉及到摄像机,所以这里专门补充下摄像机的用法 /*-------------------------------------- ...

  6. 一天搞定DXUT三步曲之二:添加文本

    添加文本比较简单,自己看源码吧,我就不多说了 //--------------------------------------------------------------------------- ...

  7. DXUT框架剖析(5)

    本文版权归博客园 lovedday 所有,转载请详细标明原创作者及原文出处,以示尊重! 原创作者: lovedday  原文出处:DXUT框架剖析(5) 修改可用的设备 应用程序可以通过DXUTSet ...

  8. DXUT框架剖析(4)

    本文版权归博客园 lovedday 所有,转载请详细标明原创作者及原文出处,以示尊重! 原创作者: lovedday  原文出处:DXUT框架剖析(4) 创建一个设备 通常可以用标准的Direct3D ...

  9. DXUT框架剖析(1)

    本文版权归博客园 lovedday 所有,转载请详细标明原创作者及原文出处,以示尊重! 原创作者: lovedday 原文出处:DXUT框架剖析(1) DXUT(也称sample framework) ...

最新文章

  1. 【我的Android进阶之旅】解决SDK升级到27.0.3遇到的GLIBC_2.14 not found、no acceptable C compiler found in $PATH等问题...
  2. Log4j2的性能为什么这么好?
  3. HDFS文件系统基本文件命令、编程读写HDFS
  4. C语言实现变步长求积分算法
  5. 多线程:happens-before原则
  6. wxWidgets:wxCalendarCtrl 示例
  7. GetAdaptersInfo获取MAC地址
  8. 李宏毅机器学习(二)自注意力机制
  9. ETL异构数据源Datax_限速设置_06
  10. 支付宝小程序组件库开发之手写板组件
  11. 60-40-030-序列化-传统Avro序列化
  12. oracle until freed,ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法...
  13. C#通过Socket在网络间发送和接收图片的演示源码
  14. 进入32位保护模式之路
  15. 谷歌个性化地图瓦片_对Google广告个性化的调查
  16. Reporter对象的几个鲜为人知的方法
  17. Model of an Electric Arc for Circuit Analysis(翻译)
  18. Java是什么,Java是什么意思
  19. c语言 main()可否省略,main函数中省略返回语句
  20. 270天不回家的“空中飞人们” 下一步要去哪里?

热门文章

  1. C++用户自定义转换(User-Defined Conversion)
  2. 机器视觉:PCI和PCI-E总线简介
  3. 重新配对_最容易旧情复燃的星座配对,念念不忘,重新在一起
  4. 不了解这些“高级货”,活该你面试当炮灰。。。【石杉的架构笔记】
  5. react中父子组件数据传递,子组件之间的数据传递
  6. 芈珺:iOS自动化测试工具总览
  7. 回到顶部 jquery
  8. 医疗信息化 医学信息 医院管理 资料下载
  9. 联想小新增加固态硬盘后安装不了系统_4千价位也能面面俱到?小新Air14 2020锐龙版体验测试...
  10. 关于Android的自动化测试,你需要了解的5个测试框架