首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于VC跟OpenGL

2012-08-15 
关于VC和OpenGL工作需要, 要在windows上用VC6和OpenGL做东西, 由于我一直在Linux下做开发, 所以对这些东东

关于VC和OpenGL
工作需要, 要在windows上用VC6和OpenGL做东西, 由于我一直在Linux下做开发, 所以对这些东东一点也不知道,
各位仁兄帮我一下哈, 分数是大大地有!
1)OpenGL的库到哪去弄?
2)如何用VC6调用OpenGL?
3)给一个完整的例子, 加注释说明的!

[解决办法]

C/C++ code
//使用vc6开发opengl工程[原创]//首先要在工程设置连接中加入opengl32.lib//微软在Visual C++中已提供了三个OpenGL的函数库(glu32.lib, glau.lib,OpenGL32.lib)//注意要加入头文件#i nclude <gl/gl.h>//因为gl.h文件在vc的include 下面的gl目录下面//下面是一个基于win32的例子/************************** * Includes * **************************/#i nclude <windows.h>#i nclude <gl/gl.h>/************************** * Function Declarations * **************************/LRESULT CALLBACK WndProc (HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam);void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);/************************** * WinMain * **************************/int WINAPI WinMain (HINSTANCE hInstance,                    HINSTANCE hPrevInstance,                    LPSTR lpCmdLine,                    int iCmdShow){    WNDCLASS wc;    HWND hWnd;    HDC hDC;    HGLRC hRC;            MSG msg;    BOOL bQuit = FALSE;    float theta = 0.0f;    /* register window class */    wc.style = CS_OWNDC;    wc.lpfnWndProc = WndProc;    wc.cbClsExtra = 0;    wc.cbWndExtra = 0;    wc.hInstance = hInstance;    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);    wc.hCursor = LoadCursor (NULL, IDC_ARROW);    wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);    wc.lpszMenuName = NULL;    wc.lpszClassName = "GLSample";    RegisterClass (&wc);    /* create main window */    hWnd = CreateWindow (      "GLSample", "OpenGL Sample",       WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,      0, 0, 256, 256,      NULL, NULL, hInstance, NULL);    /* enable OpenGL for the window */    EnableOpenGL (hWnd, &hDC, &hRC);    /* program main loop */    while (!bQuit)    {        /* check for messages */        if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))        {            /* handle or dispatch messages */            if (msg.message == WM_QUIT)            {                bQuit = TRUE;            }            else            {                TranslateMessage (&msg);                DispatchMessage (&msg);            }        }        else        {            /* OpenGL animation code goes here */            glClearColor (0.0f, 0.0f, 0.0f, 0.0f);            glClear (GL_COLOR_BUFFER_BIT);            glPushMatrix ();            glRotatef (theta, 0.0f, 0.0f, 1.0f);            glBegin (GL_TRIANGLES);            glColor3f (1.0f, 0.0f, 0.0f);   glVertex2f (0.0f, 1.0f);            glColor3f (0.0f, 1.0f, 0.0f);   glVertex2f (0.87f, -0.5f);            glColor3f (0.0f, 0.0f, 1.0f);   glVertex2f (-0.87f, -0.5f);            glEnd ();            glPopMatrix ();            SwapBuffers (hDC);            theta += 1.0f;            Sleep (1);        }    }    /* shutdown OpenGL */    DisableOpenGL (hWnd, hDC, hRC);    /* destroy the window explicitly */    DestroyWindow (hWnd);    return msg.wParam;}/******************** * Window Procedure * ********************/LRESULT CALLBACK WndProc (HWND hWnd, UINT message,                          WPARAM wParam, LPARAM lParam){    switch (message)    {    case WM_CREATE:        return 0;    case WM_CLOSE:        PostQuitMessage (0);        return 0;    case WM_DESTROY:        return 0;    case WM_KEYDOWN:        switch (wParam)        {        case VK_ESCAPE:            PostQuitMessage(0);            return 0;        }        return 0;    default:        return DefWindowProc (hWnd, message, wParam, lParam);    }}/******************* * Enable OpenGL * *******************/void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC){    PIXELFORMATDESCRIPTOR pfd;    int iFormat;    /* get the device context (DC) */    *hDC = GetDC (hWnd);    /* set the pixel format for the DC */    ZeroMemory (&pfd, sizeof (pfd));    pfd.nSize = sizeof (pfd);    pfd.nVersion = 1;    pfd.dwFlags = PFD_DRAW_TO_WINDOW |       PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;    pfd.iPixelType = PFD_TYPE_RGBA;    pfd.cColorBits = 24;    pfd.cDepthBits = 16;    pfd.iLayerType = PFD_MAIN_PLANE;    iFormat = ChoosePixelFormat (*hDC, &pfd);    SetPixelFormat (*hDC, iFormat, &pfd);    /* create and enable the render context (RC) */    *hRC = wglCreateContext( *hDC );    wglMakeCurrent( *hDC, *hRC );}/****************** * Disable OpenGL * ******************/void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC){    wglMakeCurrent (NULL, NULL);    wglDeleteContext (hRC);    ReleaseDC (hWnd, hDC);} 


[解决办法]
1)OpenGL的库到哪去弄? 

vc 带了几本的opengl库,头文件在 vc 的gl目录下,你只要include 就可以了

比如 #include <gl/GL.h> ,但不包含辅助库 glut,你要自己去下

http://download.csdn.net/source/695037
 
2)如何用VC6调用OpenGL? 
3)给一个完整的例子, 加注释说明的!

给你一个最简单的例子

C/C++ code
#include "gl\glut.h"void display(void){    glClear (GL_COLOR_BUFFER_BIT);/* clear all pixels    */    glColor3f (1.0, 1.0, 1.0);    glBegin(GL_POLYGON);/* draw white polygon with corners at(0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)*/    glVertex3f (0.25, 0.25, 0.0);    glVertex3f (0.75, 0.25, 0.0);    glVertex3f (0.75, 0.75, 0.0);    glVertex3f (0.25, 0.75, 0.0);    glEnd();    glFlush ();/* start processing buffered OpenGL routines    */}void init (void){    glClearColor (0.0, 0.0, 0.0, 0.0);/* select clearing color    */    glMatrixMode(GL_PROJECTION);    glLoadIdentity();    glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);/* initialize viewing values    */}int main(int argc, char** argv){    glutInit(&argc, argv);    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);/*Declare initial display mode(single buffer and RGBA).*/    glutInitWindowSize (250, 250); /*Declare initial window size.*/    glutInitWindowPosition (100, 100);/*Declare initial window position.*/    glutCreateWindow ("hello");/*Open window with "hello"in its title bar.*/      init ();/*Call initialization routines.*/    glutDisplayFunc(display); /*Register callback function to display graphics.*/    glutMainLoop();/*Enter main loop and process events.*/    return 0;     /* ANSI C requires main to return int. */}
[解决办法]
C/C++ code
1。下载GLUT库:http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip2。将压缩包内的glut.h放到...\Microsoft Visual Studio\VC98\Include\GL目录下     将glut32.lib放到...\Microsoft Visual Studio\VC98\Lib目录下     将glut32.dll放到X:\windows\systom32目录下(win98用户放到X:\windows\systom目录下)3。建立一个控制台工程Win32 Console Application,加入hello.c并运行:#i nclude <GL/glut.h>void display(void){     glClear (GL_COLOR_BUFFER_BIT);/* clear all pixels    */     glColor3f (1.0, 1.0, 1.0);     glBegin(GL_POLYGON);/* draw white polygon with corners at(0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)*/        glVertex3f (0.25, 0.25, 0.0);        glVertex3f (0.75, 0.25, 0.0);        glVertex3f (0.75, 0.75, 0.0);        glVertex3f (0.25, 0.75, 0.0);     glEnd();     glFlush ();/* start processing buffered OpenGL routines    */}void init (void){     glClearColor (0.0, 0.0, 0.0, 0.0);/* select clearing color    */     glMatrixMode(GL_PROJECTION);     glLoadIdentity();     glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);/* initialize viewing values    */}int main(int argc, char** argv){     glutInit(&argc, argv);     glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);/*Declare initial display mode(single buffer and RGBA).*/     glutInitWindowSize (250, 250); /*Declare initial window size.*/     glutInitWindowPosition (100, 100);/*Declare initial window position.*/     glutCreateWindow ("hello");/*Open window with "hello"in its title bar.*/       init ();/*Call initialization routines.*/     glutDisplayFunc(display); /*Register callback function to display graphics.*/     glutMainLoop();/*Enter main loop and process events.*/     return 0;     /* ANSI C requires main to return int. */}
[解决办法]
在VC下配置openGL 一、下载并安装glut库
opengl的glut库 GLUT不是OpenGL所必须的,但它会给学习带来一定的方便,推荐安装。
Windows环境下的GLUT下载地址:(大小约为150k)
http://www.opengl.org/resources/libraries/glut/glutdlls37beta.zip
Windows环境下安装GLUT的步骤:
1、将下载的压缩包解开,将得到5个文件
2、在“我的电脑”中搜索“gl.h”,并找到其所在文件夹(Program Files\Microsoft Visual Studio\VC98\Include\GL文件夹”)。把解压得到的glut.h放到这个文件夹。


