You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
167 lines
4.6 KiB
167 lines
4.6 KiB
#include "ggl.h"
|
|
#include "scene.h"
|
|
#include "utils.h"
|
|
#pragma comment(lib, "opengl32.lib")
|
|
#pragma comment(lib, "glu32.lib")
|
|
#pragma comment(lib, "winmm.lib")
|
|
|
|
POINT originalPos;//鼠标右键被按下时的位置
|
|
bool rotateView = false;
|
|
|
|
|
|
unsigned char* LoadFileContent(const char* path, int &filesize) {
|
|
unsigned char* fileContent = nullptr;
|
|
filesize = 0;
|
|
|
|
FILE *pFile = fopen(path, "rb");
|
|
|
|
if (pFile) {
|
|
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';
|
|
filesize = nLen;
|
|
}
|
|
fclose(pFile);
|
|
}
|
|
return fileContent;
|
|
}
|
|
|
|
/**
|
|
* 获取每帧的消耗时间
|
|
*/
|
|
float GetFrameTime() {
|
|
static unsigned long lastTime = 0, timeSinceComputerStart = 0;
|
|
timeSinceComputerStart = timeGetTime();
|
|
unsigned long frameTime = lastTime == 0 ? 0 : timeSinceComputerStart - lastTime;
|
|
lastTime = timeSinceComputerStart;
|
|
return float(frameTime) / 1000.0f;
|
|
}
|
|
|
|
/**
|
|
* @hwnd 发起消息的窗口
|
|
* @msg 消息类型
|
|
*/
|
|
LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
|
|
switch (msg) {
|
|
case WM_RBUTTONDOWN:
|
|
GetCursorPos(&originalPos);//获取坐标位置
|
|
ShowCursor(false);//隐藏鼠标
|
|
rotateView = true;
|
|
return 0;
|
|
case WM_RBUTTONUP:
|
|
SetCursorPos(originalPos.x, originalPos.y);
|
|
ShowCursor(true);
|
|
rotateView = false;
|
|
return 0;
|
|
case WM_MOUSEMOVE:
|
|
if (rotateView) {
|
|
POINT currentPos;
|
|
GetCursorPos(¤tPos);//再一次获取鼠标当前位置
|
|
int deltaX = currentPos.x - originalPos.x;
|
|
int deltaY = currentPos.y - originalPos.y;
|
|
OnMouseMove(deltaX, deltaY);
|
|
SetCursorPos(originalPos.x, originalPos.y);
|
|
}
|
|
return 0;
|
|
case WM_KEYDOWN:
|
|
OnKeyDown(wParam);
|
|
return 0;
|
|
case WM_KEYUP:
|
|
OnKeyUp(wParam);
|
|
return 0;
|
|
case WM_CLOSE:
|
|
PostQuitMessage(0);
|
|
return 0;
|
|
}
|
|
|
|
return DefWindowProc(hwnd, msg, wParam, lParam);//其他消息调用window默认处理函数
|
|
}
|
|
|
|
/**
|
|
* @hinstance 本应用程序启动实例
|
|
* @hPrevInstance 上一次应用程式的实例
|
|
* @IpCmdLine 命令行的参数
|
|
* @ShowCmd 怎么显示窗口
|
|
*/
|
|
INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR IpCmdLine, int ShowCmd) {
|
|
|
|
/*注册窗口*/
|
|
WNDCLASSEX wndclass;
|
|
wndclass.cbClsExtra = 0; //窗口类型的额外空间,这里不需要
|
|
wndclass.cbSize = sizeof(WNDCLASSEX); //窗口实际占用的内存
|
|
wndclass.cbWndExtra = 0; //窗口的额外空间,这里不需要
|
|
wndclass.hbrBackground = NULL; //窗口背景
|
|
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //定义鼠标类型
|
|
wndclass.hIcon = NULL; //应用程序exe文件显示图标
|
|
wndclass.hIconSm = NULL; //应用程序运行时左上角图标
|
|
wndclass.hInstance = hinstance; //本应用程序实例
|
|
wndclass.lpfnWndProc = GLWindowProc; //如果用户操作了窗口,本函数会被调用
|
|
wndclass.lpszClassName = L"GLWindow";//窗口名称
|
|
wndclass.lpszMenuName = NULL;//菜单名称
|
|
wndclass.style = CS_VREDRAW | CS_HREDRAW;//窗口更新时的重绘方式,这里使用垂直重绘和水平重绘
|
|
ATOM atom = RegisterClassEx(&wndclass);
|
|
if (!atom) {
|
|
MessageBox(NULL, L"Register failed", L"Error", MB_OK);
|
|
return 0;
|
|
}
|
|
|
|
|
|
/*创建窗口*/
|
|
//调整窗口大小
|
|
RECT rect;
|
|
rect.left = 0;
|
|
rect.right = 800;
|
|
rect.top = 0;
|
|
rect.bottom = 600;
|
|
AdjustWindowRect(&rect, WS_EX_OVERLAPPEDWINDOW, NULL);
|
|
int windowWidth = rect.right - rect.left;
|
|
int windowHeight = rect.bottom - rect.top;
|
|
|
|
//窗口名称一定要和刚才注册窗口的保持一致
|
|
HWND hwnd = CreateWindowEx(NULL, L"GLWindow", L"OpenGL Window", WS_OVERLAPPEDWINDOW, 100, 100, windowWidth, windowHeight, NULL, NULL, hinstance, NULL);
|
|
|
|
//设置渲染环境
|
|
HDC dc = GetDC(hwnd);//获取设备上下文
|
|
PIXELFORMATDESCRIPTOR pfd;
|
|
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
|
|
pfd.nVersion = 1;
|
|
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
|
|
pfd.cColorBits = 32; //颜色缓冲区每个像素为32比特4通道RGBA
|
|
pfd.cDepthBits = 24; //深度缓冲区每个像素大小,24比特表示一个浮点数
|
|
pfd.cStencilBits = 8; //蒙板缓冲区每像素为8比特
|
|
pfd.iPixelType = PFD_TYPE_RGBA; //设置像素类型为RGBA
|
|
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER ; //表示像素最后输出到窗口
|
|
//设置像素格式
|
|
int pixelFormat = ChoosePixelFormat(dc, &pfd);
|
|
SetPixelFormat(dc, pixelFormat, &pfd);
|
|
//创建OpenGL渲染环境
|
|
HGLRC rc = wglCreateContext(dc);
|
|
wglMakeCurrent(dc, rc);//设置OpenGL渲染环境生效
|
|
|
|
Init();
|
|
|
|
/*显示窗口*/
|
|
ShowWindow(hwnd, SW_SHOW);
|
|
UpdateWindow(hwnd);
|
|
|
|
/*监听并处理用户请求*/
|
|
MSG msg;
|
|
while (true) {
|
|
if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) {
|
|
if (msg.message == WM_QUIT) {
|
|
break;
|
|
}
|
|
TranslateMessage(&msg);
|
|
DispatchMessage(&msg);
|
|
}
|
|
|
|
Draw();
|
|
SwapBuffers(dc);
|
|
}
|
|
|
|
return 0;
|
|
}
|