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

📄 perlinnoisedlg.cpp

📁 Perlin Noise with visual c++!
💻 CPP
字号:
// perlinNoiseDlg.cpp : implementation file
//

#include "stdafx.h"
#include "perlinNoise.h"
#include "perlinNoiseDlg.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define  Range   400


int noisex1[1000];
int noisey1[1000];
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPerlinNoiseDlg dialog

CPerlinNoiseDlg::CPerlinNoiseDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPerlinNoiseDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPerlinNoiseDlg)
	m_Persistence = 0.0f;
	m_Ocatves = 0;
	m_Step1 = 1;

	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	brush.CreateSolidBrush(RGB(0,0,0));
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPerlinNoiseDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPerlinNoiseDlg)
	DDX_Text(pDX, IDC_EDIT2, m_Persistence);
	DDV_MinMaxFloat(pDX, m_Persistence, 0.f, 1.f);
	DDX_Text(pDX, IDC_EDIT3, m_Ocatves);
	DDX_Text(pDX, IDC_EDIT4, m_Step1);
	DDV_MinMaxInt(pDX, m_Step1, 1, 20);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPerlinNoiseDlg, CDialog)
	//{{AFX_MSG_MAP(CPerlinNoiseDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_EN_UPDATE(IDC_EDIT2, OnUpdateEdit2)
	ON_EN_UPDATE(IDC_EDIT3, OnUpdateEdit3)
	ON_EN_UPDATE(IDC_EDIT4, OnUpdateEdit4)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPerlinNoiseDlg message handlers

BOOL CPerlinNoiseDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CPerlinNoiseDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CPerlinNoiseDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
   CClientDC  dc(this);
   CRect  rect;
   GetClientRect(rect);
   dc.SelectObject(brush);
   dc.FillSolidRect(rect.left ,rect.top ,rect.right-180,rect.bottom,RGB(255,255,255));
   dc.MoveTo(60,0);
   
   dc.LineTo(60,rect.bottom-30);
   dc.LineTo(rect.right-180,rect.bottom-30);
//方向符号的确立
   dc.MoveTo(60,0);
   dc.LineTo(30,20);
   dc.MoveTo(60,0);
   dc.LineTo(90,20);

   //把x轴进行等份划分
   int width;
   CString  str;

   width=rect.right-180-20;
   for (int  bb=60;bb<=width;bb+=m_Step1)
   {

       str.Format("%d",bb-60);
	   dc.MoveTo(bb,rect.bottom-30);
	   dc.LineTo(bb,rect.bottom-35);
       if(bb%8==0)
	       dc.TextOut(bb,rect.bottom-30-30,str);
   }

/////////////////////////////////////////
///////先产生噪音数据,然后进行插枝,最后进行平滑
  
	Noise();//噪音数据,线形插枝后的WAVE
	Cos_lineInterpolate();//COS插枝后的WAVE
	SmoothNoise();//数据进行平滑处理后的WAVE
 
/////////////////////////////////////////
///////先产生噪音数据,然后进行平滑,最后进行插枝
 /*	Noise();//噪音数据,线形插枝后的WAVE

	SmoothNoise();//数据进行平滑处理后的WAVE
	Cos_lineInterpolate();//COS插枝后的WAVE
 */
   
   









}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CPerlinNoiseDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CPerlinNoiseDlg::OnButton1() 
{

	
	Invalidate(TRUE);
	
	
	
	
	
	
	
	
	// TODO: Add your control notification handler code here
	
}

void CPerlinNoiseDlg::OnUpdateEdit2() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_UPDATE flag ORed into the lParam mask.
	UpdateData(TRUE);	
	// TODO: Add your control notification handler code here
	
}

void CPerlinNoiseDlg::OnUpdateEdit3() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_UPDATE flag ORed into the lParam mask.
	UpdateData(TRUE);
	// TODO: Add your control notification handler code here
	
}


float CPerlinNoiseDlg::Gauss(float min, float max)//高斯分布
{ 
	
	
	
	//在给定的两个数min和max中选择一个数,这个满足高斯分布
	
	int a = rand(), b = rand(), c = rand(), d = rand();
	int x = a + b + c + d;
	double y = (x*sqrt(3))/RAND_MAX-sqrt(12);
	double mid = (min + max)/2;
	float z = (float)(mid + (max - mid)*y/(2*sqrt(3)));
	
	
	
	
	
	
	return z;
}


