OpenGL的MFC编程-蓝天白云


OpenGL的MFC编程
 

 

建立一个基于对话框的工程(名称:CloudGL)

并且在工程设置的Link里加入库opengl32.lib glu32.lib glaux.lib

删除对话框上的所有控件,属性中的边框设置为RESIZING,勾选最大化和最小话按钮。

为CCloudGLDlg类添加两个函数BOOL SetWindowPixelFormat(HDC hDC); 和BOOL CreateViewGLContext(HDC hDC);设置OpenGL显示环境。

为CCloudGLDlg类添加变量HDC hrenderDC; 在OnInitDialog函数中加入下面代码:
// TODO: Add extra initialization here
hrenderDC=::GetDC(this->m_hWnd);
if(SetWindowPixelFormat(hrenderDC)==FALSE)
return 0;
if(CreateViewGLContext(hrenderDC)==FALSE)
return 0;
在CloudGLDlg.cpp中加入两个全局变量:

float pos[4] = {3,3,3,1};
float dir[3] = {-1,-1,-1};

 

全部代码如下所示:

 

// CloudGLDlg.h : header file
//

#if !defined(AFX_CLOUDGLDLG_H__EDFC1E1D_57E0_4262_8B22_DC7C7EC4D5EA__INCLUDED_)
#define AFX_CLOUDGLDLG_H__EDFC1E1D_57E0_4262_8B22_DC7C7EC4D5EA__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

/////////////////////////////////////////////////////////////////////////////
// CCloudGLDlg dialog

class CCloudGLDlg : public CDialog
{
// Construction
public:
CCloudGLDlg(CWnd* pParent = NULL); // standard constructor

// Dialog Data
//{{AFX_DATA(CCloudGLDlg)
enum { IDD = IDD_CLOUDGL_DIALOG };
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA

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

// Implementation
protected:
HICON m_hIcon;

BOOL SetWindowPixelFormat(HDC hDC); //设定象素格式
BOOL CreateViewGLContext(HDC hDC); //View GL Context
HDC hrenderDC; //DC
HGLRC hrenderRC; //RC
int PixelFormat;

void DrawGLScene();

float map32[32 * 32];
float map256[256 * 256];

void Init();
void LoopForever();
void ExpFilter(float *map);
void OverlapOctaves(float *map32, float *map256);
float Interpolate(float x, float y, float *map);
void SetNoise(float *map);
float Noise(int x, int y, int random);

// Generated message map functions
//{{AFX_MSG(CCloudGLDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnSize(UINT nType, int cx, int cy);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_CLOUDGLDLG_H__EDFC1E1D_57E0_4262_8B22_DC7C7EC4D5EA__INCLUDED_)

///////////////////////////////////////////////

 

/////////////////////////////////////////////

// CloudGLDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CloudGL.h"
#include "CloudGLDlg.h"
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

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

float pos[4] = {3,3,3,1};
float dir[3] = {-1,-1,-1};

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CCloudGLDlg dialog

CCloudGLDlg::CCloudGLDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCloudGLDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCloudGLDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCloudGLDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCloudGLDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCloudGLDlg, CDialog)
//{{AFX_MSG_MAP(CCloudGLDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_WM_SIZE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCloudGLDlg message handlers

BOOL CCloudGLDlg::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
PixelFormat=0;
hrenderDC=::GetDC(this->m_hWnd);
if(SetWindowPixelFormat(hrenderDC)==FALSE)
return 0;
if(CreateViewGLContext(hrenderDC)==FALSE)
return 0;

CRect rect;
GetClientRect(&rect);
glViewport(0,0,rect.Width(),rect.Height());
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(-0.5,0.5, -0.5,0.5, 0.2,1.2);
gluLookAt( 0,0,0.5, 0,0,0, 0,0.1,0 );
glMatrixMode( GL_MODELVIEW );

glEnable(GL_DEPTH_TEST);

glEnable(GL_COLOR_MATERIAL);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

glEnable(GL_AUTO_NORMAL);

glLightfv(GL_LIGHT0, GL_POSITION, pos);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, dir);

Init();
SetTimer(1,500,0);
return TRUE; // return TRUE unless you set the focus to a control
}

void CCloudGLDlg::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 CCloudGLDlg::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
{
DrawGLScene();
CDialog::OnPaint();
}
}

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

BOOL CCloudGLDlg::SetWindowPixelFormat(HDC hDC)
{
PIXELFORMATDESCRIPTOR pixelDesc;
pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pixelDesc.nVersion = 1;
pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER |
PFD_TYPE_RGBA;
pixelDesc.iPixelType = PFD_TYPE_RGBA;
pixelDesc.cColorBits = 32;
pixelDesc.cRedBits = 0;
pixelDesc.cRedShift = 0;
pixelDesc.cGreenBits = 0;
pixelDesc.cGreenShift = 0;
pixelDesc.cBlueBits = 0;
pixelDesc.cBlueShift = 0;
pixelDesc.cAlphaBits = 0;
pixelDesc.cAlphaShift = 0;
pixelDesc.cAccumBits = 0;
pixelDesc.cAccumRedBits = 0;
pixelDesc.cAccumGreenBits = 0;
pixelDesc.cAccumBlueBits = 0;
pixelDesc.cAccumAlphaBits = 0;
pixelDesc.cDepthBits = 0;
pixelDesc.cStencilBits = 1;
pixelDesc.cAuxBuffers = 0;
pixelDesc.iLayerType = PFD_MAIN_PLANE;
pixelDesc.bReserved = 0;
pixelDesc.dwLayerMask = 0;
pixelDesc.dwVisibleMask = 0;
pixelDesc.dwDamageMask = 0;
PixelFormat = ChoosePixelFormat(hDC,&pixelDesc);
if(PixelFormat==0) // Choose default
{
PixelFormat = 1;
if(DescribePixelFormat(hDC,PixelFormat,
sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0)
{
return FALSE;
}
}
if(SetPixelFormat(hDC,PixelFormat,&pixelDesc)==FALSE)
{
return FALSE;
}
return TRUE;
}
BOOL CCloudGLDlg::CreateViewGLContext(HDC hDC)
{
hrenderRC = wglCreateContext(hDC);
if(hrenderRC==NULL)
return FALSE;
if(wglMakeCurrent(hDC,hrenderRC)==FALSE)
return FALSE;
return TRUE;
}

void CCloudGLDlg::DrawGLScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

LoopForever(); //Our cloud function

char texture[256][256][3]; //Temporary array to hold texture RGB values

for(int i=0; i<256; i++) //Set cloud color value to temporary array
for(int j=0; j<256; j++)
{
float color = map256[i*256+j];
texture[i][j][0]=color;
texture[i][j][1]=color;
texture[i][j][2]=color;

if(color < 150)
texture[i][j][2] = 200;
}

unsigned int ID; //Generate an ID for texture binding
glGenTextures(1, &ID); //Texture binding
glBindTexture(GL_TEXTURE_2D, ID);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, 256, 256, GL_RGB, GL_UNSIGNED_BYTE, texture);

glMatrixMode(GL_TEXTURE); //Let's move the clouds from left to right
static float x;
x+=0.01f;
glTranslatef(x,0,0);

glEnable(GL_TEXTURE_2D); //Render the cloud texture
glBegin(GL_QUADS);
glTexCoord2d(1,1); glVertex3f(0.5f, 0.5f, 0.);
glTexCoord2d(0,1); glVertex3f(-0.5f, 0.5f, 0.);
glTexCoord2d(0,0); glVertex3f(-0.5f, -0.5f, 0.);
glTexCoord2d(1,0); glVertex3f(0.5f, -0.5f, 0.);
glEnd();

SwapBuffers(hrenderDC);
}

float CCloudGLDlg::Noise(int x, int y, int random)
{
int n = x + y * 57 + random * 131;
n = (n<<13) ^ n;
return (1.0f - ( (n * (n * n * 15731 + 789221) +
1376312589)&0x7fffffff)* 0.000000000931322574615478515625f);
}

void CCloudGLDlg::SetNoise(float *map)
{
float temp[34][34];

int random=rand() % 5000;

for (int y=1; y<33; y++)
for (int x=1; x<33; x++)
{
temp[x][y] = 128.0f + Noise(x, y, random)*128.0f;
}

for (int x=1; x<33; x++)
{
temp[0][x] = temp[32][x];
temp[33][x] = temp[1][x];
temp[x][0] = temp[x][32];
temp[x][33] = temp[x][1];
}
temp[0][0] = temp[32][32];
temp[33][33] = temp[1][1];
temp[0][33] = temp[32][1];
temp[33][0] = temp[1][32];

for (y=1; y<33; y++)
for (int x=1; x<33; x++)
{
float center = temp[x][y]/4.0f;
float sides = (temp[x+1][y] + temp[x-1][y] + temp[x][y+1] + temp[x][y-1])/8.0f;
float corners = (temp[x+1][y+1] + temp[x+1][y-1] + temp[x-1][y+1] + temp[x-1][y-1])/16.0f;

map32[((x-1)*32) + (y-1)] = center + sides + corners;
}
}

float CCloudGLDlg::Interpolate(float x, float y, float *map)
{
int Xint = (int)x;
int Yint = (int)y;

float Xfrac = x - Xint;
float Yfrac = y - Yint;

int X0 = Xint % 32;
int Y0 = Yint % 32;
int X1 = (Xint + 1) % 32;
int Y1 = (Yint + 1) % 32;

float bot = map[X0*32 + Y0] + Xfrac * (map[X1*32 + Y0] - map[X0*32 + Y0]);
float top = map[X0*32 + Y1] + Xfrac * (map[X1*32 + Y1] - map[X0*32 + Y1]);

return (bot + Yfrac * (top - bot));
}

void CCloudGLDlg::OverlapOctaves(float *map32, float *map256)
{
for (int x=0; x<256*256; x++)
{
map256[x] = 0;
}

for (int octave=0; octave<4; octave++)
for (int x=0; x<256; x++)
for (int y=0; y<256; y++)
{
float scale = 1 / pow(2, 3-octave);
float noise = Interpolate(x*scale, y*scale , map32);
map256[(y*256) + x] += noise / pow(2, octave);
}
}

void CCloudGLDlg::ExpFilter(float *map)
{
float cover = 20.0f;
float sharpness = 0.95f;

for (int x=0; x<256*256; x++)
{
float c = map[x] - (255.0f-cover);
if (c<0) c = 0;
map[x] = 255.0f - ((float)(pow(sharpness, c))*255.0f);
}
}

void CCloudGLDlg::Init()
{
SetNoise(map32);
}

void CCloudGLDlg::LoopForever()
{
OverlapOctaves(map32, map256);
ExpFilter(map256);
}

 

void CCloudGLDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
Invalidate(0);
CDialog::OnTimer(nIDEvent);

// Eat spurious WM_TIMER message
MSG msg;
while( ::PeekMessage( & msg, m_hWnd, WM_TIMER, WM_TIMER, PM_REMOVE ) )
;
}

void CCloudGLDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);

// TODO: Add your message handler code here
CRect rect;
GetClientRect(&rect);
glViewport(0,0,rect.Width(),rect.Height());
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(-0.5,0.5, -0.5,0.5, 0.2,1.2);
gluLookAt( 0,0,0.5, 0,0,0, 0,0.1,0 );
glMatrixMode( GL_MODELVIEW );
}

首页