2014年5月16日 星期五

01160581 機器的路

大學生做出來的超非凡機器人
不管是來個滑板滑壘
還是想來學個忍術
非凡機器人讓你十個願望一次滿足:3
這個價值非凡的機器人怎麼製作?
很簡單動動你的手指鍵盤滑鼠按幾下就可以了。
而且 完全免費
接下來 齁康報給你知,程式碼下收
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
float angle[8]={0,0,0,0,0,0,0,0};
int angleID=0;
GLuint id[4];//技巧2: 使用陣列, 讓變數 變 簡單 simple
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++){//技巧3: 迴圈, 可以讓程式碼, 做重覆的事可以很爽 清爽
        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()//技巧1: 重覆的程式碼, 用函式拉出來, 就不用一直copy啦
{//函式就是 黑盒子 幫你做事情!
    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();
}
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;
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()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myTexture();
glutMouseFunc(mouse);
glutMotionFunc(motion);
//以上不要犯蠢,上面寫了一堆結果忘記呼叫,讓你哭天搶地機器人也回不來了
glutMainLoop();
}

最後特別大感謝
葉正聖老師友情提供程式碼

沒有留言:

張貼留言