PDA

View Full Version : Problems creating gl window in new thread



Gizmo of Dewitt
11-16-2005, 06:05 AM
I am having problems while creating a separate thread to create a window, set the pixel format, create a rendering context, and render, while the main thread handles a console window and other threads for network and sound

basically the window gets created and the pixel format and create context functions don't fail, but none of the gl commands do anything and the window sits there as a grey box.



...winmain...

does stuff, makes the console...

beginthread((void *)GWindow(),100000,*Main);

...

now in the thread
void GWindow(void){

creates window

if(Main->Graphics.Options.Hwnd!=NULL){
AddLog(" Game window created. ");
if((Main->Graphics.Options.Hdc=GetDC(Main->Graphics.Options.Hwnd))!=NULL){
AddLog(" Got HDC. ");

if(Main->Graphics.Options.APIMode==FXOPENGL){
static PIXELFORMATDESCRIPTOR pfd={
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
32, // Select Our Color Depth
0,0,0,0,0,0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0,0,0,0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0,0,0}; // Layer Masks Ignored
pfd.cColorBits=32;
GLint PixelFormat;
if((PixelFormat=ChoosePixelFormat(Main->Graphics.Options.Hdc,&pfd))!=0){
AddLog(" Got PixelFormat. ");
if((SetPixelFormat(Main->Graphics.Options.Hdc,PixelFormat,&pfd))!=0){
AddLog(" Pixel format success. ");
if((Main->Graphics.Options.Hrc=wglCreateContext(Main->Graphics.Options.Hdc))!=NULL){
AddLog(" Context created ");
if(wglMakeCurrent(Main->Graphics.Options.Hdc,Main->Graphics.Options.Hrc)){
AddLog(" Context made current. ");
ShowWindow(Main->Graphics.Options.Hwnd,SW_SHOW);
//hardwired for now, but is the same rect we make the window with rc.top=0;
rc.left=0;
rc.bottom=200;
rc.right=200;
glViewport(rc.left,rc.top,rc.right,rc.bottom); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
gluPerspective(45.0f,(GLfloat)(abs(rc.right-rc.left)+1)/(GLfloat)(abs(rc.bottom-rc.top)+1),0.1f,100.0f);
glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glShadeModel(GL_SMOOTH); glClearColor(1.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
Main->Graphics.Options.IsOpen=TRUE;
Main->Console.DoPlay=TRUE;
Main->Console.IsPlay=TRUE;
glFlush();
SwapBuffers(Main->Graphics.Options.Hdc);

}else{.....



this is simply copied and pasted from a single threaded win 32 app and slighty modified

nothing is drawn yet, but at least the window should turn black (clear the depth buffer)

it does nothing and has a grey background, not the windows background, but opengl paints the window grey and does nothing when functions are called

do i have to create a context in the main thread then share it with this thread even though the window belongs to this thread?

do i need to give in and use createthread (windows) instead of beginthread (process.h)
i have directx in there and it works, so i thought it was the redering context to blame

and can somebody explain wglShareList in some detail please, i havent seen anything about it yet

thank you

Gizmo of Dewitt
11-16-2005, 06:12 AM
oh yea...by the way

addlog(char *) simply writes the string to a log file so i know where it crashes (it doesnt crash thnx god)

and all those if's have matching curlies, its just there is like 30 lines of error handling (switching res, fx api's etc)

there is direct x in there, it is implemented so the user can switch api's from d3d8 to gl depending on his/her prefs

but im still at the window creation and initial depth buffer clearing stage

but the only thing i can imagine going wrong is with the hrc

Gizmo of Dewitt
11-17-2005, 02:14 AM
turns out it was a job for wglsharelists

i just had to create and makeactive a hrc for both threads and use wglsharelists to basically make them talk
just make sure to delte each hrc at the end of both threads

wglsharelists can be called in either thread, but use it in the thread that stays open the least, so when that thread gets excuted again it will do wglsharelists again

my ultilization was i had a window making thread that set up opengl and directx, depending on the user's settings
while another thread was made to render, another for sound , and another for network
so no matter how buggy the usb sound card (they freeze up in hardware excel mode), or how laggy the server, the fps will stay smooth and they will not slow down the response time for user input enough to notice, just make sure you use like sleep(20) for the render thread and something like sleep(100); for the others, so not a single thread will use over 20% cpu
if you dont use sleep then it will run horribly slow and it would defeat the purpose of multithreading since sleep lets the os run through all threads before returning

sleep is a must