以文本方式查看主题 - 计算机科学论坛 (http://bbs.xml.org.cn/index.asp) -- 『 C/C++编程思想 』 (http://bbs.xml.org.cn/list.asp?boardid=61) ---- 高级纹理映射技术(3) (http://bbs.xml.org.cn/dispbbs.asp?boardid=61&rootid=&id=70238) |
-- 作者:卷积内核 -- 发布时间:12/8/2008 3:24:00 PM -- 高级纹理映射技术(3) 对一些特殊的应用需要对纹理坐标进行处理,主要包括纹理坐标自动生成和纹理坐标变换。下图显示了纹理坐标的来源、处理过程以及到达光栅处理器的过程。
纹理坐标自动生成 在Direct3D程序中,不仅可以在模型载入阶段或渲染阶段指定物体的纹理坐标,还可以通过Direct3D渲染引擎自动生成纹理坐标,用于诸如环境映射等特殊的视觉效果。与手动设置纹理坐标相比,纹理坐标自动生成在Direct3D坐标变换和光照流水线中完成,执行速度更快。 Direct3D系统可以使用经过变换的摄像机空间顶点位置坐标、法线信息来生成纹理坐标。如果使用纹理坐标自动生成,那么在顶点中就可以不用包含纹理坐标数据,从而可以降低图形渲染时的数据传输量。纹理坐标自动生成主要用于产生一些特殊效果,在大多数情况下还是手工为每个顶点指定纹理坐标。 通过调用SetTextureStageState()并将第二个参数设置为D3DTSS_TEXCOORDINDEX来控制Direct3D系统如何自动生成纹理坐标。 D3DTSS_TEXCOORDINDEX Additionally, applications can include, as logical OR with the index being set, one of the constants to request that Direct3D automatically generate the input texture coordinates for a texture transformation. For a list of all the constants, see D3DTSS_TCI. With the exception of D3DTSS_TCI_PASSTHRU, which resolves to zero, if any of the following values is included with the index being set, the system uses the index strictly to determine texture wrapping mode. These flags are most useful when performing environment mapping. 其中第三个参数可以设为下列列表中的成员: D3DTSS_TCI #define Value Description These constants are used by D3DTSS_TEXCOORDINDEX. D3DTSS_TEXCOORDINDEX用于指定特定纹理层使用顶点中的第几组纹理坐标,但如果指定了上表中的成员值,Direct3D将忽略顶点中的纹理坐标,转而使用自动生成的纹理坐标。 D3DTSS_TEXTURETRANSFORMFLAGS用来控制生成的纹理坐标的输出,在大多数情况下纹理坐标是二维的,即将D3DTSS_TEXTURETRANSFORMFLAGS设置为D3DTTFF_COUNT2。但当绘制线段或三维纹理时,纹理坐标可能是一维或三维的。 D3DTSS_TEXTURETRANSFORMFLAGS Defines texture coordinate transformation values. typedef enum D3DTEXTURETRANSFORMFLAGS{ D3DTTFF_DISABLE = 0, D3DTTFF_COUNT1 = 1, D3DTTFF_COUNT2 = 2, D3DTTFF_COUNT3 = 3, D3DTTFF_COUNT4 = 4, D3DTTFF_PROJECTED = 256, D3DTTFF_FORCE_DWORD = 0x7fffffff,} D3DTEXTURETRANSFORMFLAGS, *LPD3DTEXTURETRANSFORMFLAGS; 首先,我们定义顶点结构和格式: struct sCustomVertex{ float x, y, z;}; // create vertex buffer and fill data // create texture coordinate using vertex position in camera space 运行效果: 若设置D3DTSS_TEXCOORDINDEX为以下D3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR,则效果为: 若设置D3DTSS_TEXCOORDINDEX为以下D3DTSS_TCI_SPHEREMAP,则效果为: |
-- 作者:卷积内核 -- 发布时间:12/8/2008 3:25:00 PM -- 主程序: #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 const D3DXCOLOR FONT_COLOR(0.55f, 0.85f, 0.65f, 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"texture.jpg", &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 pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE); // set texture color blend method, disalbe alpha blend. pd3dDevice->SetTexture(0, g_texture); // create texture coordinate using vertex position in camera space 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();
[B][URL=http://www.cppblog.com/Files/changingnow/TexCoordGenerate.rar]下载示例工程[/URL][/B] |
-- 作者:卷积内核 -- 发布时间:12/8/2008 3:26:00 PM -- 纹理坐标变换 Direct3D提供了对生成的纹理坐标进行坐标变换的功能,与顶点坐标变换相类似,可以指定一个4x4的纹理坐标变换矩阵,把它与生成的纹理坐标相乘,然后将变换之后的纹理坐标输出至Direct3D渲染流水线。使用纹理坐标变换可以对纹理坐标进行诸如平移、旋转和缩放等三维变换。纹理坐标变换对于生成一些特殊效果是非常有用的,它不用直接修改顶点的纹理坐标。例如可以通过一个简单的平移矩阵对纹理坐标进行变换,从而使物体表面上的纹理不断变换位置,产生动画效果。纹理坐标自动生成在三维图形程序中最广泛的应用是环境映射。 可通过函数IDirect3DDevice9::SetTransform()来设置4x4的纹理坐标变换矩阵,它以D3DTS_TEXTURE0~ D3DTS_TEXTURE7作为第一个参数,表示设置纹理层0~7的纹理矩阵。下列代码对纹理层0设置了一个将纹理坐标u、v缩小到原来一半的纹理矩阵: D3DXMATRIX mat; 下面的代码将原来的纹理坐标平移(1.0, 1.0, 0)个单位。 D3DXMATRIX mat; 示例程序通过下列代码对自动生成的纹理坐标进行变换: // texture coordinate transform D3DXMATRIX mat_texture, mat_scale, mat_trans; D3DXMatrixIdentity(&mat_texture); mat_texture = mat_texture * mat_scale * mat_trans;
运行效果图:
主程序: #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_DIFFUSE) const D3DXCOLOR FONT_COLOR(1.0f, 0.5f, 0.25f, 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"texture.jpg", &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 pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE); // set texture color blend method, disalbe alpha blend. pd3dDevice->SetTexture(0, g_texture); // create texture coordinate using vertex position in camera space // texture coordinate transform D3DXMATRIX mat_texture, mat_scale, mat_trans; D3DXMatrixIdentity(&mat_texture); mat_texture = mat_texture * mat_scale * mat_trans; 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();
[B][URL=http://www.cppblog.com/Files/changingnow/TexCoordTransform.rar]下载示例工程[/URL][/B] |
-- 作者:卷积内核 -- 发布时间:12/8/2008 3:27:00 PM -- 三维场景中的物体不仅受光照影响,而且受周围环境的影响,可以映射出周围环境的图像,环境映射就是模拟物体光滑表面映射周围环境的一种技术。实际上,就是将一幅包含物体周围环境场景的纹理贴到物体表面上,这样就可以在一定程度上模拟出物体对周围环境的映射,而无须使用像光线跟踪算法这种复杂的计算技术。 在图形工业中有多种环境映射,下面介绍两种最常用的环境映射:立方体环境映射和球形环境映射。
立方体环境映射 立方体环境映射图有时又称为立方体映射图,是一组包含物体周围环境图像的纹理贴图,好像物体在立方体的中心。立方体环境图的每个面覆盖水平和垂直方向上各90度视角区域,一共6个面,各面的方向如下图所示: 立方体环境映射图的6个面布局如下图所示:
球形环境映射 球形环境映射图(或称为球形映射图)和立方体环境映射图类似,也是一幅包含周围场景图像的特殊纹理。和立方体环境映射图不同的是,球形环境映射图不直接代表物体周围的环境。球形映射图就好像通过鱼眼(fish-eye)凸透镜观察到的景象一样,是一个物体周围环境360度全方位视域的三维表现。 首先使用一张球形的背景图作为环境映射纹理,如下所示: 接着在回调函数OnCreateDevice()中加载纹理和网格模型: V_RETURN(D3DXCreateTextureFromFile(pd3dDevice, L"spheremap.bmp", &g_env_texture)); 接着在回调函数OnResetDevice()中自动生成纹理坐标,并设置纹理阶段颜色混合方法: // set texture color blend method, disalbe alpha blend. pd3dDevice->SetTexture(0, g_env_texture); pd3dDevice->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_SPHEREMAP); 最后在回调函数OnFrameRender()中渲染茶壶: // Clear the render target and the zbuffer V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(0, 0, 0, 0), 1.0f, 0) ); 球形环境纹理映射的实现比较简单,和普通纹理映射基本相同,唯一的区别是使用自动生成的球形纹理坐标。 |
-- 作者:卷积内核 -- 发布时间:12/8/2008 3:27:00 PM -- 主程序: #pragma warning(disable : 4127 4995 4996) #define release_com(p) do { if(p) { (p)->Release(); (p) = NULL; } } while(0) #define IDC_TOGGLE_FULLSCREEN 1 const D3DXCOLOR FONT_COLOR(1.0f, 0.5f, 0.25f, 1.0f); ID3DXFont* g_font; CDXUTDialogResourceManager g_dlg_resource_manager; ID3DXMesh* g_mesh; //-------------------------------------------------------------------------------------- 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"spheremap.bmp", &g_env_texture)); V_RETURN(D3DXLoadMeshFromXW(L"teapot.x", D3DXMESH_MANAGED, pd3dDevice, NULL, &material_buffer, NULL, D3DXMATERIAL* xmaterials = (D3DXMATERIAL*) material_buffer->GetBufferPointer(); for(DWORD i = 0; i < g_num_materials; i++) // .x file do not save ambient data, so set it here. material_buffer->Release(); 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 material D3DMATERIAL9 material; material.Diffuse.r = material.Ambient.r = 1.0f; pd3dDevice->SetMaterial(&material); // setup light D3DLIGHT9 light; light.Type = D3DLIGHT_DIRECTIONAL; D3DXVECTOR3 light_dir(10.0f, -10.0f, -10.0f); pd3dDevice->SetRenderState(D3DRS_AMBIENT, 0x00FF8080); // set texture color blend method, disalbe alpha blend. pd3dDevice->SetTexture(0, g_env_texture); return S_OK; //-------------------------------------------------------------------------------------- release_com(g_text_sprite); delete[] g_mesh_materials; release_com(g_font); //-------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------- // show frame and device states // show helper information if(g_show_help) text_helper.End(); //-------------------------------------------------------------------------------------- if(g_settings_dlg.IsActive()) float time = timeGetTime() / 1000.0f; D3DXMATRIX mat_world; // 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/SphericalEnvMapping.rar]下载示例工程[/URL][/B] |
W 3 C h i n a ( since 2003 ) 旗 下 站 点 苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》 |
5,972.656ms |