MFC实现三维图形绘制(6)纹理实现
程序员文章站
2022-06-11 10:46:38
...
将图片贴到物体上,可以通过颜色映射来实现,即将纹理的颜色作为材质的漫反射光反射率和环境光反射率。
material->SetDiffuse(tf.c);//用纹理颜色作为材质的漫反射光反射率
material->SetAmbient(tf.c);//用纹理颜色作为材质的环境光反射率
为了使物体大小与纹理大小像贴合,用CT2类保存面上各顶点相对于纹理图像的坐标,在绘图时通过线性插值的方式实现一一对应。
#include"RGB.h"
class CT2
{
public:
CT2(void);
virtual ~CT2(void);
CT2(double,double);
friend CT2 operator +(const CT2 &,const CT2 &);//运算符重载
friend CT2 operator -(const CT2 &,const CT2 &);
friend CT2 operator *(const CT2 &,double);
friend CT2 operator *(double,const CT2 &);
friend CT2 operator /(const CT2 &,double);
friend CT2 operator+=(CT2 &,CT2 &);
friend CT2 operator-=(CT2 &,CT2 &);
friend CT2 operator*=(CT2 &,double);
friend CT2 operator/=(CT2 &,double);
public:
double u;
double v;
CRGB c;
};
CT2 CFill::Interpolation(double m, double m0, double m1, CT2 T0, CT2 T1)//纹理地址线性插值
{
CT2 Texture;
Texture = (m1 - m) / (m1 - m0) * T0 + (m - m0) / (m1 - m0) * T1;
return Texture;
}
创建CTexture类保存纹理图像
class CTexture
{
public:
CTexture(void);
virtual~CTexture(void);
void GetImage(UINT nIDResource);//准备位图
void DeleteObject(void);//释放位图
public:
BYTE* image;
BITMAP bmp;//BITMAP结构体变量
};
#include "Texture.h"
CTexture::CTexture(void)
{
image = NULL;
}
CTexture::~CTexture(void)
{
}
void CTexture::GetImage(UINT nIDResource)//准备位图
{
CBitmap NewBitmap;
NewBitmap.LoadBitmap(nIDResource);
NewBitmap.GetBitmap(&bmp);//将CBitmap的信息保存到Bitmap结构体中
int nbytesize = bmp.bmWidthBytes*bmp.bmHeight;
image = new BYTE[nbytesize];
NewBitmap.GetBitmapBits(nbytesize, (LPVOID)image);
}
void CTexture::DeleteObject(void)//释放位图
{
if(NULL != image)
delete []image;
}
若要实现纹理的凹凸,则加入凹凸算法:
CRGB frontU = GetTextureColor(ROUND(tf.u - 1), ROUND(tf.v), pTexture);
CRGB backU = GetTextureColor(ROUND(tf.u + 1), ROUND(tf.v), pTexture);
double Bu = (frontU.blue - backU.blue) / 2;
CRGB frontV = GetTextureColor(ROUND(tf.u), ROUND(tf.v - 1), pTexture);
CRGB backV = GetTextureColor(ROUND(tf.u), ROUND(tf.v + 1), pTexture);
double Bv = (frontV.blue - backV.blue) / 2;
CVector3 DNormal = CVector3(Bu, Bv, 0);
double BumpScale = 100.0;
vf = vf + BumpScale * DNormal;
vf = vf.Normalize();
上一篇: django项目部署
下一篇: 深夜发帖,有些灵异