Where to put the OpenGL code...

Started by
6 comments, last by Drew_Benton 19 years, 3 months ago
Where in this Windows code do I place all of the OpenGL code in? It seems like no matter where I put it, it won't any output on the screen. Here's the example Windows application from a book I bought:

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{            
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
          
          
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);  
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}
Thank you.
Advertisement
http://nehe.gamedev.net ... that's all i got to say [smile]. Look at some tutorials there, very helpful.

~zix~
---------------------------------------------------Game Programming Resources, Tutorials, and Multimedia | Free Skyboxes
Hi, the most OpenGL initializing code are put after you have created the window(HWND) with CreateWindowEx and you drawing code are in the message loop(while (GetMessage (&messages, NULL, 0, 0)){...})

I can also see one thing that you have which shouldn't be there in an OpenGL app or any other real-time apps. The GetMessage call(inside while) this call waits until your app gets a message from Windows, the problem with this is that you won't draw unless you get messages all the time, in normal window apps you'ld only have to draw when you get a message, but in games and other real-time applications you'll have to draw all the time. The fix for this is to instead of using GetMessage you could make an infinite loop (while(1){...}), maybe add a "bool done;" variable to indicate when your application are done.
Inside the loop to retrieve the message you'll use PeekMessage, this will return a non-zero value when there is a message and NULL if there aren't any messages. So your app should look something like this:
#include <windows.h>#include <gl/gl.h>#include <gl/glu.h>#include <gl/glaux.h>/*  Declare Windows procedure  */LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);/*  Make the class name into a global variable  */char szClassName[ ] = "WindowsApp";int WINAPI WinMain (HINSTANCE hThisInstance,                    HINSTANCE hPrevInstance,                    LPSTR lpszArgument,                    int nFunsterStil){                HWND hwnd;               /* This is the handle for our window */    MSG messages;            /* Here messages to the application are saved */    WNDCLASSEX wincl;        /* Data structure for the windowclass */    /* The Window structure */    wincl.hInstance = hThisInstance;    wincl.lpszClassName = szClassName;    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */    wincl.cbSize = sizeof (WNDCLASSEX);    /* Use default icon and mouse-pointer */    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);    wincl.lpszMenuName = NULL;                 /* No menu */    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */    wincl.cbWndExtra = 0;                      /* structure or the window instance */    /* Use Windows's default color as the background of the window */    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;    /* Register the window class, and if it fails quit the program */    if (!RegisterClassEx (&wincl))        return 0;    /* The class is registered, let's create the program*/    hwnd = CreateWindowEx (           0,                   /* Extended possibilites for variation */           szClassName,         /* Classname */           "Windows App",       /* Title Text */           WS_OVERLAPPEDWINDOW, /* default window */           CW_USEDEFAULT,       /* Windows decides the position */           CW_USEDEFAULT,       /* where the window ends up on the screen */           544,                 /* The programs width */           375,                 /* and height in pixels */           HWND_DESKTOP,        /* The window is a child-window to desktop */           NULL,                /* No menu */           hThisInstance,       /* Program Instance handler */           NULL                 /* No Window Creation data */           );    /* Here should go the OpenGL initialization code */    /* Make the window visible on the screen */    ShowWindow (hwnd, nFunsterStil);    /*We aren't done yet*/    bool done = false;    /* Run the message loop. It will run until done is true*/    while (done == false)    {                  /*Check if there are any messages, if there        are remove them(PM_REMOVE) and place them in        messages*/        if(PeekMessage(&messages,NULL,0,0,PM_REMOVE))        {              TranslateMessage(&messages);              DispatchMessage(&messages);                /* Check if the PostQuitMessage has been called */              if(messages.message == WM_QUIT)              {                   /* We are done */                   done = true;              }        }               /* Here goes the OpenGL drawing code*/    }    /* The program return-value is 0 - The value that PostQuitMessage() gave */    return messages.wParam;}/*  This function is called by the Windows function DispatchMessage()  */LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){    switch (message)                  /* handle the messages */    {        case WM_DESTROY:            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */            break;        default:                      /* for messages that we don't deal with */            return DefWindowProc (hwnd, message, wParam, lParam);    }    return 0;}
I really appreciate the help guys. CHI-k, I tried putting some drawing code where it says to put it in the program but there's still nothing showing up on the screen. This is the drawing code I put in:

       glColor3f(1.0f, 0.0f, 0.0f);              glBegin(GL_TRIANGLES);           glVertex3f(-2.0, -1.0, 0.0);           glVertex3f(3.0, 1.0, 0.0);           glVertex3f(0.0, 3.0, 0.0);       glEnd();
