2019/5/23 Week14
(1)主題:計時器Timer
(2)主題:內差公式
(3)主題:動畫
(4)期末作品
一如既往的先有10行茶壺程式碼
#include <GL/glut.h>void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot( 0.3 );
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);///變大了
glutCreateWindow("week 14 timer");
glutDisplayFunc(display);
glutMainLoop();
}
今天要加入Timer=>
先到wav下載聲音檔,之前有交過匯入音檔,再加入Timer程式碼就好,再利用變數t去計算過了幾秒鐘
利用excel來學習內差公式
內差公式來跑茶壺
後面恍神了...回家有看小葉的影片,貼上程式碼表示看過~~
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include "glm.h"
#include <stdio.h>
#include <windows.h>
#include <mmsystem.h>
float oldAngle[20];
float newAngle[20];
float angle[20];
float a=0;
GLMmodel* pmodel = NULL;///呼叫外掛,glm需要它(body)
GLMmodel* pmodel2 = NULL;///righthand1
GLMmodel* pmodel3 = NULL;///righthand2
GLMmodel* pmodel4 = NULL;///lefthand1
GLMmodel* pmodel5 = NULL;///lefthand2
GLMmodel* pmodel6 = NULL;///rightleg1
GLMmodel* pmodel7 = NULL;///rightleg2
GLMmodel* pmodel8 = NULL;///leftleg1
GLMmodel* pmodel9 = NULL;///leftleg2
int angleID=2;///NOW3
FILE * fout = NULL;///for fprintf()
FILE * fin = NULL;///for fscanf()
IplImage * img=cvLoadImage("filename.jpg");///讀圖
GLuint myTexture(char * filename) ///貼圖需要它
{
IplImage * img = cvLoadImage(filename); ///NOW OpenCV ///貼圖需要它
cvCvtColor(img, img, CV_BGR2RGB);///convert color ///NOW OpenCV ///貼圖需要它
GLuint id; ///貼圖需要它
glEnable(GL_TEXTURE_2D); ///貼圖需要它
glGenTextures(1, &id ); ///貼圖需要它
glBindTexture(GL_TEXTURE_2D, id); ///貼圖需要它, 綁貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); ///貼圖需要它
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); ///貼圖需要它
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); ///貼圖需要它
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); ///貼圖需要它
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData); ///貼圖需要它
return id;///貼圖需要它
}
float nowX=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glDisable(GL_LIGHTING);///jsyeh: 我幫你加這行
glEnable(GL_TEXTURE_2D);///jsyeh: 我幫你加這行
glBegin(GL_POLYGON);
glTexCoord2f(0,0); glVertex2f(-1,+1);
glTexCoord2f(0,1); glVertex2f(-1,-1);
glTexCoord2f(1,1); glVertex2f(+1,-1);
glTexCoord2f(1,0); glVertex2f(+1,+1);
glEnd();
glDisable(GL_TEXTURE_2D);///jsyeh: 我幫你加這行
glEnable(GL_LIGHTING);///jsyeh: 我幫你加這行
///body
glPushMatrix();
glTranslatef(0, 0.3, 0);
//glRotatef(a,0,1,0);
glScalef(0.4,0.4,0.4);
if (!pmodel) {///glm需要它
pmodel = glmReadOBJ("body.obj");///glm需要它
if (!pmodel) exit(0);///glm需要它
glmUnitize(pmodel);///glm需要它
glmFacetNormals(pmodel);///glm需要它
glmVertexNormals(pmodel, 90.0);///glm需要它
}
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
///body
///righthand1(1)p2
glPushMatrix();
glTranslatef(0.25, 0.3, 0);///(3)掛上去
glRotatef(angle[1], 0,0,1);///(2)旋轉
glTranslatef(0.2, -0.1, 0);///(1)旋轉中心
glScalef(0.2,0.2,0.2);
if (!pmodel2) {///glm需要它
pmodel2 = glmReadOBJ("righthand1.obj");///glm需要它
if (!pmodel2) exit(0);///glm需要它
glmUnitize(pmodel2);///glm需要它
glmFacetNormals(pmodel2);///glm需要它
glmVertexNormals(pmodel2, 90.0);///glm需要它
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
//glPopMatrix();
///righthand1(1)p2
///righthand2(2)p3
glPushMatrix();
glTranslatef(1, -0.9, 0);///(3)掛上去
glRotatef(angle[2], 0,0,1);///(2)旋轉
glTranslatef(0, -0.8, 0);///(1)旋轉中心
glScalef(1,1,1);
if (!pmodel3) {///glm需要它
pmodel3 = glmReadOBJ("righthand2.obj");///glm需要它
if (!pmodel3) exit(0);///glm需要它
glmUnitize(pmodel3);///glm需要它
glmFacetNormals(pmodel3);///glm需要它
glmVertexNormals(pmodel3, 90.0);///glm需要它
}
glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
///righthand2(2)p3
///lefthand1(3)p4
glPushMatrix();
glTranslatef(-0.25, 0.3, 0);///(3)掛上去
glRotatef(angle[3], 0,0,1);///(2)旋轉
glTranslatef(-0.2, -0.1, 0);///(1)旋轉中心
glScalef(0.2,0.2,0.2);
if (!pmodel4) {///glm需要它
pmodel4 = glmReadOBJ("lefthand1.obj");///glm需要它
if (!pmodel4) exit(0);///glm需要它
glmUnitize(pmodel4);///glm需要它
glmFacetNormals(pmodel4);///glm需要它
glmVertexNormals(pmodel4, 90.0);///glm需要它
}
glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
//glPopMatrix();
///lefthand1(3)p4
///lefthand2(4)p5
glPushMatrix();
glTranslatef(-1, -0.9, 0);///(3)掛上去
glRotatef(angle[4], 0,0,1);///(2)旋轉
glTranslatef(0, -0.8, 0);///(1)旋轉中心
glScalef(1,1,1);
if (!pmodel5) {///glm需要它
pmodel5 = glmReadOBJ("righthand2.obj");///glm需要它
if (!pmodel5) exit(0);///glm需要它
glmUnitize(pmodel5);///glm需要它
glmFacetNormals(pmodel5);///glm需要它
glmVertexNormals(pmodel5, 90.0);///glm需要它
}
glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
///lefthand2(4)p5
///rightleg1(5)p6
glPushMatrix();
glTranslatef(0.1, 0, 0);///(3)掛上去
glRotatef(angle[5], 0,0,1);///(2)旋轉
glTranslatef(0.1, -0.2, 0);///(1)旋轉中心
glScalef(0.15,0.15,0.15);
if (!pmodel6) {///glm需要它
pmodel6 = glmReadOBJ("rightleg1.obj");///glm需要它
if (!pmodel6) exit(0);///glm需要它
glmUnitize(pmodel6);///glm需要它
glmFacetNormals(pmodel6);///glm需要它
glmVertexNormals(pmodel6, 90.0);///glm需要它
}
glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);
//glPopMatrix();
///rightleg1(5)p6
///rightleg2(6)p7
glPushMatrix();
glTranslatef(0.5, -1, 0);///(3)掛上去
glRotatef(angle[6], 0,0,1);///(2)旋轉
glTranslatef(0, -1.5, 0);///(1)旋轉中心
glScalef(2,2,2);
if (!pmodel7) {///glm需要它
pmodel7 = glmReadOBJ("rightleg2.obj");///glm需要它
if (!pmodel7) exit(0);///glm需要它
glmUnitize(pmodel7);///glm需要它
glmFacetNormals(pmodel7);///glm需要它
glmVertexNormals(pmodel7, 90.0);///glm需要它
}
glmDraw(pmodel7, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
///rightleg2(6)p7
///leftleg1(7)p8
glPushMatrix();
glTranslatef(-0.1, 0, 0);///(3)掛上去
glRotatef(angle[7], 0,0,1);///(2)旋轉
glTranslatef(-0.1, -0.2, 0);///(1)旋轉中心
glScalef(0.15,0.15,0.15);
if (!pmodel8) {///glm需要它
pmodel8 = glmReadOBJ("leftleg1.obj");///glm需要它
if (!pmodel8) exit(0);///glm需要它
glmUnitize(pmodel8);///glm需要它
glmFacetNormals(pmodel8);///glm需要它
glmVertexNormals(pmodel8, 90.0);///glm需要它
}
glmDraw(pmodel8, GLM_SMOOTH | GLM_MATERIAL);
//glPopMatrix();
///leftleg1(7)p8
///leftleg2(8)p9
glPushMatrix();
glTranslatef(-0.5, -1, 0);///(3)掛上去
glRotatef(angle[8], 0,0,1);///(2)旋轉
glTranslatef(0, -1.5, 0);///(1)旋轉中心
glScalef(2,2,2);
if (!pmodel9) {///glm需要它
pmodel9 = glmReadOBJ("leftleg2.obj");///glm需要它
if (!pmodel9) exit(0);///glm需要它
glmUnitize(pmodel9);///glm需要它
glmFacetNormals(pmodel9);///glm需要它
glmVertexNormals(pmodel9, 90.0);///glm需要它
}
glmDraw(pmodel9, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
///leftleg2(8)p9
//glutSolidTeapot(0.3);
glutSwapBuffers();
}
int oldX=0;///NOW2
void mouse(int button, int state, int x, int y)///NOW2
{///NOW2
oldX = x;///NOW2
}
#include <stdio.h>///NOW3
void motion(int x, int y)///NOW2
{///NOW2
angle[angleID] += x-oldX;///NOW3
oldX = x;///NOW2
/*if(fout == NULL) fout = fopen("motion.txt","w+");///now file
for(int i=0;i<20;i++){///NOW3
//printf(" %.2f ", angle[i]);///NOW3
//fprintf(fout, " %.2f ", angle[i]);
}
//printf("\n");///NOW3
//fprintf(fout,"\n");*/
display();///NOW2
}
void timer(int t)
{///NOW4
glutTimerFunc(33, timer, t+1);///NOW4 變快了... 撥好下一個timer的時間.... 等一下它會醒來
if(t%30==0){///關鍵的時間 每過1秒,讀新的 ///NOW4
if(fin==NULL) fin=fopen("motion.txt", "r");///NOW4
for(int i=0; i<20; i++){///NOW4
oldAngle[i] = newAngle[i];///NOW4 原本的新的,就變舊的
fscanf(fin, "%f", &newAngle[i]);///NOW4 再從檔案,讀入新的
}///NOW2
}
float alpha= (t%30)/30.0;///NOW4 算出alpha值 0.0 ... 1.0
for(int i=0; i<20; i++){///NOW4 20個關節角度
angle[i] = alpha * newAngle[i] + (1-alpha) * oldAngle[i];///NOW4 都要做內插
}
glutPostRedisplay();///NOW4 重畫畫面
}
///內插公式 ans = alpha * future + (1-alpha) * old
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=='5') angleID=5;
if(key=='6') angleID=6;
if(key=='7') angleID=7;
if(key=='8') angleID=8;
if(key=='w'){///只有按下'w'才存一行動作
if(fout==NULL) fout=fopen("motion.txt","w+");
for(int i=0;i<20;i++){
fprintf(fout," %.1f ", angle[i]);
}
fprintf(fout,"\n");
}
if(key=='r'){
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<20;i++){
fscanf(fin, "%f",&angle[i]);
}
}
if(key=='p'){
glutTimerFunc(500,timer,0);
if(fin==NULL) fin=fopen("motion.txt", "r");
for(int i=0;i<20;i++){
fscanf(fin, "%f",&newAngle[i]);
}
}
glutPostRedisplay();
}
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)
{
PlaySoundA("C:\\Users\\陳昱辰\\Desktop\\Victory",NULL,SND_ASYNC);///音樂
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(600,600);
glutCreateWindow("期末作品");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
//glutTimerFunc(1000,timer,0);
myTexture("filename.jpg");///background
glutDisplayFunc(display);
glClearColor(1,1,1,1);
//glEnable(GL_CULL_FACE);
//glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
//glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);///glm需要它
glEnable(GL_NORMALIZE);///glm需要它
glEnable(GL_COLOR_MATERIAL);///glm需要它
glEnable(GL_LIGHTING);///glm需要它
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);///glm需要它
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);///glm需要它
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);///glm需要它
glLightfv(GL_LIGHT0, GL_POSITION, light_position);///glm需要它
glutMainLoop();
}
沒有留言:
張貼留言