步骤:

1、创建windows窗口,得到窗口句柄hwnd

2、获取该窗口的设备环境hDC(当然也可以获取其他的设备环境,但我们一般是在创建的窗口上绘制)

3、创建OpenGL绘制环境RC,这个只能从hDC创建

4、将hDC和RC绑定到当前的线程

注:RC表示OpenGL的绘制环境,所有的OpenGL命令都会在RC这个绘制环境中作用,所以必须在RC绑定到当前线程之后才能调用OpenGL命令,否则运行出错,内存访问错误。

  一般的笔刷绘制,在hDC下即可。

封装的窗口类如下:

  GLWindow.h

#pragma once
#include <windows.h>
#include <GL/glew.h>
#include <iostream>
class GLContext
{
public:
GLContext();
~GLContext();
void Setup(HWND,HDC);
void SetupPixelFormat(HDC);
private:
HWND hWnd;
HDC hDC;
HGLRC hRC;
int format;
}; GLContext::GLContext()
{
this->hWnd = ;
this->hDC = ;
this->hRC = ;
this->format = ;
}
GLContext::~GLContext()
{
} void GLContext::SetupPixelFormat(HDC hDC) {
int pixelFormat; PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), // size
, // version
PFD_SUPPORT_OPENGL | // OpenGL window
PFD_DRAW_TO_WINDOW | // render to window
PFD_DOUBLEBUFFER, // support double-buffering
PFD_TYPE_RGBA, // color type
, // prefered color depth
, , , , , , // color bits (ignored)
, // no alpha buffer
, // alpha bits (ignored)
, // no accumulation buffer
, , , , // accum bits (ignored)
, // depth buffer
, // no stencil buffer
, // no auxiliary buffers
PFD_MAIN_PLANE, // main layer
, // reserved
, , , // no layer, visible, damage masks
}; pixelFormat = ChoosePixelFormat(hDC, &pfd);
SetPixelFormat(hDC, pixelFormat, &pfd);
} void GLContext::Setup(HWND hwnd, HDC hdc) {
this->hWnd = hwnd;
this->hDC = hdc;
SetupPixelFormat(hDC);
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC); //initialize glew
glewExperimental = GL_TRUE;
glewInit();
if (AllocConsole())
{
freopen("CONOUT$", "w+t", stdout);
freopen("CONOUT$", "w+t", stderr);
const GLubyte* Devise = glGetString(GL_RENDERER); //返回一个渲染器标识符,通常是个硬件平台
const GLubyte* str = glGetString(GL_VERSION);
printf("OpenGL实现的版本号:%s\n", str);
printf("硬件平台:%s\n", Devise);
}
} LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); class GLWindow
{
public:
GLWindow();
~GLWindow();
void Setup(HINSTANCE, HINSTANCE, LPSTR, int);
//LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void RegisterDisplayFunc(void (*display)()) {
this->Display = display;
}
void Run(); private:
void(*Display)(); private:
bool exiting = false;
long windowWidth = ;
long windowHeight = ;
long windowBits = ;
bool fullscreen = false; WNDCLASSEX windowClass; // window class
HWND hwnd; // window handle
HDC hDC;
MSG msg; // message
DWORD dwExStyle; // Window Extended Style
DWORD dwStyle; // Window Style
RECT windowRect; GLContext glContext;
};
GLWindow::GLWindow() {}
GLWindow::~GLWindow() {} void GLWindow::Setup(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
windowRect.left = (long); // Set Left Value To 0
windowRect.right = (long)windowWidth; // Set Right Value To Requested Width
windowRect.top = (long); // Set Top Value To 0
windowRect.bottom = (long)windowHeight; // Set Bottom Value To Requested Height // fill out the window class structure
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc = MainWindowProc; //当窗体触发任何一个事件时,便会调用该函数
windowClass.cbClsExtra = ;
windowClass.cbWndExtra = ;
windowClass.hInstance = hInstance;
windowClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); // default icon
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW); // default arrow
windowClass.hbrBackground = NULL; // don't need background
windowClass.lpszMenuName = NULL; // no menu
windowClass.lpszClassName = L"Windows API";
windowClass.hIconSm = LoadIcon(NULL, IDI_WINLOGO); // windows logo small icon // register the windows class
if (!RegisterClassEx(&windowClass)) {
puts("Register Class Failed");
}
dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
dwStyle = WS_OVERLAPPEDWINDOW; // Windows Style
AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size // class registered, so now create our window
hwnd = CreateWindowEx(NULL, // extended style
L"Windows API", // class name
L"OpenGL", // app name
dwStyle | WS_CLIPCHILDREN |
WS_CLIPSIBLINGS,
, , // x,y coordinate
windowRect.right - windowRect.left,
windowRect.bottom - windowRect.top, // width, height
NULL, // handle to parent
NULL, // handle to menu
hInstance, // application instance
NULL); // no extra params ShowWindow(hwnd, SW_SHOW); // display the window
UpdateWindow(hwnd); // update the window
hDC = GetDC(hwnd);
glContext.Setup(hwnd,hDC);
} LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
int height, width; // dispatch messages
switch (uMsg)
{
case WM_CREATE: // window creation
break; case WM_DESTROY: // window destroy
case WM_QUIT:
CloseWindow(hWnd);
break;
case WM_CLOSE: // windows is closing // deselect rendering context and delete it
//wglMakeCurrent(hDC, NULL);
//wglDeleteContext(hRC);
// send WM_QUIT to message queue
PostQuitMessage();
break; case WM_SIZE:
height = HIWORD(lParam); // retrieve width and height
width = LOWORD(lParam);
break; case WM_ACTIVATEAPP: // activate app
break; case WM_PAINT: // paint
PAINTSTRUCT ps;
BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break; case WM_LBUTTONDOWN: // left mouse button
break; case WM_RBUTTONDOWN: // right mouse button
break; case WM_MOUSEMOVE: // mouse movement
break; case WM_LBUTTONUP: // left button release
break; case WM_RBUTTONUP: // right button release
break; case WM_KEYUP:
break; case WM_KEYDOWN:
int fwKeys;
LPARAM keyData;
fwKeys = (int)wParam; // virtual-key code
keyData = lParam; // key data switch (fwKeys)
{
case VK_ESCAPE:
PostQuitMessage();
break;
default:
break;
}
break; default:
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
} void GLWindow::Run() {
while (true)
{
(*Display)();
SwapBuffers(hDC);
while (PeekMessage(&msg, NULL, , , PM_NOREMOVE))
{
if (!GetMessage(&msg, NULL, , ))
{
exiting = true;
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}

主函数调用代码示例:

#include <Windows.h>
#include <cameras/phc.h>
#include "shader.h"
#include "Cube.h"
#include "GLWindow.h"
redips::PhC phc;
GLWindow window; Shader* shader;
Cube* cube; void Display() {
cube->Draw(*shader, phc);
} void Initialize(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
phc.lookAt(redips::float3(, , ), redips::float3(, , ), redips::float3(, , ));
window.Setup(hInstance, hPrevInstance, lpCmdLine, nShowCmd);
window.RegisterDisplayFunc(Display); shader = new Shader("./joint.vert", "./joint.frag");
cube = new Cube();
} int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
Initialize(hInstance, hPrevInstance, lpCmdLine, nShowCmd); window.Run();
return ;
}

运行:

Windows API 搭建OpenGL窗口的更多相关文章

  1. 用C#调用Windows API向指定窗口发送按键消息 z

    用C#调用Windows API向指定窗口发送 一.调用Windows API. C#下调用Windows API方法如下: 1.引入命名空间:using System.Runtime.Interop ...

  2. 【转】用C#调用Windows API向指定窗口发送

    一.调用Windows API. C#下调用Windows API方法如下: 1.引入命名空间:using System.Runtime.InteropServices; 2.引用需要使用的方法,格式 ...

  3. 用C#调用Windows API向指定窗口发送按键消息

    一.调用Windows API. C#下调用Windows API方法如下: 1.引入命名空间:using System.Runtime.InteropServices; 2.引用需要使用的方法,格式 ...

  4. windows API下的模板缓冲(stencil buffer)

    在windows API搭建的OpenGL窗口中使用模板缓冲,需要在像素格式描述表中设置stencil buffer位宽为8,这样窗口会自动生成stencil buffer,然后可以在opengl环境 ...

  5. 善于 调用Windows API

    前一段时间看见别人做的一个自动填写信息并且点击登录的程序,觉得很有意思. 其实就是在程序中调用Windows的API,那么如何调用,下面就做个简单的介绍. 写的简单粗暴, 不喜轻喷. 0.首先引入名称 ...

  6. Windows MFC 两个OpenGL窗口显示与线程RC问题

    问题为:背景界面是一个OpenGL窗口(对话框),在其上弹出一个OpenGL窗口(模态对话框)时, 1.上方的OpenGL窗口能响应鼠标操作等并刷新: 2.当移动或放大缩小上方的OpenGL窗口时,其 ...

  7. [Modern OpenGL系列(二)]创建OpenGL窗口

    本文已同步发表在CSDN:http://blog.csdn.net/wenxin2011/article/details/51295663 在博主的上一篇文章中已经介绍了OpenGL开发环境的搭建,本 ...

  8. Android学习——windows下搭建Cygwin环境

    在上一篇博文<Android学习——windows下搭建NDK_r9环境>中,我们详细的讲解了在windows下进行Android NDK开发环境的配置,我们也讲到了在NDk r7以后,我 ...

  9. OpenGL学习之windows下安装opengl的glut库

    OpenGL学习之windows下安装opengl的glut库 GLUT不是OpenGL所必须的,但它会给我们的学习带来一定的方便,推荐安装.  Windows环境下的GLUT下载地址:(大小约为15 ...

随机推荐

  1. RAC改动归档文件夹

    逐个节点改动 关闭全部节点,启动单节点(rac1)到mount状态 SQL> startup mount; 改动server參数配置 SQL> alter system set clust ...

  2. JS 计算2个日期相差的天数

    <span style="font-size:18px;">function getDays(strDateStart,strDateEnd){ var strSepa ...

  3. ORA-07445 第一參数为:kkqljpmpr

      在版本号11.2.0.1.0上,在pl/sql developer中运行一条SQL会导致连接中断,这样的错误要到trace文件夹下找到错误日志文件,再定位.查了一下资料,是这个版本号的bug. D ...

  4. HDU 3039 Go Home

    今天本来解决的很好,本来可以不聊那么结束,但是我想更完美一点,多聊几句,谁知道就聊了很长时间,很傻逼.耽误了时间! /***************************************** ...

  5. Matlab依据样本随机数求概率曲线

    相关Matlab函数:hist, bar, cdfplot, ksdensity (1) hist函数 n = hist(Y, x)  假设x是一个向量,返回x的长度个以x为中心的,Y的分布情况. 比 ...

  6. mysql下distinct和group by区别对比

    在数据表中记录了用户验证时使用的书目,现在想取出所有书目,用DISTINCT和group by都取到了我想要的结果,但我发现返回结果排列不同,distinct会按数据存放顺序一条条显示,而group ...

  7. AppFuse 3的乱码问题

    书接上回:AppFuse 3常见问题与解决方法,一个新问题:乱码! 在3.0.0版本号下,运行mvn appfuse:full-source命令后使用mvn jetty:run启动工程,页面上就出现了 ...

  8. sql建表,建索引注意事项

    建表注意 .建议字段定义为NOT NULL 搜索引擎 MyISAM InnoDB 区别 InnoDB和MyISAM是许多人在使用MySQL时最常用的两个表类型,这两个表类型各有优劣,视具体应用而定.基 ...

  9. python 1: 解决linux系统下python中的matplotlib模块内的pyplot输出图片不能显示中文的问题

    问题: 我在ubuntu14.04下用python中的matplotlib模块内的pyplot输出图片不能显示中文,怎么解决呢? 解决: 1.指定默认编码为UTF-8: 在python代码开头加入如下 ...

  10. ubuntu gcc低版本过低引起错误

    错误内容: 正在读取软件包列表... 完成正在分析软件包的依赖关系树 正在读取状态信息... 完成 您可能需要运行“apt-get -f install”来纠正下列错误:下列软件包有未满足的依赖关系: ...