Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Open Gl question

OrcbloodOrcblood Member Posts: 44
Im new to 3D programming... I know about x, y and z axises and I was wondering how I would add Open Gl to a windows program. For example, I am going to be making a plug-in for MilkShape 3D where you can add lights, cameras and other effects. I was wondering how I would make 4 subwindows inside the main window.

For example:

Window 1 (top view) | Window 2 (front view)
|
|
------------------------------------------------------------------------
Window 3 (side view) | Window 4 (perspective)
|
|

Would I have to create another .cpp file with the Open Gl programming and attach it to the WinMain file?

Thanks, if you need me to explain it further then say so.

orcblood

Comments

  • ZaffireZaffire Member Posts: 22
    : Im new to 3D programming... I know about x, y and z axises and I was wondering how I would add Open Gl to a windows program. For example, I am going to be making a plug-in for MilkShape 3D where you can add lights, cameras and other effects. I was wondering how I would make 4 subwindows inside the main window.
    :
    : For example:
    :
    : Window 1 (top view) | Window 2 (front view)
    : |
    : |
    : ------------------------------------------------------------------------
    : Window 3 (side view) | Window 4 (perspective)
    : |
    : |
    :
    : Would I have to create another .cpp file with the Open Gl programming and attach it to the WinMain file?
    :
    : Thanks, if you need me to explain it further then say so.
    :
    : orcblood
    :
    this is what worked for me -- just creating child windows within the main window:

    [code]
    LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    static HWND hGLWnd;
    static WNDCLASSEX glClass;
    static RECT clientRect;


    switch(msg)
    {
    case WM_CREATE:
    glClass.cbClsExtra = 0;
    glClass.cbSize = sizeof(WNDCLASSEX);
    glClass.cbWndExtra = 0;
    glClass.hbrBackground = NULL;
    glClass.hCursor = LoadCursor(NULL, IDC_CROSS);
    glClass.hIcon = NULL;
    glClass.hIconSm = NULL;
    glClass.hInstance = GetModuleHandle(NULL);
    glClass.lpfnWndProc = GLWndProc;
    glClass.lpszClassName = "openGL";
    glClass.lpszMenuName = NULL;
    glClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;

    RegisterClassEx(&glClass);
    break;

    case WM_SIZE:
    DestroyWindow(hGLWnd);

    GetClientRect(hWnd, &clientRect);

    hGLWnd = CreateWindowEx(WS_EX_CLIENTEDGE, "openGL", "",
    WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
    clientRect.left + BORDERWIDTH,
    clientRect.top + CONTROLSPACE,
    clientRect.right - clientRect.left - 2 * BORDERWIDTH,
    clientRect.bottom - clientRect.top - (CONTROLSPACE + BORDERWIDTH),
    hWnd,
    NULL,
    GetModuleHandle(NULL),
    NULL);
    break;
    return DefWindowProc(hWnd, msg, wParam, lParam);
    }

    LRESULT CALLBACK GLWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    static GLuint PixelFormat;
    static HGLRC hRC;
    static RECT frameRect;
    static PIXELFORMATDESCRIPTOR pfd =
    {
    sizeof(PIXELFORMATDESCRIPTOR),
    1,
    PFD_DRAW_TO_WINDOW |
    PFD_SUPPORT_OPENGL |
    PFD_DOUBLEBUFFER,
    PFD_TYPE_RGBA,
    32,
    0, 0, 0, 0, 0, 0,
    0,
    0,
    0,
    0, 0, 0, 0,
    16,
    0,
    0,
    0, 0, 0
    };



    switch(msg)
    {
    case WM_CREATE:
    hDC = GetDC(hWnd);
    PixelFormat = ChoosePixelFormat(hDC, &pfd);
    SetPixelFormat(hDC, PixelFormat, &pfd);
    hRC = wglCreateContext(hDC);
    wglMakeCurrent(hDC, hRC);
    break;

    case WM_SIZE:
    glViewport(0, 0, LOWORD(lParam), HIWORD(lParam));
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    gluPerspective(45.0f, (GLfloat)LOWORD(lParam) / (GLfloat)HIWORD(lParam), 0.1f, 100.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glShadeModel(GL_SMOOTH);
    glClearColor(0.6f, 0.6f, 0.6f, 1.0f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    break;

    case WM_DESTROY:
    wglMakeCurrent(NULL, NULL);
    wglDeleteContext(hRC);
    ReleaseDC(hWnd, hDC);
    UnregisterClass("openGL", GetModuleHandle(NULL));
    break;
    }

    return DefWindowProc(hWnd, msg, wParam, lParam);
    }[/code]
    this creates a child frame in the main window that supports openGL, leaving CONTROLSPACE between the top of the main window and the top of the frame and BORDERWIDTH between the child window and the edge of the main window. it shouldn't take much to make it create four frames instead of one. also, the child window and openGL scene are resized whenever the window is, so that's all taken care of. hope that helps.


    Zaffire

  • SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
    Zaff, that code isn't complete, and there are no checks on either the creation or destruction of the GL window(s). GL is faster and prettier than DX, but only works on cards that are near 100% OpenGL compliant, so you should add checks to virtually everything dealing with GL windows or calls, otherwise you could REALLY eat up resources when something fails and not even know it. I could show you an example if you'd like.

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

  • OrcbloodOrcblood Member Posts: 44
    : : Im new to 3D programming... I know about x, y and z axises and I was wondering how I would add Open Gl to a windows program. For example, I am going to be making a plug-in for MilkShape 3D where you can add lights, cameras and other effects. I was wondering how I would make 4 subwindows inside the main window.
    : :
    : : For example:
    : :
    : : Window 1 (top view) | Window 2 (front view)
    : : |
    : : |
    : : ------------------------------------------------------------------------
    : : Window 3 (side view) | Window 4 (perspective)
    : : |
    : : |
    : :
    : : Would I have to create another .cpp file with the Open Gl programming and attach it to the WinMain file?
    : :
    : : Thanks, if you need me to explain it further then say so.
    : :
    : : orcblood
    : :
    : this is what worked for me -- just creating child windows within the main window:
    :
    : [code]
    : LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    : {
    : static HWND hGLWnd;
    : static WNDCLASSEX glClass;
    : static RECT clientRect;
    :
    :
    : switch(msg)
    : {
    : case WM_CREATE:
    : glClass.cbClsExtra = 0;
    : glClass.cbSize = sizeof(WNDCLASSEX);
    : glClass.cbWndExtra = 0;
    : glClass.hbrBackground = NULL;
    : glClass.hCursor = LoadCursor(NULL, IDC_CROSS);
    : glClass.hIcon = NULL;
    : glClass.hIconSm = NULL;
    : glClass.hInstance = GetModuleHandle(NULL);
    : glClass.lpfnWndProc = GLWndProc;
    : glClass.lpszClassName = "openGL";
    : glClass.lpszMenuName = NULL;
    : glClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    :
    : RegisterClassEx(&glClass);
    : break;
    :
    : case WM_SIZE:
    : DestroyWindow(hGLWnd);
    :
    : GetClientRect(hWnd, &clientRect);
    :
    : hGLWnd = CreateWindowEx(WS_EX_CLIENTEDGE, "openGL", "",
    : WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
    : clientRect.left + BORDERWIDTH,
    : clientRect.top + CONTROLSPACE,
    : clientRect.right - clientRect.left - 2 * BORDERWIDTH,
    : clientRect.bottom - clientRect.top - (CONTROLSPACE + BORDERWIDTH),
    : hWnd,
    : NULL,
    : GetModuleHandle(NULL),
    : NULL);
    : break;
    : return DefWindowProc(hWnd, msg, wParam, lParam);
    : }
    :
    : LRESULT CALLBACK GLWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    : {
    : static GLuint PixelFormat;
    : static HGLRC hRC;
    : static RECT frameRect;
    : static PIXELFORMATDESCRIPTOR pfd =
    : {
    : sizeof(PIXELFORMATDESCRIPTOR),
    : 1,
    : PFD_DRAW_TO_WINDOW |
    : PFD_SUPPORT_OPENGL |
    : PFD_DOUBLEBUFFER,
    : PFD_TYPE_RGBA,
    : 32,
    : 0, 0, 0, 0, 0, 0,
    : 0,
    : 0,
    : 0,
    : 0, 0, 0, 0,
    : 16,
    : 0,
    : 0,
    : 0, 0, 0
    : };
    :
    :
    :
    : switch(msg)
    : {
    : case WM_CREATE:
    : hDC = GetDC(hWnd);
    : PixelFormat = ChoosePixelFormat(hDC, &pfd);
    : SetPixelFormat(hDC, PixelFormat, &pfd);
    : hRC = wglCreateContext(hDC);
    : wglMakeCurrent(hDC, hRC);
    : break;
    :
    : case WM_SIZE:
    : glViewport(0, 0, LOWORD(lParam), HIWORD(lParam));
    : glMatrixMode(GL_PROJECTION);
    : glLoadIdentity();
    :
    : gluPerspective(45.0f, (GLfloat)LOWORD(lParam) / (GLfloat)HIWORD(lParam), 0.1f, 100.0f);
    :
    : glMatrixMode(GL_MODELVIEW);
    : glLoadIdentity();
    :
    : glShadeModel(GL_SMOOTH);
    : glClearColor(0.6f, 0.6f, 0.6f, 1.0f);
    : glClearDepth(1.0f);
    : glEnable(GL_DEPTH_TEST);
    : glDepthFunc(GL_LEQUAL);
    : glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    : break;
    :
    : case WM_DESTROY:
    : wglMakeCurrent(NULL, NULL);
    : wglDeleteContext(hRC);
    : ReleaseDC(hWnd, hDC);
    : UnregisterClass("openGL", GetModuleHandle(NULL));
    : break;
    : }
    :
    : return DefWindowProc(hWnd, msg, wParam, lParam);
    : }[/code]
    : this creates a child frame in the main window that supports openGL, leaving CONTROLSPACE between the top of the main window and the top of the frame and BORDERWIDTH between the child window and the edge of the main window. it shouldn't take much to make it create four frames instead of one. also, the child window and openGL scene are resized whenever the window is, so that's all taken care of. hope that helps.
    :
    :
    : Zaffire
    :
    :

    Would I use this as my WinMain or would I place this into my already existing WinMain?

    Thanks for the help buddy!
    orcblood
  • ZaffireZaffire Member Posts: 22
    : Would I use this as my WinMain or would I place this into my already existing WinMain?
    :
    : Thanks for the help buddy!
    : orcblood
    :
    no, this would be the window procedure for your main WNDCLASS. In WinMain, you should have declared a WNDCLASS or WNDCLASSEX structure. one of the data members is 'lpfnWndProc' this should be set to a pointer to the main window procedure, 'MainWndProc'. Here is the code I used for this particular program:
    [code]
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
    {
    MSG msg;
    HWND hWnd;
    WNDCLASSEX wc; // here is the definition of the WNDCLASSEX structure
    char szClassName[] = "grapher";
    char szWindowCaption[] = "Grapher 2.0";
    bool done = false;

    // filling in all the members
    wc.cbClsExtra = 0;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.cbWndExtra = 0;
    wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    wc.hInstance = hInstance;
    wc.lpfnWndProc = MainWndProc; // here is the pointer to MainWndProc
    wc.lpszClassName = szClassName;
    wc.lpszMenuName = szClassName;
    wc.style = CS_HREDRAW | CS_VREDRAW;

    // registering class
    RegisterClassEx(&wc);

    hWnd = CreateWindow(szClassName,
    szWindowCaption,
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    780,
    580,
    NULL,
    NULL,
    hInstance,
    NULL);

    ShowWindow(hWnd, nShowCmd);
    UpdateWindow(hWnd);

    while(!done)
    {
    if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    {
    if(msg.message == WM_QUIT)
    {
    done = true;
    }
    else
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    }
    else
    {
    DrawScene();
    SwapBuffers(hDC);
    }
    }

    return msg.wParam;
    }
    [/code]
    whenever something happens in windows like a mouse click or something, messages are sent to the window procedure corresponding to the window class. the window procedure is a separate function defined outside of WinMain, but pointed to in the class structure. also, sephiroth is right. this code is not at all forgiving. hope this helps!

    to seph: if you could post an example where everything is checked, that would be awesome. i know that i tend to forget checks and stuff when i write my own code. thanks.


    Zaffire

  • SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
    I'll post it when I get home from my family from the holiday. Also, your PeekMessage() loop is decent, but flawed in one major way. I bet you got that from NeHe's site :D! I used an identical loop myself, until I had problems with my world editor. I'll post how I do it now so it does the main loop whether or not you get a message for that window.

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

  • OrcbloodOrcblood Member Posts: 44
    : I'll post it when I get home from my family from the holiday. Also, your PeekMessage() loop is decent, but flawed in one major way. I bet you got that from NeHe's site :D! I used an identical loop myself, until I had problems with my world editor. I'll post how I do it now so it does the main loop whether or not you get a message for that window.
    :
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
    :
    :

    Yea, Im going through Nehe's site. I think that he explains it well, atleast he admits that his code might have some problems with it though.

    orcblood
  • ZaffireZaffire Member Posts: 22
    : I'll post it when I get home from my family from the holiday. Also, your PeekMessage() loop is decent, but flawed in one major way. I bet you got that from NeHe's site :D! I used an identical loop myself, until I had problems with my world editor. I'll post how I do it now so it does the main loop whether or not you get a message for that window.
    :
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
    :
    :
    hey! how'd you know?!?!? i don't claim to be an expert at openGL, but almost everything i [italic]do[/italic] know, i learned from his site. always eager to learn anything new!


    Zaffire

  • gautamgautam Member Posts: 642
    Hi,

    Lol, His code is addictive, and afterall once you have a winmain why will you bother to look at it again?, just copy and paste right?
    Well even I was going through it till I got the red book and after that I don't look at any other tutorials for OpenGL. I will be looking at Game Programming Gems now when I get it in my hands.

    : hey! how'd you know?!?!? i don't claim to be an expert at openGL, but almost everything i [italic]do[/italic] know, i learned from his site. always eager to learn anything new!
    :
    :
    : Zaffire
    :
    :

  • gautamgautam Member Posts: 642
    Hi,

    You can add 4 windows by using glViewport. I was playing with it when I was doing some profile rendering for my engineering project. We needed 2 windows - one for the dimensions(the fonts) and other for the rendering. We used the glViewport function to divide it. If I can figure out my 6 months old code(which I haven't seen since 6 months now), I will post an example of how I did it then.


    An eg from the red book :
    glViewport(0, 0, sizex/2, sizey)
    ...
    ...
    glViewport(sizex/2, 0, sizex/2, sizey);

    With this you can achieve something like split screens.



    : Im new to 3D programming... I know about x, y and z axises and I was wondering how I would add Open Gl to a windows program. For example, I am going to be making a plug-in for MilkShape 3D where you can add lights, cameras and other effects. I was wondering how I would make 4 subwindows inside the main window.
    :
    : For example:
    :
    : Window 1 (top view) | Window 2 (front view)
    : |
    : |
    : ------------------------------------------------------------------------
    : Window 3 (side view) | Window 4 (perspective)
    : |
    : |
    :
    : Would I have to create another .cpp file with the Open Gl programming and attach it to the WinMain file?
    :
    : Thanks, if you need me to explain it further then say so.
    :
    : orcblood
    :

Sign In or Register to comment.