2014年5月23日 星期五

機器的路2

上週大受好評! 立即開拍機器的路2
想要個有聲又有影又會自己動的機器人嗎?
沒錯!只要動動手指頭,免錢又帥氣機器人讓你帶回家!
程式碼下收

#include <stdio.h>
#include <windows.h>
#include <mmsystem.h>//Multi Media System
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include "CMP3_MCI.h"

CMP3_MCI myMP3;
float angle[8]={0,0,0,0,0,0,0,0};
int angleID=0;
GLuint id[4];
IplImage * img[4];
char filename[4][100]={"imgBody.png", "imgHand0.png", "imgLeg0.png", "imgLeg1.png"};
void myTexture()
{
glEnable(GL_TEXTURE_2D);
for(int i=0; i<4; i++){
img[i] = cvLoadImage( filename[i] );
cvCvtColor(img[i], img[i], CV_BGR2RGB);
glGenTextures(1, &id[i]);
glBindTexture(GL_TEXTURE_2D, id[i]);//head
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img[i]->width, img[i]->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img[i]->imageData);
}
}
void myRect()
{
glPushMatrix();
glScalef(0.2, 0.5, 0.5);
glBegin(GL_POLYGON);
glTexCoord2f(0,0);  glVertex3f(-0.5, 0.5,0);
glTexCoord2f(0,1);  glVertex3f(-0.5,-0.5,0);
glTexCoord2f(1,1);  glVertex3f( 0.5,-0.5,0);
glTexCoord2f(1,0);  glVertex3f( 0.5, 0.5,0);
glEnd();
glPopMatrix();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0,0.35,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix();    
glBindTexture(GL_TEXTURE_2D, id[0]);
myRect();//body
glPushMatrix();//left hand
glTranslatef(-0.1, 0.15, 0);
glRotatef(angle[0], 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();
glPushMatrix();
glTranslatef(0, -0.25, 0);
glutSolidSphere(0.12,30,30);
glRotatef(angle[1], 0,0,1);
glTranslatef(0, -0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right hand
glTranslatef(0.1, 0.15, 0);
glRotatef(angle[2], 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();
glPushMatrix();
glTranslatef(0, -0.25, 0);
glutSolidSphere(0.12,30,30);
glRotatef(angle[3], 0,0,1);
glTranslatef(0, -0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//left leg
glTranslatef( -0.05, -0.25, 0);
glRotatef(angle[4], 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix();//left foot
glTranslatef( 0, 0.25, 0);
glutSolidSphere(0.12,30,30);
glRotatef(angle[5], 0,0,1);
glTranslatef(0, 0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[3]);
myRect();
glPopMatrix();  
glPopMatrix();
glPushMatrix();//right leg
glTranslatef( 0.05, -0.25, 0);
glRotatef(angle[6], 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix();//left foot
glTranslatef( 0, 0.25, 0);
glutSolidSphere(0.12,30,30);
glRotatef(angle[7], 0,0,1);
glTranslatef(0, 0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[3]);
myRect();
glPopMatrix();          
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
FILE *fout=NULL,*fin=NULL;
void timer(int t)//timer是你機器人最好的夥伴
{
glutTimerFunc(1000,timer,0);
if(fin==NULL)fin=fopen("animation.txt","r");
fscanf(fin,"%f %f %f %f %f %f %f %f\n",&angle[0],&angle[1],&angle[2],&angle[3],&angle[4],&angle[5],&angle[6],&angle[7]);
printf("%f %f %f %f %f %f %f %f\n", angle[0],angle[1],angle[2],angle[3],angle[4],angle[5],angle[6],angle[7]);
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') angleID=0;
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=='s')//按下一次s 就可以記錄你帥氣機器人的英姿
{
if(fout==NULL)fout = fopen("animation.txt","w+");
printf("%f %f %f %f %f %f %f %f\n", angle[0],angle[1],angle[2],angle[3],angle[4],angle[5],angle[6],angle[7]);
fprintf(fout,"%f %f %f %f %f %f %f %f\n", angle[0],angle[1],angle[2],angle[3],angle[4],angle[5],angle[6],angle[7]);
}
if(key=='r')//按下r (請記得先儲存後再按) 就可以再看一次你家帥氣機器人的英姿
{
if(fin==NULL)fin=fopen("animation.txt","r");
fscanf(fin,"%f %f %f %f %f %f %f %f\n",&angle[0],&angle[1],&angle[2],&angle[3],&angle[4],&angle[5],&angle[6],&angle[7]);
printf("%f %f %f %f %f %f %f %f\n", angle[0],angle[1],angle[2],angle[3],angle[4],angle[5],angle[6],angle[7]);
}
if(key=='p')//加上play更帥氣,不用瘋狂按 每秒自己就會跑
{
glutTimerFunc(1000,timer,0);
}
glutPostRedisplay();//請電腦重新畫一次畫面
}
int oldX=0, oldY=0;
void motion(int x, int y)
{
angle[angleID] += x-oldX;  oldX=x;
glutPostRedisplay();
}
void mouse(int button, int state, int x, int y)
{
oldX=x; oldY=y;
}

int main()
{
myMP3.Load("Life.mp3");
myMP3.Play();
//PlaySound("ccheer.wav",NULL,SND_ASYNC);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myTexture();
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}  


沒有留言:

張貼留言