void  CPerlinNoiseDlg::Noise()
{


             CClientDC  dc(this);

            
          
			 float  p;
	
			 int n1;
	         float  total=0;
			 p=m_Persistence;
			 float  frequency;
			 float  amplitude;
			 float temp1,temp2;
			 int  noise1;
			 theNumberNoise=0;
			 float   yyy;
			 for (int xx=1;xx<=Range;xx+=m_Step1)
			 {
				 theNumberNoise=theNumberNoise+1;
				 for (int h=0;h<=m_Ocatves;h++)
				 {
					 frequency=pow(2.0,h);
					 //frequency=m_Frequency;
					 amplitude=pow(p,h);
					 noise1=xx*frequency;
					 n1=(noise1<<13) ^noise1;
					 temp1 = (float)(1.0-((n1*(n1*n1*15731+789221)+1376312589)&0x7fffffff)/1073741824.0);
					 temp2=temp1*amplitude;
					 total=total+temp2;
				 }
				// yyy=total*4.0+20*sin(3.14/200*xx);//////噪音的数据
				 yyy=total*5+20*sin(3.14/200*xx);
				 noisex1[theNumberNoise]=xx+60;
				 noisey1[theNumberNoise]=(50-(int)yyy);
				 
			 }
			 //V = V0 osin( t+ )+ oPerlinNoise( t)


			 ///5*sin(2*3.14*t)+10*perlinNoise();
			 dc.TextOut(noisex1[1]-60,noisey1[1]+240,"线形插枝");
			 dc.MoveTo(noisex1[1],noisey1[1]+240);
			 for (int kk=2;kk<theNumberNoise;kk++)
			 {
				 dc.LineTo(noisex1[kk],noisey1[kk]+240);
				 
				 
			 }



			 dc.TextOut(noisex1[1]-60,noisey1[1]+340,"噪音数据");
		//	 dc.MoveTo(noisex1[1],noisey1[1]+340);
			 for (int kkk=1;kkk<theNumberNoise;kkk++)
			 {
				 //dc.LineTo(noisex1[kk],noisey1[kk]+340);
				 dc.SetPixel(noisex1[kkk],noisey1[kkk]+340,RGB(0,0,0));
				 
			 }









           
} 

void  CPerlinNoiseDlg::SmoothNoise()//进行平滑处理
{
 
			 //对数值进行平滑处理后的数据表

            	CClientDC  dc(this);
			 
			 for (int v=1;v<=theNumberNoise;v++)
			 {
				 
				 if (v==1)
				 {
					 noisey1[v]=noisey1[v]/2  +  noisey1[v+1]/4;
					 
				 }
				 
				 else
				 {
					 if (v==theNumberNoise)
					 {
						 
						 noisey1[v]=noisey1[v]/2  +  noisey1[v-1]/4 ;
						 
					 }
					 
					 else
						 noisey1[v]=noisey1[v]/2+noisey1[v-1]/4+noisey1[v+1]/4;
				 }
				 
			 }

             dc.TextOut(noisex1[1]-60,noisey1[1]+30,"平滑处理"); 
             dc.MoveTo(noisex1[1],noisey1[1]+30);
			 for (int kk=2;kk<theNumberNoise;kk++)
			 {
				 dc.LineTo(noisex1[kk],noisey1[kk]+30);
				 
				 
			 }





}


void  CPerlinNoiseDlg::Cos_lineInterpolate()//进行COS插值
{

             CClientDC  dc(this);
             float ff;
			 float  ft;
			 float f;
			 float  yy;
			 dc.TextOut(noisex1[1]-60,noisey1[1]+130,"COS插值");

	         dc.MoveTo(noisex1[1],noisey1[1]+130);
			 
			 
			 for ( int kk=1;kk<theNumberNoise;kk++)
			 {
				 for ( int mm=1;mm<10;mm++)
				 {
					 
					 
					 ff=(float)(((float)mm)/10.0);
					 ft= (float)(ff * 3.1415927);
					 f=(float)((1-cos(ft))*0.5);
					 yy=noisey1[kk]*(1-f)+noisey1[kk+1]*f;
					 
					 dc.LineTo((int)(noisex1[kk]+mm),(int)(yy+130));
					 
					 
				 }
				 
				 
				 
				 
			 }







}

void CPerlinNoiseDlg::OnUpdateEdit4() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_UPDATE flag ORed into the lParam mask.
	UpdateData(TRUE);
	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

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