wglMakeCurrent

wglMakeCurrent 函数设定OpenGL当前线程的渲染环境。以后这个线程所有的OpenGL调用都是在这个hdc标识的设备上绘制。你也可以使用wglMakeCurrent 函数来改变调用线程的当前渲染环境,使之不再是当前的渲染环境。



BOOL wglMakeCurrent(
HDC hdc, // device context of device that OpenGL calls are
// to be drawn on
HGLRC hglrc // OpenGL rendering context to be made the calling
// thread's current rendering context
);



参数:

hdc

设备环境的句柄。调用这个函数的线程接下来的所有OpenGL调用都将在这个hdc所标识的设备上绘制。

hglrc

函数设定的OpenGL渲染环境的句柄,作为当前线程的渲染环境。

如果hglrc为NULL,函数将使调用线程的当前渲染环境不再作为当前的渲染环境,并释放这个渲染环境所使用的设备环境。这种情况下hdc参数将被忽略。



返回值:

当wglMakeCurrent 函数调用成功,将返回TRUE,否则返回FALSE,要想得到更多的信息,请调用

GetLastError。



注意:

hdc参数的绘制表面必须是支持OpenGL的。它不需是wglCreateContext 创建hglrc时所传进来的hdc,但它必须是相同的设备并具有相同的像素格式。GDI转换的hdc将不被渲染环境所支持。当前的渲染环境将一直使用hdc设备环境直到它不再是当前渲染环境。


在切换到新的渲染环境之前,OpenGL将冲刷掉当前调用线程之前的所有渲染环境。



一个线程可以有一个渲染环境。所以一个多线程进程可以有多个渲染环境。一个线程在调用任何OpenGL函数之前必须设置一个当前渲染环境。否则所有OpenGL调用都将忽略。



在某一时刻渲染环境只能属于一个线程,你不能使一个渲染环境同时属于多个线程。



一个应用程序可以通过不同线程中的不同的当前渲染环境产生多个绘图,它支持各个线程都拥有自己的渲染环境的设备环境。



如果有错误发生,wglMakeCurrent 函数在返回之前将使渲染环境不作为线程当前的渲染环境。



MSDN原文:

wglMakeCurrent
The wglMakeCurrent function makes a specified OpenGL rendering context the calling thread's current rendering context. All subsequent OpenGL calls made by the thread are drawn on the device identified by hdc. You can also use wglMakeCurrent to change the calling thread's current rendering context so it's no longer current.

BOOL wglMakeCurrent(
HDC hdc, // device context of device that OpenGL calls are
// to be drawn on
HGLRC hglrc // OpenGL rendering context to be made the calling
// thread's current rendering context
);
Parameters
hdc
Handle to a device context. Subsequent OpenGL calls made by the calling thread are drawn on the device identified by hdc.
hglrc
Handle to an OpenGL rendering context that the function sets as the calling thread's rendering context.
If hglrc is NULL, the function makes the calling thread's current rendering context no longer current, and releases the device context that is used by the rendering context. In this case, hdc is ignored.

Return Values
When the wglMakeCurrent function succeeds, the return value is TRUE; otherwise the return value is FALSE. To get extended error information, call GetLastError.

Remarks
The hdc parameter must refer to a drawing surface supported by OpenGL. It need not be the same hdc that was passed to wglCreateContext when hglrc was created, but it must be on the same device and have the same pixel format. GDI transformation and clipping in hdc are not supported by the rendering context. The current rendering context uses the hdc device context until the rendering context is no longer current.

Before switching to the new rendering context, OpenGL flushes any previous rendering context that was current to the calling thread.

A thread can have one current rendering context. A process can have multiple rendering contexts by means of multithreading. A thread must set a current rendering context before calling any OpenGL functions. Otherwise, all OpenGL calls are ignored.

A rendering context can be current to only one thread at a time. You cannot make a rendering context current to multiple threads.

An application can perform multithread drawing by making different rendering contexts current to different threads, supplying each thread with its own rendering context and device context.

If an error occurs, the wglMakeCurrent function makes the thread's current rendering context not current before returning.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
GLfloat initX = 0, initY = 0; GLfloat oldx = 0, oldy = 0; int times = 0; bool gDrawline = false; void drawkoch(GLfloat dir, GLfloat len, GLint iter) { ​GLdouble dirRad = dir * 3.1415926 / 180.f; ​GLfloat newX = oldx + len * cos(dirRad); ​GLfloat newY = oldy + len * sin(dirRad); ​if (iter == 0) { ​​glVertex2f(oldx, oldy); ​​glVertex2f(newX, newY); ​​oldx = newX; ​​oldy = newY; ​} ​else { ​​iter--; ​​len = len / 3; ​​drawkoch(dir, len, iter); ​​dir += 60; ​​drawkoch(dir, len, iter); ​​dir -= 120; ​​drawkoch(dir, len, iter); ​​dir += 60; ​​drawkoch(dir, len, iter); ​} } typedef GLfloat point2d[2]; int iter = 0; float snowAngle = 0; point2d p = { 960, 20 }; point2d anyline[2]; point2d pp; void CMFCGLSetupView::OnDraw(CDC* pDC) { ​CMFCGLSetupDoc* pDoc = GetDocument(); ​ASSERT_VALID(pDoc); ​if (!pDoc) ​​return; ​// TODO: add draw code for native data here ​wglMakeCurrent(pDC->m_hDC, m_hRC); ​oldx = initX; ​oldy = initY; ​glClearColor(0.0f, 0.0f, 0.0f, 0.0f); ​glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ​glMatrixMode(GL_PROJECTION); ​glLoadIdentity(); ​glOrtho(0, Cx, 0, Cy, -1000, 1000); ​switch (drawMode) { ​case 0: ​​glTranslatef(400, 450, 0); ​​glBegin(GL_LINES); ​​glColor3f(0.0, 1.0, 0.0); ​​drawkoch(0, 500, iter); ​​glEnd(); ​​break; ​case 1: ​​glTranslatef(400, 450, 0); ​​glPushMatrix(); ​​glTranslated(250, -125 * sqrt(3), 0); ​​glRotated(snowAngle, 0, 1, 0); ​​glTranslated(-250, 125 * sqrt(3), 0); ​​glBegin(GL_LINES); ​​glColor3f(0.0, 1.0, 0.0); ​​drawkoch(0, 500, iter); ​​drawkoch(-120, 500, iter); ​​drawkoch(-240, 500, iter); ​​glEnd(); ​​glPopMatrix(); ​​break; ​case 2: ​​if (gDrawline) { ​​​oldx = anyline[0][0]; ​​​oldy = anyline[0][1]; ​​​point2d v; ​​​v[0] = (anyline[1][0] - anyline[0][0]); ​​​v[1] = (anyline[1][1] - anyline[0][1]); ​​​float len = sqrt(v[0] * v[0] + v[1] * v[1]); ​​​v[0] /= len; ​​​v[1] /= len; ​​​point2d n = { 1, 0 }; ​​​float cosTheta = v[0] * n[0] + v[1] * n[1]; ​​​float angle = acos(cosTheta) * 180.f / PI; ​​​if (anyline[1][1] < anyline[0][1]) { ​​​​angle = -angle; ​​​} ​​​glBegin(GL_LINES); ​​​drawkoch(angle, len, iter); ​​​glEnd(); ​​} ​​break; ​default: break; ​} ​glBegin(GL_POINTS); ​glVertex2f(pp[0], pp[1]); ​glEnd(); ​glFlush(); ​wglMakeCurrent(pDC->m_hDC, NULL); }
最新发布
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值