將GLFW窗口嵌入Win32 SDK窗口及其多線程渲染方法

這篇文章(MFC單文檔視圖中嵌入GLFW窗口)提到了glfw嵌入mfc的辦法,採用的查找進程PID再嵌入的方法,進程間通信採用UDP,略微繁瑣。

其實不必如此麻煩,SetParent直接就可以辦到。

先上最終效果,其中的三角形是實時旋轉的:
在這裏插入圖片描述

第1步 創建標準Win32 SDK窗口

#include <windows.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
	static TCHAR szAppName[] = TEXT ("HelloWin") ;
	HWND   hwnd ;
	MSG    msg ;
	WNDCLASS wndclass ;//WNDCLASSEX比WNDCLASS多兩個結構成員--cbSize(指定WNDCLASSEX結構的大小--字節)  --hIconSm(標識類的小圖標)
	wndclass.style        = CS_HREDRAW | CS_VREDRAW ;
	wndclass.lpfnWndProc  = WndProc ;
	wndclass.cbClsExtra   = 0 ;
	wndclass.cbWndExtra   = 0 ;
	wndclass.hInstance    = hInstance ;
	wndclass.hIcon        = LoadIcon (NULL, IDI_APPLICATION) ;
	wndclass.hCursor      = LoadCursor (NULL, IDC_ARROW) ;
	wndclass.hbrBackground= (HBRUSH) (COLOR_WINDOW+1) ;//白色//(HBRUSH)(COLOR_MENU  +1)界面灰
	wndclass.lpszMenuName  = NULL ;
	wndclass.lpszClassName= szAppName ;

	if (!RegisterClass (&wndclass))
	{
		MessageBox (  NULL, TEXT ("This program requires Windows NT!"),szAppName, MB_ICONERROR) ;
		return 0 ;
	}
	hwnd = CreateWindow( szAppName,      // window class name
		TEXT ("The Hello Program"),   // window caption
		WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU,
		CW_USEDEFAULT,// initial x position
		CW_USEDEFAULT,// initial y position
		CW_USEDEFAULT,// initial x size
		CW_USEDEFAULT,// initial y size
		NULL,                 // parent window handle
		NULL,            // window menu handle
		hInstance,   // program instance handle
		NULL) ;      // creation parameters

	ShowWindow (hwnd, iCmdShow) ;
	UpdateWindow (hwnd) ;

	while (GetMessage (&msg, NULL, 0, 0))
	{
		TranslateMessage (&msg) ;
		DispatchMessage (&msg) ;
	}
	return msg.wParam ;
}


LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_CREATE:
		return 0;
	case WM_PAINT:
		HDC hdc;
		PAINTSTRUCT ps ;
		hdc = BeginPaint(hwnd, &ps);
		//DrawText (hdc, s, -1, &rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
		EndPaint(hwnd, &ps);
		return 0 ;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0 ;
	case WM_QUIT:
		return 0;
	}

	return DefWindowProc (hwnd, message, wParam, lParam);
}

這一長段麻煩又難記,我每次都是複製粘貼了再改。要麼就是直接用封裝好的窗口類(涉及靜態函數做消息轉發,有人看再寫吧,再說這部分內容網上也多)。

第2步 在WinMain中創建glfw窗口

在CreateWindow後加入glfw的窗口創建過程:

	//初始化glfw
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	//創建glfw窗口
	window = glfwCreateWindow(400, 400, "openGL", NULL, NULL);	
	if (window == NULL)
	{
		OutputDebugString("Failed to create GLFW window");
		glfwTerminate();
		return -1;
	}
	glfwMakeContextCurrent(window);

	//註冊glad函數地址
	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
	{
		OutputDebugString("Failed to initialize GLAD");
		return -1;
	}
	glViewport(0, 0, 400, 400);

	//背景顏色
	glClearColor(0.2f, 0.3f, 0.3f, 1.0f);

添加include,鏈接好lib,然後上面這段直接插入到主消息循環之前(就是WinMain裏那個while的前面)。
注意:有人說glfw的init只能做一次,做多次會發生其他窗口不渲染的bug,我自己沒有試過。

此時會打開2個窗口,並且glClearColor設置的顏色並沒有生效,單獨關閉opengl窗口也沒反應。第1個問題是因爲opengl的渲染循環沒有建立;第2個問題是因爲GLFW截獲了WM_CLOSE消息的響應,要設置glfwSetWindowShouldClose才能讓他捕獲關閉事件。
在這裏插入圖片描述

第3步 使用SetParent將GLFW窗口嵌入主窗口

繼續加入代碼:

	//取得glfw窗口句柄並將其嵌入父窗口
	HWND hwndGLFW = glfwGetWin32Window(window);
	SetWindowLong(hwndGLFW, GWL_STYLE, WS_VISIBLE);
	MoveWindow(hwndGLFW, 0, 0, 400, 400, TRUE);
	SetParent(hwndGLFW, hwnd);

注意glfwGetWin32Window函數是不在glfw3.h裏的,要在include處加入以下代碼才能使用:

#define GLFW_EXPOSE_NATIVE_WIN32
#include <GLFW/glfw3native.h>

SetWindowLong這句是重設窗口外觀,不加的話GLFW的窗口會嵌入主窗口,但是標題欄什麼的一應俱全,只是不能拖出主窗口外而已。不加的話效果就像這樣:
在這裏插入圖片描述
MoveWindow這句如果不加的話,因爲GLFW窗口彈出的位置不固定,所有你會發現每次打開主程序時GLFW窗口都在隨機的位置。

第4步 在主消息循環中加入opengl渲染

在主消息循環中加入:

	if (!glfwWindowShouldClose(window))
	{

		//刷新顏色緩衝和深度
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		glfwSwapBuffers(window);
		glfwPollEvents();
	}

現在的主消息循環長這樣:

	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!glfwWindowShouldClose(window))
		{
			//刷新顏色緩衝和深度
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

			glfwSwapBuffers(window);
			glfwPollEvents();
		}

		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

此時opengl已經開始渲染了,可以看到底色了。
在這裏插入圖片描述
然後可以畫個三角形,再讓它隨時間旋轉。如果這樣做了,你就會發現,只有鼠標在窗口上不停移動,三角形纔會轉,一停下就不轉了。這是因爲主消息循環只有在接收到消息時才刷新,只有你不停地造,動鼠標啊,按鍵盤啊,拖滾輪什麼的它才更新。

這顯然不符合要求。所以我們還需要開一個新線程。

第5步 使用多線程爲GLFW窗口進行渲染

加入一個函數:

void RenderProc()
{
	glfwMakeContextCurrent(window);
	while (!glfwWindowShouldClose(window))
	{
		float time = glfwGetTime();

		//刷新顏色緩衝和深度
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		shader->UseProgram();
		shader->Uniform("angle", time);

		triangle->Bind();
		triangle->DrawTriangles();

		//
		glfwSwapBuffers(window);
		glfwPollEvents();
	}
	delete shader;
	delete triangle;
	glfwTerminate();
}

其中的shader和triangle分別是對着色器和VAO的封裝。其初始化函數爲:

void Init()
{
	float triangle_vertex[] =
	{
		0.0f,0.5f,0.0f,1.0f,0.0f,0.0f,
		-0.5f,-0.5f,0.0f,0.0f,1.0f,0.0f,
		0.5f,-0.5f,0.0f,0.0f,0.0f,1.0f
	};
	triangle=new TVertexArray(sizeof(triangle_vertex), triangle_vertex, { 3,3 });

	shader=new TShader("tri_vertex.glsl", "tri_fragment.glsl");
}

在glClearColor後加入:

	Init();

	//將渲染移交線程前需要將當前上下文設爲null
	glfwMakeContextCurrent(NULL);

	std::thread RenderThread(RenderProc);

這裏首先初始化了shader和triangle指針,然後將opengl的context設爲Null,這是因爲glfwMakeContextCurrent的說明裏說了,將渲染函數移交到新線程的時候,要先在舊線程裏把上下文設爲空,再在新的線程裏設置上下文。否則的話,在渲染中GetLocation和VAO的綁定操作等都會出錯。

因爲渲染已經移交新線程,主消息循環可以刪掉和glfw, opengl相關的內容了。
然後消息循環後需要把thread阻塞一下,確認關閉:

	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	RenderThread.join();

最後在WndProc中WM_DESTROY消息處加入glfwSetWindowShouldClose,讓主窗口帶着glfw窗口關閉:

	case WM_DESTROY:
		glfwSetWindowShouldClose(window, true);

		PostQuitMessage(0);
		return 0;

現在的關閉流程是這樣:
主窗口點擊關閉->WM_DESTROY消息發出->glfwSetWindowShouldClose函數設置glfw窗口可以關閉->PostQuitMessage函數調用->主消息循環退出->RenderThread線程阻塞,等待glfw窗口、渲染循環以及RenderThread線程退出(glfw窗口可能在主消息循環結束前就已經退出,此處阻塞主要起檢查並等待的作用)->主程序返回

現在流程就很完善了。寫個旋轉三角形,三角形可以不停旋轉,主窗口也可以正常響應。

最終效果:
在這裏插入圖片描述
最後是整個main.cpp文件:

#include <windows.h>

#include <thread>
#include <memory>

#include <glad/glad.h>
#include <GLFW/glfw3.h>

#define GLFW_EXPOSE_NATIVE_WIN32
#include <GLFW/glfw3native.h>

