OpenGL获取设备上下文 [英] OpenGL get Device Context

查看:392
本文介绍了OpenGL获取设备上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Windows上创建OpenGL应用程序.据我了解,我必须获得的第一件事情是设备上下文,它必须传递给选择和设置像素格式并创建渲染上下文的几个函数.我使用 OpenGL Wiki 来大致了解我应该做什么. 我的代码是这样的:

I am trying to create an OpenGL application on windows. 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. 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;
}

此打印

HDC: 0

我假设设备上下文指的是物理设备,但是我在某处读到它指的是任何可绘制的表面".在这两种情况下,我的问题是:如何获得非空DC?还是应该执行一套完全不同的步骤来设置整个OpenGL系统?

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? Or should I perform a completely different set of steps in order to set up this whole OpenGL system?

我在线上找到了很多教程,但是它们都使用GLUT,GLEW,GLFW,X11,SDL等库.库使某些事情变得容易,但是它们通常不会执行没有使用它们就不可能完成的任务.这次,我想尝试用困难的方式做事,因此不使用任何库,仅使用普通的OpenGL.

I found a lot of tutorials online, but they all use GLUT, GLEW, GLFW, X11, SDL etc. which are libraries. 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.

我终于找到了教程,该教程仅将Windows库用于创建一个窗口.

I found, at last, a tutorial that only used the windows libraries for creating a window.

推荐答案

您没有说明您的 OS ,但是我假设函数名称为Windows.问题与Reto Koradi在评论中指出的完全一样.要设置 OpenGL ,您需要执行以下操作:

You did not state your OS but I assume Windows from the function names. The problem is exactly as Reto Koradi stated in the comment. To set up OpenGL you need to do this:

  1. 获取具有有效设备上下文的对象的操作系统句柄

它可以是 OS 窗口或 OS 位图.如果您只有控制台应用程序,则需要首先创建一个有效的 OS 窗口并使用其句柄(据我所知,控制台没有Canvas).

It can be OS window or OS bitmap. 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).

您可以使用 GLUT 进行窗口创建,或者如果您的编译器 IDE 具有窗口应用程序,则可以使用该窗口.您还可以结合使用 OpenGL 和Window组件. VCL 也不是问题(我在 OpenGL 上使用了很多年)

you can use GLUT for the window creation or If your compiler IDE has an window App you can use that. You can also combine OpenGL and Window components. VCL is also not a problem (I am using it for years with OpenGL)

在Windows中,您可以使用 CreateWindowEx ,所以用Google为其示例...

In windows you can use CreateWindowEx so google an example for it...

无论如何,您都应该将句柄放在一个变量中,例如:

Anyway you should have your handle in a variable like:

HWND hwin=NULL;

如果您没有使用Windows应用程序的经验,请使用 GLUT .否则,您将需要学习很多知识,仅涉及窗口创建,事件的消息处理以及用户/应用程序交互,这对于没有指导的菜鸟来说真的是不知所措.

If you have no experience with windows applications then use GLUT for this. 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.

获取该句柄的设备上下文

HDC hdc = GetDC(hwin);

  • 设置设备上下文所需的像素格式

    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);
    

  • 为设备上下文创建OpenGL渲染上下文

    HGLRC hrc = wglCreateContext(hdc);
    

  • 将其设置为默认OpenGL上下文

    wglMakeCurrent(hdc, hrc);
    

  • 这是绝对最小值,没有任何错误检查,其他缓冲区等.有关更多信息和实际代码,请参见相关的 QA :

    This is absolute minimum without any error checking , additional buffers etc. For more info and actual code see related QA's:

    • How to render an openGL frame in C++ builder? for oldstyle GL
    • simple complete GL+VAO/VBO+GLSL+shaders example in C++ with the new stuff

    您可以全部使用 GLUT .这是我通过快速搜索发现的第一击:

    You can use GLUT for all of this. This is first hit I found by quick search:

    或者按照 OpenGL 教程进行操作,其中有很多...

    Or follow OpenGL tutorials there are tons of them out there ...

    这篇关于OpenGL获取设备上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