用opengl库在mfc 对话框程序中创建简单的opengl程序。
步骤:
1.用vs2010创建mfc dialog对话框程序
直接点击完成,创建工程结束。
2.删掉对话框上面的lable,及button按钮
3.打开对话框页,并选择“”项目“中的 ”添加类向导“ 打开添加类向导对话框,添加WM_CLOSE .
4.打开Dlg_OpenGLDlg.h文件,修改如下代码:
public:
afx_msg void OnTimer(UINT_PTR nIDEvent);
afx_msg void OnClose();
HDC m_dc;
void DrawFunc();
{
m_dc = ::GetDC(m_hWnd);
PIXELFORMATDESCRIPTOR pfd;
pfd.nVersion = 1;
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.cColorBits = 32;
pfd.cDepthBits = 24;
pfd.cStencilBits = 8;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.iLayerType = PFD_MAIN_PLANE;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
int nPixelFormat = ChoosePixelFormat(m_dc, &pfd);
SetPixelFormat(m_dc, nPixelFormat, &pfd);
HGLRC rc = wglCreateContext(m_dc);
wglMakeCurrent(m_dc, rc);
glMatrixMode(GL_PROJECTION);
gluPerspective(50, 800/600, 0.1f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
Texture texture;
texture.Init("test.bmp");//init opengl texture
glBindTexture(GL_TEXTURE_2D, texture.mTextureID);
SetTimer(0, 50, NULL);
void COpenGLDlg::DrawFunc()
{
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
{
glBegin(GL_TRIANGLES);//start to draw something
//glNormal3f(0.0f,1.0f,0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, -0.5f, -2.0f);
//glNormal3f(0.0f, 1.0f, 0.0f);
glTexCoord2f(2.0f, 0.0f);
glVertex3f(1.0f, -0.5f, -2.0f);
//glNormal3f(0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0, 2.0f);
glVertex3f(-1.0f, -0.5f, -3.0f);
//glNormal3f(0.0f, 1.0f, 0.0f);
glTexCoord2f(2.0f, 0.0f);
glVertex3f(1.0f, -0.5f, -2.0f);
//glNormal3f(0.0f, 1.0f, 0.0f);
glTexCoord2f(2.0f, 2.0f);
glVertex3f(1.0f, -0.5f, -3.0f);
//glNormal3f(0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0, 2.0f);
glVertex3f(-1.0f, -0.5f, -3.0f);
glEnd();//end
}
SwapBuffers(m_dc);
}
void COpenGLDlg::OnTimer(UINT_PTR nIDEvent)
{
DrawFunc();
CDialogEx::OnTimer(nIDEvent);
}
void COpenGLDlg::OnClose()
{
KillTimer(0);
::ReleaseDC(m_hWnd, m_dc);
CDialogEx::OnClose();
}
读取bmp并去掉bmp头:utils.h
#pragma once
#include
unsigned char* LoadFileContent(const char*filePath);
utils.cpp
#include "utils.h"
unsigned char* LoadFileContent(const char*filePath)
{
unsigned char* fileContent = nullptr;
FILE*pFile = fopen(filePath, "rb");
if (pFile)
{
//read
fseek(pFile,0,SEEK_END);
int nLen = ftell(pFile);
if (nLen>0)
{
rewind(pFile);
fileContent = new unsigned char[nLen+1];
fread(fileContent,sizeof(unsigned char),nLen,pFile);
fileContent[nLen]='\0';
}
fclose(pFile);
}
return fileContent;
}
#pragma once
#include
#include
class Texture
{
public:
GLuint mTextureID;//gpu
void Init(const char*imagePath);
};
texture.cpp
#include "texture.h"
#include "utils.h"
unsigned char* DecodeBMP(unsigned char*bmpFileData, int&width, int&height)
{
if (0x4D42==*((unsigned short*)bmpFileData))
{
int pixelDataOffset = *((int*)(bmpFileData + 10));
width = *((int*)(bmpFileData+18));
height = *((int*)(bmpFileData + 22));
unsigned char*pixelData = bmpFileData + pixelDataOffset;
//bgr bgr bgr ....
//rgb rgb rgb
for (int i=0;i
demo下载地址:http://download.csdn.net/detail/sz76211822/9761124