Sign in to follow this  
norbs27

OpenGL OpenGL Windows

Recommended Posts

Does OpenGL have to be run through its own window or can you run it through a section of a window, such as a box within the centre?

Share this post


Link to post
Share on other sites
No, I mean say you have your window setup with your various dialog boxes arranged wherever, can you have OpenGL render to a picturebox (or an equivalent compatible object) or does it have to have its own window?

Share this post


Link to post
Share on other sites
Quote:
Original post by norbs27
No, I mean say you have your window setup with your various dialog boxes arranged wherever, can you have OpenGL render to a picturebox (or an equivalent compatible object) or does it have to have its own window?


I'm not really sure what you mean by picturebox but you can set up a view port that takes only part of a window... So then you have all your other stuff somewhere safe where ogl won't overwrite them.

Share this post


Link to post
Share on other sites
It is also possible to create a rendering context for (for example) a panel.
In this program i made 2 panels with each a different rendering context.

Here is some of my code.
I just fooled around with rendering context and device context.
But hey... it works.

Screenshot


...
hdcGridPanel = GetDC(GridPanel->Handle); // handle to GridPanel device context
hdcWorldPanel = GetDC(WorldPanel->Handle); // handle to WorldPanel device context
SetPixelFormatDescriptor();
hrcGridPanel = wglCreateContext(hdcGridPanel); // create a rendering context for the GridPanel
hrcWorldPanel = wglCreateContext(hdcWorldPanel); // create a rendering context for the WorldPanel
if (hrcGridPanel==NULL) // check if the handles are created
ShowMessage(":-)~ hrcGridPanel == NULL");
if (hrcWorldPanel==NULL)
ShowMessage(":-)~ hrcWorldPanel == NULL");
...


void __fastcall TMain_Form::SetPixelFormatDescriptor()
{
// set a pixel format for the device context
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
24, // 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
32, // 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
};
PixelFormat = ChoosePixelFormat(hdcGridPanel, &pfd);
SetPixelFormat(hdcGridPanel, PixelFormat, &pfd);
PixelFormat = ChoosePixelFormat(hdcWorldPanel, &pfd);
SetPixelFormat(hdcWorldPanel, PixelFormat, &pfd);
}


void __fastcall TMain_Form::CloseMainForm(TObject *Sender,
TCloseAction &Action)
{
// release Context
wglMakeCurrent(NULL, NULL); // no current rendering context
wglDeleteContext(hrcGridPanel); // free the system resources
wglDeleteContext(hrcWorldPanel);
ReleaseDC(GridPanel->Handle, hdcGridPanel);
ReleaseDC(WorldPanel->Handle, hdcWorldPanel);
...
}

void __fastcall TMain_Form::UpdateGridPanel()
{
if (wglMakeCurrent(hdcGridPanel, hrcGridPanel) == false) // associate the hrcGridPanel to the
ShowMessage("Could not MakeCurrent: GridPanel"); // hdcGridPanel => draw to GridPanel
setGridViewPort();
//drawGrid();
SwapBuffers(hdcGridPanel);
}


void __fastcall setGridViewPort()
{
glViewport (0, 0, Main_Form->GridPanel->ClientWidth, Main_Form->GridPanel->ClientHeight);
glScissor(0, 0, Main_Form->GridPanel->ClientWidth, Main_Form->GridPanel->ClientHeight);
glEnable(GL_SCISSOR_TEST); // only pixels in box will be affected (standard)
glMatrixMode(GL_PROJECTION); // reset projection matrix
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); // reset modelview matrix
// Set orthogonal projection (perspective)
glOrtho(center.x - (Main_Form->GridPanel->ClientWidth/2)*distancePerPixel,
center.x + (Main_Form->GridPanel->ClientWidth/2)*distancePerPixel,
center.y - (Main_Form->GridPanel->ClientHeight/2)*distancePerPixel,
center.y + (Main_Form->GridPanel->ClientHeight/2)*distancePerPixel,
-100.0f, 100.0f);
...
}





I hope you find something useful among these snippets of code.

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