close

這次功課主要是用glutTimerFunc(int delay,void (*timer_func),int value)達到delay並佐課本給的方程式來達到點旋轉的效果。

 

//Timer 為自己取的名子

void Timer(int value){

  glutTimerFunc(50,Timer,0);

  //50:延遲50/1000(s),Timer:呼叫void Timer(int value),0:傳給Timer的參數。

}

結果:

hw8.PNG

程式碼:

/********************
Computer Graghics
Author:SHEN ZHI-XUN
Time:2016/11/26
********************/
#include <iostream>
#include <cmath>
#include <GL/glut.h>
using namespace std;
void display() {
static GLdouble dRadius = 0.1;
static GLdouble dAngle = 0.0;
glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
if (dAngle == 0.0) glClear(GL_COLOR_BUFFER_BIT); //當角度等於0時,清除畫面
glBegin(GL_POINTS);
glVertex2d(dRadius*cos(dAngle), dRadius*sin(dAngle)); //在此座標打上點。
glEnd();
dRadius *= 1.01;
dAngle += 0.1;
if (dAngle > 30.0) {
dRadius = 0.1;
dAngle = 0.0;
}
glFlush();
}
void delayTime(int time) {
glutPostRedisplay();
glutTimerFunc(time, delayTime, 25);
}
void reshape(int w,int h) {
GLfloat aspectRatio;
if (h <= 0) h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glLoadIdentity();
aspectRatio = (GLfloat)w / (GLfloat)h;
if (w <= h)
glOrtho(-2, 2, -2 / aspectRatio, 2 / aspectRatio, 2, -2);
else
glOrtho(-2*aspectRatio, 2*aspectRatio, -2, 2, 2, -2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc,char** argv) {
glutInitWindowSize(400, 400);
glutCreateWindow("Spiral_Points");
glutReshapeFunc(reshape);
glutTimerFunc(25, delayTime, 25); //delay
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
view raw main.cpp hosted with ❤ by GitHub

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 a7069810 的頭像
    a7069810

    紀錄自己的程式人生

    a7069810 發表在 痞客邦 留言(0) 人氣()