• 热门专题

图解VC++opengl环境配置和几个入门例子

作者:  发布日期:2016-04-05 22:54:35
Tag标签:例子  环境  
  • VC6下载

    http://blog.csdn.net/bcbobo21cn/article/details/44200205

    demo工程和glut库下载

    http://pan.baidu.com/s/1pLSpKiZ

    一 环境配置

    参阅

    http://blog.itpub.net/17267437/viewspace-545635/

    解压glut库

    拷贝glut.h到如下目录;

    拷贝图1的两个lib到如下目录;

    拷贝图1的两个dll到如下目录(偶是64位系统);

    工程设置添加四个lib

    二例子1

    #include <GL/glut.h>
    #include <math.h>

    const int n = 200;
    const GLfloat R = 0.5f;
    const GLfloat Pi = 3.1415926536f;

    void myDisplay(void)
    {
    int i;
    // 画一个绿色正方形
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(0.0f,1.0f,0.0f);
    glRectf(-0.5f, 0.4f, 0.0f, 0.9f); //画一个绿色的正方形
    //glFlush();

    //glClearColor(0.0,0.0,0.0,0.0);
    //glClear(GL_COLOR_BUFFER_BIT);
    glColor4f(1.0,0.0,0.0,1.0); // set the quad color
    glBegin(GL_QUADS);
    glVertex3f(-0.2,-0.2,0.0);
    glVertex3f(0.2,-0.2,0.0);
    glVertex3f(0.2,0.2,0.0);
    glVertex3f(-0.2,0.2,0.0);
    glEnd();

    glColor4f(1.0,0.0,0.0,1.0); // set the point color
    glPointSize(10);
    glBegin(GL_POINTS);
    glVertex3f(-0.5,-0.5,0.0);
    glVertex3f(0.5,-0.5,0.0);
    glVertex3f(0.5,0.5,0.0);
    glVertex3f(-0.5,0.5,0.0);
    glEnd();

    glColor4f(1.0,0.0,0.0,1.0); // set the line color
    glLineWidth(5);
    glBegin(GL_LINES);
    glVertex3f(-0.5,-0.5,0.0);
    glVertex3f(0.5,-0.5,0.0);
    glVertex3f(0.5,0.5,0.0);
    glVertex3f(-0.5,0.5,0.0);
    glEnd();

    /*glBegin(GL_POLYGON);
    for(i=0; i<n; ++i)
    glVertex2f(R*cos(2*Pi/n*i), R*sin(2*Pi/n*i));
    glEnd();*/

    glFlush();
    }

    int main(int argc, char *argv[])
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(100, 0);
    glutInitWindowSize(1024, 768);
    glutCreateWindow("OpenGL基本绘制");
    glutDisplayFunc(&myDisplay);
    glutMainLoop();
    return 0;
    }

    网上说要改下图的_CONSOLE,我没改也能在开发环境下运行;

    三 例子2

    #include <GL/glut.h>

    void myDisplay(void)
    {
    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;
    }

    四 例子3

    #include <GL/glut.h>

    //Called to draw scene
    void RenderSence(void)
    {
    //Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT);
    //Flush drawing commands
    glFlush();
    }
    //Set up the rendering state
    void SetupRC(void)
    {
    glClearColor(0.0f,0.0f,1.0f,1.0f); //此时背景色为蓝色
    }

    int main(int argc, char *argv[])
    {

    //glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_SINGLE|GLUT_RGB);
    glutCreateWindow("Simple"); //窗口名为“Simple”
    glutDisplayFunc(RenderSence);
    SetupRC();
    glutMainLoop();

    return 0;
    }

    编译,出现一堆下述错误;

    demo4.cpp
    y:dddd10 c6opengldemodemo4demo4.cpp(5) : error C2018: unknown character '0xa1'
    y:dddd10 c6opengldemodemo4demo4.cpp(5) : error C2018: unknown character '0xa1'
    y:dddd10 c6opengldemodemo4demo4.cpp(10) : error C2018: unknown character '0xa1'

    ......

    是因为代码中包含全角字符;寻找发现该全角字符是空格;用替换功能,把全角空格全部替换为半角空格再编译即可;

    五 例子4

    #include <GL/glut.h>

    void background(void)
    {
    glClearColor(0.0,0.0,0.0,0.0);//设置背景颜色为黑色
    }

    void myDisplay(void)
    {
    glClear(GL_COLOR_BUFFER_BIT);//buffer设置为颜色可写

    glBegin(GL_TRIANGLES);//开始画三角形
    glShadeModel(GL_SMOOTH);//设置为光滑明暗模式

    glColor3f(1.0,0.0,0.0);//设置第一个顶点为红色
    glVertex2f(-1.0,-1.0);//设置第一个顶点的坐标为(-1.0,-1.0)

    glColor3f(0.0,1.0,0.0);//设置第二个顶点为绿色
    glVertex2f(0.0,-1.0);//设置第二个顶点的坐标为(0.0,-1.0)

    glColor3f(0.0,0.0,1.0);//设置第三个顶点为蓝色
    glVertex2f(-0.5,1.0);//设置第三个顶点的坐标为(-0.5,1.0)
    glEnd();//三角形结束

    glFlush();//强制OpenGL函数在有限时间内运行
    }

    void myReshape(GLsizei w,GLsizei h)
    {
    glViewport(0,0,w,h);//设置视口

    glMatrixMode(GL_PROJECTION);//指明当前矩阵为GL_PROJECTION
    glLoadIdentity();//将当前矩阵置换为单位阵

    if(w <= h)
    gluOrtho2D(-1.0,1.5,-1.5,1.5*(GLfloat)h/(GLfloat)w);//定义二维正视投影矩阵
    else
    gluOrtho2D(-1.0,1.5*(GLfloat)w/(GLfloat)h,-1.5,1.5);
    glMatrixMode(GL_MODELVIEW);//指明当前矩阵为GL_MODELVIEW
    }

    int main(int argc,char ** argv)
    {
    /*初始化*/
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(400,400);
    glutInitWindowPosition(200,200);

    /*创建窗口*/
    glutCreateWindow("Triangle");

    /*绘制与显示*/
    background();
    glutReshapeFunc(myReshape);
    glutDisplayFunc(myDisplay);

    glutMainLoop();
    return(0);
    }

    六 opengl资源链接

    http://www.cnblogs.com/phinecos/category/90224.html

    基于MFC的opengl编程

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规