c++ - How can I draw inside two separate 3D windows within the same application on Windows using OpenGL? -


i implementing plug-in inside 3rd party program in c++ on windows.

the 3rd party program has window displays 3d graphics using opengl. need plug-in create window displays 3d graphics using opengl.

do need create new opengl rendering context window or there way can "reuse" opengl rendering context used 3rd party program?

i assumed had create new opengl rendering context , tried following:

// create rendering context   hglrc = wglcreatecontext (hdc);   // make calling thread's current rendering context  wglmakecurrent (hdc, hglrc); 

however last function failed. reading the documentation of wglmakecurrent notice

a thread can have 1 current rendering context. process can have multiple rendering contexts means of multithreading.

does mean window need run in separate thread 3rd party program?

you didn't post error code generated wglmakecurrent(), won't guessing reason. it's not binding itself, however. sentence 'a thread can have 1 current rendering context' means, new context 'replace' old 1 , become current. don't know why trying set 2 contexts current (or run thread), it's not way go. avoid multithreading in rendering unless it's absolutely necessary. so, answering question:

yes, can 'reuse' opengl rendering context.

why, may ask? rendering context created specific device context (hdc), exclusive property of each window (hwnd)! how possible, then?!

well, seems somehow impossible because of function prototypes:

      hwnd my_window = createwindow(...);     hdc my_dc = getdc(my_new_window);      //setup pixel format 'my_dc'...      hglrc my_rc = wglcreatecontext(my_dc);     wglmakecurrent(my_dc, my_rc);  

this lets think rendering context bound specific device context , valid it. it's not. critical part comment (setup pixel format). rendering context created specific class of dcs, more precise: dcs same pixel format. code below valid:

      //window_1 = main window, window_2 = window      hdc dc_1 = getdc(window_1);     set_pixel_format_for_dc_1(); //usual stuff     hglrc rc = wglcreatecontext(dc_1);      wglmakecurrent(dc_1, rc);     ultra_super_draw();      //.....      hdc dc_2 = getdc(window_2);      //get dc_1's pf make sure it's compatible rc.     int pf_index = getpixelformat(dc_1);     pixelformatdescriptor pfd;     zeromemory(&pfd, sizeof(pixelformatdescriptor));     describepixelformat(dc_1, pf_index, sizeof(pixelformatdescriptor), &pfd);      setpixelformat(dc_2, pf_index, &pfd);      wglmakecurrent(dc_2, rc);     another_awesome_render();      wglmakecurrent(null, null);  

if still not convinced, msdn:

wglmakecurrent(hdc, hglrc): hdc parameter must refer drawing surface supported opengl. need not same hdc passed wglcreatecontext when hglrc created, must on same device , have same pixel format.

i guess familiar these calls. now, don't know conditions rendering must meet, without additional requirements, don't see difficulties point:

      hdc my_dc = create_my_dc();      //...      void my_new_render     {         //probably want save current binding:         hdc current_dc = wglgetcurrentdc();         hglrc current_context = wglgetcurrentcontext();          wglmakecurrent(my_dc, current_context);          myultrasuperrender(...);          wglmakecurrent(current_dc, current_context);     }  

hope helps :)


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -