步骤:

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. J2SE核心开发实战(二)——字符串与包装类

    字符串与包装类 一.实验简单介绍 在本章.我们将学习一些用于处理字符串的API以及包装类的相关知识. 本章知识点 字符串API 包装类及其应用 二.认识字符串类 1. 什么是字符串类 Java字符串类 ...

  2. Linux MySQL主从复制(Replication)配置

    MySQL是开源的关系型数据库系统.复制(Replication)是从一台MySQL数据库服务器(主服务器master)复制数据到另一个服务器(从服务器slave)的一个进程. 配置主服务器(mast ...

  3. 还在为开发APP发愁? 这里就有现成通用的代码!

    1.开源控件 1)首页: 1.1)首先是下拉刷新数据的 SwipeRefreshLayout 地址:https://github.com/hanks-zyh/SwipeRefreshLayout 1. ...

  4. Remove FileUtil#copyMerge

    [HADOOP-12967] Remove FileUtil#copyMerge - ASF JIRA https://issues.apache.org/jira/browse/HADOOP-129 ...

  5. queue — A synchronized queue class

    https://docs.python.org/3.6/library/queue.html https://github.com/python/cpython/blob/3.6/Lib/queue. ...

  6. 使用Qt发送HTTPS请求

    示例代码: #include "mainwindow.h" #include "ui_mainwindow.h" #include <QNetworkAc ...

  7. linux静默安装Oracle 11g

    ./runInstaller -silent -force -responseFile /home/oracle/software/database/response/orcl_install.rsp ...

  8. LA-4356&&hdu-2469 (极角排序+扫描线)

    题目链接: Fire-Control System Time Limit: 12000/5000 MS (Java/Others)     Memory Limit: 32768/32768 K (J ...

  9. 【POJ 1655】 Balancing Act

    [题目链接] 点击打开链接 [算法] 树形DP求树的重心 [代码] #include <algorithm> #include <bitset> #include <cc ...

  10. Android控件之CalendarView 日历对话框

    在Android 3.0中新增的日历视图控件可以显示网格状的日历内容,android.widget.CalendarView是从android.widget.FrameLayout中继承. Calen ...