3、把解压得到的glut.lib和glut32.lib放到静态函数库所在文件夹(Program Files\Microsoft Visual Studio\VC98\lib”文件夹)。
4、把解压得到的glut.dll和glut32.dll放到操作系统目录下面的system32文件夹内。(典型的位置为:C:\Windows\System32) 

二、VC工程配置:
  1)创建一个Win32 Console Application。
  2)链接OpenGL libraries。单击Project,再单击Settings,再找到Link单击,最后在Object/library modules 的最前面加上opengl32.lib glut32.lib glu32.lib glut.lib glaux.lib

如果仅仅是需要使用glut,问题还好,但是由于VC中本身没有glut,于是我们可到网上去下载但是下载回来之后,编译成功,链接往往会出现如下错误:

LNK2001: unresolved external symbol ___glutInitWithExit@12
chapter1.obj : error LNK2001: unresolved external symbol ___glutCreateWindowWithExit@8
chapter1.obj : error LNK2001: unresolved external symbol ___glutCreateMenuWithExit@8

这是怎么回事呢?(这里已经假定你已经在【工程】-【设置】-【link】那里加上了glut32.lib,而且已经把glut32.lib文件放到了VC的LIB文件夹下,把glut32.dll放进了WINDOWS\system32中,或者这些都放进了你的当前目录下,也就是说排除了这些常识性的错误)那么为什么还是错误呢?而且根据错误好像是没有找到一些函数的实现体的样子。遇到这样的问题也是真够郁闷的。

我上网查了很久,很多人都在说是【工程】-【设置】-【link】的问题(但是前面已经说过可以排除了)后来,我才知道原来是glut的版本的问题,也就是说,如果glut32.lib的版本太旧可能就会引起以上错误了,于是我去找了一个新的(我也不知道是否最新,2001年的),终于可以使用了,那一刻的感觉真的很爽,希望大家看了这篇文章之后可以节省一些时间,呵呵!


[解决办法]
windows都差不多了介绍一个linux的吧
一、安装JDK以及Eclipse,我这里就不多介绍了,请自行参考网上资料。

二、安装CDT:

1.可以在Eclipse update中输入以下地址来安装:http://download.eclipse.org/tools/cdt/releases/ganymede

2.也可以下载CDT Master update : http://download.eclipse.org/tools/cdt/releases/ganymede/ 

然后也是在Eclipse Update中选用本地路径来安装CDT。具体路径为:Help-->Software updates-->Available Software,选择Add site-->local,然后把刚才下载好的CDT Master导入。最后在左边列表选项中选择CDT,右键选择安装就可以了。 

三、接下来准备OpenGL开发的条件:安装freeGlut,Gcc等。命令行如下:

1.sudo apt-get install mesa-common-dev mesademos libgl1-mesa-dev libglu1-mesa-dev freeglut3-dev

2. sudo aptitude install build-essential

四、打开Eclipse,新建C++ Project.并且新增一个c++ source file。代码如下:

 

C/C++ code
#include <GL/glut.h>#define window_width 640#define window_height 480// Main loopvoid main_loop_function(){// Z anglestatic float angle;// Clear color (screen) // And depth (used internally to block obstructed objects)glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);// Load identity matrixglLoadIdentity();// Multiply in translation matrixglTranslatef(0,0, -10);// Multiply in rotation matrixglRotatef(angle, 0, 0, 1);// Render colored quadglBegin(GL_QUADS);glColor3ub(255, 000, 000); glVertex2f(-1, 1);glColor3ub(000, 255, 000); glVertex2f( 1, 1);glColor3ub(000, 000, 255); glVertex2f( 1, -1);glColor3ub(255, 255, 000); glVertex2f(-1, -1);glEnd();// Swap buffers (color buffers, makes previous render visible)glutSwapBuffers();// Increase angle to rotateangle+=0.25;}// Initialze OpenGL perspective matrixvoid GL_Setup(int width, int height){glViewport( 0, 0, width, height );glMatrixMode( GL_PROJECTION );glEnable( GL_DEPTH_TEST );gluPerspective( 45, (float)width/height, .1, 100 );glMatrixMode( GL_MODELVIEW );}// Initialize GLUT and start main loopint main(int argc, char** argv) {glutInit(&argc, argv);glutInitWindowSize(window_width, window_height);glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);glutCreateWindow("GLUT Example!!!");glutIdleFunc(main_loop_function);GL_Setup(window_width, window_height);glutMainLoop();} 

热点排行