我要发帖 回复

正式会员

7

主题

75

积分

0

专家分

兴趣点(最多三项):

视景仿真

私信
发表时间 : 2014-5-22 17:11:32 | 浏览 : 1775    评论 : 1
本帖最后由 lxs 于 2014-5-26 10:08 AM 编辑

要加定时器刷新画面吗?应该怎么使用?{:3_87:}{:3_90:}{:3_95:}
0020503698J-2.jpg

程序:
#include <GL/glut.h>  

#include <GL/gl.h>
#include <GL/glaux.h>
#include "windows.h"  
#include "math.h"
#include "time.h"

#define MAP_W 100
#define PI 3.14


double Random(void);
double height(double x,double z,double t);


void myinit(void);
void CALLBACK display(void);
void CALLBACK reshape(GLsizei w,GLsizei h);

void myinit(void)
{
/*初始化:*/
auxInitDisplayMode(AUX_DOUBLE|AUX_RGBA);

auxInitPosition(0,0,500,500);

auxInitWindow("wave");

glClearColor(0.0,0.0,0.0,0.0);

glClear(GL_COLOR_BUFFER_BIT);

}  

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


//随机数(初相位)

double Random(void)
{
srand( (unsigned)time(NULL));
return ((double)(rand() % 100) / 100*2*PI);

}




//海浪高度计算  

double height(double x,double z,double t)
{
double a=0.0081;
double g=9.8;
double belta=0.74;
double c1=a*pow(g,2);
double c2=-belta*pow(g,4);
double omega=(double)2*PI*8.0;
int M=6;
double del_omega=(double)omega/M;
double avg_omega=del_omega*0.5;
double thelta=40.0;
int N=7;
double del_thelta=(double)thelta/N;
double avg_thelta=del_thelta*0.5;
double sw;
double gw;
double u=0.5;
double wm=8.596/u;
double p,q;
double tmp;
double h=0;
double heightmap=0;
double cosalpha1p,cosalpha1q,cosalpha2p,cosalpha2q,cosalpha1,cosalpha2,
sinalpha1,sinalpha2,cosalphap,cosalphaq,cosalpha,sinalpha;
double cosdel_thelta1=(double)cos(2*del_thelta);
double cosdel_thelta2=(double)cos(4*del_thelta);
int i,j;



for(i=0;i<M;i++)
{
avg_thelta=del_thelta*0.5;
sw=(c1/pow(avg_omega,5))*exp(c2/pow(u*avg_omega,4));
tmp=exp(-0.50*pow(avg_omega/wm,4));
p=0.5+0.82*tmp;
q=0.32*tmp;
for(j=0;j<N;j++)
{
if(j==0)
{
cosalpha=cosalpha1=cos(avg_thelta);
sinalpha=sinalpha1=sin(avg_thelta);
cosalpha1p=cos(2*avg_thelta);
cosalpha1q=cos(4*avg_thelta);
gw=(1/PI)*(1+p*cosalpha1p+q*cosalpha1q);

}
else if(j==1)
{
avg_thelta+=del_thelta;
cosalpha=cosalpha2=cos(avg_thelta);
sinalpha=sinalpha2=sin(avg_thelta);
cosalpha2p=cos(2*avg_thelta);
cosalpha2q=cos(4*avg_thelta);
gw=(1/PI)*(1+p*cosalpha2p+q*cosalpha2q);

}
else
{
cosalphap=2*cosalpha2p*cosdel_thelta1-cosalpha1p;
cosalphaq=2*cosalpha2q*cosdel_thelta2-cosalpha1q;
cosalpha=2*cosalpha2*cos(del_thelta)-cosalpha1;
sinalpha=2*sinalpha2*cos(del_thelta)-sinalpha1;
gw=(1/PI)*(1+p*cosalphap+q*cosalphaq);
cosalpha1p=cosalpha2p;
cosalpha1q=cosalpha2q;
cosalpha1=cosalpha2;
sinalpha1=sinalpha2;
cosalpha2p=cosalphap;
cosalpha2q=cosalphaq;
cosalpha2=cosalpha;
sinalpha2=sinalpha;
}
h=(double)sqrt(2*sw*gw*del_omega*del_thelta);


heightmap+=(double)h*cos(avg_omega*t-(pow(avg_omega,2)/g)*(x*cosalpha+z*sinalpha)+Random());

}
avg_omega+=del_omega;

}

return heightmap;

}


//画网格

void CALLBACK display(void)
{
int i,j;

for(i=0;i<MAP_W;i++)
{
for(j=0;j<MAP_W;j++)
{


points[0]=i;//x
points[2]=j;//z
points[1]=height(i,j,t);//y

}

}

glBegin(GL_LINE_STRIP);

for(i=0;i<MAP_W-1;i++)
{



glVertex3f(points[0],points[1],points[2]);
glVertex3f(points[i+1][0],points[i+1][1],points[i+1][2]);



}
glEnd();
glFlush();
glutSwapBuffers();
}  

void main(void)
{
myinit();
auxReshapeFunc(reshape);
auxMainLoop(display);
}

最近VR访客

lxs 评论于2014-7-22 15:35:25
谢谢!!!我也觉得挺乱的。

手机版|VR开发网 统计 津ICP备18009691号
网安备12019202000257

GMT+8, 2021-9-22 07:49 PM

返回顶部