Sample Code

Sample Code

Post by Jon » Thu, 07 Aug 1997 04:00:00



I'm trying to learn OpenGL, but I cant find much sample code lying
around.  Can anyone point me to a  complete sample program written with
the OpenGL libs  (NOT mfc or owl)?

             ~~~~ Thanx

 
 
 

Sample Code

Post by Paul » Fri, 08 Aug 1997 04:00:00


|> > I'm trying to learn OpenGL, but I cant find much sample code lying
|> > around.  Can anyone point me to a  complete sample program written with
|> > the OpenGL libs  (NOT mfc or owl)?
|>
|> The problem with this request is that OpenGL has no windowing
|> support.
|>
|> The closest you can come to portable code is to use a commonly
|> supported API like Aux or Glut, for windowing. You should be
|> aware that you are using another library for your windowing.
|>
|> The examples in the "Red Book" use Aux or Glut depending on the
|> Edition. Aux is waning.

Angus is correct. Most of our samples have been converted to GLUT. We
do have one or two pure Window samples. Here is a simple cube sample.

---

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <GL/gl.h>
#include "wglext.h"

HWND hWin;
HDC hDC;
HGLRC hGLRC;

#define IDM_APPLICATION_EXIT 101
HMENU hMainPup;

GLfloat v[6][4][3] = {
    {
        {
            1.0f, 1.0f, -1.0f
        },
        {
            -1.0f, 1.0f, -1.0f
        },
        {
            -1.0f, -1.0f, -1.0f
        },
        {
            1.0f, -1.0f, -1.0f
        }
    },
    {
        {
            1.0f, 1.0f, 1.0f
        },
        {
            1.0f, 1.0f, -1.0f
        },
        {
            1.0f, -1.0f, -1.0f
        },
        {
            1.0f, -1.0f, 1.0f
        }
    },
    {
        {
            -1.0f, 1.0f, 1.0f
        },
        {
            1.0f, 1.0f, 1.0f
        },
        {
            1.0f, -1.0f, 1.0f
        },
        {
            -1.0f, -1.0f, 1.0f
        }
    },
    {
        {
            -1.0f, 1.0f, -1.0f
        },
        {
            -1.0f, 1.0f, 1.0f
        },
        {
            -1.0f, -1.0f, 1.0f
        },
        {
            -1.0f, -1.0f, -1.0f
        }
    },
    {
        {
            -1.0f, 1.0f, 1.0f
        },
        {
            -1.0f, 1.0f, -1.0f
        },
        {
            1.0f, 1.0f, -1.0f
        },
        {
            1.0f, 1.0f, 1.0f
        }
    },
    {
        {
            -1.0f, -1.0f, -1.0f
        },
        {
            -1.0f, -1.0f, 1.0f
        },
        {
            1.0f, -1.0f, 1.0f
        },
        {
            1.0f, -1.0f, -1.0f
        }
    }

};

static void SetupDC(HDC hDC)
{
    PIXELFORMATDESCRIPTOR pfd = {
        sizeof(PIXELFORMATDESCRIPTOR),  /* size of this pfd */
        1,                              /* version num */
        PFD_DRAW_TO_WINDOW |            /* window types */
        PFD_SUPPORT_OPENGL |
        PFD_DOUBLEBUFFER,
        PFD_TYPE_RGBA,                  /* RGBA type */
        8,                              /* 8-bit color depth */
        0, 0, 0, 0, 0, 0,               /* color bits (ignored) */
        0,                              /* no alpha buffer */
        0,                              /* alpha bits (ignored) */
        0,                              /* no accumulation buffer */
        0, 0, 0, 0,                     /* accum bits (ignored) */
        16,                             /* 16-bit depth buffer */
        0,                              /* no stencil buffer */
        0,                              /* no auxiliary buffers */
        PFD_MAIN_PLANE,                 /* main layer */
        0,                              /* reserved */
        0, 0, 0,                        /* no layer, visible, damage masks */
    };
    int SelectedPixelFormat;
    BOOL retVal;

    /* see if the pixel format exists */
    SelectedPixelFormat = ChoosePixelFormat(hDC, &pfd);
    if (SelectedPixelFormat == 0) {
        MessageBox(WindowFromDC(hDC),
                   "Failed to find acceptable pixel format.",
                   "OpenGL application error", MB_ICONERROR|MB_OK);
        exit(1);
    }

    /* use the pixel format */
    retVal = SetPixelFormat(hDC, SelectedPixelFormat, &pfd);
    if (retVal != TRUE) {
        MessageBox(WindowFromDC(hDC), "Failed to set pixel format.",
                   "OpenGL application error", MB_ICONERROR|MB_OK);
        exit(1);
    }

}