Probably because there's no buffer refresh; try adding:

HDC hDC_ = GetDC(hWnd);
SwapBuffers(hDC_);

immediately after your drawing code.

Jim.

Edit : that actually doesn't work either. Thinking about it it might be the lack of PixelFormat. Will try and put together a working example for you.

Jim.
I made a little example, i put a /* !!CHANGE!! */ comment at everything i changed or added, but please read a tutorial or book on the subject so you know what is really going on or wou'll quickly lose the interest. Myself i started OpenGL programming copying the first 8 chapters of "OpenGL Game Programming" and then lost the interest because the depth buffer weren't working and i thought it was a bug in OpenGL, yeasterday i took it up again and know i know more than i knew at that time(6 months ago).

#include <windows.h>#include <gl/gl.h>#include <gl/glu.h>#include <gl/glaux.h>/*  Declare Windows procedure  */LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);/*  Make the class name into a global variable  */char szClassName[ ] = "WindowsApp";/* !!CHANGE!! *//*The global device context*/HDC DeviceContext;/*The global rendering context*/HGLRC RenderingContext;int WINAPI WinMain (HINSTANCE hThisInstance,                    HINSTANCE hPrevInstance,                    LPSTR lpszArgument,                    int nFunsterStil){                HWND hwnd;               /* This is the handle for our window */    MSG messages;            /* Here messages to the application are saved */    WNDCLASSEX wincl;        /* Data structure for the windowclass */    /* The Window structure */    wincl.hInstance = hThisInstance;    wincl.lpszClassName = szClassName;    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */    wincl.cbSize = sizeof (WNDCLASSEX);    /* Use default icon and mouse-pointer */    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);    wincl.lpszMenuName = NULL;                 /* No menu */    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */    wincl.cbWndExtra = 0;                      /* structure or the window instance */    /* Use Windows's default color as the background of the window */    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;    /* Register the window class, and if it fails quit the program */    if (!RegisterClassEx (&wincl))        return 0;    /* The class is registered, let's create the program*/    hwnd = CreateWindowEx (           0,                   /* Extended possibilites for variation */           szClassName,         /* Classname */           "Windows App",       /* Title Text */           WS_OVERLAPPEDWINDOW, /* default window */           CW_USEDEFAULT,       /* Windows decides the position */           CW_USEDEFAULT,       /* where the window ends up on the screen */           544,                 /* The programs width */           375,                 /* and height in pixels */           HWND_DESKTOP,        /* The window is a child-window to desktop */           NULL,                /* No menu */           hThisInstance,       /* Program Instance handler */           NULL                 /* No Window Creation data */           );    /* !!CHANGE!! */	/* Get the device context from our window */	DeviceContext = GetDC(hwnd);	/* Fill out a description of the pixel format */	PIXELFORMATDESCRIPTOR PixelFormatDescriptor = 	{	sizeof(PIXELFORMATDESCRIPTOR),		//Size of the structure	1,									//version	PFD_DRAW_TO_WINDOW|	PFD_SUPPORT_OPENGL|	PFD_DOUBLEBUFFER,					//Flags	PFD_TYPE_RGBA,						//RGBA color format.	32,									//BPP	0,0,0,0,0,0,0,0,					//We won't use these	0,0,0,0,0,							//No accumulation buffer	16,									//16 bit depth buffer	0,0,								//No stencil- and auxillary buffer	0,0,0,0,0							//Won't be used	};	const int PixelFormat = ChoosePixelFormat(DeviceContext,&PixelFormatDescriptor);	SetPixelFormat(DeviceContext,PixelFormat,&PixelFormatDescriptor);	/* Create the rendering context */	RenderingContext = wglCreateContext(DeviceContext);	/* Set the current contexts */	wglMakeCurrent(DeviceContext,RenderingContext);	/* Set the viewport(drawing area) */	glViewport(0,0,544,375);	/*Reset the projection matrix*/	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	gluPerspective(45.0,		/* Y field of view in degrees */		544.0f/375.0f,			/* Width/Height */		1.0f,					/* Near plane */		1000.0f					/* Far plane */		);	/* Reset model view matrix */	glMatrixMode(GL_MODELVIEW);	glLoadIdentity();    /* Make the window visible on the screen */    ShowWindow (hwnd, nFunsterStil);    /*We aren't done yet*/    bool done = false;    /* Run the message loop. It will run until done is true*/    while (done == false)    {                  /*Check if there are any messages, if there        are remove them(PM_REMOVE) and place them in        messages*/        if(PeekMessage(&messages,NULL,0,0,PM_REMOVE))        {              TranslateMessage(&messages);              DispatchMessage(&messages);                /* Check if the PostQuitMessage has been called */              if(messages.message == WM_QUIT)              {                   /* We are done */                   done = true;              }        }        				/* !!CHANGE!! */		/* Rendering */		/* Clear the buffers we drawed on last frame */		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);		/* Reset current matrix. */		glLoadIdentity();		/*Set red color*/       glColor3f(1.0f, 0.0f, 0.0f);       	   /*Draw a triangle*/       glBegin(GL_TRIANGLES);	   /*Change: Added -10.0 as last parameter to move the triangle "into" the screen */           glVertex3f(-2.0, -1.0, -10.0);           glVertex3f(3.0, 1.0, -10.0);           glVertex3f(0.0, 3.0, -10.0);       glEnd();	   /*Swap the back and front buffers*/	   SwapBuffers(DeviceContext);    }    /* The program return-value is 0 - The value that PostQuitMessage() gave */    return messages.wParam;}/*  This function is called by the Windows function DispatchMessage()  */LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){    switch (message)                  /* handle the messages */    {        case WM_DESTROY:            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */            break;        default:                      /* for messages that we don't deal with */            return DefWindowProc (hwnd, message, wParam, lParam);    }    return 0;}


