Sign in to follow this  
fredrik90

Win32 rendering

Recommended Posts

you can set up opengl in windows mode. I suggest downloading glut as this will hide many of the complexities from you. makes it a snap.

google glut


Share this post


Link to post
Share on other sites
You don't need GLUT. Just make a rendering context inside the client area as you normally would. I throw GL rendering contexts all over the place inside windows and dialogs and I never have to do anything 'complex'. It all works the same.

Share this post


Link to post
Share on other sites
I'd suggest creating a sub-control (probably a frame of some sort, not sure exactly what the native windows widgets are since I mainly do GUIs in .NET) that has an HDC associated with it. You should be able to render OpenGL to it with the usual configuration after that.

Share this post


Link to post
Share on other sites
I use OpenGLAPPanel with C++ Builder and I can just drop controls right on the window the OpenGL context is attached to. If you try just putting it on the window as you normally would, but it disappears look at how you're handling WM_ERASEBKGND.

Share this post


Link to post
Share on other sites

Make a static control in the client area of the window, using GetClientRect to get the right size. I forget how to manually create one, because I use RESED for that, to make one in a dialog.

Now, I used this code to create my context. This is out of my class, but you should be able to modify it easy. If you need to see what's in SetupPixelFormat(), just look at the first nehe tutorial.

//init opengl in a premade control
int gl_video::MakeControl(HWND &hWnd)
{
//make sure the handle points to a valid control
if (hWnd == NULL)
{
MessageBox(NULL, "ERROR: Could not find control handle.", m_AppTitle, MB_OK | MB_ICONERROR);
return false;
}

m_WindowHandle = hWnd;

//atempt to create a device context
m_DeviceContext = GetDC(m_WindowHandle);
if (m_DeviceContext == NULL)
{
MessageBox(NULL,"ERROR: Could not create device context.",m_AppTitle, MB_OK | MB_ICONERROR);
return false;
}

//attempt to get a pixel format
if (!SetupPixelFormat())
{
MessageBox(NULL,"ERROR: Could not set pixel format.",m_AppTitle, MB_OK | MB_ICONERROR);
return false;
}

//attempt to initialize the rendering context
m_RenderContext = wglCreateContext(m_DeviceContext);
if (m_RenderContext == NULL)
{
MessageBox(NULL,"ERROR: Could not create rendering context.",m_AppTitle, MB_OK | MB_ICONERROR);
return false;
}

if (!wglMakeCurrent(m_DeviceContext, m_RenderContext))
{
MessageBox(NULL,"ERROR: Could not activate rendering context.",m_AppTitle, MB_OK | MB_ICONERROR);
return false;
}

//get the size of the control into a rect
//and set the window size member variables
RECT WindowRect;
GetWindowRect(m_WindowHandle, &WindowRect);
m_WindowWidth = WindowRect.right-WindowRect.left;
m_WindowHeight = WindowRect.bottom-WindowRect.top;

return true;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by fredrik90
I was thinking about, how do you do it with WM_PAINT and WM_ERRASBGND
Do you plan to be constantly rendering? When I do that it's never a problem. If your only rendering after set events, then I don't know how to handle it.

Share this post


Link to post
Share on other sites
All above is to much work :)
Forget it

Maybe forget the window all together and start thinking Dialog Boxes.

Just make a dialog box with the resource editor, specify an area for opengl en render in there (set de device context there etc using getClientRect)

This way you can create a window, with menu, just as a normal window, BUT you can also add buttons everywhere and all kinds of other crap (which you can do as simply in a normal window)
The best thing is, that it takes almost no code (especially comparing to a normal window set-up)
Another great advantage is that the area for opengl can be dragged and sized everywhere in the dialog with the editor.

One slight glitch here, is when you start to work with mouse-coords, they will be 0,0 at the top left of the dialog screen, instead of the window for openGL but this is also very easily fixed with 3 lines of code

about 10 lines MAX and you're good to go, with much more flexibility than the standard window

Share this post


Link to post
Share on other sites
start of with a main handle ;

HWND MainWindow = CreateDialog ( Instance, (LPCTSTR)ID_OPENGL_WINDOW, MainWindow, ProgramProc );

Then get the opengl screen(eg, a picture which is just put in the dialog using the resource editor, can be anything.)
the place and size of this object will be where you render the openGL

HWND Screen = GetDlgItem ( MainWindow, ID_WINDOW );

set device context
DC = GetDC ( Screen );

get the rectangle dimension for the openGL screen
GetClientRect ( Screen, &WindowRectangle );

set the pixelformat
SetPixelFormat ( bits );

do the rendering context
RenCont() = wglCreateContext ( DevCont() );
wglMakeCurrent ( DevCont(), RenCont() );

And then simply show the program
ShowWindow ( MainWindow,SW_SHOW );
SetFocus ( MainWindow );
ReSizeGLScene ( WinRect().right, WinRect().bottom );
InitGL ( );

Thats it

Don't even need to register the windowsclass (but it will be handy in the future)

Share this post


Link to post
Share on other sites
I`m still unsure where to put my render func. I know I have asked for alot and you have all been very helpfull. What I want to render is a terrain but first a load screen. A single tga which is on a quad.

Share this post


Link to post
Share on other sites
Asking is what this forum (mainly) is for :)

Make your main something like:

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
{

LoadMenu ( Instance(), (LPCSTR) SID_MENU ); // load your menu
LaunchWindow ( " Title", 32 ); // open the window (the code from the previous post

while ( !done ) // start the drawin loop
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
// do message handling
}
else
{
DrawGLScene (); <--// GL render function here
}

SwapBuffers(DevCont());
}
return (msg.wParam);
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this