#include "TShader.h"
#include "TVertexArray.h"

GLFWwindow* window;
TVertexArray *triangle;
TShader *shader;
void Init();
void RenderProc();


LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
	static TCHAR szAppName[] = TEXT("HelloWin");
	HWND   hwnd;
	MSG    msg;
	WNDCLASS wndclass;//WNDCLASSEX比WNDCLASS多兩個結構成員--cbSize(指定WNDCLASSEX結構的大小--字節)  --hIconSm(標識類的小圖標)
	wndclass.style = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc = WndProc;
	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hInstance = hInstance;
	wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);//白色//(HBRUSH)(COLOR_MENU  +1)界面灰
	wndclass.lpszMenuName = NULL;
	wndclass.lpszClassName = szAppName;

	if (!RegisterClass(&wndclass))
	{
		MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR);
		return 0;
	}
	hwnd = CreateWindow(szAppName,      // window class name
		TEXT("The Hello Program"),   // window caption
		WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_SIZEBOX,
		CW_USEDEFAULT,// initial x position
		CW_USEDEFAULT,// initial y position
		CW_USEDEFAULT,// initial x size
		CW_USEDEFAULT,// initial y size
		NULL,                 // parent window handle
		NULL,            // window menu handle
		hInstance,   // program instance handle
		NULL);      // creation parameters

	//初始化glfw
	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	//創建glfw窗口
	window = glfwCreateWindow(400, 400, "openGL", NULL, NULL);	
	if (window == NULL)
	{
		OutputDebugString("Failed to create GLFW window");
		glfwTerminate();
		return -1;
	}
	glfwMakeContextCurrent(window);

	//註冊glad函數地址
	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
	{
		OutputDebugString("Failed to initialize GLAD");
		return -1;
	}
	glViewport(0, 0, 400, 400);

	//取得glfw窗口句柄並將其嵌入父窗口
	HWND hwndGLFW = glfwGetWin32Window(window);
	SetWindowLong(hwndGLFW, GWL_STYLE, WS_VISIBLE);
	MoveWindow(hwndGLFW, 0, 0, 400, 400, TRUE);
	SetParent(hwndGLFW, hwnd);

	//背景顏色
	glClearColor(0.2f, 0.3f, 0.3f, 1.0f);

	Init();

	//將渲染移交線程前需要將當前上下文設爲null
	glfwMakeContextCurrent(NULL);

	std::thread RenderThread(RenderProc);

	ShowWindow(hwnd, iCmdShow);
	UpdateWindow(hwnd);

	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	RenderThread.join();

	return msg.wParam;
}

void Init()
{
	float triangle_vertex[] =
	{
		0.0f,0.5f,0.0f,1.0f,0.0f,0.0f,
		-0.5f,-0.5f,0.0f,0.0f,1.0f,0.0f,
		0.5f,-0.5f,0.0f,0.0f,0.0f,1.0f
	};
	triangle=new TVertexArray(sizeof(triangle_vertex), triangle_vertex, { 3,3 });

	shader=new TShader("tri_vertex.glsl", "tri_fragment.glsl");
}

void RenderProc()
{
	glfwMakeContextCurrent(window);
	while (!glfwWindowShouldClose(window))
	{
		float time = glfwGetTime();

		//刷新顏色緩衝和深度
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		shader->UseProgram();
		shader->Uniform("angle", time);

		triangle->Bind();
		triangle->DrawTriangles();

		//
		glfwSwapBuffers(window);
		glfwPollEvents();
	}
	delete shader;
	delete triangle;
	glfwTerminate();
}


LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_CREATE:
		return 0;
	case WM_PAINT:
		HDC hdc;
		PAINTSTRUCT ps;
		RECT rect;
		GetClientRect(hwnd, &rect);
		hdc = BeginPaint(hwnd, &ps);
		DrawText (hdc, "This is text", -1, &rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
		EndPaint(hwnd, &ps);
		return 0;
	case WM_DESTROY:
		glfwSetWindowShouldClose(window, true);

		PostQuitMessage(0);
		return 0;
	case WM_QUIT:
		return 0;
	}

	return DefWindowProc(hwnd, message, wParam, lParam);
}

至於opengl窗口和主窗口的通信,就按多線程的通信方式來就行,最簡單就直接用全局變量,其他地方寫,RenderProc裏讀,就可以修改渲染內容了。或者用mutex啊condition_variable這些設施進行雙向通信都行。

限於篇幅TShader類和TVertexArray類就不粘貼了,你看過LearnOpenGL網站的話相信能寫出來,或者替換成你自己的渲染過程也行。

參考文獻
[1] MFC單文檔視圖中嵌入GLFW窗口
[2] cv::namedWindow, GLFWwindow以及其他程序嵌入到MFC中的教程

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章