使用vc6开发opengl工程

本文介绍如何使用Visual C++ 6.0创建OpenGL应用程序。通过示例代码详细讲解了设置OpenGL环境、绘制旋转三角形的过程。适用于初学者快速上手。
//使用vc6开发opengl工程[原创]
None.gif
//首先要在工程设置连接中加入opengl32.lib
None.gif
//微软在Visual C++中已提供了三个OpenGL的函数库(glu32.lib, glau.lib,OpenGL32.lib)
None.gif
//注意要加入头文件#i nclude <gl/gl.h>
None.gif
//因为gl.h文件在vc的include 下面的gl目录下面
None.gif
//下面是一个基于win32的例子
ExpandedBlockStart.gif
/**************************
InBlock.gif * Includes
InBlock.gif *
ExpandedBlockEnd.gif *************************
*/

None.gif
None.gif#i nclude 
<windows.h>
None.gif#i nclude 
<gl/gl.h>
None.gif
None.gif
ExpandedBlockStart.gif
/**************************
InBlock.gif * Function Declarations
InBlock.gif *
ExpandedBlockEnd.gif *************************
*/

None.gif
None.gifLRESULT CALLBACK WndProc (HWND hWnd, UINT message,
None.gifWPARAM wParam, LPARAM lParam);
None.gif
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
None.gif
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
None.gif
None.gif
ExpandedBlockStart.gif
/**************************
InBlock.gif * WinMain
InBlock.gif *
ExpandedBlockEnd.gif *************************
*/