EDIT: I haven't added any cleanup code because the example was just to demonstrate how to do it. The cleanup code should go before the return 0.

[Edited by - CHI-k on January 17, 2005 2:23:42 PM]
Ah well, beaten to it.

What he said....

It might also be worth your while looking at NeHe, or checking the Forum FAQ.

Jim.
Quote:Original post by Caldus
I really appreciate the help guys. CHI-k, I tried putting some drawing code where it says to put it in the program but there's still nothing showing up on the screen. This is the drawing code I put in:

       glColor3f(1.0f, 0.0f, 0.0f);              glBegin(GL_TRIANGLES);           glVertex3f(-2.0, -1.0, 0.0);           glVertex3f(3.0, 1.0, 0.0);           glVertex3f(0.0, 3.0, 0.0);       glEnd();


I have not read all the code everyone has posted but you do need to make a call to
glTranslatef(0,0,-5);


to move the viewport so you can see what you are drawing - that is why you cannot see anything:

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // taken from CHI-k's code [smile]glLoadIdentity(); // CHI-k'sglTranslatef(0,0,-5); // mineglColor3f(1.0f, 0.0f, 0.0f);// yoursglBegin(GL_TRIANGLES);// yours    glVertex3f(-2.0, -1.0, 0.0);// yours    glVertex3f(3.0, 1.0, 0.0);// yours    glVertex3f(0.0, 3.0, 0.0);// yoursglEnd(); // yoursSwapBuffers(DeviceContext); // CHI-k's


- Drew

This topic is closed to new replies.

Advertisement