成果展示
Youtube連結 https://youtu.be/86dSok4kgis
程式碼
#include <GL/glut.h>
#include <stdio.h>
#include "glm.h"
#include "CMP3_MCI.h"
GLMmodel * pmodel=NULL;
GLMmodel * pmodel2=NULL;
GLMmodel * pmodel3=NULL;
GLMmodel * pmodel4=NULL;
GLMmodel * pmodel5=NULL;
GLMmodel * pmodel6=NULL;
FILE * fout=NULL;
FILE * fin=NULL;
CMP3_MCI myMp3;
float angle[20],angleNew[20],angleOld[20],Na[]={},Oa[]={},a[]={},oldX=0,oldY=0;
int ID=0;
void timer(int t)
{
glutTimerFunc(33,timer,t+1);
if(t%30==0)
{
for(int i=0;i<20;i++)
{
angleOld[i]=angleNew[i];
fscanf(fin,"%f",&angleNew[i]);
}
}
float alpha= (t%30)/30.0;
for(int i=0;i<20;i++)
{
angle[i] = alpha * angleNew[i] + (1-alpha)*angleOld[i];
}
glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='0') ID=0;
if(key=='1') ID=1;
if(key=='2') ID=2;
if(key=='3') ID=3;
if(key=='4') ID=4;
if(key=='5') ID=5;
if(key=='6') ID=6;
if(key=='7') ID=7;
if(key=='8') ID=8;
if(key=='s' || key=='S' || key=='w' || key=='W')
{
if(fout==NULL)fout=fopen("output.txt","w+");
for(int i=0;i<20;i++)
{
fprintf(fout,"%.2f ",angle[i]);
}
fprintf(fout,"\n");
}
if(key=='r' || key=='R')
{
if(fin==NULL) fin=fopen("output.txt","r");
for(int i=0;i<20;i++)
{
fscanf(fin,"%f",&angle[i]);
}
}
if(key=='p' || key=='P')
{
myMp3.Load("cut.mp3");
myMp3.Play();
if(fin==NULL) fin=fopen("output.txt","r");
glutTimerFunc(1000,timer,0);
for(int i=0;i<20;i++)
{
fscanf(fin,"%f",&angleNew[i]);
}
glutTimerFunc(33, timer,0);
}
glutPostRedisplay();
}
void motion(int x,int y)
{
angle[ID] += (x-oldX);
oldX=x;
glutPostRedisplay();
}
void mouse(int button,int state,int x,int y)
{
oldX=x;oldY=y;
}
static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
///glutSolidTeapot(0.3);
if(!pmodel)
{
pmodel = glmReadOBJ("head.obj");
if(!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel,90.0);
}
if(!pmodel2)
{
pmodel2 = glmReadOBJ("lefthand.obj");///左上臂
if(!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2,90.0);
}
if(!pmodel3)
{
pmodel3 = glmReadOBJ("body.obj");
if(!pmodel3) exit(0);
glmUnitize(pmodel3);
glmFacetNormals(pmodel3);
glmVertexNormals(pmodel3,90.0);
}
if(!pmodel4)
{
pmodel4 = glmReadOBJ("righthand.obj");
if(!pmodel4) exit(0);
glmUnitize(pmodel4);
glmFacetNormals(pmodel4);
glmVertexNormals(pmodel4,90.0);
}
if(!pmodel5)
{
pmodel5 = glmReadOBJ("lefthleg.obj");
if(!pmodel5) exit(0);
glmUnitize(pmodel5);
glmFacetNormals(pmodel5);
glmVertexNormals(pmodel5,90.0);
}
glPushMatrix();///左上手
glRotatef(90,0,0,1);
glTranslatef(0.15,0.1,0);
glRotatef(angle[0],0,0,1);
glTranslatef(0,0.15,0);
glPushMatrix();
glScalef(1,1.3,1);
glRotatef(90,0,1,0);
glmDraw(pmodel4,GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPushMatrix();///頭
glTranslatef(0,0,0);
glRotatef(angle[4],0,1,0);
glTranslatef(0,0.6,0);
glScalef(0.5,0.5,0.5);
glRotatef(185,0,1,0);
glmDraw(pmodel,GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();///身體
glTranslatef(0,0,0);
glTranslatef(0,0.05,0);
glRotatef(180,0,1,0);
glScalef(1,1,1);
glmDraw(pmodel3,GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();///右上手
glTranslatef(0.15,0.15,0.3);
glRotatef(angle[1],0,0,1);
glTranslatef(0.1,0,0);
glPushMatrix();
glScalef(1,1.3,1);
glRotatef(90,1,0,0);
glmDraw(pmodel4,GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPushMatrix();///右上腳
glTranslatef(0.1,-0.2,0);
glRotatef(angle[2],0,0,1);
glTranslatef(0,-0.15,0);
glPushMatrix();
glScalef(1,1.3,1);
glRotatef(90,0,1,0);
glmDraw(pmodel4,GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPushMatrix();///左上腳
glTranslatef(-0.1,-0.2,0);
glRotatef(angle[3],0,0,1);
glTranslatef(0,-0.15,0);
glPushMatrix();
glScalef(1,1.3,1);
glRotatef(90,0,1,0);
glmDraw(pmodel4,GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
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 };
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("05160566_期末作品");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glClearColor(1,1,1,1);
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();
}
沒有留言:
張貼留言