首先先開一個貝殼專案
Project build options --- > freeglut , opengl32 , glu32 , gdi32 , winmm
#include <GL/glut.h>
float angle1=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glColor3f(0,0,1);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.5,0.15,0);///將整個橢圓移到想要放置的位置((關節位置))
glRotatef(angle1,0,0,1);///使橢圓轉動
glTranslatef(0.3,0,0);///將橢圓形整個向右移0.3使得旋轉中心從橢圓中間改到橢圓左邊
glScalef(1,0.3,0.3);
glColor3f(1,0,0);
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=0;
float angle[10]={};
int now=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()
{
glColor3ub(166,132,100);
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(); ///這是右上的手臂
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[now] -= 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 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;
glutPostRedisplay();
}
void timer(int t)
{
glutTimerFunc(20, timer, t+1);
//angle++;
glutPostRedisplay();
}
int main(int argc,char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("T-R-T 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 angle[10]={},oldX=0,oldY=0;
int angleID=1;
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()
{
glColor3ub(166,132,100);
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(); ///這是右上的手臂
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,"%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 mouse(int button,int state,int x, int y)
{
if(state==GLUT_DOWN){oldX=x; oldY=y;}
}
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 timer(int t)
{
glutTimerFunc(20, timer, t+1);
//angle++;
glutPostRedisplay();
}
int main(int argc,char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("T-R-T test");
glutDisplayFunc(display);
glutTimerFunc(20, timer, 0);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
#include <stdio.h>
FILE * fout=NULL;
FILE * fin=NULL;
float angle[10]={},oldX=0,oldY=0;
int angleID=1;
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()
{
glColor3ub(166,132,100);
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(); ///這是右上的手臂
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,"%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 mouse(int button,int state,int x, int y)
{
if(state==GLUT_DOWN){oldX=x; oldY=y;}
}
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 timer(int t)
{
glutTimerFunc(20, timer, t+1);
//angle++;
glutPostRedisplay();
}
int main(int argc,char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("T-R-T test");
glutDisplayFunc(display);
glutTimerFunc(20, timer, 0);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
紀錄完按 "s"並播放 "p"自己的模型
#include <GL/glut.h>
#include <stdio.h>
FILE * fout=NULL;
FILE * fin=NULL;
float angle[8]={},oldX=0,oldY=0;
float angleOld[8]={};
float angleNew[8]={};
int angleID=1;
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 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()
{
glColor3ub(166,132,100);
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(); ///這是右上的手臂
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 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 mouse(int button,int state,int x, int y)
{
if(state==GLUT_DOWN){oldX=x; oldY=y;}
}
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(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 timer(int t)
{
glutTimerFunc(20, timer, t+1);
//angle++;
glutPostRedisplay();
}
int main(int argc,char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("T-R-T test");
glutDisplayFunc(display);
glutTimerFunc(20, timer, 0);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
沒有留言:
張貼留言