void idle(void) { static int frames; static float time; float ifps; vec3 v; vec4 q0,q1,q2; matrix m; ifps = 1.0 / getfps(); if(!my_pause) angle += ifps * 360.0 / 4.0; v_set(sin(angle * DEG2RAD / 3),cos(angle * DEG2RAD / 7),2,light); v_set(0,0,1,v); q_set(v,psi,q0); v_set(0,1,0,v); q_set(v,phi,q1); q_multiply(q0,q1,q2); q_to_matrix(q2,m); v_set(dist,0,0,camera); v_transform(camera,m,camera); v_add(camera,mesh,camera); frames++; time += ifps; if(time > 1.0) { printf("%d frames %.2f fps\n",frames,(float)frames / time); frames = 0; time = 0; } }
void mainloop() { //x,y记录当前位置,dx,dy记录速度向量,r记录圆半径 int x = 0, y = 0, dx = 1, dy = 1, r = 100; for ( ; is_run(); delay_fps(60) ) { // todo: 逻辑更新 // 当前位置 + 速度 x += dx; y += dy; if (x < r) dx = 1; //碰左 if (y < r) dy = 1; //碰上 if (x >= ege::getwidth() - r) dx = -1; //碰右 if (y >= ege::getheight() - r) dy = -1; //碰下 // todo: 图形更新 cleardevice(); setcolor(GREEN); setfillcolor(BLUE); fillellipse(x, y, r, r); {// 画帧率文字 char str[20]; sprintf(str, "fps %.02f", getfps()); //调用getfps取得当前帧率 setcolor(WHITE); outtextxy(0, 0, str); } } }
// 主函数 int main( int argc, char* argv[] ) { int i, ms_x = -1024, ms_y = -1024, exitflag = 0; int fps = 60; double dtime; int mode = preinit( argc, argv ); // 记录初始化模式 if ( mode < 0 ) return 0; randomize(); // 初始化随机种子 initgraph( -1, -1 ); // 打开图形窗口,以全屏模式 showmouse( mode ); sc_width = getwidth(); sc_heigh = getheight(); // 初始化所有星星 for ( i = 0; i < g_max; i++ ) { InitStar( i ); star[i].x = randomf(); } // 绘制星空,按任意键或移动鼠标退出 setfont( 12, 6, "宋体" ); setrendermode( RENDER_MANUAL ); dtime = fclock(); while ( kbmsg() ) getkey(); for ( ; !exitflag && is_run() && kbmsg() == 0; delay_fps( fps ) ) { //每秒画120帧,kbhit(1)是获取键盘任意键的消息,详见pdf // 如果有鼠标消息 while ( mousemsg() ) { mouse_msg msg = getmouse(); if ( ms_x <= -1024 ) { ms_x = msg.x; ms_y = msg.y; } // 处理鼠标,移动超出范围就退出 if ( mode == 0 ) { // 仅全屏模式才处理鼠标 int x = msg.x, y = msg.y; x -= ms_x; y -= ms_y; if ( x * x + y * y > 400 ) exitflag = 1; } } // 显示星星 double dt = 1.0 / fps; //fclock() - dtime; dtime += dt; for ( int i = 0; i < g_max; i++ ) { MoveStar( i, dt ); } // 显示FPS { char str[60]; sprintf( str, "%8.2f FPS", getfps()); outtextxy( 0, 0, str ); //显示fps } } closegraph(); // 关闭图形窗口 return 0; }
bool isDoubleLbutton()//鼠标双击算法 { static int x,y; static int state=0; static int dTimeB=0; if (isLButton()&&state==0) { state=1; x=mouse(X); y=mouse(Y); } if ((!isLButton())&&state==1) { state=2; dTimeB=dTimeB+1000/getfps(); } if ((!isLButton())&&state==2) { dTimeB=dTimeB+1000/getfps(); } if (state==2&&isLButton()) { x=mouse(X); y=mouse(Y); state=3; dTimeB=dTimeB+1000/getfps(); } if (dTimeB>inf.mouse_speed&&state==2) { dTimeB=0; state=0; } if (dTimeB<=inf.mouse_speed&&state==3) { if(abs(mouse(X)-x)<4&&abs(mouse(Y)-y)<4) { state=0; dTimeB=0; return true; } } return false; }
void idle(void) { float ifps; fps = getfps(); ifps = 1.0 / fps; if(!pause) view += ifps; VectorSet(0,120,240 * cos(view / 3),camera); VectorSet(0,0,0,dir); VectorCopy(camera,light); light[3] = 1.0; angle -= ifps * 360.0 * 0.2; glutPostRedisplay(); }
void idle(void) { float ifps; fps = getfps(); ifps = 1.0 / fps; angle += ifps * 0.1; VectorSet(dist * cos(angle),dist * sin(angle), dist * (cos(alpha) + 1) / 2,camera); VectorSet(0,0,0,dir); VectorSet(0,0,1000,light); light[3] = 1.0; glutPostRedisplay(); }
void displayhdl::render() { glDisable(GL_LIGHTING); char str[256]; framenum++; if (show_framerate) { sprintf(str, "%f seconds", gettime()); renderstring2d(str, 1.0, 1.0, 1.0, 0.0, 1.0); sprintf(str, "%f frames", framenum); renderstring2d(str, 1.0, 1.0, 1.0, 0.0, .98); sprintf(str, "%f fps ave", framenum/currtime); renderstring2d(str, 1.0, 1.0, 1.0, 0.0, .96); sprintf(str, "%f fps", getfps()); renderstring2d(str, 1.0, 1.0, 1.0, 0.0, .94); } glEnable(GL_LIGHTING); }
void display(void) { float mview[16],imview[16]; char buffer[128]; shadowmap(); glClearColor(0,0,0,1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45,4.0 / 3.0,1,2048); glMatrixMode(GL_TEXTURE); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(camerapos[0],camerapos[1],camerapos[2], cameradir[0],cameradir[1],cameradir[2], 0,0,1); glLightfv(GL_LIGHT0,GL_POSITION,lightpos); glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_GEN_T); glTexGeni(GL_S,GL_TEXTURE_GEN_MODE,GL_SPHERE_MAP); glTexGeni(GL_T,GL_TEXTURE_GEN_MODE,GL_SPHERE_MAP); glEnable(GL_LIGHTING); glEnable(GL_TEXTURE_2D); // draw model glBindTexture(GL_TEXTURE_2D,texturemodel); glPushMatrix(); glTranslatef(objpos[0],objpos[1],objpos[2]); glRotatef(modelangle,0,0,1); glCallList(modellist); glPopMatrix(); glDisable(GL_TEXTURE_GEN_S); glDisable(GL_TEXTURE_GEN_T); glDisable(GL_LIGHTING); glBindTexture(GL_TEXTURE_2D,textureground); // draw ground glCallList(groundlist); glDisable(GL_TEXTURE_2D); glPushMatrix(); // light sources glTranslatef(lightpos[0],lightpos[1],lightpos[2]); glColor4f(0,1,0,1); glCallList(lightlist); glColor4f(1,1,1,1); glPopMatrix(); glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_GEN_T); glEnable(GL_TEXTURE_GEN_R); glEnable(GL_TEXTURE_GEN_Q); glTexGeni(GL_S,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR); glTexGeni(GL_T,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR); glTexGeni(GL_R,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR); glTexGeni(GL_Q,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR); glGetFloatv(GL_MODELVIEW_MATRIX,mview); MatrixInverse(mview,imview); glMatrixMode(GL_TEXTURE); glLoadIdentity(); glTranslatef(0.5,0.5,0); glScalef(0.5,0.5,1.0); glOrtho(-modelsize,modelsize,-modelsize,modelsize,-1,1); gluLookAt(lightpos[0],lightpos[1],lightpos[2], objpos[0],objpos[1],objpos[2], 0,1,0); glMultMatrixf(imview); glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); // shadow glBlendFunc(GL_ZERO,GL_ONE_MINUS_SRC_COLOR); glBindTexture(GL_TEXTURE_2D,textureshadow); glCallList(groundlist); glDisable(GL_BLEND); glMatrixMode(GL_MODELVIEW); glDisable(GL_TEXTURE_GEN_S); glDisable(GL_TEXTURE_GEN_T); glDisable(GL_TEXTURE_GEN_R); glDisable(GL_TEXTURE_GEN_Q); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1,1,-1,1,-1,1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glColor4f(1,1,1,1); glDisable(GL_DEPTH_TEST); glDisable(GL_TEXTURE_2D); glDisable(GL_LIGHTING); sprintf(buffer,"fps: %.2f",getfps()); drawstring(-0.95,0.95,buffer); glEnable(GL_DEPTH_TEST); glutSwapBuffers(); }