⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 image.cpp

📁 蒙特卡罗方法可以有效地解决复杂的工程问题
💻 CPP
字号:
// Image.cpp: implementation of the CImage class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "RayTrace.h"
#include "Image.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

/*************************************************************************
 *
 * 函数名称:
 *   CImage()
 *
 * 参数:
 *   CString BmpFileName         - 位图文件路径名
 *
 * 返回值:
 *   无
 *
 * 说明:
 *   构造函数.
 *	 从位图文件读入数据,ImageBuffer指向所读入的数据,位图文件路径由参数BmpFileName制定.
 *	 为类成员变量TextureBuffer申请内存空间
 *
 ************************************************************************/

CImage::CImage(CString BmpFileName)
{

	unsigned char *ImageBuffer = NULL;
	Bmp2Arry(BmpFileName,Height,Width,ImageBuffer);

	TextureBuffer = new double[3*Height*Width];

	if( !TextureBuffer )
	{
		AfxMessageBox("纹理用内存不足!");
		free(ImageBuffer);
		return;
	}

	if( !CreatTexture( ImageBuffer ) )
	{
		free(ImageBuffer);
		delete TextureBuffer;
		AfxMessageBox("纹理用内存不足!");
		return;
	}
	else
		free(ImageBuffer);
}

/*************************************************************************
 *
 * 函数名称:
 *   ~CImage()
 *
 * 参数:
 *   无
 *
 * 返回值:
 *   无
 *
 * 说明:
 *   析构函数.
 *	 释放类成员变量TextureBuffer所申请的内存空间
 *
 ************************************************************************/
CImage::~CImage()
{
	delete TextureBuffer;
}

/*************************************************************************
 *
 * 函数名称:
 *   CreatTexture()
 *
 * 参数:
 *   unsigned char * ImageBuffer         - 指向位图文件数据区的指针
 *
 * 返回值:
 *   BOOL								 - 创建成功返回TRUE,否则返回FALSE
 *
 * 说明:
 *   该函数由位图文件数据创建相应的纹理数据。
 *
 ************************************************************************/

BOOL CImage::CreatTexture(unsigned char * ImageBuffer)
{
	unsigned char col;
	int icol;
	double fcol;
	long BufferLength = 3*Height*Width - 1;

	for( int i = 0; i <= BufferLength; i++ )
	{
		col = *(ImageBuffer+i);
		icol = (int)col;
		fcol = (double)col/255.;
		*(TextureBuffer+i) = fcol;
	}
	return TRUE;
}

/*************************************************************************
 *
 * 函数名称:
 *   GetColor()
 *
 * 参数:
 *   double u,double v         - 纹理空间位置坐标
 *
 * 返回值:
 *   CVector				   - 景物空间对应点的RGB颜色值
 *
 * 说明:
 *   虚函数,实现二维纹理映射。
 *
 ************************************************************************/
CVector CImage::GetColor(double u,double v)
{
	double tempu,tempv;
	double uInt,vInt;
	double deltau,deltav;
	int uIndex,vIndex;
	double w00,w01,w10,w11;
	double red,grn,blu;

	tempu = u * (double)(Width-1);
	tempv = v * (double)(Height-1);
	uInt = floor(tempu);
	vInt = floor(tempv);
	deltau = tempu-uInt;
	deltav = tempv-vInt;
	uIndex = (int)uInt;
	vIndex = (int)vInt;

	w00 = ( 1.0 - deltau ) * ( 1.0 - deltav );
	w01 = ( 1.0 - deltau ) * deltav;
	w10 = deltau * ( 1.0 - deltav );
	w11 = deltau * deltav;

	long location00,location01,location10,location11;

	location00 = vIndex * Width * 3 + uIndex * 3;
	location10 = location00 + 3;
	location01 = ( vIndex + 1 ) * Width * 3 + uIndex * 3;
	location11 = location01 + 3;

	red = w00 * ( *( TextureBuffer + location00 ) )
		+ w01 * ( *( TextureBuffer + location01 ) )
		+ w10 * ( *( TextureBuffer + location10 ) )
		+ w11 * ( *( TextureBuffer + location11 ) );

	location00 += 1;
	location01 += 1;
	location10 += 1;
	location11 += 1;

	grn = w00 * ( *( TextureBuffer + location00 ) )
		+ w01 * ( *( TextureBuffer + location01 ) )
		+ w10 * ( *( TextureBuffer + location10 ) )
		+ w11 * ( *( TextureBuffer + location11 ) );

	location00 += 1;
	location01 += 1;
	location10 += 1;
	location11 += 1;

	blu = w00 * ( *( TextureBuffer + location00 ) )
		+ w01 * ( *( TextureBuffer + location01 ) )
		+ w10 * ( *( TextureBuffer + location10 ) )
		+ w11 * ( *( TextureBuffer + location11 ) );

	return CVector(red,grn,blu);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -