顯示具有 01160661_李名遙 標籤的文章。 顯示所有文章
顯示具有 01160661_李名遙 標籤的文章。 顯示所有文章

2014年6月12日 星期四

Week 16 , 期末作業


https://www.youtube.com/watch?v=-u4Tk-ycTf0&feature=youtu.be

Week 15 , 音效與動畫

#include <windows.h>
#include <mmsystem.h>

int main()
{
PlaySound("bbb.wav",NULL,SND_ASYNC);
//SYNC要等同步 ASYNC不等同步
}



*先把CMP3_MCI.h放入專案資料夾中
#include "CMP3_MCI.h"
CMP3_MCI myMP3;

int main()
{
myMP3.Load("aaa.mp3");
myMP3.Play();
}














#include <stdio.h>
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
//float angle0=0, angle1=0, angle2=0, angle3=0, angle4=0, angle5=0, angle6=0, angle7=0;
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();
glBindTexture(GL_TEXTURE_2D, id[0]);
myRect();//body//技巧1: 用函式, 哥就是帥!
glPushMatrix();//left hand 今天的技巧1: 大的手
glTranslatef(-0.1, 0.15, 0);
glRotatef(angle[0], 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();//技巧1: 用函式, 哥就是帥!
glPushMatrix();// 今天的技巧2: 小的手
glTranslatef(0, -0.25, 0);//(3) T: 掛在該掛的位子
glRotatef(angle[1], 0,0,1);//(2) R:
glTranslatef(0, -0.25, 0);//(1) T: 改變旋轉軸的位置
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();//技巧1: 用函式, 哥就是帥!
glPushMatrix();// 今天的技巧2: 小的手
glTranslatef(0, -0.25, 0);//(3) T: 掛在該掛的位子
glRotatef(-angle[3], 0,0,1);//(2) R:
glTranslatef(0, -0.25, 0);//(1) T: 改變旋轉軸的位置
myRect();
glPopMatrix();   
glPopMatrix();
glPushMatrix();//left leg
glTranslatef( -0.05, -0.25, 0);
glRotatef(180-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);
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(-(180-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);
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;
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'){
if(fout==NULL) fout = fopen("a.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]);
}


glutPostRedisplay();//請電腦重新畫一次畫面
}
int oldX=0, oldY=0;
void mouse(int button, int state, int x, int y)
{
oldX=x; oldY=y;
}
void motion(int x, int y)
{
angle[angleID] += x-oldX;  oldX=x;
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myTexture();

glutMainLoop();
}

Week 14 - 運鏡



#include <GL/glut.h>

#include <math.h>
#include <stdlib.h>
float eyeX=-0.3, eyeY=1.2, eyeZ=0.4, objX=-0.2, objY=-0.5, objZ=-0.2, UpX=0, UpY=1, UpZ=0;
float angleX =0,angleA=0,angleY=0;
float thesize=0.2;
void timerRotateShow(int t);
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleA+=5,angleX+=5,angleY=(1);
if(key=='2') angleA-=5,angleX-=5,angleY=0;
if(key==' '){
objX+=0.02;
}
if(key=='0'){
glutTimerFunc(0, timerRotateShow,0);
}


glutPostRedisplay();
}
void timerRotateShow(int t)
{
if(t%10==0)glClearColor((rand()%255)/255.0, (rand()%255)/255.0, (rand()%255)/255.0, (rand()%255)/255.0);
glutTimerFunc(30, timerRotateShow,t+1);
eyeX=cos(t/100.0*3.1415);
eyeZ=sin(t/100.0*3.1415);
eyeY-=0.005;
thesize+=0.01;
glutPostRedisplay();
}
void myLight()
{
const GLfloat light_position[] = { 2.0f, 5.0f, -1.0f, 0.0f };
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
gluLookAt(eyeX, eyeY, eyeZ, objX, objY, objZ, UpX, UpY, UpZ);
glScalef(thesize,thesize,thesize);
glPushMatrix();//Eyes
glRotatef(angleX, 0,0.1,0);
glColor3f(0,0,0);
glTranslatef(0.1,0.3,-0.25);
glutSolidSphere(0.02,10,10);
glPushMatrix();
glTranslatef(-0.2,0,0);
glutSolidSphere(0.02,10,10);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glRotatef(angleX, 0,1,0);
glColor3f(0.5,1,2);
glTranslatef(0, 0.2, 0);
glutSolidSphere(0.25, 20, 20);
glPopMatrix();
glPushMatrix();
glRotatef(angleX, 0,1,0);
glColor3f(0.5,1,2);
glutSolidTeapot(0.29);
glutWireTeapot(0.3);
glPopMatrix();
glPushMatrix();
glRotatef(angleA, 1,0,0);
glTranslatef(-0.1, -0.4, 0);
glColor3f(0.5,1,2);
glScalef(1, 3, 1);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();
glRotatef(angleA, 1,0,0);
glTranslatef(-0.1, -0.68, 0);
glRotatef(angleA, -0.15,0,0);
glColor3f(1,1,1);
glScalef(1.2, 2, 1.4);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();
glRotatef(angleA, -1,0,0);
glTranslatef(0.1, -0.4, 0);
glColor3f(0.5,1,2);
glScalef(1, 3, 1);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();
glRotatef(angleA, -1,0,0);
glTranslatef(0.1, -0.68, 0);
glRotatef(angleA, 0.15,0,0);
glColor3f(1,1,1);
glScalef(1.2, 2, 1.4);
glutSolidCube(0.15);
glPopMatrix();
glPushMatrix();
glRotatef(angleX, 0,1,0);
glColor3f(1, 1, 1);
glTranslatef(-0.4, 0, 0);
glutSolidSphere(0.15, 20, 20);
glPushMatrix();
glRotatef(angleA, 0, 1, 1);
glColor3f(0.5,1,2);
glScalef(1, 2, 1);
glTranslatef(0.02, -0.1, 0);
glutSolidCube(0.15);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glRotatef(angleX, 0, 1, 0);
glColor3f(1, 1, 1);
glTranslatef(0.4, 0, 0);
glutSolidSphere(0.15, 20, 20);
glPushMatrix();
glRotatef(angleA, 0, 1, 1);
glColor3f(0.5,1,2);
glScalef(1, 2, 1);
glTranslatef(0.02, -0.1, 0);
glutSolidCube(0.15);
glPopMatrix();
glPopMatrix();
glPopMatrix();

glutSwapBuffers();
}

