------------------------------------------------------------------------------------------
Week15 2018/06/08期末成果
成果展示
△完成的作品在Youtube有成果展示呀~~
YouTube連結: https://youtu.be/NnQzmCxu_cY
△以下是我的程式碼:
#include <GL/glut.h>
#include <stdio.h>
#include "glm.h"
#include <mmsystem.h>
GLMmodel * pmodel0=NULL;
GLMmodel * pmodel1=NULL;
GLMmodel * pmodel2=NULL;
GLMmodel * pmodel3=NULL;
GLMmodel * pmodel4=NULL;
GLMmodel * pmodel5=NULL;
GLMmodel * pmodel6=NULL;
GLMmodel * pmodel7=NULL;
GLMmodel * pmodel8=NULL;
GLMmodel * pmodel9=NULL;
GLMmodel * pmodel10=NULL;
GLMmodel * pmodel11=NULL;
GLMmodel * pmodel12=NULL;
GLMmodel * pmodel13=NULL;
GLMmodel * pmodel14=NULL;
GLMmodel * pmodel15=NULL;
FILE * fout=NULL;
FILE * fin=NULL;
float oldX=0,oldY=0;
float angle[20],angleNew[20],angleOld[20];
int now=0;///用keyboard的數字鍵,來切換關節
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') 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=='9') now=9;
if(key=='a') now=10;
if(key=='b') now=11;
if(key=='c') now=12;
if(key=='d') now=13;
if(key=='e') now=14;
if(key=='f') now=15;
if(key=='g') now=16;
if(key=='h') now=17;
if(key=='i') now=18;
if(key=='s' || key=='S' || key=='w' || key=='W')
{
if(fout==NULL) fout=fopen("output.txt", "w+");
for(int i=0;i<20;i++){
printf("%.3f ", angle[i]);///Now5: 把每一個陣列裡的角度,都寫一次
fprintf(fout, "%.3f ", angle[i]);///Now5: 把每一個陣列裡的角度,都寫一次
}
. printf("\n");
fprintf(fout,"\n");
}///3個角度glutTimerFunc(100, timer, 0);的值,印到檔案去
if(key=='r' || key=='R'){///Now4:
if(fin==NULL) fin=fopen("output.txt", "r");///Now4: 第一次讀檔時,要先開檔
for(int i=0;i<20;i++){///Now5: for迴圈
fscanf(fin, "%f", &angle[i]);
}
}
if(key=='p' || key=='P'){///P為重新撥放
PlaySound("A.wav",NULL,SND_ASYNC);
if(fin==NULL) fin=fopen("output.txt", "r");
for(int i=0;i<20;i++){
fscanf(fin, "%f", &angleNew[i]);
}
glutTimerFunc(100, timer, 0);
}
glutPostRedisplay();
}
void motion(int x,int y){
angle[now] += (x-oldX);
oldX=x;
glutPostRedisplay();
}
void mouse(int button, int state,int x, int y)///當滑鼠移動時
{
oldX=x; oldY=y;
}
void display()
{
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if(!pmodel0){
pmodel0=glmReadOBJ("body1.obj");///身體
if(!pmodel0)exit(0);
glmUnitize(pmodel0);
glmFacetNormals(pmodel0);
glmVertexNormals(pmodel0,90.0);
}
if(!pmodel1){
pmodel1=glmReadOBJ("handup1.obj");///前肢上半部
if(!pmodel1)exit(0);
glmUnitize(pmodel1);
glmFacetNormals(pmodel1);
glmVertexNormals(pmodel1,90.0);
}
if(!pmodel2){
pmodel2=glmReadOBJ("handdown1.obj");///前肢下半部
if(!pmodel2)exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2,90.0);
}
if(!pmodel3){
pmodel3=glmReadOBJ("handdown3.obj");///前肢腳底
if(!pmodel3)exit(0);
glmUnitize(pmodel3);
glmFacetNormals(pmodel3);
glmVertexNormals(pmodel3,90.0);
}
if(!pmodel4){
pmodel4=glmReadOBJ("handbotten.obj");
if(!pmodel4)exit(0);
glmUnitize(pmodel4);
glmFacetNormals(pmodel4);
glmVertexNormals(pmodel4,90.0);
}
if(!pmodel7){
pmodel7=glmReadOBJ("legup.obj");///後肢上半部
if(!pmodel7)exit(0);
glmUnitize(pmodel7);
glmFacetNormals(pmodel7);
glmVertexNormals(pmodel7,90.0);
}
if(!pmodel8){
pmodel8=glmReadOBJ("legdown1.obj");///後肢下半部
if(!pmodel8)exit(0);
glmUnitize(pmodel8);
glmFacetNormals(pmodel8);
glmVertexNormals(pmodel8,90.0);
}
if(!pmodel9){
pmodel9=glmReadOBJ("legbotten.obj");///後肢腳底
if(!pmodel9)exit(0);
glmUnitize(pmodel9);
glmFacetNormals(pmodel9);
glmVertexNormals(pmodel9,90.0);
}
if(!pmodel10){
pmodel10=glmReadOBJ("neck13.obj");///脖子靠近身體關節1
if(!pmodel10)exit(0);
glmUnitize(pmodel10);
glmFacetNormals(pmodel10);
glmVertexNormals(pmodel10,90.0);
}
if(!pmodel11){
pmodel11=glmReadOBJ("neck2.obj");///脖子靠近身體關節2
if(!pmodel11)exit(0);
glmUnitize(pmodel11);
glmFacetNormals(pmodel11);
glmVertexNormals(pmodel11,90.0);
}
if(!pmodel12){
pmodel12=glmReadOBJ("head2.obj");///頭
if(!pmodel12)exit(0);
glmUnitize(pmodel12);
glmFacetNormals(pmodel12);
glmVertexNormals(pmodel12,90.0);
}
glPushMatrix();///body
glTranslatef(angle[17]/500,0,0);
glTranslatef(0,angle[18]/500,0);
glScaled(0.6,0.6,0.6);
glRotated(90,0,1,0);
glTranslatef(0,0,0);
glRotatef(angle[0],0,1,0);
glTranslatef(0,0,0);
glmDraw(pmodel0,GLM_SMOOTH|GLM_MATERIAL);
glPushMatrix();///左前腳up
glScaled(0.8,0.8,0.8);
glRotated(90,0,1,0);
glTranslatef(-0.45,-0.1,-0.15);
glRotatef(angle[1],0,0,1);
glTranslatef(0,-0.35,0);
glmDraw(pmodel1,GLM_SMOOTH|GLM_MATERIAL);
glPushMatrix();///左前腳down
glScaled(0.8,0.6,0.8);
glRotated(90,0,1,0);
glTranslatef(0.02,-0.65,-0.04);
glRotatef(angle[2],1,0,0);
glTranslatef(0,-0.35,0);
glmDraw(pmodel2,GLM_SMOOTH|GLM_MATERIAL);
glPushMatrix();///左前腳down
glScaled(0.8,0.8,0.8);
glRotated(180,0,1,0);
glTranslatef(0,-0.5,0.07);
glRotatef(angle[2],-1,0,0);
glTranslatef(0,0.02,0);
glmDraw(pmodel3,GLM_SMOOTH|GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPushMatrix();///左前腳up
glScaled(0.8,0.8,0.8);
glRotated(90,0,1,0);
glTranslatef(-0.45,-0.1,0.15);
glRotatef(angle[4],0,0,1);
glTranslatef(0,-0.35,0);
glmDraw(pmodel1,GLM_SMOOTH|GLM_MATERIAL);
glPushMatrix();///左前腳down
glScaled(0.8,0.6,0.8);
glRotated(90,0,1,0);
glTranslatef(0.02,-0.65,-0.04);
glRotatef(angle[5],1,0,0);
glTranslatef(0,-0.35,0);
glmDraw(pmodel2,GLM_SMOOTH|GLM_MATERIAL);
glPushMatrix();///左前腳botten
glScaled(0.8,0.8,0.8);
glRotated(180,0,1,0);
glTranslatef(0,-0.5,0.07);
glRotatef(angle[5],-1,0,0);
glTranslatef(0,0.02,0);
glmDraw(pmodel3,GLM_SMOOTH|GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPushMatrix();///右後腳up
glScaled(0.8,0.8,0.8);
glRotated(0,0,1,0);
glTranslatef(0.2,0,-0.43);
glRotatef(angle[7],1,0,0);
glTranslatef(0,-0.3,0);
glmDraw(pmodel7,GLM_SMOOTH|GLM_MATERIAL);
glPushMatrix();///右後腳down
glScaled(0.7,0.7,0.7);
glRotated(0,0,1,0);
glTranslatef(0,-0.5,-0.2);
glRotatef(angle[8],1,0,0);
glTranslatef(0,-0.35,0);
glmDraw(pmodel8,GLM_SMOOTH|GLM_MATERIAL);
glPushMatrix();///右後腳botten
glScaled(0.7,0.7,0.7);
glRotated(0,0,1,0);
glTranslatef(-0.04,-0.75,0.3);
glRotatef(angle[8],1,0,0);
glTranslatef(0,0.02,0);
glmDraw(pmodel9,GLM_SMOOTH|GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPushMatrix();///左後腳up
glScaled(0.8,0.8,0.8);
glRotated(0,0,1,0);
glTranslatef(-0.2,0,-0.43);
glRotatef(angle[10],1,0,0);
glTranslatef(0,-0.3,0);
glmDraw(pmodel7,GLM_SMOOTH|GLM_MATERIAL);
glPushMatrix();///左後腳down
glScaled(0.7,0.7,0.7);
glRotated(0,0,1,0);
glTranslatef(0,-0.5,-0.2);
glRotatef(angle[11],1,0,0);
glTranslatef(0,-0.35,0);
glmDraw(pmodel8,GLM_SMOOTH|GLM_MATERIAL);
glPushMatrix();///左後腳botten
glScaled(0.7,0.7,0.7);
glRotated(0,0,1,0);
glTranslatef(-0.03,-0.75,0.3);
glRotatef(angle[8],1,0,0);
glTranslatef(0,0.02,0);
glmDraw(pmodel9,GLM_SMOOTH|GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPushMatrix();///neck1
glScaled(1.3,1.16,1.1);
glRotated(0,0,1,0);
glTranslatef(0,-0.28,0.52);
glRotatef(angle[13],1,0,0);
glTranslatef(0,0.3,0);
glmDraw(pmodel10,GLM_SMOOTH|GLM_MATERIAL);
glPushMatrix();///neck2
glScaled(1.3,1.16,1.1);
glRotated(10,1,0,0);
glTranslatef(0,-0.05,0.05);
glRotatef(angle[14],1,0,0);
glTranslatef(0,0.1,0);
glmDraw(pmodel11,GLM_SMOOTH|GLM_MATERIAL);
glPushMatrix();///neck2
glScaled(1,1,0.8);
glRotated(-10,1,0,0);
glTranslatef(0,0.03,0.1);
glRotatef(angle[15],1,0,0);
glTranslatef(0,0.1,0);
glmDraw(pmodel11,GLM_SMOOTH|GLM_MATERIAL);
glPushMatrix();///head
glScaled(1.6,1.6,1.6);
glRotated(0,1,0,0);
glTranslatef(0,0.06,0.03);
glRotatef(angle[16],1,0,0);
glTranslatef(0,0.2,0);
glmDraw(pmodel12,GLM_SMOOTH|GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };///打光73~80
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, -10.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);
glutInitWindowSize(600,600);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("05160124");
glutDisplayFunc(display);
glutMotionFunc(motion);///motion才能動
glutMouseFunc(mouse);///mouse才能動
glutKeyboardFunc(keyboard);
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();
}

沒有留言:
張貼留言