• VC6.0 VS2008 openGL环境配置 [和glut库的加入]


    openGL VC6.0(Microsoft Visual C++ 6.0)环境配置

    1,头文件的包含:openGL相关的头文件在 */include/gl 目录中,主要有 gl.h glu.h glaux.h 根据需要include就行。
    2,连接库的设置:菜单- 工程- 设置- 连接- 对象/库模块 中加入opengl32.lib glu32.lib,注意用空格隔开各*.lib。

    [VC6.0具体示例]
    1,新建工程:菜单-文件-新建-工程-Win32 Application-工程名-确定-一个空工程-完成。
    2,加入源文件:文件-新建-C++ Source File-文件名-确定-编写代码(可以参考后面的示例代码)。
    3,设置连接库: 工程- 设置- 连接- 对象/库模块 中加入opengl32.lib glu32.lib,注意用空格隔开各*.lib。
    4,编译连接运行。


    openGL VS2008(Microsoft Visual Studio 2008)环境配置

    1,头文件的包含:openGL相关的头文件在 */include/gl 目录中,主要有 gl.h glu.h glaux.h 根据需要include就行。
    2,连接库的设置:项目-属性(快捷键ALT+F7) -配置属性-连接器-输入-附加依赖项 中加入opengl32.lib glu32.lib,注意用空格隔开各*.lib。

    [VS2008具体示例]
    1,新建工程:菜单-文件-新建-项目-Visual C++-Win32 项目-工程名-确定-空项目-完成。
    2,加入源文件:解决方案资源管理器中的源文件点击右键-添加-新建项-C++文件-输入名称-确定-编写代码(可以参考后面的示例代码)。
    3,设置连接库: 项目-属性(快捷键ALT+F7) -配置属性-连接器-输入-附加依赖项 中加入opengl32.lib glu32.lib,注意用空格隔开各*.lib。
    4,编译连接运行。

    [示例代码]
    #include <windows.h>
    #include <gl/gl.h>
    #include <gl/glu.h>

    HWND hWnd;
    HDC hDC;
    HGLRC hRC;

    void SetupPixelFormat()
    {
    PIXELFORMATDESCRIPTOR pfd,* ppfd;
    int pixelformat;
    ppfd=&pfd;
    ppfd->nSize=sizeof(PIXELFORMATDESCRIPTOR);
    ppfd->nVersion=1;
    ppfd->dwFlags=PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    ppfd->dwLayerMask=PFD_MAIN_PLANE;
    ppfd->iPixelType=PFD_TYPE_COLORINDEX;
    ppfd->cColorBits=16;
    ppfd->cDepthBits=16;
    ppfd->cAccumBits=0;
    ppfd->cStencilBits=0;

    pixelformat=ChoosePixelFormat(hDC,ppfd);
    SetPixelFormat(hDC,pixelformat,ppfd);
    }

    void InitGraphics()
    {
    hDC=GetDC(hWnd);
    SetupPixelFormat();
    hRC=wglCreateContext(hDC);
    wglMakeCurrent(hDC,hRC);
    glClearColor(0,0,0,0.5);
    glClearDepth(1.0);
    glEnable(GL_DEPTH_TEST);
    }

    void ResizeGraphics()
    {
    RECT rect;
    GetClientRect(hWnd,&rect);
    int width=rect.right;
    int height=rect.bottom;

    GLfloat aspect=(GLfloat)width/height;

    glViewport(0,0,width,height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0,aspect,1.0,100.0);
    glMatrixMode(GL_MODELVIEW);
    }

    void DrawGraphics()
    {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslated(0,0,-10);
    glBegin(GL_QUADS);
    glColor3d(1,0,0);
    glVertex3d(-2,2,0);
    glVertex3d(2,2,0);
    glVertex3d(2,-2,0);
    glVertex3d(-2,-2,0);
    glEnd();
    SwapBuffers(hDC);
    }

    long WINAPI MainWndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
    {
    switch(uMsg)
    {
    case WM_SIZE:
       ResizeGraphics();
       break;
    case WM_CLOSE:
       DestroyWindow(hWnd);
       break;
    case WM_DESTROY:
       PostQuitMessage(0);
       break;
    default:
       return DefWindowProc(hWnd,uMsg,wParam,lParam);
    }
    return 1;
    }

    int WINAPI WinMain(HINSTANCE hInstance,
           HINSTANCE hPrevInstance,
           LPSTR lpCmdLine,
           int nShowCmd)
    {
    const TCHAR AppName[]=TEXT("OpenGL Sample");
    WNDCLASS cwnd;
    MSG msg;

    cwnd.style=0;
    cwnd.lpfnWndProc=MainWndProc;
    cwnd.cbClsExtra=0;
    cwnd.cbWndExtra=0;
    cwnd.hInstance=hInstance;
    cwnd.hIcon=LoadIcon(hInstance,AppName);
    cwnd.hCursor=LoadCursor(NULL,IDC_ARROW);
    cwnd.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);
    cwnd.lpszMenuName=AppName;
    cwnd.lpszClassName=AppName;

    if(! RegisterClass(&cwnd))
       return 1;
    hWnd=CreateWindow(AppName,AppName,
       WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
       CW_USEDEFAULT,
       CW_USEDEFAULT,
       800,600,NULL,NULL,hInstance,NULL);
    if(! hWnd) return 0;
    InitGraphics();
    ShowWindow(hWnd,nShowCmd);
    UpdateWindow(hWnd);
    while(1)
    {
       if(PeekMessage(&msg,NULL,0,0,PM_NOREMOVE))
       {
        if(! GetMessage(&msg,NULL,0,0))
         return true;
        TranslateMessage(&msg);
        DispatchMessage(&msg);
       }
       DrawGraphics();
    }

    wglDeleteContext(hRC);
    ReleaseDC(hWnd,hDC);
    return msg.wParam;
    }

    另外:
    openGL有一个glut库能支持更快的开发openGL程序,里面包含了glut.h glut.lib glut.dll glut32.lib glut32.dll

     下载之后


    *.dll 当然是复制到windows/system32中
    *.lib
    如果是VC6.0,复制到 */VC98/Lib 中
    如果是VS2008,复制到 */VC/lib 中

    Glut.h
    如果是VC6.0,复制到 */VC98/include/gl 中
    如果是VS2008,复制到 */VC/include/gl (没有gl目录就新建一个) 中

    [一个glut支持的代码(可以用来检测glut是否添加成功)]
    #include <GL/glut.h>
    void myDisplay()
    {
    glClear(GL_COLOR_BUFFER_BIT);
    glRectf(-0.5f, -0.5f, 0.5f, 0.5f);
    glFlush();
    }
    int main(int argc, char *argv[])
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(400, 400);
    glutCreateWindow("第一个OpenGL程序");
    glutDisplayFunc(&myDisplay);
    glutMainLoop();
    return 0;
    }

  • 相关阅读:
    vs.net 2005 C# WinForm GroupBOX 的BUG?尝试读取或写入受保护的内存。这通常指示其他内存已损坏
    Git安装及基本使用
    c++实现将表达式转换为逆波兰表达式
    2015年倒数第6周学习报告
    读过的书及读后感
    c++实现队列
    链表插入排序(insertion-sort-list)
    test
    [转]maven入门
    几个学习Maven不错的网址
  • 原文地址:https://www.cnblogs.com/frustrate2/p/2588353.html
Copyright © 2020-2023  润新知