int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week14");

glutDisplayFunc(display);
glutKeyboardFunc(keyboard);

myLight();

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2,2,-2,2,-10,10);
glMatrixMode(GL_MODELVIEW);

glutMainLoop();
}

2014年5月16日 星期五

Week 13 , 用滑鼠操控Robot


注 : -lcv -lcxcore -lhighgui 在專案選項記得加
dll 檔案記得丟到資料夾
#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();
}

2014年5月2日 星期五

Week 11 (機器人)


#include <GL/glut.h>

float angleX =0 , angle2=0;
 void keyboard(unsigned char key, int x, int y)
 {
  if(key=='1') angleX+=5;
  if(key=='2') angleX-=5;
  if(key=='3') angleX+=5;
  if(key=='4') angleX-=5;

  glutPostRedisplay();//請電腦重新畫一次畫面
 }

void myLight()
 {
      const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
      glEnable(GL_LIGHT0);
      glEnable(GL_NORMALIZE);
      glEnable(GL_COLOR_MATERIAL);
      glEnable(GL_LIGHTING);
      glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}

void display()
 {
   glEnable(GL_DEPTH_TEST);
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   glPushMatrix();
       glRotatef(angleX, 0,1,0);
       glutWireTeapot(0.3);
 
   glPushMatrix();
       glTranslatef(0,0.4,0);
       glutSolidSphere(0.3,30,30);
   glPopMatrix();
 
   glPushMatrix();//右肩膀
        glTranslatef(0.4,0,0);
        glutSolidSphere(0.23,30,30);
        glPushMatrix();//右邊手臂
            glRotatef(angle2,0,0,1);
            glTranslatef(0,-0.2,0);
            glScalef(0.4,1,0.4);
            glutSolidCube(0.5);
          glPopMatrix();
        glPopMatrix();
 
   glPushMatrix();//左肩膀
        glTranslatef(-0.4,0,0);
        glutSolidSphere(0.23,30,30);
        glPushMatrix();//左邊手臂
            glRotatef(-angle2,0,0,1);
            glTranslatef(0,-0.2,0);
            glScalef(0.4,1,0.4);
            glutSolidCube(0.5);
          glPopMatrix();
        glPopMatrix();

     glPopMatrix();
  glPopMatrix();
  glutSwapBuffers();
 }

int main()
 {
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
  glutCreateWindow("week11");

 glutDisplayFunc(display);
  glutKeyboardFunc(keyboard);

 myLight();
  glutMainLoop();
 }

2014年4月25日 星期五

Week 10 (打光)

#include <GL/glut.h>
#include <stdio.h>
float angleX=0;
void keyboard(unsigned char key,int x,int y)
{
 printf("%c %d %d\n",key,x,y);
 if(key=='a') angleX+=3;
 if(key=='b') angleX-=3;
 glutPostRedisplay();
}
void myLight()
{
 const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f,0.0f };
 glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);
 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}
void display()
{
 glEnable(GL_DEPTH_TEST);
 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
 glPushMatrix();
  glRotatef(angleX,0,1,0);
  glutSolidTeapot(0.3);
 glPopMatrix();
 glutSwapBuffers();
}

int main()
{
 glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
 glutCreateWindow("week10");

 glutDisplayFunc(display);
 glutKeyboardFunc(keyboard);

 myLight();

 glutMainLoop();
}

Week 10 , 用鍵盤控制茶壺

#include <GL/glut.h>
#include <stdio.h>
float angle=0;
void keyboard(unsigned char key , int x , int y)
{
     printf("%c %d %d\n",key,x,y);
     if(key=='a') angle+=3;
     if(key=='b') angle-=3;
     glutPostRedisplay();
   
     //glutSwapBuffers();
}
void display()
{
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
     glPushMatrix();
        glRotatef(angle,0,1,0);
        glutSolidTeapot(0.3);
     glPopMatrix();
     glutSwapBuffers();
}
int main()
{
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week 10");
 
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
 
    glutMainLoop();
}

注 : 專案 ---> 專案選項 ---- > Win32Console ---> 確定