步骤:

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. Android--向SD卡读写数据

    // 向SD卡写入数据 private void writeSDcard(String str) { try { // 推断是否存在SD卡 if (Environment.getExternalSto ...

  2. 阿里云 ubuntu 14.04 模板上安装 docker

    ubuntu 14.04 的内核是 3.13 ,所以内核不用升级. 安装过程例如以下: # apt-get update # apt-get install apt-transport-https # ...

  3. android 项目R文件丢失解决的方法

    R文件丢失的原因有非常多,这里提供几种解决的方法: 1.  选中项目,点击 Project - Clean , 清理一下项目. 2. 选中项目,右键 选择 Android Tools  - Fix P ...

  4. C++ string 实现大整数相加减

    随意两个大整数的加减算法.可自己主动推断正负号.代码例如以下: #include <iostream> #include <vector> #include <cstri ...

  5. mysql使用“.frm”文件恢复表结构

    mysql创建每张表后都会在“mysql安装目录/data/数据库名/”目录下创建一个“表名.frm”文件. 该.frm文件并不能直接打开,但是它可以帮助你恢复你的表结构~~ 具体操作如下: 我现在准 ...

  6. locate和grep命令

    这任然是一篇读书笔记,以下内容来源<linux命令速查手册>这本书. 1.locate命令 用文件名或者文件名的部分来搜索文件在系统中的位置.locate不搜索具体目录,而是搜索一个数据库 ...

  7. Hive 自定义函数 UDF UDAF UDTF

    1.UDF:用户定义(普通)函数,只对单行数值产生作用: 继承UDF类,添加方法 evaluate() /** * @function 自定义UDF统计最小值 * @author John * */ ...

  8. HDU4280 Island Transport —— 最大流 ISAP算法

    题目链接:https://vjudge.net/problem/HDU-4280 Island Transport Time Limit: 20000/10000 MS (Java/Others)   ...

  9. 一步一步学Silverlight 2系列(17):数据与通信之ADO.NET Data Services

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  10. 关于base64编码的原理及实现

    我们的图片大部分都是可以转换成base64编码的data:image. 这个在将canvas保存为img的时候尤其有用.虽然除ie外,大部分现代浏览器都已经支持原生的基于base64的encode和d ...