设为首页 加入收藏

TOP

VC中利用OpenGL实现三维绘图(二)
2012-11-04 15:20:51 来源: 作者: 【 】 浏览:419
Tags:利用 OpenGL 实现 三维 绘图


  3、在VC下实现程序编译

  在VC编辑器下键入下述代码后,保存为后缀是.cpp的C++(www.cppentry.com)文件。开始编译,在“The build command requires an active project workspace”。“Would you like to create a default project workspace”? 的提示后,选择“是(Y)”。进入“Project”菜单,选择“Setting”项,弹出“Project Setting”对话框,选择“Link”项,在“Libaray”栏目中加入OpenGL提供的函数库:“opengl32.lib glu32.lib glaux.lib”。(注意:在执行程序时,Windows的system目录下要包含opengl32.dll及glu32.dll两个动态连接库)。附源程序代码:

  #include "windows.h"
  #include "gl/gl.h"
  #include "gl/glaux.h"
  #include "gl/glu.h"
  #include "math.h"

  void myinit()
  {
    glClearColor(1,1,0,0);
    GLfloat ambient[]={.5,.5,.5,0};
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
    GLfloat mat_ambient[]={.8,.8,.8,1.0};
    GLfloat mat_diffuse[]={.8,.0,.8,1.0};
    GLfloat mat_specular[]={1.0,.0,1.0,1.0};
    GLfloat mat_shininess[]={50.0};
    GLfloat light_diffuse[]={0,0,.5,1};
    GLfloat light_position[]={0,0,1.0,0};
    glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_ambient);
    glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,mat_diffuse);
    glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_specular);
    glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,mat_shininess);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
    glLightfv(GL_LIGHT0,GL_POSITION, light_position);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glDepthFunc(GL_LESS);
    glEnable(GL_DEPTH_TEST);
  }

  void CALLBACK display()
  {
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    auxSolidSphere(1.0);  // 绘制半径为1.0的实体球
    glFlush();       // 强制输出图像
    auxSwapBuffers();    // 交换绘图缓存
    _sleep(100);
  }

  void CALLBACK Idledisplay()
  {
    // x,y满足x2+y2=0.01。这样可以使物体沿该圆轨迹运动。
    static float x=-.1,y=0.0;
    static BOOL mark=TRUE;
    static float step=.01;
    x+=step;
    if(x<=.1&&x>=-.1)
    {
      if(step>0)
        y=sqrt(.01-x*x);
      else
        y=-sqrt(.01-x*x);
      glTranslatef(x,y,0);
    }
    else
    {
      step=0-step;
    }
    display();
  }

  void CALLBACK myReshape(GLsizei w,GLsizei h)
  {
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if(w<=h)
      glOrtho(-3.5,3.5,-3.5*(GLfloat)w/(GLfloat)h, 3.5*(GLfloat)w/(GLfloat)h,-10,10);
    else
      glOrtho(-3.5*(GLfloat)w/(GLfloat)h,3.5* (GLfloat)w/(GLfloat)h,-3.5,3.5,-10,10);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
  }

  void main()
  {
    auxInitDisplayMode(AUX_DOUBLE|AUX_RGBA);
    auxInitPosition(0,0,400,400);
    auxInitWindow(" circle ");
    myinit();
    auxReshapeFunc(myReshape);
    auxIdleFunc(Idledisplay);
    auxMainLoop(display);
  }
首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇VC编程实现文本语音转换 下一篇VC数字图像处理编程讲座之五

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: