OpenGL中的渐变颜色绘图(应力可视化)-LMLPHP

 #include <GL/glut.h>
#include <iostream>
#include <cmath>
using namespace std;
const GLsizei width=;
const GLsizei height=;//设置窗口的宽和高 void myInit()
{
glClearColor(,,,);
} void show()
{
GLsizei dw;
int n,i;
dw=;
n=(width-)/dw;
cout<<"n="<<n<<endl;
glClear(GL_COLOR_BUFFER_BIT);
GLsizei r,g,b;
for (i=;i<n;i++)
{
//绘制应力云图的关键就是下面的这五种不同的颜色对应关系
//根据自己需要可以设置不同的等级,四种也是可以的
if (i>=&&i<=n/)
{
r=;
g=i*/(n/);
b=;
}
else if (i>n/&&i<=*n/)
{
r=-(i-n/)*/(n/);
g=;
b=;
}
else if(i>*n/&&i<=*n/)
{
r=;
g=;
b=(i-*n/)*/(n/);
}
else if(i>*n/&&i<=*n/)
{
r=;
g=-(i-*n/)*/(n/);
b=;
}
else
{
r=(i-*n/)*/(n/);
g=;
b=;
}
glColor3f(r/255.0f,g/255.0f,b/255.0f);
glRectf(1.0*i*dw,0.0,1.0*(i+)*dw,height);
}
glFlush();
}
void reshape(int w,int h)
{
glViewport(,,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(,w,,h,-,);//设置视野的范围,(左,右,上,下,前,后)
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
} int main(int argc,char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(,);
glutInitWindowSize(width,height);
glutCreateWindow("应力云图绘制");
myInit();
glutDisplayFunc(show);
glutReshapeFunc(reshape);
glutMainLoop();
return ;
}

  OpenGL中的渐变颜色绘图(应力可视化)-LMLPHP

05-11 22:56