游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2737|回复: 5

如何以平铺方式渲染指定纹理坐标像素??

[复制链接]

67

主题

390

帖子

392

积分

中级会员

Rank: 3Rank: 3

积分
392
发表于 2011-5-24 23:37:00 | 显示全部楼层 |阅读模式
假如有个512×512的纹理,现在要将纹理坐标为 { 100, 100, 200, 200 }的内容以平铺的形式渲染到指定RECT区域,如何实现??
下面这段代码是将整个纹理以平铺形式渲染,但我现在只想将纹理坐标为 { 100, 100, 200, 200 }的内容渲染出来.为难了。

  1. float width = float(pDstRect->right - pDstRect->left);
  2. float height = float(pDstRect->bottom - pDstRect->top);
  3. float u_width = width/float(m_Width);
  4. float v_height = height/float(m_Height);

  5. SVertex2D vertexData[] =
  6. {
  7.         {       float(pDstRect->left), float(pDstRect->top), 0, 1, color, 0, 0 },
  8.         { float(pDstRect->left+width), float(pDstRect->top), 0, 1, color, u_width, 0 },
  9.         { float(pDstRect->left+width), float(pDstRect->top+height), 0, 1, color, u_width, v_height},

  10.         { float(pDstRect->left+width), float(pDstRect->top+height), 0, 1, color, u_width, v_height},
  11.         { float(pDstRect->left), float(pDstRect->top+height), 0, 1, color, 0, v_height },
  12.         { float(pDstRect->left), float(pDstRect->top), 0, 1, color, 0, 0 }
  13. };

  14. // 移半个像
  15. for (int i=0; i<RECT_VERTEX_COUNT; i++)
  16. {
  17.         vertexData[i].tu += 0.5f/m_Width;
  18.         vertexData[i].tv += 0.5f/m_Height;
  19. }
复制代码

1

主题

49

帖子

49

积分

注册会员

Rank: 2

积分
49
发表于 2011-5-25 16:42:00 | 显示全部楼层

Re:如何以平铺方式渲染纹理??

用软件做纹理取样做的事嘛..

67

主题

390

帖子

392

积分

中级会员

Rank: 3Rank: 3

积分
392
 楼主| 发表于 2011-5-25 17:10:00 | 显示全部楼层

Re: Re:如何以平铺方式渲染纹理??

??b我心: Re:如何以平铺方式渲染纹理??

用软件做纹理取样做的事嘛..


自己写自然容易,我是想知道D3D本身有没有这种功能,像纹理寻址模式好像就不行了,它也同样需要整个纹理坐标

18

主题

116

帖子

218

积分

中级会员

Rank: 3Rank: 3

积分
218
发表于 2011-5-30 17:43:00 | 显示全部楼层

Re:如何以平铺方式渲染指定纹理坐标像素??

轩辕兄为何不考虑直接用纹理采样器,改个设置外带渲6个顶点就好了,标准的D3D本身的机制嘛~
还是说我理解错了老兄的意思?

67

主题

390

帖子

392

积分

中级会员

Rank: 3Rank: 3

积分
392
 楼主| 发表于 2011-5-30 19:06:00 | 显示全部楼层

Re: Re:如何以平铺方式渲染指定纹理坐标像素??

思月行云: Re:如何以平铺方式渲染指定纹理坐标像素??

轩辕兄为何不考虑直接用纹理采样器,改个设置外带渲6个顶点就好了,标准的D3D本身的机制嘛~
还是说我理解错了老兄的意思?


没听明白你的意思。
你知道windows的桌面壁纸的平铺和拉伸功能吧?我要做的正是这个平铺效果,并且我的条件是可以自由选择纹理坐标区域,而不是对整个纹理进行平铺。
我暂时用自己的代码实现了这个功能

  1. // 以平铺方式渲染指定纹理坐标像素到pDstRect区域
  2. void CYLTexture::FillRect(const RECT* pSrcRect, const RECT* pDstRect, DWORD color/*=0xffffffff*/)
  3. {
  4.         if (!(m_State & TEXSTATE_LOADOK) || !(m_State & TEXSTATE_SHOW))        return;

  5.         float width = float(pSrcRect->right - pSrcRect->left);        // 纹理宽度
  6.         float height = float(pSrcRect->bottom - pSrcRect->top);        // 纹理高度
  7.         float u_Left = pSrcRect->left/float(m_Width);                        // 纹理坐标left
  8.         float v_Top = pSrcRect->top/float(m_Height);                        // 纹理坐标top
  9.         float u_Right = pSrcRect->right/float(m_Width);                        // 纹理坐标right
  10.         float v_Bottom = pSrcRect->bottom/float(m_Height);                // 纹理坐标bottom

  11.         int horizon_Count = int((pDstRect->right-pDstRect->left) / width);                // 横向平铺数
  12.         int vertical_Count = int((pDstRect->bottom-pDstRect->top) / height);        // 纵向平铺数
  13.         int spareWidth = (pDstRect->right-pDstRect->left) % int(width);                        // 余出宽度
  14.         int spareHeight = (pDstRect->bottom-pDstRect->top) % int(height);                // 余出高度
  15.         horizon_Count += (spareWidth>0? 1 : 0);
  16.         vertical_Count += (spareHeight>0? 1 : 0);

  17.         float x = float(pDstRect->left);        // 目标X位置
  18.         float y = float(pDstRect->top);                // 目标Y位置
  19.         float dstWidth, dstHeight;                        // 目标宽高
  20.         float dst_u_Right, dst_v_Bottom;        // 目标纹理坐标right和bottom

  21.         // 绘制
  22.         for (int h=0, w=0; h<vertical_Count; h++, y+=height)
  23.         {
  24.                 for (w=0, x=float(pDstRect->left); w<horizon_Count; w++, x+=width)
  25.                 {
  26.                         dstWidth = width;
  27.                         dstHeight = height;
  28.                         dst_u_Right = u_Right;
  29.                         dst_v_Bottom = v_Bottom;

  30.                         // 横向有余出部分
  31.                         if (w==horizon_Count-1 && spareWidth>0)
  32.                         {
  33.                                 dstWidth = float(spareWidth);
  34.                                 dst_u_Right = (pSrcRect->left+spareWidth)/float(m_Width);
  35.                         }
  36.                         // 纵向有余出部分
  37.                         if (h==vertical_Count-1 && spareHeight > 0)
  38.                         {
  39.                                 dstHeight = float(spareHeight);
  40.                                 dst_v_Bottom = (pSrcRect->top+spareHeight)/float(m_Height);
  41.                         }

  42.                         SVertex2D vertexData[] =
  43.                         {
  44.                                 {                float(x),                           float(y), 0, 1, color,           u_Left,                  v_Top        },
  45.                                 { float(x+dstWidth),                   float(y), 0, 1, color, dst_u_Right,                  v_Top        },
  46.                                 { float(x+dstWidth), float(y+dstHeight), 0, 1, color, dst_u_Right, dst_v_Bottom        },

  47.                                 { float(x+dstWidth), float(y+dstHeight), 0, 1, color, dst_u_Right, dst_v_Bottom        },
  48.                                 {                float(x),         float(y+dstHeight), 0, 1, color,           u_Left, dst_v_Bottom        },
  49.                                 {                float(x),                           float(y), 0, 1, color,           u_Left,                  v_Top        }
  50.                         };

  51.                         // 移半个像
  52.                         for (int i=0; i<RECT_VERTEX_COUNT; i++)
  53.                         {
  54.                                 vertexData[i].tu += 0.5f/m_Width;
  55.                                 vertexData[i].tv += 0.5f/m_Height;
  56.                         }

  57.                         g_pRenderSystem->AddTextureVertex(m_Handle, vertexData, RECT_VERTEX_COUNT);
  58.                 }
  59.         }
  60. }
复制代码

18

主题

116

帖子

218

积分

中级会员

Rank: 3Rank: 3

积分
218
发表于 2011-5-31 12:03:00 | 显示全部楼层

Re: Re: Re:如何以平铺方式渲染指定纹理坐标像素??

轩辕崇正: Re: Re:如何以平铺方式渲染指定纹理坐标像素??
没听明白你的意思。
你知道windows的桌面壁纸的平铺和拉伸功能吧?我要做的正是这个平铺效果,并且我的...

汗~是我理解错老兄的意图了~
不好意思~
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-8-10 16:25

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表