1 #include <GL/glut.h> 2 #include <iostream> 3 #include <cmath> 4 using namespace std; 5 const GLsizei width=640; 6 const GLsizei height=480;//设置窗口的宽和高 7 8 void myInit() 9 { 10 glClearColor(0,0,0,0); 11 } 12 13 14 void show() 15 { 16 GLsizei dw; 17 int n,i; 18 dw=2; 19 n=(width-1)/dw; 20 cout<<"n="<<n<<endl; 21 glClear(GL_COLOR_BUFFER_BIT); 22 GLsizei r,g,b; 23 for (i=0;i<n;i++) 24 { 25 //绘制应力云图的关键就是下面的这五种不同的颜色对应关系 26 //根据自己需要可以设置不同的等级,四种也是可以的 27 if (i>=0&&i<=n/5) 28 { 29 r=255; 30 g=i*255/(n/5); 31 b=0; 32 } 33 else if (i>n/5&&i<=2*n/5) 34 { 35 r=255-(i-n/5)*255/(n/5); 36 g=255; 37 b=0; 38 } 39 else if(i>2*n/5&&i<=3*n/5) 40 { 41 r=0; 42 g=255; 43 b=(i-2*n/5)*255/(n/5); 44 } 45 else if(i>3*n/5&&i<=4*n/5) 46 { 47 r=0; 48 g=255-(i-3*n/5)*255/(n/5); 49 b=255; 50 } 51 else 52 { 53 r=(i-4*n/5)*255/(n/5); 54 g=0; 55 b=255; 56 } 57 glColor3f(r/255.0f,g/255.0f,b/255.0f); 58 glRectf(1.0*i*dw,0.0,1.0*(i+1)*dw,height); 59 } 60 glFlush(); 61 } 62 void reshape(int w,int h) 63 { 64 glViewport(0,0,(GLsizei)w,(GLsizei)h); 65 glMatrixMode(GL_PROJECTION); 66 glLoadIdentity(); 67 glOrtho(0,w,0,h,-100,100);//设置视野的范围,(左,右,上,下,前,后) 68 glMatrixMode(GL_MODELVIEW); 69 glLoadIdentity(); 70 } 71 72 int main(int argc,char *argv[]) 73 { 74 glutInit(&argc, argv); 75 glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); 76 glutInitWindowPosition(100,100); 77 glutInitWindowSize(width,height); 78 glutCreateWindow("应力云图绘制"); 79 myInit(); 80 glutDisplayFunc(show); 81 glutReshapeFunc(reshape); 82 glutMainLoop(); 83 return 0; 84 }