|

楼主 |
发表于 2011-5-30 19:06:00
|
显示全部楼层
Re: Re:如何以平铺方式渲染指定纹理坐标像素??
思月行云: Re:如何以平铺方式渲染指定纹理坐标像素??
轩辕兄为何不考虑直接用纹理采样器,改个设置外带渲6个顶点就好了,标准的D3D本身的机制嘛~
还是说我理解错了老兄的意思?
没听明白你的意思。
你知道windows的桌面壁纸的平铺和拉伸功能吧?我要做的正是这个平铺效果,并且我的条件是可以自由选择纹理坐标区域,而不是对整个纹理进行平铺。
我暂时用自己的代码实现了这个功能
- // 以平铺方式渲染指定纹理坐标像素到pDstRect区域
- void CYLTexture::FillRect(const RECT* pSrcRect, const RECT* pDstRect, DWORD color/*=0xffffffff*/)
- {
- if (!(m_State & TEXSTATE_LOADOK) || !(m_State & TEXSTATE_SHOW)) return;
- float width = float(pSrcRect->right - pSrcRect->left); // 纹理宽度
- float height = float(pSrcRect->bottom - pSrcRect->top); // 纹理高度
- float u_Left = pSrcRect->left/float(m_Width); // 纹理坐标left
- float v_Top = pSrcRect->top/float(m_Height); // 纹理坐标top
- float u_Right = pSrcRect->right/float(m_Width); // 纹理坐标right
- float v_Bottom = pSrcRect->bottom/float(m_Height); // 纹理坐标bottom
- int horizon_Count = int((pDstRect->right-pDstRect->left) / width); // 横向平铺数
- int vertical_Count = int((pDstRect->bottom-pDstRect->top) / height); // 纵向平铺数
- int spareWidth = (pDstRect->right-pDstRect->left) % int(width); // 余出宽度
- int spareHeight = (pDstRect->bottom-pDstRect->top) % int(height); // 余出高度
- horizon_Count += (spareWidth>0? 1 : 0);
- vertical_Count += (spareHeight>0? 1 : 0);
- float x = float(pDstRect->left); // 目标X位置
- float y = float(pDstRect->top); // 目标Y位置
- float dstWidth, dstHeight; // 目标宽高
- float dst_u_Right, dst_v_Bottom; // 目标纹理坐标right和bottom
- // 绘制
- for (int h=0, w=0; h<vertical_Count; h++, y+=height)
- {
- for (w=0, x=float(pDstRect->left); w<horizon_Count; w++, x+=width)
- {
- dstWidth = width;
- dstHeight = height;
- dst_u_Right = u_Right;
- dst_v_Bottom = v_Bottom;
- // 横向有余出部分
- if (w==horizon_Count-1 && spareWidth>0)
- {
- dstWidth = float(spareWidth);
- dst_u_Right = (pSrcRect->left+spareWidth)/float(m_Width);
- }
- // 纵向有余出部分
- if (h==vertical_Count-1 && spareHeight > 0)
- {
- dstHeight = float(spareHeight);
- dst_v_Bottom = (pSrcRect->top+spareHeight)/float(m_Height);
- }
- SVertex2D vertexData[] =
- {
- { float(x), float(y), 0, 1, color, u_Left, v_Top },
- { float(x+dstWidth), float(y), 0, 1, color, dst_u_Right, v_Top },
- { float(x+dstWidth), float(y+dstHeight), 0, 1, color, dst_u_Right, dst_v_Bottom },
- { float(x+dstWidth), float(y+dstHeight), 0, 1, color, dst_u_Right, dst_v_Bottom },
- { float(x), float(y+dstHeight), 0, 1, color, u_Left, dst_v_Bottom },
- { float(x), float(y), 0, 1, color, u_Left, v_Top }
- };
- // 移半个像
- for (int i=0; i<RECT_VERTEX_COUNT; i++)
- {
- vertexData[i].tu += 0.5f/m_Width;
- vertexData[i].tv += 0.5f/m_Height;
- }
- g_pRenderSystem->AddTextureVertex(m_Handle, vertexData, RECT_VERTEX_COUNT);
- }
- }
- }
复制代码 |
|