分享

OpenGL VS2008 环境配置

 缘梦逍遥 2010-11-27
 


[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);
}

//To init the function

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

下载并安装glut库 opengl的glut库 GLUT不是OpenGL所必须的,但它会给我们的学习带来一定的方便,推荐安装。 Windows环境下的GLUT下载地址:(大小约为150k) http://www./resources/libraries/glut/glutdlls37beta.zip 
Windows环境下安装GLUT的步骤:  

*.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;
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多