本日課程 : 計時器、內差公式、動畫
今天的程式碼 ↓
#include <GL/glut.h>
#include <stdio.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///NOW4
glRotatef(angle, 0, 0, 1);///NOW4
glutSolidTeapot( 0.3 );///NOW4
glPopMatrix();///NOW4
glutSwapBuffers();
}
void timer(int t)///NOW (2)
{
glutTimerFunc(30, timer, t+1);///NOW4 ///每隔3/100秒跑一次t+1
printf("現在的 timer(t)的t是: %d\n", t);///NOW3
angle = t;///NOW4
glutPostRedisplay();///NOW4 要更新畫面
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);///大一點
glutCreateWindow("06160795");
glutDisplayFunc(display);
glutTimerFunc(1000, timer, 0);///NOW (1)///間隔一秒後呼叫timer函式
glutMainLoop();
}
P.S. 在主程式呼叫完Timer後, timer函式裡會開始持續呼叫 timer跑 t+1 讓圖轉起來~~!!
--------------------------------------------------------------------------------------------------------------------------
圖是會動的 ↑
新增這行程式碼 ↓
float alpha = (t%30) / 30.0;///把時間變成距離的單位
nowX = alpha*1.0 + (1-alpha)* -1.0;///利用內差公式計算位移距離
--------------------------------------------------------------------------------------------------------------------------
最後的程式碼
#include <stdio.h>///NOW1 for fprintf()
FILE * fout=NULL;///NOW1 for fopen() + fprintf()
FILE * fin =NULL;///NOW2 for fopen() + fscanf()
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel21 = NULL;
GLMmodel* pmodel3 = NULL;
GLM model* pmodel31 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel41 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel51 = NULL;
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
float angle[20];///NOW1
int angleID=1;
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
///身體
glPushMatrix();
glTranslatef(0, 0.3,0 );
glScalef(0.5,0.5,0.5);
if (!pmodel) {
pmodel = glmReadOBJ("data/06160451.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
///右上臂
glPushMatrix();
glTranslatef(0.45,0.3,0 );
glScalef(0.5,0.5,0.5);
glRotatef(angle[1], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPushMatrix();
glTranslatef(0,-1.1,0 );
glScalef(1,1,1);
glRotatef(angle[2], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.45,0.3,0 );
glScalef(0.5,0.5,0.5);
glRotatef(angle[3], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPushMatrix();
glTranslatef(0,-1.1,0 );
glScalef(1,1,1);
glRotatef(angle[4], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0.2,-1.05,0 );
glScalef(0.5,0.5,0.5);
glRotatef(angle[5], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPushMatrix();
glTranslatef(0,-1.2,0 );
glScalef(1,1,1);
glRotatef(angle[6], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2,-1.05,0 );
glScalef(0.5,0.5,0.5);
glRotatef(angle[7], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPushMatrix();
glTranslatef(0,-1.2,0 );
glScalef(1,1,1);
glRotatef(angle[8], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int oldX=0;
void mouse(int button, int state, int x, int y)///NOW2
{
oldX = x;
}
#include <stdio.h>
void motion(int x, int y)
{
angle[angleID] += x-oldX;
oldX = x;
for(int i=0;i<20;i++){
printf(" %.2f ", angle[i]);
}
printf("\n");
display();
}
void timer(int t)///NOW3
{
glutTimerFunc(1000, timer, t+1);///NOW3
if(fin==NULL) fin=fopen("motion.txt", "r");///NOW3
for(int i=0; i<20; i++){///NOW3
fscanf(fin, "%f", &angle[i]);///NOW3
}
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleID=1;
if(key=='2') angleID=2;
if(key=='3') angleID=3;
if(key=='4') angleID=4;
if(key=='5') angleID=5;
if(key=='6') angleID=6;
if(key=='7') angleID=7;
if(key=='8') angleID=8;
if(key=='w'){///NOW1
if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW1
for(int i=0; i<20; i++){///NOW1
fprintf(fout, "%.1f ", angle[i]);///NOW1
}///NOW1
fprintf(fout, "\n");///NOW1
}
if(key=='r'){///NOW2
if(fin==NULL) fin=fopen("motion.txt", "r");///NOW2
for(int i=0; i<20; i++){///NOW2
fscanf(fin, "%f", &angle[i]);///NOW2
}
}
if(key=='t' || key=='p'){///NOW3 ///啟動timer 或是 play
glutTimerFunc(1000, timer, 0);///NOW3
if(fin==NULL) fin=fopen("motion.txt", "r");///NOW3
for(int i=0; i<20; i++){///NOW3
fscanf(fin, "%f", &angle[i]);///NOW3
}
}
glutPostRedisplay();///NOW2 請讀到angle[i]之後, 重畫畫面
}
int main(int argc,char**argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week06");
glutDisplayFunc(display);
glutIdleFunc(display);
glClearColor(1,1,1,1);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glutKeyboardFunc(keyboard);///NOW3
glutMouseFunc(mouse);///NOW2
glutMotionFunc(motion);///NOW2
glutDisplayFunc(display);
glutMainLoop();
}
--------------------------------------------------------------------------------------------------------------------------
最後的程式碼
#include <stdio.h>///NOW1 for fprintf()
FILE * fout=NULL;///NOW1 for fopen() + fprintf()
FILE * fin =NULL;///NOW2 for fopen() + fscanf()
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel21 = NULL;
GLMmodel* pmodel3 = NULL;
GLM model* pmodel31 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel41 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel51 = NULL;
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
float angle[20];///NOW1
int angleID=1;
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
///身體
glPushMatrix();
glTranslatef(0, 0.3,0 );
glScalef(0.5,0.5,0.5);
if (!pmodel) {
pmodel = glmReadOBJ("data/06160451.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
///右上臂
glPushMatrix();
glTranslatef(0.45,0.3,0 );
glScalef(0.5,0.5,0.5);
glRotatef(angle[1], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPushMatrix();
glTranslatef(0,-1.1,0 );
glScalef(1,1,1);
glRotatef(angle[2], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.45,0.3,0 );
glScalef(0.5,0.5,0.5);
glRotatef(angle[3], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPushMatrix();
glTranslatef(0,-1.1,0 );
glScalef(1,1,1);
glRotatef(angle[4], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0.2,-1.05,0 );
glScalef(0.5,0.5,0.5);
glRotatef(angle[5], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPushMatrix();
glTranslatef(0,-1.2,0 );
glScalef(1,1,1);
glRotatef(angle[6], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2,-1.05,0 );
glScalef(0.5,0.5,0.5);
glRotatef(angle[7], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPushMatrix();
glTranslatef(0,-1.2,0 );
glScalef(1,1,1);
glRotatef(angle[8], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int oldX=0;
void mouse(int button, int state, int x, int y)///NOW2
{
oldX = x;
}
#include <stdio.h>
void motion(int x, int y)
{
angle[angleID] += x-oldX;
oldX = x;
for(int i=0;i<20;i++){
printf(" %.2f ", angle[i]);
}
printf("\n");
display();
}
void timer(int t)///NOW3
{
glutTimerFunc(1000, timer, t+1);///NOW3
if(fin==NULL) fin=fopen("motion.txt", "r");///NOW3
for(int i=0; i<20; i++){///NOW3
fscanf(fin, "%f", &angle[i]);///NOW3
}
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleID=1;
if(key=='2') angleID=2;
if(key=='3') angleID=3;
if(key=='4') angleID=4;
if(key=='5') angleID=5;
if(key=='6') angleID=6;
if(key=='7') angleID=7;
if(key=='8') angleID=8;
if(key=='w'){///NOW1
if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW1
for(int i=0; i<20; i++){///NOW1
fprintf(fout, "%.1f ", angle[i]);///NOW1
}///NOW1
fprintf(fout, "\n");///NOW1
}
if(key=='r'){///NOW2
if(fin==NULL) fin=fopen("motion.txt", "r");///NOW2
for(int i=0; i<20; i++){///NOW2
fscanf(fin, "%f", &angle[i]);///NOW2
}
}
if(key=='t' || key=='p'){///NOW3 ///啟動timer 或是 play
glutTimerFunc(1000, timer, 0);///NOW3
if(fin==NULL) fin=fopen("motion.txt", "r");///NOW3
for(int i=0; i<20; i++){///NOW3
fscanf(fin, "%f", &angle[i]);///NOW3
}
}
glutPostRedisplay();///NOW2 請讀到angle[i]之後, 重畫畫面
}
int main(int argc,char**argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week06");
glutDisplayFunc(display);
glutIdleFunc(display);
glClearColor(1,1,1,1);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glutKeyboardFunc(keyboard);///NOW3
glutMouseFunc(mouse);///NOW2
glutMotionFunc(motion);///NOW2
glutDisplayFunc(display);
glutMainLoop();
}
沒有留言:
張貼留言