None.gif
None.gif
int WINAPI WinMain (HINSTANCE hInstance,
None.gif                    HINSTANCE hPrevInstance,
None.gif                    LPSTR lpCmdLine,
None.gif                    
int iCmdShow)
ExpandedBlockStart.gif
{
InBlock.gif    WNDCLASS wc;
InBlock.gif    HWND hWnd;
InBlock.gif    HDC hDC;
InBlock.gif    HGLRC hRC;        
InBlock.gif    MSG msg;
InBlock.gif    BOOL bQuit = FALSE;
InBlock.gif    float theta = 0.0f;
InBlock.gif
ExpandedSubBlockStart.gif    /* register window class */
InBlock.gif    wc.style = CS_OWNDC;
InBlock.gif    wc.lpfnWndProc = WndProc;
InBlock.gif    wc.cbClsExtra = 0;
InBlock.gif    wc.cbWndExtra = 0;
InBlock.gif    wc.hInstance = hInstance;
InBlock.gif    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
InBlock.gif    wc.hCursor = LoadCursor (NULL, IDC_ARROW);
InBlock.gif    wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
InBlock.gif    wc.lpszMenuName = NULL;
InBlock.gif    wc.lpszClassName = "GLSample";
InBlock.gif    RegisterClass (&wc);
InBlock.gif
ExpandedSubBlockStart.gif    /* create main window */
InBlock.gif    hWnd = CreateWindow (
InBlock.gif      "GLSample", "OpenGL Sample", 
InBlock.gif      WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
InBlock.gif      0, 0, 256, 256,
InBlock.gif      NULL, NULL, hInstance, NULL);
InBlock.gif
ExpandedSubBlockStart.gif    /* enable OpenGL for the window */
InBlock.gif    EnableOpenGL (hWnd, &hDC, &hRC);
InBlock.gif
ExpandedSubBlockStart.gif    /* program main loop */
InBlock.gif    while (!bQuit)
ExpandedSubBlockStart.gif    {
ExpandedSubBlockStart.gif        /* check for messages */
InBlock.gif        if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
ExpandedSubBlockStart.gif        {
ExpandedSubBlockStart.gif            /* handle or dispatch messages */
InBlock.gif            if (msg.message == WM_QUIT)
ExpandedSubBlockStart.gif            {
InBlock.gif                bQuit = TRUE;
ExpandedSubBlockEnd.gif            }
InBlock.gif            else
ExpandedSubBlockStart.gif            {
InBlock.gif                TranslateMessage (&msg);
InBlock.gif                DispatchMessage (&msg);
ExpandedSubBlockEnd.gif            }
ExpandedSubBlockEnd.gif        }
InBlock.gif        else
ExpandedSubBlockStart.gif        {
ExpandedSubBlockStart.gif            /* OpenGL animation code goes here */
InBlock.gif
InBlock.gif            glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
InBlock.gif            glClear (GL_COLOR_BUFFER_BIT);
InBlock.gif
InBlock.gif            glPushMatrix ();
InBlock.gif            glRotatef (theta, 0.0f, 0.0f, 1.0f);
InBlock.gif            glBegin (GL_TRIANGLES);
InBlock.gif            glColor3f (1.0f, 0.0f, 0.0f);   glVertex2f (0.0f, 1.0f);
InBlock.gif            glColor3f (0.0f, 1.0f, 0.0f);   glVertex2f (0.87f, -0.5f);
InBlock.gif            glColor3f (0.0f, 0.0f, 1.0f);   glVertex2f (-0.87f, -0.5f);
InBlock.gif            glEnd ();
InBlock.gif            glPopMatrix ();
InBlock.gif
InBlock.gif            SwapBuffers (hDC);
InBlock.gif
InBlock.gif            theta += 1.0f;
InBlock.gif            Sleep (1);
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
InBlock.gif
ExpandedSubBlockStart.gif    /* shutdown OpenGL */
InBlock.gif    DisableOpenGL (hWnd, hDC, hRC);
InBlock.gif
ExpandedSubBlockStart.gif    /* destroy the window explicitly */
InBlock.gif    DestroyWindow (hWnd);
InBlock.gif
InBlock.gif    return msg.wParam;
ExpandedBlockEnd.gif}

None.gif
None.gif
ExpandedBlockStart.gif
/********************
InBlock.gif * Window Procedure
InBlock.gif *
ExpandedBlockEnd.gif *******************
*/

None.gif
None.gifLRESULT CALLBACK WndProc (HWND hWnd, UINT message,
None.gif                          WPARAM wParam, LPARAM lParam)
ExpandedBlockStart.gif
{
InBlock.gif
InBlock.gif    switch (message)
ExpandedSubBlockStart.gif    {
InBlock.gif    case WM_CREATE:
InBlock.gif        return 0;
InBlock.gif    case WM_CLOSE:
InBlock.gif        PostQuitMessage (0);
InBlock.gif        return 0;
InBlock.gif
InBlock.gif    case WM_DESTROY:
InBlock.gif        return 0;
InBlock.gif
InBlock.gif    case WM_KEYDOWN:
InBlock.gif        switch (wParam)
ExpandedSubBlockStart.gif        {
InBlock.gif        case VK_ESCAPE:
InBlock.gif            PostQuitMessage(0);
InBlock.gif            return 0;
ExpandedSubBlockEnd.gif        }
InBlock.gif        return 0;
InBlock.gif
InBlock.gif    default:
InBlock.gif        return DefWindowProc (hWnd, message, wParam, lParam);
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}

None.gif
None.gif
ExpandedBlockStart.gif
/*******************
InBlock.gif * Enable OpenGL
InBlock.gif *
ExpandedBlockEnd.gif ******************
*/

None.gif
None.gif
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
ExpandedBlockStart.gif
{
InBlock.gif    PIXELFORMATDESCRIPTOR pfd;
InBlock.gif    int iFormat;
InBlock.gif
ExpandedSubBlockStart.gif    /* get the device context (DC) */
InBlock.gif    *hDC = GetDC (hWnd);
InBlock.gif
ExpandedSubBlockStart.gif    /* set the pixel format for the DC */
InBlock.gif    ZeroMemory (&pfd, sizeof (pfd));
InBlock.gif    pfd.nSize = sizeof (pfd);
InBlock.gif    pfd.nVersion = 1;
InBlock.gif    pfd.dwFlags = PFD_DRAW_TO_WINDOW | 
InBlock.gif      PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
InBlock.gif    pfd.iPixelType = PFD_TYPE_RGBA;
InBlock.gif    pfd.cColorBits = 24;
InBlock.gif    pfd.cDepthBits = 16;
InBlock.gif    pfd.iLayerType = PFD_MAIN_PLANE;
InBlock.gif    iFormat = ChoosePixelFormat (*hDC, &pfd);
InBlock.gif    SetPixelFormat (*hDC, iFormat, &pfd);
InBlock.gif
ExpandedSubBlockStart.gif    /* create and enable the render context (RC) */
InBlock.gif    *hRC = wglCreateContext( *hDC );
InBlock.gif    wglMakeCurrent( *hDC, *hRC );
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif
ExpandedBlockStart.gif
/******************
InBlock.gif * Disable OpenGL
InBlock.gif *
ExpandedBlockEnd.gif *****************
*/

None.gif
None.gif
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
ExpandedBlockStart.gif
{
InBlock.gif    wglMakeCurrent (NULL, NULL);
InBlock.gif    wglDeleteContext (hRC);
InBlock.gif    ReleaseDC (hWnd, hDC);
ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值