简体   繁体   English

OpenGL获取设备上下文

[英]OpenGL get Device Context

I am trying to create an OpenGL application on windows. 我正在尝试在Windows上创建OpenGL应用程序。 As far as I can understand, one of the first things I must acquire is a Device Context, which must be passed on to a couple of functions that choose and set a pixel format and create a rendering context. 据我了解,我必须获得的第一件事情是设备上下文,它必须传递给选择和设置像素格式并创建渲染上下文的几个函数。 I used the OpenGL wiki to get a rough idea about what I should do. 我使用OpenGL Wiki大致了解了我应该怎么做。 My code is something like: 我的代码是这样的:

#include <iostream>
#include <windef.h>
#include <wingdi.h>

HDC hdc;

int main() {
    hdc = wglGetCurrentDC();
    std::cout << "HDC: " << hdc << std::endl;
    return 0;
}

This prints 此打印

HDC: 0

I assumed a Device Context refers to a physical device, but I read somewhere that it refers to any drawable "surface". 我假设设备上下文是指物理设备,但我在某处读到它指的是任何可绘制的“表面”。 In both cases is my question: how can I obtain a non-null DC? 在这两种情况下,我的问题是:如何获得非空DC? Or should I perform a completely different set of steps in order to set up this whole OpenGL system? 还是应该执行一套完全不同的步骤来设置整个OpenGL系统?

I found a lot of tutorials online, but they all use GLUT, GLEW, GLFW, X11, SDL etc. which are libraries. 我在线上找到了很多教程,但是它们都使用GLUT,GLEW,GLFW,X11,SDL等库。 Libraries make certain things easier, but they usually do not perform tasks that are impossible without using them. 库使某些事情变得容易,但是它们通常不会执行没有使用它们就不可能完成的任务。 This time, I want to try to do things the hard way and therefore use no libraries, just plain OpenGL. 这次,我想尝试用困难的方式做事,因此不使用任何库,仅使用普通的OpenGL。

I found, at last, a tutorial that only used the windows libraries for creating a window. 我终于找到只使用Windows库创建窗口的教程

You did not state your OS but I assume Windows from the function names. 您没有说明您的操作系统,但我从功能名称假定为Windows。 The problem is exactly as Reto Koradi stated in the comment. 问题完全与Reto Koradi在评论中指出的一样。 To set up OpenGL you need to do this: 要设置OpenGL,您需要执行以下操作:

  1. Obtain OS handle to object with valid device context 获取具有有效设备上下文的对象的操作系统句柄

    It can be OS window or OS bitmap. 它可以是OS窗口或OS位图。 If you have just console app then you need to create a valid OS window first and use its handle (to my knowledge console does not have Canvas). 如果您只有控制台应用程序,则需要首先创建一个有效的OS窗口并使用其句柄(据我所知,控制台没有Canvas)。

    you can use GLUT for the window creation or If your compiler IDE has an window App you can use that. 您可以使用GLUT进行窗口创建,或者如果您的编译器IDE具有窗口应用程序,则可以使用GLUT You can also combine OpenGL and Window components. 您也可以结合使用OpenGL和Window组件。 VCL is also not a problem (I am using it for years with OpenGL ) VCL也不是问题(我在OpenGL中使用了多年)

    In windows you can use CreateWindowEx so google an example for it... 在Windows中,您可以使用CreateWindowEx,以便在Google上找到它的示例...

    Anyway you should have your handle in a variable like: 无论如何,您都应将句柄放在类似以下变量中:

     HWND hwin=NULL; 

    If you have no experience with windows applications then use GLUT for this. 如果您没有使用Windows应用程序的经验,请为此使用GLUT Otherwise you would need to learn a lot of stuff just to cover window creation, message handling of events and user/app interaction which can be really overwhelming for a rookie without guide. 否则,您将需要学习很多知识,仅涉及窗口创建,事件的消息处理以及用户/应用程序交互,这对于没有指导的新手来说真的是不堪重负。

  2. Get Device context for that handle 获取该句柄的设备上下文

     HDC hdc = GetDC(hwin); 
  3. Set pixel format you need of device context 设置设备上下文所需的像素格式

     PIXELFORMATDESCRIPTOR pfd; ZeroMemory( &pfd, sizeof( pfd ) ); // set the pixel format for the DC pfd.nSize = sizeof( pfd ); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 24; pfd.cDepthBits = 24; pfd.iLayerType = PFD_MAIN_PLANE; SetPixelFormat(hdc,ChoosePixelFormat(hdc, &pfd),&pfd); 
  4. Create OpenGL rendering context for device context 为设备上下文创建OpenGL渲染上下文

     HGLRC hrc = wglCreateContext(hdc); 
  5. Set it as default OpenGL context 将其设置为默认OpenGL上下文

     wglMakeCurrent(hdc, hrc); 

This is absolute minimum without any error checking , additional buffers etc. For more info and actual code see related QA 's: 这是绝对最小值,没有任何错误检查,其他缓冲区等。有关更多信息和实际代码,请参见相关的QA

You can use GLUT for all of this. 您可以对所有这些使用GLUT This is first hit I found by quick search: 这是我通过快速搜索发现的第一击:

Or follow OpenGL tutorials there are tons of them out there ... 或按照OpenGL教程进行操作,其中有很多...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM