声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 6362|回复: 12

[C/C++] OpenGL练习(一):二维图形和鼠标键盘事件

[复制链接]
发表于 2011-5-3 20:43 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
本帖最后由 Rainyboy 于 2011-5-3 20:58 编辑

作为工程师的基本技能,无论是机械设计图还是规律变化图,画图本身不是什么难事。然而OpenGL作为使用得最广泛的底层绘图库类之一,是值得学习的。本帖包含了四个例子,是近来在重新学习这一图形工具时的习作,分别是:
1,一个单摆的演示动画;
          单摆.jpg


2,一个旋转的正方形(可以通过键盘控制转动方向);
         旋转正方形.jpg


3,一个可以画圆、矩形和直线的小绘图工具(可以通过键盘控制绘图类型,有最简单的绘图预览);
         简单鼠标绘图器.jpg


4,一个弹性球在窗口内四处碰撞的演示动画。
       弹性球.jpg


=========================================================


主程序的代码和预编译头文件的内容在这里给出,后续四个楼层分别给出这四个练习的具体代码。



===程序的入口点:OpenGLTest.cpp====

  1. #include "stdafx.h"
  2. #include "Simple_Pendulum.h"
  3. #include "Rotat_Block.h"
  4. #include "SimplePaint.h"
  5. #include "ElasticBall.h"


  6. int _tmain(int argc, _TCHAR* argv[])
  7. {
  8.         system("cls");
  9.         cout<<"OpenGL练习 StageA: 二维图形和鼠标事件"<<endl;
  10.         cout<<"1,单摆"<<endl;
  11.         cout<<"2,旋转正方形"<<endl;
  12.         cout<<"3,简单鼠标绘图器"<<endl;
  13.         cout<<"4,弹性碰撞球"<<endl;
  14.         cout<<"输入选项:";
  15.         int c;
  16.         cin>>c;
  17.         switch(c)
  18.         {
  19.         case 1:
  20.                 Simple_Pendulum(argc,argv);
  21.                 break;
  22.         case 2:
  23.                 Rotat_Block(argc,argv);
  24.                 break;
  25.         case 3:
  26.                 Simple_Paint(argc,argv);
  27.                 break;
  28.         case 4:
  29.                 Elastic_Ball(argc,argv);
  30.                 break;
  31.         }
  32.         return 0;
  33. }
复制代码


===预编译头文件 stdafx.h =====

  1. #pragma once

  2. #include "targetver.h"

  3. #include <stdio.h>
  4. #include <tchar.h>



  5. // TODO: 在此处引用程序需要的其他头文件
  6. #include <windows.h>
  7. #include <gl/GL.h>
  8. #include <gl/GLU.h>
  9. #include <gl/glut.h>
  10. #include <math.h>
  11. #include <stdlib.h>
  12. #include <iostream>
  13. #include <vector>
  14. using namespace std;
复制代码





OpenGLTest.rar

16.53 KB, 下载次数: 18

编译后的程序

回复
分享到:

使用道具 举报

 楼主| 发表于 2011-5-3 20:46 | 显示全部楼层
本帖最后由 Rainyboy 于 2011-5-3 20:49 编辑

====单摆演示动画,头文件:Simple_Pendulum.h=======
  1. #include "stdafx.h"
  2. class struct_SP_Global
  3. {
  4. public:
  5.         struct_SP_Global()
  6.         {
  7.                 theta = 0;
  8.                 theta_inc =1.0;
  9.                 amplitude = 10.0;
  10.                 DEG_TO_RAD = 0.017453;
  11.         }
  12. public:
  13.         GLsizei ww;
  14.         GLsizei hh;
  15.         GLfloat theta;
  16.         GLfloat theta_inc;
  17.         GLfloat amplitude;
  18.         GLfloat DEG_TO_RAD;
  19. };
  20. void SP_userReshape(GLsizei w,GLsizei h);
  21. void SP_display();
  22. void SP_init();
  23. void SP_myidle();
  24. int Simple_Pendulum(int argc, _TCHAR* argv[]);
复制代码


====单摆演示动画,代码文件:Simple_Pendulum.cpp======

  1. #include "stdafx.h"
  2. #include "Simple_Pendulum.h"

  3. struct_SP_Global SP_Global;
  4. void SP_userReshape(GLsizei w,GLsizei h)
  5. {
  6.         glMatrixMode(GL_PROJECTION);
  7.         glLoadIdentity();
  8.         gluOrtho2D(-(GLdouble)w/2,(GLdouble)w/2,-(GLdouble)h/2,(GLdouble)h/2);
  9.         glMatrixMode(GL_MODELVIEW);
  10.         glViewport(0,0,w,h);
  11.         SP_Global.ww=w;
  12.         SP_Global.hh=h;
  13. }
  14. void SP_display()
  15. {
  16.         glClear(GL_COLOR_BUFFER_BIT);
  17.         GLfloat startLoc[2] = {0,SP_Global.hh/2};
  18.         GLfloat Length = SP_Global.hh*9/10;
  19.         GLfloat Angle = SP_Global.amplitude*SP_Global.DEG_TO_RAD*sin(2*SP_Global.DEG_TO_RAD*SP_Global.theta);
  20.         GLfloat endLoc[2] = {startLoc[0]+Length*sin(Angle),startLoc[1]-Length*cos(Angle)};
  21.         GLfloat EdgeLoc[2] ={0.0,0.0};
  22.         //画摆线
  23.         glBegin(GL_LINES);
  24.         {
  25.                 glColor3f(1.0,1.0,1.0);
  26.                 glVertex2fv(startLoc);
  27.                 glVertex2fv(endLoc);
  28.         }
  29.         glEnd();
  30.         //画圆:以多边形边界逼近
  31.         glBegin(GL_LINE_LOOP);
  32.         {
  33.                 glColor3f(1.0,1.0,1.0);
  34.                 for(int i=0;i<20;++i)
  35.                 {
  36.                         GLfloat da = 360/20;
  37.                         EdgeLoc[0] = endLoc[0] + 20*cos(i*SP_Global.DEG_TO_RAD*da);
  38.                         EdgeLoc[1] = endLoc[1] + 20*sin(i*SP_Global.DEG_TO_RAD*da);
  39.                         glVertex2fv(EdgeLoc);;
  40.                 }
  41.         }
  42.         glEnd();
  43.         //抗锯齿
  44.         glLineWidth(2);
  45.         glEnable(GL_LINE_SMOOTH|GL_POINT_SMOOTH);
  46.         glutSwapBuffers();
  47. }

  48. void SP_init()
  49. {
  50.         glClearColor(0.0,0.0,0.0,0.0);

  51.         glColor3f(1.0,1.0,1.0);

  52.         glMatrixMode(GL_PROJECTION);
  53.         glLoadIdentity();
  54.         gluOrtho2D(-1.0,1.0,-1.0,1.0);
  55. }
  56. void SP_myidle()
  57. {
  58.         SP_Global.theta += SP_Global.theta_inc;
  59.         if(SP_Global.theta > 360.0)
  60.         {
  61.                 SP_Global.theta -=360.0;
  62.         }
  63.         glutPostRedisplay();
  64. }
  65. int Simple_Pendulum(int argc, _TCHAR* argv[])
  66. {
  67.         glutInit(& argc,argv);
  68.         glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
  69.         glutInitWindowSize(150,400);
  70.         glutInitWindowPosition(0,0);
  71.         glutCreateWindow("Simple Pendulum");
  72.         //设置回调函数
  73.         glutDisplayFunc(SP_display);
  74.         glutReshapeFunc(SP_userReshape);
  75.         glutIdleFunc(SP_myidle);
  76.         //进入主消息循环
  77.         SP_init();
  78.         glutMainLoop();
  79.         return 0;
  80. }
复制代码



 楼主| 发表于 2011-5-3 20:49 | 显示全部楼层
====旋转正方形:头文件  Rotat_Block.h============
  1. #include "stdafx.h"

  2. class struct_RB_Global
  3. {
  4. public:
  5.         struct_RB_Global()
  6.         {
  7.                 theta=0.0;
  8.                 DEG_TO_RAD=0.017453;
  9.                 theta_inc =2.0;
  10.         }
  11. public:
  12.         GLsizei ww,hh;
  13.         GLfloat theta;
  14.         GLfloat DEG_TO_RAD;
  15.         GLfloat theta_inc;
  16. };

  17. void RB_userReshape(GLsizei w,GLsizei h);
  18. void RB_display();
  19. void RB_init();
  20. void RB_myidle();
  21. void RB_mykey(unsigned char key,int x,int y);
  22. int Rotat_Block(int argc, _TCHAR* argv[]);
复制代码



====旋转正方形:代码文件  Rotat_Block.cpp============


  1. #include "stdafx.h"
  2. #include "Rotat_Block.h"


  3. struct_RB_Global RB_Global;
  4. void RB_userReshape(GLsizei w,GLsizei h)
  5. {
  6.         glMatrixMode(GL_PROJECTION);
  7.         glLoadIdentity();
  8.         gluOrtho2D(-(GLdouble)w/2,(GLdouble)w/2,-(GLdouble)h/2,(GLdouble)h/2);
  9.         glMatrixMode(GL_MODELVIEW);
  10.         glViewport(0,0,w,h);
  11.         RB_Global.ww=w;
  12.         RB_Global.hh=h;
  13. }
  14. void RB_display()
  15. {
  16.         glClear(GL_COLOR_BUFFER_BIT);
  17.         GLsizei lw = RB_Global.ww>RB_Global.hh?RB_Global.hh:RB_Global.ww;
  18.         lw = lw/2;
  19.         GLfloat ALPHA = RB_Global.DEG_TO_RAD*RB_Global.theta;
  20.         glBegin(GL_POLYGON);
  21.         {
  22.                 glColor3f(0.0,1.0,0.0);
  23.                 glVertex2f(lw * sin(ALPHA),lw * cos(ALPHA));
  24.                 glColor3f(1.0,0.0,0.0);
  25.                 glVertex2f(-lw * cos(ALPHA),lw * sin(ALPHA));
  26.                 glColor3f(0.0,0.0,1.0);
  27.                 glVertex2f(-lw * sin(ALPHA),-lw * cos(ALPHA));
  28.                 glColor3f(0.0,1.0,0.0);
  29.                 glVertex2f(lw * cos(ALPHA),-lw * sin(ALPHA));
  30.         }
  31.         glEnd();
  32.        
  33.         glutSwapBuffers();
  34. }

  35. void RB_init()
  36. {
  37.         glClearColor(0.0,0.0,0.0,0.0);

  38.         glColor3f(1.0,1.0,1.0);

  39.         glMatrixMode(GL_PROJECTION);
  40.         glLoadIdentity();
  41.         gluOrtho2D(-1.0,1.0,-1.0,1.0);
  42. }
  43. void RB_myidle()
  44. {
  45.         RB_Global.theta += RB_Global.theta_inc;
  46.         if(RB_Global.theta > 360.0)
  47.         {
  48.                 RB_Global.theta -=360.0;
  49.         }
  50.         glutPostRedisplay();
  51. }
  52. void RB_mykey(unsigned char key,int x,int y)
  53. {
  54.         printf("%d  ,  %d\n",x,y);
  55.         if (key=='R'||key=='r')
  56.         {
  57.                 RB_Global.theta_inc *= -1;
  58.         }
  59.         else if(key=='S'||key=='s')
  60.         {
  61.                 glutIdleFunc(NULL);
  62.         }
  63.         else if(key=='G'||key=='g')
  64.         {
  65.                 glutIdleFunc(RB_myidle);
  66.         }
  67.         glutPostRedisplay();
  68. }

  69. int Rotat_Block(int argc, _TCHAR* argv[])
  70. {
  71.         glutInit(& argc,argv);
  72.         glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
  73.         glutInitWindowSize(500,500);
  74.         glutInitWindowPosition(0,0);
  75.         glutCreateWindow("Rotat Block");
  76.         //设置回调函数
  77.         glutDisplayFunc(RB_display);
  78.         glutReshapeFunc(RB_userReshape);
  79.         glutIdleFunc(RB_myidle);
  80.         glutKeyboardFunc(RB_mykey);
  81.         //进入主消息循环
  82.         RB_init();
  83.         //
  84.         cout<<"R :反转选装方向"<<endl;
  85.         cout<<"S :停止旋转"<<endl;
  86.         cout<<"G :重新开始旋转"<<endl;
  87.         //
  88.         glutMainLoop();
  89.         return 0;
  90. }
复制代码





 楼主| 发表于 2011-5-3 20:51 | 显示全部楼层
====简单绘图器,头文件  SimplePaint.h===============
  1. #include "stdafx.h"

  2. #define PT_FIRST_CLICK 0
  3. #define PT_SENCOND_CLICK 1
  4. #define PT_NEVER_CLICK 2
  5. #define ST_LINE 10
  6. #define ST_BLOCK 11
  7. #define ST_CIRCLE 12

  8. struct PicItem
  9. {
  10.         GLint x_1,x_2,y_1,y_2;
  11.         int PaintTag;
  12.         int StyleTag;
  13. };

  14. typedef vector<PicItem> PicItem_vec;

  15. class struct_SPAINT_Global
  16. {
  17. public:
  18.         struct_SPAINT_Global()
  19.         {
  20.                 PaintTag = PT_NEVER_CLICK;
  21.                 StyleTag = ST_CIRCLE;
  22.                 DEG_TO_RAD = 0.017453;
  23.         }
  24. public:
  25.         GLfloat DEG_TO_RAD;
  26.         GLsizei ww,hh;
  27.         GLint x_1,x_2,y_1,y_2;
  28.         int PaintTag;
  29.         int StyleTag;
  30.         PicItem_vec PicItemHist;
  31. };




  32. void PlotItem(int x_1,int y_1,int x_2,int y_2,int PaintTag,int StyleTag);
  33. void SPAINT_userReshape(GLsizei w,GLsizei h);
  34. void SPAINT_display();
  35. void SPAINT_init();
  36. void SPAINT_mykey(unsigned char key,int x,int y);
  37. void SPAINT_mymouse(int button, int state, int x,int y);
  38. int Simple_Paint(int argc, _TCHAR* argv[]);
  39. void SPAINT_myPasstiveMotion(int x,int y);
复制代码




====简单绘图器,代码文件  SimplePaint.cpp==============
  1. #include "stdafx.h"
  2. #include "SimplePaint.h"

  3. struct_SPAINT_Global SPAINT_Global;
  4. void SPAINT_userReshape(GLsizei w,GLsizei h)
  5. {
  6.         glMatrixMode(GL_PROJECTION);
  7.         glLoadIdentity();
  8.         gluOrtho2D(-(GLdouble)w/2,(GLdouble)w/2,-(GLdouble)h/2,(GLdouble)h/2);
  9.         glMatrixMode(GL_MODELVIEW);
  10.         glViewport(0,0,w,h);
  11.         SPAINT_Global.ww=w;
  12.         SPAINT_Global.hh=h;
  13. }
  14. void SPAINT_display()
  15. {
  16.         glClear(GL_COLOR_BUFFER_BIT);
  17.         glColor3f(0.0,0.0,0.0);
  18.         //绘制已有的图形
  19.         for(int i=0;i<SPAINT_Global.PicItemHist.size();++i)
  20.         {
  21.                 PlotItem(
  22.                         SPAINT_Global.PicItemHist[i].x_1,
  23.                         SPAINT_Global.PicItemHist[i].y_1,
  24.                         SPAINT_Global.PicItemHist[i].x_2,
  25.                         SPAINT_Global.PicItemHist[i].y_2,
  26.                         SPAINT_Global.PicItemHist[i].PaintTag,
  27.                         SPAINT_Global.PicItemHist[i].StyleTag
  28.                         );
  29.         }
  30.         glColor3f(1.0,0.0,0.0);
  31.         //绘制试探的图形
  32.         PlotItem(
  33.         SPAINT_Global.x_1,
  34.         SPAINT_Global.y_1,
  35.         SPAINT_Global.x_2,
  36.         SPAINT_Global.y_2,
  37.         PT_SENCOND_CLICK,
  38.         SPAINT_Global.StyleTag
  39.         );
  40.         //抗锯齿
  41.         glLineWidth(1.5);
  42.         glPolygonMode(GL_FRONT,GL_LINE);
  43.         glPolygonMode(GL_BACK,GL_LINE);
  44.         glEnable(GL_LINE_SMOOTH|GL_POINT_SMOOTH);
  45.         glutSwapBuffers();
  46. }

  47. void PlotItem(int x_1,int y_1,int x_2,int y_2,int PaintTag,int StyleTag)
  48. {
  49.         GLfloat da;
  50.         GLfloat R =(x_1 - x_2)*(x_1 - x_2) + (y_1 - y_2)*(y_1 - y_2);
  51.         R = pow((float)R,(float)0.5)/2;
  52.         switch(PaintTag)
  53.         {
  54.         case PT_FIRST_CLICK:
  55.         case PT_NEVER_CLICK:
  56.                 return;
  57.         case PT_SENCOND_CLICK:
  58.                 switch(StyleTag)
  59.                 {
  60.                 case ST_BLOCK:
  61.                         glBegin(GL_POLYGON);
  62.                         {
  63.                                 glVertex2i(x_1,y_1);
  64.                                 glVertex2i(x_1,y_2);
  65.                                 glVertex2i(x_2,y_2);
  66.                                 glVertex2i(x_2,y_1);
  67.                         }
  68.                         glEnd();
  69.                         break;
  70.                 case ST_LINE:
  71.                         glBegin(GL_LINES);
  72.                         {
  73.                                 glVertex2i(x_1,y_1);
  74.                                 glVertex2i(x_2,y_2);
  75.                         }
  76.                         glEnd();
  77.                         break;
  78.                 case ST_CIRCLE:
  79.                         glBegin(GL_LINE_LOOP);
  80.                         {
  81.                                 for(int i=0;i<40;++i)
  82.                                 {
  83.                                         da= 360/40;
  84.                                         glVertex2f((x_1+x_2)/2 + R*cos(i*SPAINT_Global.DEG_TO_RAD*da),
  85.                                                 (y_1+y_2)/2 + R*sin(i*SPAINT_Global.DEG_TO_RAD*da));
  86.                                 }
  87.                         }
  88.                         glEnd();
  89.                         break;
  90.                 }
  91.         }
  92. }

  93. void SPAINT_init()
  94. {
  95.         glClearColor(1.0,1.0,1.0,1.0);

  96.         glColor3f(1.0,1.0,1.0);

  97.         glMatrixMode(GL_PROJECTION);
  98.         glLoadIdentity();
  99.         gluOrtho2D(-1.0,1.0,-1.0,1.0);
  100. }
  101. void SPAINT_mykey(unsigned char key,int x,int y)
  102. {
  103.         if (key=='L'||key=='l')
  104.                 SPAINT_Global.StyleTag = ST_LINE;
  105.         else if(key=='B'||key=='b')
  106.                 SPAINT_Global.StyleTag = ST_BLOCK;
  107.         else if(key=='C'||key=='c')
  108.                 SPAINT_Global.StyleTag = ST_CIRCLE;
  109. }
  110. void SPAINT_mymouse(int button, int state, int x,int y)
  111. {
  112.         if(state == GLUT_DOWN && button == GLUT_LEFT_BUTTON)
  113.         {
  114.                 if(SPAINT_Global.PaintTag == PT_FIRST_CLICK || SPAINT_Global.PaintTag == PT_NEVER_CLICK)
  115.                 {
  116.                         SPAINT_Global.x_2 = x - SPAINT_Global.ww/2;
  117.                         SPAINT_Global.y_2 = SPAINT_Global.hh/2 - y;
  118.                         SPAINT_Global.PaintTag = PT_SENCOND_CLICK;
  119.                 }
  120.                 else if(SPAINT_Global.PaintTag == PT_SENCOND_CLICK)
  121.                 {
  122.                         SPAINT_Global.x_1 = x - SPAINT_Global.ww/2;
  123.                         SPAINT_Global.y_1 = SPAINT_Global.hh/2 - y;
  124.                         PicItem tPI;
  125.                         tPI.x_1 = SPAINT_Global.x_1;
  126.                         tPI.x_2 = SPAINT_Global.x_2;
  127.                         tPI.y_1 = SPAINT_Global.y_1;
  128.                         tPI.y_2 = SPAINT_Global.y_2;
  129.                         tPI.PaintTag = SPAINT_Global.PaintTag;
  130.                         tPI.StyleTag = SPAINT_Global.StyleTag;
  131.                         SPAINT_Global.PicItemHist.push_back(tPI);
  132.                         SPAINT_Global.PaintTag = PT_FIRST_CLICK;
  133.                 }
  134.         }

  135.         glutPostRedisplay();
  136. }
  137. void SPAINT_myPasstiveMotion(int x,int y)
  138. {
  139.         if(SPAINT_Global.PaintTag == PT_FIRST_CLICK || SPAINT_Global.PaintTag == PT_NEVER_CLICK)
  140.                 return;
  141.         SPAINT_Global.x_1 = x - SPAINT_Global.ww/2;
  142.         SPAINT_Global.y_1 = SPAINT_Global.hh/2 - y;
  143.         glutPostRedisplay();
  144. }
  145. int Simple_Paint(int argc, _TCHAR* argv[])
  146. {
  147.         glutInit(& argc,argv);
  148.         glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
  149.         glutInitWindowSize(500,500);
  150.         glutInitWindowPosition(0,0);
  151.         glutCreateWindow("Simple");
  152.         //设置回调函数
  153.         glutDisplayFunc(SPAINT_display);
  154.         glutReshapeFunc(SPAINT_userReshape);
  155.         glutKeyboardFunc(SPAINT_mykey);
  156.         glutMouseFunc(SPAINT_mymouse);
  157.         glutPassiveMotionFunc(SPAINT_myPasstiveMotion);
  158.         //
  159.         cout<<"C : 画圆环"<<endl;
  160.         cout<<"L : 画直线"<<endl;
  161.         cout<<"B : 画矩形"<<endl;
  162.         //
  163.         //进入主消息循环
  164.         SPAINT_init();
  165.         glutMainLoop();
  166.         return 0;
  167. }
复制代码





 楼主| 发表于 2011-5-3 20:53 | 显示全部楼层
=====弹性小球演示动画,头文件   ElasticBall.h================
  1. #include "stdafx.h"
  2. class struct_EB_Global
  3. {
  4. public:
  5.         struct_EB_Global()
  6.         {
  7.                 R = 40.0;
  8.                 DEG_TO_RAD = 0.017453;
  9.                 CenterLoc[0] = 0;
  10.                 CenterLoc[1] = 0;
  11.                 Velocity[0]=1.4;
  12.                 Velocity[1]=2.7;
  13.                 Step = 1.0;
  14.         }
  15. public:
  16.         GLsizei ww;
  17.         GLsizei hh;
  18.         GLfloat R;
  19.         GLfloat DEG_TO_RAD;
  20.         GLint CenterLoc[2];
  21.         GLfloat Velocity[2];
  22.         GLfloat Step;
  23. };

  24. void EB_userReshape(GLsizei w,GLsizei h);
  25. void EB_display();
  26. void EB_init();
  27. void EB_myidle();
  28. int Elastic_Ball(int argc, _TCHAR* argv[]);
复制代码




=====弹性小球演示动画,代码文件   ElasticBall.cpp===============

  1. #include "stdafx.h"
  2. #include "ElasticBall.h"
  3. struct_EB_Global EB_Global;
  4. void EB_userReshape(GLsizei w,GLsizei h)
  5. {
  6.         glMatrixMode(GL_PROJECTION);
  7.         glLoadIdentity();
  8.         gluOrtho2D(-(GLdouble)w/2,(GLdouble)w/2,-(GLdouble)h/2,(GLdouble)h/2);
  9.         glMatrixMode(GL_MODELVIEW);
  10.         glViewport(0,0,w,h);
  11.         EB_Global.ww=w;
  12.         EB_Global.hh=h;
  13. }
  14. void EB_display()
  15. {
  16.         glClear(GL_COLOR_BUFFER_BIT);
  17.         //画圆:以多边形边界逼近
  18.         GLfloat EdgeLoc[2];
  19.         glBegin(GL_LINE_LOOP);
  20.         {
  21.                 glColor3f(1.0,1.0,1.0);
  22.                 for(int i=0;i<20;++i)
  23.                 {
  24.                         GLfloat da = 360/20;
  25.                         EdgeLoc[0] = EB_Global.CenterLoc[0] + EB_Global.R*cos(i*EB_Global.DEG_TO_RAD*da);
  26.                         EdgeLoc[1] = EB_Global.CenterLoc[1] + EB_Global.R*sin(i*EB_Global.DEG_TO_RAD*da);
  27.                         glVertex2fv(EdgeLoc);
  28.                 }
  29.         }
  30.         glEnd();
  31.         //抗锯齿
  32.         glLineWidth(2);
  33.         glEnable(GL_LINE_SMOOTH|GL_POINT_SMOOTH);
  34.         glutSwapBuffers();
  35. }

  36. void EB_init()
  37. {
  38.         glClearColor(0.0,0.0,0.0,0.0);

  39.         glColor3f(1.0,1.0,1.0);

  40.         glMatrixMode(GL_PROJECTION);
  41.         glLoadIdentity();
  42.         gluOrtho2D(-1.0,1.0,-1.0,1.0);
  43. }
  44. void EB_myidle()
  45. {
  46.         EB_Global.CenterLoc[0]+=EB_Global.Velocity[0]*EB_Global.Step;
  47.         if(abs(EB_Global.CenterLoc[0]) + EB_Global.R >EB_Global.hh/2)
  48.         {
  49.                 EB_Global.Velocity[0] *= -1;
  50.         }
  51.         EB_Global.CenterLoc[1]+=EB_Global.Velocity[1]*EB_Global.Step;
  52.         if(abs(EB_Global.CenterLoc[1]) + EB_Global.R >EB_Global.ww/2)
  53.         {
  54.                 EB_Global.Velocity[1] *= -1;
  55.         }
  56.         glutPostRedisplay();
  57. }
  58. int Elastic_Ball(int argc, _TCHAR* argv[])
  59. {
  60.         glutInit(& argc,argv);
  61.         glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
  62.         glutInitWindowSize(500,500);
  63.         glutInitWindowPosition(0,0);
  64.         glutCreateWindow("Simple Pendulum");
  65.         //设置回调函数
  66.         glutDisplayFunc(EB_display);
  67.         glutReshapeFunc(EB_userReshape);
  68.         glutIdleFunc(EB_myidle);
  69.         //进入主消息循环
  70.         EB_init();
  71.         glutMainLoop();
  72.         return 0;
  73. }
复制代码





评分

1

查看全部评分

发表于 2011-5-4 08:12 | 显示全部楼层
院长,猛男!程序运行了下,感觉不错,多谢分享哈!
 楼主| 发表于 2011-5-4 09:52 | 显示全部楼层
回复 6 # meiyongyuandeze 的帖子

哪里,过奖了……远未触及OpenGL的精髓……路还长啊,呵呵
发表于 2011-5-19 15:22 | 显示全部楼层
恩……我想问下……我编写OpenGL代码时,主函数如果是 _tmain(int argc, _TCHAR* argv[])
会报错,只可以在int main(int argc, char **argv) 或int main(int argc, char *argv[])……这是什么原因呢
 楼主| 发表于 2011-5-19 15:28 | 显示全部楼层
回复 8 # hityrj 的帖子

哦,可能是由于UNICODE兼容方面的问题吧,你看看这个
http://baike.baidu.com/view/3478231.htm
发表于 2011-5-19 15:58 | 显示全部楼层
采用Java 3d来做也不错,用它做个输电塔振型动画。
 楼主| 发表于 2011-5-19 16:03 | 显示全部楼层
回复 10 # impulse 的帖子

嗯嗯,做动画的方式还是很多的,达到目的就行
发表于 2011-5-28 10:54 | 显示全部楼层
学习了!
发表于 2012-3-30 14:28 | 显示全部楼层
学习了~~~~~~~~~~
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-10 04:31 , Processed in 0.251488 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表