內插講解
內差公式:d*新+(1-d)*舊
在之前的機器人裡加入float alpha=0.0;float oldAngle[10]={0,0,0,0,0,0,0,0,0,0};float newAngle[10]={10,20,30,40,50,60,70,80,90,100};
void interpolation(){for(int i=0;i<9;i++){angle[i]=alpha*newAngle[i]+(1-alpha)*oldAngle[i];}
alpha+=0.1;},修改倒數器裡加入interpolation();
以下是程式碼:
#include <string.h>#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel1 = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel6 = NULL;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;
int now = 0;
float angle[10]={};
float alpha=0.0;
float oldAngle[10]={0,0,0,0,0,0,0,0,0,0};
float newAngle[10]={10,20,30,40,50,60,70,80,90,100};
float anglee=0;
void interpolation()
{
for(int i=0;i<9;i++)
{
angle[i]=alpha*newAngle[i]+(1-alpha)*oldAngle[i];
}
alpha+=0.1;
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
if (!pmodel1) {//head
pmodel1 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/head.obj");
if (!pmodel1) exit(0);
glmUnitize(pmodel1);
glmFacetNormals(pmodel1);
glmVertexNormals(pmodel1, 90.0);
}
if (!pmodel2) {//body
pmodel2 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/body.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
if (!pmodel3) {
pmodel3 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/up1.obj");
if (!pmodel3) exit(0);
glmUnitize(pmodel3);
glmFacetNormals(pmodel3);
glmVertexNormals(pmodel3, 90.0);
}
if (!pmodel4) {
pmodel4 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/up2.obj");
if (!pmodel4) exit(0);
glmUnitize(pmodel4);
glmFacetNormals(pmodel4);
glmVertexNormals(pmodel4, 90.0);
}
if (!pmodel5) {
pmodel5 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/hand.obj");
if (!pmodel5) exit(0);
glmUnitize(pmodel5);
glmFacetNormals(pmodel5);
glmVertexNormals(pmodel5, 90.0);
}
if (!pmodel6) {
pmodel6 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down1.obj");
if (!pmodel6) exit(0);
glmUnitize(pmodel6);
glmFacetNormals(pmodel6);
glmVertexNormals(pmodel6, 90.0);
}
if (!pmodel7) {
pmodel7 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down2.obj");
if (!pmodel7) exit(0);
glmUnitize(pmodel7);
glmFacetNormals(pmodel7);
glmVertexNormals(pmodel7, 90.0);
}
if (!pmodel8) {
pmodel8 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down3.obj");
if (!pmodel8) exit(0);
glmUnitize(pmodel8);
glmFacetNormals(pmodel8);
glmVertexNormals(pmodel8, 90.0);
}
glPushMatrix();
//glRotatef(anglee,0,1,0);//旋轉
glPushMatrix();//頭
glTranslatef(0,0.65,0);//移動
glRotatef(90,0,1,0);//旋轉
glScalef(0.6,0.6,0.6);//縮放
glmDraw(pmodel1, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix(); //身體
glTranslatef(0,0.1,0);//移動
glRotatef(90,0,1,0);//旋轉
glScalef(0.4,0.4,0.4);//縮放
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();//右臂
glTranslatef(0.43,0.3,0);
glRotatef(angle[0],1,0,0);
glTranslatef(-0.45,-0.3,0);
glPushMatrix();
glTranslatef(0.45,0.3,0);
glRotatef(90,0,1,0);
glScalef(0.5,0.5,0.5);
glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();//右籌
glTranslatef(0.43,0.3,0);
glRotatef(angle[2],0,1,0);
glTranslatef(-0.45,-0.3,0);
glPushMatrix();
glTranslatef(0.55,0,0);
glRotatef(90,0,1,0);
glScalef(0.5,0.5,0.5);
glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();//右長
glTranslatef(0.8,0,0);
glRotatef(270,0,1,0);
glScalef(0.5,0.5,0.5);
glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPushMatrix();//左臂
glTranslatef(0.46,0.3,0);
glRotatef(angle[1],1,0,0);
glTranslatef(-0.45,-0.3,0);
glPushMatrix();
glTranslatef(-0.45,0.3,0);
glRotatef(90,0,1,0);
glScalef(0.5,0.5,0.5);
glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();//左籌
glTranslatef(-0.46,-0.3,0);
glRotatef(angle[3],0,1,0);
glTranslatef(0.45,0.3,0);
glPushMatrix();
glTranslatef(-0.55,0,0);
glRotatef(90,0,1,0);
glScalef(0.5,0.5,0.5);
glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();//左掌
glTranslatef(-0.8,0,0);
glRotatef(90,0,1,0);
glScalef(0.5,0.5,0.5);
glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPushMatrix();//左大腿
glTranslatef(-0.2,-0.35,0);
glRotatef(angle[4],0,1,0);
glTranslatef(0.2,0.3,0);
glPushMatrix();
glTranslatef(-0.2,-0.35,0);
glRotatef(90,0,1,0);
glScalef(0.2,0.25,0.25);
glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();//左小腿
glTranslatef(-0.25,-0.25,0);
glRotatef(angle[5],0,1,0);
glTranslatef(0.25,0.3,0);
glPushMatrix();
glTranslatef(-0.25,-0.7,0);
glRotatef(90,0,1,0);
glScalef(0.5,0.4,0.5);
glmDraw(pmodel7, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();//左掌
glTranslatef(-0.3,-0.9,0);
glRotatef(90,0,1,0);
glScalef(0.5,0.5,0.5);
glmDraw(pmodel8, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPushMatrix();//右大腿
glTranslatef(0.2,0.25,0);
glRotatef(angle[6],0,1,0);
glTranslatef(-0.2,-0.3,0);
glPushMatrix();
glTranslatef(0.2,-0.35,0);
glRotatef(270,0,1,0);
glScalef(0.2,0.25,0.25);
glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();//右小腿
glTranslatef(0.25,0.35,0);
glRotatef(angle[7],0,1,0);
glTranslatef(-0.25,-0.3,0);
glPushMatrix();
glTranslatef(0.25,-0.7,0);
glRotatef(90,0,1,0);
glScalef(0.5,0.4,0.5);
glmDraw(pmodel7, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();//右腳掌
glTranslatef(0.3,-0.9,0);
glRotatef(90,0,1,0);
glScalef(0.5,0.5,0.5);
glmDraw(pmodel8, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int oldx=0,oldy=0;
void mouse(int button, int state, int x, int y)
{
oldx=x;
oldy=y;
glutPostRedisplay();
}
void motion(int x, int y)
{
angle[now]+=(x-oldx);
oldx = x;
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') now=0;
if(key=='1') now=1;
if(key=='2') now=2;
if(key=='3') now=3;
if(key=='4') now=4;
if(key=='5') now=5;
if(key=='6') now=6;
if(key=='7') now=7;
if(key=='8') now=8;
}
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 };
#include <stdio.h>
void timer(int t)
{
glutTimerFunc(10,timer,t+1);
interpolation();
anglee++;
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
glutCreateWindow("04160480");
glutDisplayFunc(display);
glutIdleFunc(display);
glutTimerFunc(0,timer,0);
glClearColor(1,1,1,1);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
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);
glutMainLoop();
}
老師講解圖:
讀檔跟存檔
新增以下程式碼:
#include <stdio.h>
FILE * fout=NULL;
FILE * fin=NULL;
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') now=0;
if(key=='1') now=1;
if(key=='2') now=2;
if(key=='3') now=3;
if(key=='4') now=4;
if(key=='5') now=5;
if(key=='6') now=6;
if(key=='7') now=7;
if(key=='8') now=8;
if(key=='s' || key=='S')
{
if(fout==NULL){
fout=fopen("file.txt","w+");
printf("產生FILE了\n");
}
fprintf(fout,"%.3f %.3f %.3f %.3f %.3f\n",angle[0],angle[1],angle[2],angle[3],angle[4]);
printf("文件有資料了喔%.3f %.3f %.3f %.3f %.3f\n",angle[0],angle[1],angle[2],angle[3],angle[4]);
}
if(key=='r' || key=='R')
{
if(fin==NULL){
fin=fopen("file.txt","r");
printf("123");
}
fscanf(fin,"%f %f %f %f %f",&angle[0],&angle[1],angle[2],&angle[3],&angle[4]);
glutPostRedisplay();
}
}
老師講解圖:







沒有留言:
張貼留言