// OpenGLCubeDemoDlg.cpp: 实现文件
//
#include "pch.h"
#include "framework.h"
#include "OpenGLCubeDemo.h"
#include "OpenGLCubeDemoDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define STBI_WINDOWS_UTF8
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
// COpenGLCubeDemoDlg 对话框
//#define IMAGE_PATH "Dragons.jpg"
#define IMAGE_PATH "dice.png"
COpenGLCubeDemoDlg::COpenGLCubeDemoDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_OPENGLCUBEDEMO_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void COpenGLCubeDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(COpenGLCubeDemoDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_SIZE()
ON_WM_TIMER()
ON_WM_DESTROY()
END_MESSAGE_MAP()
// COpenGLCubeDemoDlg 消息处理程序
BOOL COpenGLCubeDemoDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
SetWindowText(_T("OpenGL Cube Demo - blog.csdn.net/blackwoodcliff"));
//ShowWindow(SW_MAXIMIZE);
// TODO: 在此添加额外的初始化代码
InitializeOpenGL(this->GetDC()->GetSafeHdc());
ShowWindow(SW_MAXIMIZE);
m_glTexture = LoadGLTexture();
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。 对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void COpenGLCubeDemoDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
glClearColor(0.0f, 0.05f, 0.15f, 1.0f);
//glClearColor(0.06f, 0.06f, 0.06f, 1.0f);
//glClearColor(0.085f, 0.2f, 0.414f, 1.0f);
//glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
//glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); //复位
//DrawRect();
//DrawCube();
//DrawTextureCube();
DrawDice();
glFlush();
SwapBuffers(wglGetCurrentDC());
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR COpenGLCubeDemoDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
bool COpenGLCubeDemoDlg::InitializeOpenGL(HDC hDC)
{
//设置 DC 像素格式
if (false == SetDCPixelFormat(hDC))
{
return false;
}
//创建 RC
HGLRC hRC = wglCreateContext(hDC);
if (hRC == NULL)
{
return false;
}
//为当前线程设置 RC
if (wglMakeCurrent(hDC, hRC) == FALSE)
{
return false;
}
glClearDepth(1.0f);
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
//glEnable(GL_MULTISAMPLE);
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
return true;
}
bool COpenGLCubeDemoDlg::SetDCPixelFormat(HDC hDC)
{
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), //pfd结构的大小
1, //版本号
PFD_DRAW_TO_WINDOW | //支持在窗口中绘图
PFD_SUPPORT_OPENGL | //支持OpenGL
PFD_DOUBLEBUFFER, //支持双缓冲
PFD_TYPE_RGBA, //RGBA颜色模式
32, //32位颜色深度
0, 0, 0, 0, 0, 0, //忽略颜色位
0, //没有非透明度缓存
0, //忽略移位位
0, //无累计缓存
0, 0, 0, 0, //忽略累计位
32, //32位深度缓存
0, //无模板缓存
0, //无辅助缓存
PFD_MAIN_PLANE, //主层
0, //保留
0, 0, 0 //忽略层,可见性和损毁掩模
};
//得到 DC 最匹配的像素格式
int pixelFormat = ChoosePixelFormat(hDC, &pfd);
if (0 == pixelFormat)
{
//如果没有找到,就调用 DescribePixelFormat 函数来选择索引值为 1 的像素格式
pixelFormat = 1;
if (DescribePixelFormat(hDC, pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd) == 0)
{
MessageBox(_T("ChoosePixelFormat 失败"));
return false;
}
}
//设置 DC 像素格式
if (SetPixelFormat(hDC, pixelFormat, &pfd) == FALSE)
{
MessageBox(_T("SetPixelFormat 失败"));
return false;
}
return true;
}
void COpenGLCubeDemoDlg::DrawRect()
{
//glColor3ub(128, 0, 0);
glColor3ub(96, 0, 0);
glBegin(GL_QUADS);
glVertex2f(-0.5, 0.5); //左上角
glVertex2f(-0.5, -0.5); //左下角
glVertex2f(0.5, -0.5); //右下角
glVertex2f(0.5, 0.5); //右上角
glEnd();
}
void COpenGLCubeDemoDlg::DrawCube()
{
glTranslatef(0.0f, 0.0f, -2.5);
glRotatef(m_rotationX, 1.0f, 0.0f, 0.0f);
glRotatef(m_rotationY, 0.0f, 1.0f, 0.0f);
glBegin(GL_QUADS);
// Front Face
glColor3ub(128, 0, 0); //红
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
// Back Face
glColor3ub(128, 128, 0); //黄
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
// Top Face
glColor3ub(0, 0, 128); //蓝
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
// Bottom Face
glColor3ub(128, 0, 128); //紫
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, -0.5f);
// Left Face
glColor3ub(0, 128, 0); //绿
glVertex3f(-0.5f, -0.5f, -0.5f);
glVertex3f(-0.5f, -0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, 0.5f);
glVertex3f(-0.5f, 0.5f, -0.5f);
// Right face
glColor3ub(0, 128, 128); //青
glVertex3f(0.5f, -0.5f, 0.5f);
glVertex3f(0.5f, -0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, -0.5f);
glVertex3f(0.5f, 0.5f, 0.5f);
glEnd();
}
void COpenGLCubeDemoDlg::DrawTextureCube()
{
glTranslatef(0.0f, 0.0f, -2.5);
glRotatef(m_rotationX, 1.0f, 0.0f, 0.0f);
glRotatef(m_rotationY, 0.0f, 1.0f, 0.0f);
glBindTexture(GL_TEXTURE_2D, m_glTexture);
glBegin(GL_QUADS);
// Front Face
glNormal3i(0, 0, 1);
glTexCoord2i(0, 0);
glVertex3f(-0.5f, -0.5f, 0.5f);
glTexCoord2i(1, 0);
glVertex3f(0.5f, -0.5f, 0.5f);
glTexCoord2i(1, 1);
glVertex3f(0.5f, 0.5f, 0.5f);
glTexCoord2i(0, 1);
glVertex3f(-0.5f, 0.5f, 0.5f);
// Back Face
glNormal3i(0, 0, -1);
glTexCoord2i(1, 0);
glVertex3f(-0.5f, -0.5f, -0.5f);
glTexCoord2i(0, 0);
glVertex3f(0.5f, -0.5f, -0.5f);
glTexCoord2i(0, 1);
glVertex3f(0.5f, 0.5f, -0.5f);
glTexCoord2i(1, 1);
glVertex3f(-0.5f, 0.5f, -0.5f);
// Top Face
glNormal3i(0, 1, 0);
glTexCoord2i(0, 0);
glVertex3f(-0.5f, 0.5f, 0.5f);
glTexCoord2i(1, 0);
glVertex3f(0.5f, 0.5f, 0.5f);
glTexCoord2i(1, 1);
glVertex3f(0.5f, 0.5f, -0.5f);
glTexCoord2i(0, 1);
glVertex3f(-0.5f, 0.5f, -0.5f);
// Bottom Face
glNormal3i(0, -1, 0);
glTexCoord2i(0, 0);
glVertex3f(-0.5f, -0.5f, 0.5f);
glTexCoord2i(1, 0);
glVertex3f(0.5f, -0.5f, 0.5f);
glTexCoord2i(1, 1);
glVertex3f(0.5f, -0.5f, -0.5f);
glTexCoord2i(0, 1);
glVertex3f(-0.5f, -0.5f, -0.5f);
// Left Face
glNormal3i(-1, 0, 0);
glTexCoord2i(0, 0);
glVertex3f(-0.5f, -0.5f, -0.5f);
glTexCoord2i(1, 0);
glVertex3f(-0.5f, -0.5