今日教學內容:
1. 移動、旋轉、縮放
2. 寫檔、讀檔3. 內插(Interpolate)動作
4. 期末作品
先打開右上角的貝殼
打入這些資訊
把freeglut放進去
#include<GL/glut.h>
float angle1=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();///part0
glColor3f(0,1,1);
glutSolidTeapot(0.3);///body base 先做基本的身體在藏起來
glPopMatrix();
glFlush();
}
void motion(int x,int y)
{
angle1=x;
glutPostRedisplay();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutCreateWindow("Robot");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
在劃出一個圈圈
#include<GL/glut.h>
float angle1=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();///part0
glColor3f(0,1,1);
glutSolidTeapot(0.3);///body base 先做基本的身體在藏起來
glPushMatrix();///Upper Arm
glColor3f(1,1,1);
glutSolidSphere(0.3,30,30);
glPopMatrix();
glPopMatrix();
glFlush();
}
void motion(int x,int y)
{
angle1=x;
glutPostRedisplay();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutCreateWindow("Robot");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
最後加上轉動和座標控制
#include<GL/glut.h>
float angle1=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();///part0
glColor3f(0,1,1);
glutSolidTeapot(0.3);///body base 先做基本的身體在藏起來///part4 把註解刪掉
glPushMatrix();///Upper Arm
glTranslatef(0.5,0.15,0);///part3
glRotatef(angle1,0,0,1);///part1
glTranslatef(0.3,0,0);///part2
glScalef(1,0.3,0.3);
glColor3f(1,1,1);
glutSolidSphere(0.3,30,30);
glPopMatrix();
glPopMatrix();
glFlush();
}
void motion(int x,int y)
{
angle1=x;
glutPostRedisplay();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutCreateWindow("Robot");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
#include <GL/glut.h>
float oldX=0, oldY;
float angle[10]={0,0,0,0,0,0,0,0,0,0};
int angleID=1;
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;
glutPostOverlayRedisplay();
}
void drawBody()
{
glPushMatrix();
glScalef(1, 0.5, 0.5);
glutSolidCube(1);
glPopMatrix();
}
void drawArm()
{
glPushMatrix();
glScalef(0.6, 0.3, 0.3); ///手臂角度
glutSolidCube(1);
glPopMatrix();
}
void display()
{
GLfloat pos[]={0, 0, -1, 0};
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.5, 0, 0); ///旋轉軸
glRotatef(angle[1], 0,0,1); ///角度
glTranslatef(0.25, 0,0);///
drawArm();
//glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.25, 0, 0); ///旋轉軸
glRotatef(angle[2], 0,0,1); ///角度
glTranslatef(0.25, 0,0);///
drawArm();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.5, 0, 0); ///旋轉軸
glRotatef(-angle[3], 0,0,1); ///角度
glTranslatef(-0.25, 0,0);///
drawArm();
glPushMatrix();
glTranslatef(-0.25, 0, 0); ///旋轉軸
glRotatef(-angle[4], 0,0,1); ///角度
glTranslatef(-0.25, 0,0);///
drawArm();
glPopMatrix();
glPopMatrix();
drawBody();
glFlush();
}
void motion(int x, int y)
{
angle[angleID] -= y - oldY;
oldY=y; oldX=x;
}
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN){oldX=x;oldY=y;}
}
void timer(int t)
{
glutTimerFunc(20, timer, t+1);
///angle++; 要自己動,要刪掉
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("TRT test");
glutDisplayFunc(display);
glutTimerFunc(20, timer, 0);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
2. 寫檔、讀檔
#include <GL/glut.h>
#include<stdio.h>
FILE *fout=NULL;
FILE *fin=NULL;
float oldX=0, oldY;
float angle[10]={0,0,0,0,0,0,0,0,0,0};
int angleID=1;
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;
glutPostOverlayRedisplay();
}
void drawBody()
{
glPushMatrix();
glScalef(1, 0.5, 0.5);
glutSolidCube(1);
glPopMatrix();
}
void drawArm()
{
glPushMatrix();
glScalef(0.6, 0.3, 0.3); ///手臂角度
glutSolidCube(1);
glPopMatrix();
}
void display()
{
GLfloat pos[]={0, 0, -1, 0};
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.5, 0, 0); ///旋轉軸
glRotatef(angle[1], 0,0,1); ///角度
glTranslatef(0.25, 0,0);///
drawArm();
//glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.25, 0, 0); ///旋轉軸
glRotatef(angle[2], 0,0,1); ///角度
glTranslatef(0.25, 0,0);///
drawArm();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.5, 0, 0); ///旋轉軸
glRotatef(-angle[3], 0,0,1); ///角度
glTranslatef(-0.25, 0,0);///
drawArm();
glPushMatrix();
glTranslatef(-0.25, 0, 0); ///旋轉軸
glRotatef(-angle[4], 0,0,1); ///角度
glTranslatef(-0.25, 0,0);///
drawArm();
glPopMatrix();
glPopMatrix();
drawBody();
glFlush();
}
void motion(int x, int y)
{
angle[angleID] -= y - oldY;
oldY=y; oldX=x;
if(fout==NULL)fout=fopen("a.txt","w+");
for(int i=0;i<10;i++)
{
printf("%f",angle[i]);
fprintf(fout,"\n");
}
printf("\n");
fprintf(fout,"\n");
}
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN){oldX=x;oldY=y;}
}
void timer(int t)
{
glutTimerFunc(20, timer, t+1);
///angle++; 要自己動,要刪掉
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("TRT test");
glutDisplayFunc(display);
glutTimerFunc(20, timer, 0);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
先自己動一次手臂
關掉檔案
再點'p'
手臂自己動了
然後數值也跟著一起改變
#include <GL/glut.h>
#include<stdio.h>
FILE *fout=NULL;
FILE *fin=NULL;
float oldX=0, oldY;
float angle[10]={0,0,0,0,0,0,0,0,0,0};
int angleID=1;
void motion(int x, int y)
{
angle[angleID] -= y - oldY;
oldY=y; oldX=x;
if(fout==NULL)fout=fopen("a.txt","w+");
for(int i=0;i<10;i++)
{
printf( "%f ",angle[i]);
fprintf(fout, "%f ",angle[i]);
}
printf("\n");
fprintf(fout,"\n");
}
void timerPlay(int t)
{
glutTimerFunc(500,timerPlay,t+1);
if(fin==NULL)fin=fopen("a.txt","r");
for(int i=0;i<10;i++)
{
fscanf(fin,"%f",&angle[i]);
printf("%f ",angle[i]);
}
glutPostRedisplay();
}
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=='p')glutTimerFunc(10,timerPlay,0);
}
void drawBody()
{
glPushMatrix();
glScalef(1, 0.5, 0.5);
glutSolidCube(1);
glPopMatrix();
}
void drawArm()
{
glPushMatrix();
glScalef(0.6, 0.3, 0.3); ///手臂角度
glutSolidCube(1);
glPopMatrix();
}
void display()
{
GLfloat pos[]={0, 0, -1, 0};
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.5, 0, 0); ///旋轉軸
glRotatef(angle[1], 0,0,1); ///角度
glTranslatef(0.25, 0,0);///
drawArm();
//glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.25, 0, 0); ///旋轉軸
glRotatef(angle[2], 0,0,1); ///角度
glTranslatef(0.25, 0,0);///
drawArm();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.5, 0, 0); ///旋轉軸
glRotatef(-angle[3], 0,0,1); ///角度
glTranslatef(-0.25, 0,0);///
drawArm();
glPushMatrix();
glTranslatef(-0.25, 0, 0); ///旋轉軸
glRotatef(-angle[4], 0,0,1); ///角度
glTranslatef(-0.25, 0,0);///
drawArm();
glPopMatrix();
glPopMatrix();
drawBody();
glFlush();
}
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN){oldX=x;oldY=y;}
}
void timer(int t)
{
glutTimerFunc(20, timer, t+1);
///angle++; 要自己動,要刪掉
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("TRT test");
glutDisplayFunc(display);
glutTimerFunc(20, timer, 0);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
#include <GL/glut.h>
#include<stdio.h>
FILE *fout=NULL;
FILE *fin=NULL;
float oldX=0, oldY=0;
float angle[8]={0,0,0,0,0,0,0,0};
float angleOld[8]={0,0,0,0,0,0,0,0};
float angleNew[8]={0,0,0,0,0,0,0,0};
int now=0;
float a=0;
void readNew()
{
if(fin==NULL)fin=fopen("a.txt","r");
for(int i=0;i<8;i++)
{
angleOld[i]=angleNew[i];
fscanf(fin, "%f",&angleNew[i]);
printf("%.1f ",angleNew[i]);
}
printf("\n");
}
void timerPlay(int t)
{
glutTimerFunc(20,timerPlay,t+1);
if(t%100==0)readNew();
a=(t%100)/100.0;
for(int i=0;i<8;i++)
{
angle[i]=(1-a)*angleOld[i]+(a)*angleNew[i];
}
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') now=1;
if(key=='2') now=2;
if(key=='3') now=3;
if(key=='4') now=4;
if(key=='p')glutTimerFunc(20,timerPlay,0);
if(key=='s')
{
if(fout==NULL)fout=fopen("a.txt","w+");
for(int i=0;i<8;i++)
{
printf("%.1f\t",angle[i]);
fprintf(fout,"%.1f\t",angle[i]);
}
fprintf(fout,"\n");
}
glutPostRedisplay();
}
void motion(int x, int y)
{
angle[now] -= x - oldX;
oldY=y; oldX=x;
}
void drawBody()
{
glPushMatrix();
glScalef(1, 0.5, 0.5);
glutSolidCube(1);
glPopMatrix();
}
void drawArm()
{
glPushMatrix();
glScalef(0.6, 0.3, 0.3); ///手臂角度
glutSolidCube(1);
glPopMatrix();
}
void display()
{
GLfloat pos[]={0, 0, -1, 0};
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.5, 0, 0); ///旋轉軸
glRotatef(angle[1], 0,0,1); ///角度
glTranslatef(0.25, 0,0);///
drawArm();
//glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.25, 0, 0); ///旋轉軸
glRotatef(angle[2], 0,0,1); ///角度
glTranslatef(0.25, 0,0);///
drawArm();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.5, 0, 0); ///旋轉軸
glRotatef(-angle[3], 0,0,1); ///角度
glTranslatef(-0.25, 0,0);///
drawArm();
glPushMatrix();
glTranslatef(-0.25, 0, 0); ///旋轉軸
glRotatef(-angle[4], 0,0,1); ///角度
glTranslatef(-0.25, 0,0);///
drawArm();
glPopMatrix();
glPopMatrix();
drawBody();
glFlush();
}
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN){oldX=x;oldY=y;}
}
void timer(int t)
{
glutTimerFunc(20, timer, t+1);
///angle++; 要自己動,要刪掉
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("TRT test");
glutDisplayFunc(display);
glutTimerFunc(20, timer, 0);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
#include<stdio.h>
FILE *fout=NULL;
FILE *fin=NULL;
float oldX=0, oldY=0;
float angle[8]={0,0,0,0,0,0,0,0};
float angleOld[8]={0,0,0,0,0,0,0,0};
float angleNew[8]={0,0,0,0,0,0,0,0};
int now=0;
float a=0;
void readNew()
{
if(fin==NULL)fin=fopen("a.txt","r");
for(int i=0;i<8;i++)
{
angleOld[i]=angleNew[i];
fscanf(fin, "%f",&angleNew[i]);
printf("%.1f ",angleNew[i]);
}
printf("\n");
}
void timerPlay(int t)
{
glutTimerFunc(20,timerPlay,t+1);
if(t%100==0)readNew();
a=(t%100)/100.0;
for(int i=0;i<8;i++)
{
angle[i]=(1-a)*angleOld[i]+(a)*angleNew[i];
}
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') now=1;
if(key=='2') now=2;
if(key=='3') now=3;
if(key=='4') now=4;
if(key=='p')glutTimerFunc(20,timerPlay,0);
if(key=='s')
{
if(fout==NULL)fout=fopen("a.txt","w+");
for(int i=0;i<8;i++)
{
printf("%.1f\t",angle[i]);
fprintf(fout,"%.1f\t",angle[i]);
}
fprintf(fout,"\n");
}
glutPostRedisplay();
}
void motion(int x, int y)
{
angle[now] -= x - oldX;
oldY=y; oldX=x;
}
void drawBody()
{
glPushMatrix();
glScalef(1, 0.5, 0.5);
glutSolidCube(1);
glPopMatrix();
}
void drawArm()
{
glPushMatrix();
glScalef(0.6, 0.3, 0.3); ///手臂角度
glutSolidCube(1);
glPopMatrix();
}
void display()
{
GLfloat pos[]={0, 0, -1, 0};
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.5, 0, 0); ///旋轉軸
glRotatef(angle[1], 0,0,1); ///角度
glTranslatef(0.25, 0,0);///
drawArm();
//glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.25, 0, 0); ///旋轉軸
glRotatef(angle[2], 0,0,1); ///角度
glTranslatef(0.25, 0,0);///
drawArm();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.5, 0, 0); ///旋轉軸
glRotatef(-angle[3], 0,0,1); ///角度
glTranslatef(-0.25, 0,0);///
drawArm();
glPushMatrix();
glTranslatef(-0.25, 0, 0); ///旋轉軸
glRotatef(-angle[4], 0,0,1); ///角度
glTranslatef(-0.25, 0,0);///
drawArm();
glPopMatrix();
glPopMatrix();
drawBody();
glFlush();
}
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN){oldX=x;oldY=y;}
}
void timer(int t)
{
glutTimerFunc(20, timer, t+1);
///angle++; 要自己動,要刪掉
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("TRT test");
glutDisplayFunc(display);
glutTimerFunc(20, timer, 0);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
沒有留言:
張貼留言