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的參數。
}
結果:
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************** | |
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; | |
} |
全站熱搜