用OPENGL画出麦克斯维速率分布曲线
2006-07-23 11:34:49 来源:WEB开发网核心提示:本文示例源代码或素材下载 图1 效果图内容提要: 公式在“expressions.h”中,绘图程序在“draw.h”中,用OPENGL画出麦克斯维速率分布曲线,其他部分套用OpenGL Tutorial中的OpengGL程序框架,请参见参考资料,用Release编
本文示例源代码或素材下载
图1 效果图
内容提要: 公式在“expressions.h”中,绘图程序在“draw.h”中,其他部分套用OpenGL Tutorial中的OpengGL程序框架,请参见参考资料。
步骤:
1. 用Microsoft Visual Studio .NET 2003新建一个C++的“windows应用程序”,在“项目”>>“属性”>>“C++”>>“预编译头”>>“创建/使用预编译头”中选择“不使用预编译头”。
2. 包含opengGL所需库的头文件, 如下:
#include <windows.h> // Header File For Windows
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <gl\glaux.h> // Header File For The GLaux Library
3. opengGL所需库的连接方式(VS2003的截图如下)
图2 opengGL所需库的连接图
一开始我用 Debug编译通过,用Release编译就有很多错误,研究发现Release等每个配置文件都要手动连接库。
4.程序说明
//头文件:expressions.h
/*
Name:麦克斯维速率分布函数
Copyright:Mozilla 1.1
Author: 张沈鹏
Date: 26-09-05 13:08
Description: 有问题请联系zsp747@gmail.com,QQ:375956667(不常用)
*/
#include <cmath>
using namespace std;
//定义物理常量
const long double pi=3.1415926535897932384626433832795;
const float N_A=6.022e23;
const float p_0=1.013e5;
const double T_0=273.15;
const float V_0=22.4e-3;
const float R=p_0*V_0/T_0;
const float k=R/N_A;
//返回在temperature(单位:K);quality(单位:kg); speed(单位m/s)时,分子
template<class number>
number percentage(number temperature,number quality,number speed)
{
quality=quality/N_A;
return 4*pi*sqrt( pow(quality/(2*pi*k*temperature),3) )*
pow(speed,2)*exp( ( -quality*pow(speed,2) )/( 2*k*temperature ) );
}
//头文件:draw.h
#include <gl\gl.h> // Header File For The OpenGL32 Library
#include <gl\glu.h> // Header File For The GLu32 Library
#include <gl\glaux.h> // Header File For The Glaux Library
///画函数曲线
template<class number>
void DrawExpression(number temperature,number quality)
{
number x2=0,y2=0;//前一个点
glBegin(GL_LINES);
for(number speed=0;speed<2000;speed+=10)
{
///速度单位长度250m/s
number x=speed/250;
///概率单位长度0.2%
number y=Percentage(temperature,quality,speed)*500;
glColor3f(0.0f,1.0f,0.0f);
//glVertex3f(x,0, 0.0f); //实心曲线
glVertex3f(x2,y2, 0.0f); //曲线
glVertex3f(x,y, 0.0f); //前一个点与当前点的连线
y2=y;x2=x ;
}
glEnd();
}
///画X,Y轴
void DrawXY()
{
glBegin(GL_LINES);
glVertex3f( 0.0f, 0.0f, 0.0f);
glVertex3f(0.0f,5.0f, 0.0f);
glVertex3f( 0.0f, 0.0f, 0.0f);
glVertex3f(7.0f,0.0f, 0.0f);
glEnd();
///画坐标轴的箭头
glBegin(GL_TRIANGLES);
glVertex3f(6.0f,0.1f,0.0f);
glVertex3f(6.3f,0.0f,0.0f);
glVertex3f(6.0f,-0.1f,0.0f);
glVertex3f(0.1f,4.2f,0.0f);
glVertex3f(0.0f,4.5f,0.0f);
glVertex3f(-0.1f,4.2f,0.0f);
glEnd();
}
int DrawGLScene(GLvoid) // Here''s Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(-3.0f,-2.0f,-6.0f);
DrawXY();
DrawExpression(70.0f,0.032f);
DrawExpression(273.0f,0.032f);
DrawExpression(300.0f,0.032f);
DrawExpression(373.0f,0.032f);
return TRUE; // Everything Went OK
}
遗留问题:
更多精彩
赞助商链接