static void Redraw(HDC hDC)
{
    GLint i;

    /* clear screen */
    glClearColor(1.0f, 1.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    /* draw cube */
    glColor3f(0.0f, 1.0f, 0.0f);
    for (i = 0; i < 6; i++) {
        glBegin(GL_POLYGON);
            glVertex3fv(v[i][0]);
            glVertex3fv(v[i][1]);
            glVertex3fv(v[i][2]);
            glVertex3fv(v[i][3]);
        glEnd();
    }

    SwapBuffers(hDC);

}

LRESULT APIENTRY WinProc(HWND hWin, UINT message, WPARAM wParam, LPARAM lParam)
{

    switch (message) {
        case WM_CREATE:
            hDC = GetDC(hWin);
            SetupDC(hDC);
            hGLRC = wglCreateContext(hDC);
            wglMakeCurrent(hDC, hGLRC);
            return 0;
        case WM_DESTROY:
            if (hGLRC) {
                wglMakeCurrent(NULL, NULL);
                wglDeleteContext(hGLRC);
            }
            ReleaseDC(hWin, hDC);
            PostQuitMessage(0);
            return 0;
        case WM_PAINT:
            if (hGLRC) {
                PAINTSTRUCT ps;
                BeginPaint(hWin, &ps);
                Redraw(hDC);
                EndPaint(hWin, &ps);
                return 0;
            }
            break;
        case WM_CONTEXTMENU:
            TrackPopupMenu(hMainPup, TPM_LEFTALIGN|TPM_RIGHTBUTTON,
                           LOWORD(lParam), HIWORD(lParam), 0, hWin, NULL);
            break;
        case WM_COMMAND:
            switch (LOWORD(wParam)) {
                case IDM_APPLICATION_EXIT:
                    PostQuitMessage(0);
                    break;
            }
            break;
        default:
            break;
    }

    return DefWindowProc(hWin, message, wParam, lParam);

}

int APIENTRY WinMain(HINSTANCE hCurrentInst, HINSTANCE hPreviousInst,
                     LPSTR lpszCmdLine, int nCmdShow)
{
    char *className = "OpenGL";
    char *winName = "Cube Example";
    WNDCLASS winClass;
    MSG msg;

    /* Define and register the window class */
    winClass.style = CS_HREDRAW | CS_VREDRAW;
    winClass.lpfnWndProc = WinProc;
    winClass.cbClsExtra = 0;
    winClass.cbWndExtra = 0;
    winClass.hInstance = hCurrentInst;
    winClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    winClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    winClass.hbrBackground = GetStockObject(WHITE_BRUSH);
    winClass.lpszMenuName = NULL;
    winClass.lpszClassName = className;
    RegisterClass(&winClass);

    /* create window */
    hWin = CreateWindow(className,
                        winName,
                        WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_CLIPSIBLINGS,
                        0, 0,
                        300, 300,
                        NULL,                   /* Parent window's handle */
                        NULL,                   /* Menu handle */
                        hCurrentInst,           /* Instance handle */
                        NULL);                  /* No additional data */

    /* add quit menu item */
    hMainPup = CreatePopupMenu();
    AppendMenu(hMainPup, MF_STRING, IDM_APPLICATION_EXIT, "Exit");

    ShowWindow(hWin, nCmdShow);

    UpdateWindow(hWin);

    /* process events */
    while (1) {
        if (GetMessage(&msg, NULL, 0, 0) != TRUE) {
            return msg.wParam;
        }
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

}

--

    Paul Ho, MTS                               p...@sgi.com
    Silicon Graphics, Inc.                     (415)933-1619 (Voice)
    2011 N. Shoreline Blvd, MS 8U-590          (415)965-2658 (FAX)
    Mn. View, CA 94043-1389

 
 
 

Sample Code

Post by Angus Dorbi » Fri, 08 Aug 1997 04:00:00



> I'm trying to learn OpenGL, but I cant find much sample code lying
> around.  Can anyone point me to a  complete sample program written with
> the OpenGL libs  (NOT mfc or owl)?

The problem with this request is that OpenGL has no windowing
support.

The closest you can come to portable code is to use a commonly
supported API like Aux or Glut, for windowing. You should be
aware that you are using another library for your windowing.

The examples in the "Red Book" use Aux or Glut depending on the
Edition. Aux is waning.

Cheers,Angus.