以文本方式查看主题 - 计算机科学论坛 (http://bbs.xml.org.cn/index.asp) -- 『 C/C++编程思想 』 (http://bbs.xml.org.cn/list.asp?boardid=61) ---- 高级纹理映射技术(2) (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=70034) |
-- 作者:卷积内核 -- 发布时间:12/3/2008 8:29:00 AM -- 主程序: #pragma warning(disable : 4127 4995 4996) #define release_com(p) do { if(p) { (p)->Release(); (p) = NULL; } } while(0) struct sCustomVertex #define D3DFVF_CUSTOM_VERTEX (D3DFVF_XYZ | D3DFVF_TEX1) #define IDC_TOGGLE_FULLSCREEN 1 #define BLEND_DARK 1 #define BLEND_INFO_DARK L"Dark Blend" const D3DXCOLOR FONT_COLOR(0.85f, 0.85f, 0.85f, 1.0f); CDXUTDialogResourceManager g_dlg_resource_manager; IDirect3DVertexBuffer9* g_vertex_buffer; ID3DXFont* g_font; //-------------------------------------------------------------------------------------- IDirect3D9* pD3D = DXUTGetD3DObject(); if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType, AdapterFormat, // check whether device support multi textures render return true; static bool is_first_time = true; if(is_first_time) // if using reference device, then pop a warning message box. return true; V_RETURN(g_dlg_resource_manager.OnCreateDevice(pd3dDevice)); D3DXCreateFont(pd3dDevice, 18, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, V_RETURN(D3DXCreateTextureFromFile(pd3dDevice, L"wall.bmp", &g_base_texture)); // create vertex buffer and fill data sCustomVertex vertices[] = pd3dDevice->CreateVertexBuffer(sizeof(vertices), 0, D3DFVF_CUSTOM_VERTEX, D3DPOOL_MANAGED, &g_vertex_buffer, NULL); void* ptr; return S_OK; V_RETURN(g_dlg_resource_manager.OnResetDevice()); // set dialog position and size g_button_dlg.SetLocation(pBackBufferSurfaceDesc->Width - 170, 0); // setup view matrix D3DXMATRIX mat_view; D3DXMatrixLookAtLH(&mat_view, &eye, &at, &up); // set projection matrix // setup light ZeroMemory(&g_light, sizeof(D3DLIGHT9)); g_light.Type = D3DLIGHT_DIRECTIONAL; D3DXVECTOR3 light_dir(0, 0, 10); // setup material ZeroMemory(&g_material, sizeof(D3DMATERIAL9)); g_material.Ambient.r = 1.0f; // set texture sample method return S_OK; //-------------------------------------------------------------------------------------- release_com(g_text_sprite); release_com(g_font); //-------------------------------------------------------------------------------------- if(g_blend_flag == BLEND_DARK) pd3dDevice->SetTexture(1, g_dark_texture); wcscpy(g_blend_info, BLEND_INFO_DARK); pd3dDevice->SetTexture(1, g_dark_texture); static double last_time = 0.0f; if(interval < 0.5f) wcscpy(g_blend_info, BLEND_INFO_DARK_ANIM); pd3dDevice->SetTexture(0, g_base_texture); pd3dDevice->SetTexture(0, g_base_texture); wcscpy(g_blend_info, BLEND_INFO_DARK_DIFFUSE); pd3dDevice->SetTexture(1, g_dark_texture); wcscpy(g_blend_info, BLEND_INFO_GLOW); pd3dDevice->SetTexture(1, g_detail_texture); hr = pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_ADDSIGNED); if(FAILED(hr)) wcscpy(g_blend_info, BLEND_INFO_DETAIL); //-------------------------------------------------------------------------------------- // show frame and device states // show other simple information // show helper information if(g_show_help) text_helper.DrawTextLine(L""); text_helper.End(); //-------------------------------------------------------------------------------------- if(g_settings_dlg.IsActive()) // Clear the render target and the zbuffer // Render the scene RenderText(); V(g_button_dlg.OnRender(fElapsedTime)); V( pd3dDevice->EndScene() ); if(g_settings_dlg.IsActive()) *pbNoFurtherProcessing = g_button_dlg.MsgProc(hWnd, uMsg, wParam, lParam); return 0; //-------------------------------------------------------------------------------------- case IDC_TOGGLE_REF: case IDC_CHANGE_DEVICE: //-------------------------------------------------------------------------------------- g_button_dlg.SetCallback(OnGUIEvent); int x = 35, y = 10, width = 125, height = 22; g_button_dlg.AddButton(IDC_TOGGLE_FULLSCREEN, L"Toggle full screen", x, y, width, height); //-------------------------------------------------------------------------------------- // Set the callback functions // Initialize DXUT and create the desired Win32 window and Direct3D device for the application // Start the render loop // TODO: Perform any application-level cleanup here return DXUTGetExitCode();
[B][URL=http://www.cppblog.com/Files/changingnow/TextureColorBlend.rar]下载示例工程[/URL][/B] |
-- 作者:卷积内核 -- 发布时间:12/3/2008 8:30:00 AM -- 高级纹理映射技术(5) Alpha混合操作 设置alpha操作的语句大致如下: // i为纹理层序号 参数op是属于枚举类型D3DTEXTUREOP的常量。和纹理颜色混合操作相同,纹理Alpha操作最多可接受三个颜色参数:D3DTSS_ALPHAARG0、D3DTSS_ALPHAARG1、D3DTSS_ALPHAARG2。 Direct3D在渲染一个场景时,它可以结合几种来源的颜色信息:顶点、当前材质、纹理贴图、先前写入渲染目标的颜色信息,然后将其中的一些颜色混合起来。同时也可以使用Alpha来指定Direct3D该以怎样的权重混合这些颜色,Alpha信息可以存储在顶点中、材质中、纹理贴图中。Alpha值为0表示完全透明,Alpha值为1表示不透明,其余0~1之间的值表示不同程度的透明。 如果要从一张纹理中获取Alpha值,应将D3DTA_TEXTURE作为Alpha参数。 如果要使用来自顶点中的Alpha值,应将D3DTA_DIFFUSE作为Alpha参数,并确保渲染状态D3DRS_DIFFUSEMATERIALSOURCE被设置为D3DMCS_COLOR1(这也是默认状态)。 如果要使用来自材质中的Alpha值,应将D3DTA_DIFFUSE作为Alpha参数,并确保渲染状态D3DRS_DIFFUSEMATERIALSOURCE被设置为D3DMCS_MATERIAL。 如果未用SetRenderState()设置D3DRS_DIFFUSEMATERIALSOURCE参数,则从默认来源(即顶点)获取漫反射颜色。 示例程序中将纹理颜色和Alpha混合的代码如下所示: // set texture color and alpha blend method pd3dDevice->SetTexture(0, g_texture); pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); Alpha操作从纹理中获取Alpha值,因为用到了Alpha混合,所以在上面的代码中还需要启用Alpha混合并设置Alpha混合系数。
运行效果: |
-- 作者:卷积内核 -- 发布时间:12/3/2008 8:30:00 AM -- 主程序: #pragma warning(disable : 4127 4995 4996) #define release_com(p) do { if(p) { (p)->Release(); (p) = NULL; } } while(0) #define IDC_TOGGLE_FULLSCREEN 1 struct sCustomVertex #define D3DFVF_CUSTOM_VERTEX (D3DFVF_XYZ | D3DFVF_TEX1) const D3DXCOLOR FONT_COLOR(0.85f, 0.85f, 0.85f, 1.0f); CDXUTDialogResourceManager g_dlg_resource_manager; IDirect3DVertexBuffer9* g_vertex_buffer; ID3DXFont* g_font; //-------------------------------------------------------------------------------------- IDirect3D9* pD3D = DXUTGetD3DObject(); if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType, AdapterFormat, return true; static bool is_first_time = true; if(is_first_time) // if using reference device, then pop a warning message box. return true; V_RETURN(g_dlg_resource_manager.OnCreateDevice(pd3dDevice)); D3DXCreateFont(pd3dDevice, 18, 0, FW_BOLD, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, V_RETURN(D3DXCreateTextureFromFile(pd3dDevice, L"tree.tga", &g_texture)); sCustomVertex vertices[] = pd3dDevice->CreateVertexBuffer(sizeof(vertices), 0, D3DFVF_CUSTOM_VERTEX, D3DPOOL_MANAGED, &g_vertex_buffer, NULL); void* ptr; return S_OK; V_RETURN(g_dlg_resource_manager.OnResetDevice()); // set dialog position and size g_button_dlg.SetLocation(pBackBufferSurfaceDesc->Width - 170, 0); // setup world matrix // setup view matrix D3DXMATRIX mat_view; D3DXMatrixLookAtLH(&mat_view, &eye, &at, &up); // set projection matrix // setup material D3DMATERIAL9 material; material.Ambient.r = 1.0f; pd3dDevice->SetMaterial(&material); pd3dDevice->SetRenderState(D3DRS_AMBIENT, 0xFF00FF00); // set texture color and alpha blend method pd3dDevice->SetTexture(0, g_texture); pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); return S_OK; //-------------------------------------------------------------------------------------- release_com(g_text_sprite); release_com(g_font); //-------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------- // show frame and device states // show other simple information // show helper information if(g_show_help) text_helper.End(); //-------------------------------------------------------------------------------------- if(g_settings_dlg.IsActive()) // Clear the render target and the zbuffer // Render the scene RenderText(); V(g_button_dlg.OnRender(fElapsedTime)); V( pd3dDevice->EndScene() ); if(g_settings_dlg.IsActive()) *pbNoFurtherProcessing = g_button_dlg.MsgProc(hWnd, uMsg, wParam, lParam); return 0; //-------------------------------------------------------------------------------------- case IDC_TOGGLE_REF: case IDC_CHANGE_DEVICE: //-------------------------------------------------------------------------------------- g_button_dlg.SetCallback(OnGUIEvent); int x = 35, y = 10, width = 125, height = 22; g_button_dlg.AddButton(IDC_TOGGLE_FULLSCREEN, L"Toggle full screen", x, y, width, height); //-------------------------------------------------------------------------------------- // Set the callback functions // Initialize DXUT and create the desired Win32 window and Direct3D device for the application // Start the render loop // TODO: Perform any application-level cleanup here return DXUTGetExitCode(); |
W 3 C h i n a ( since 2003 ) 旗 下 站 点 苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》 |
172.363ms |