顯示具有 04160100_劉俐伶 標籤的文章。 顯示所有文章
顯示具有 04160100_劉俐伶 標籤的文章。 顯示所有文章

2017年6月12日 星期一

Week16 三個ㄌ 的 上課筆記

1.利用內插公式讓機器人轉動更順暢

2.存檔
FILE * fout=NULL;
FILE * fin=NULL;
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=='s'){
        printf("%c\n",key);
        if(fout==NULL)
        {
            fout=fopen("file.txt","w+");
        }
        fprintf(fout,"%.2f %.2f %.2f %.2f\n",angle[0],angle[1],angle[2],angle[3]);
        printf("%.2f %.2f %.2f %.2f\n",angle[0],angle[1],angle[2],angle[3]);
    }

3.讀檔
    if(key=='r')
    {
        //printf("%c\n",key);
        if(fin==NULL)
        {
            fin=fopen("file.txt","r");
            fscanf(fin,"%f %f %f %f\n",&newAngle[0],&newAngle[1],&newAngle[2],&newAngle[3]);
        }
        if(alpha>=1.0)

        {
            for(int i=0;i<5;i++) oldAngle[i]=newAngle[i];
            fscanf(fin,"%f %f %f %f\n",&newAngle[0],&newAngle[1],&newAngle[2],&newAngle[3]);
            alpha=0.0;
        }
        for(int i=0;i<5;i++)
        {
            angle[i]=alpha*newAngle[i]+(1-alpha)*oldAngle[i];
        }


Week14 三個ㄌ 的 上課筆記

1.timer計時器

void timer(int t)

{
    glutTimerFunc(100,timer,t+1);
    angle++;
    glutPostRedisplay();
}

int main(int argc, char**argv)
{

    glutDisplayFunc(display);
    //glutIdleFunc(display);
    glutTimerFunc(2000,timer,0); 

}


2.使用滑鼠控制
void mouse(int button,int state,int x,int y)
{
    oldX=x,oldY=y;
    glutPostRedisplay();
}


void motion(int x,int y)
{
    angle[now]+=(x-oldX);
    oldX=x;
    glutPostRedisplay();
}
3.利用鍵盤滑鼠一起控制
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=='s'){
        printf("%c\n",key);
        if(fout==NULL)
        {
            fout=fopen("file.txt","w+");
        }
        fprintf(fout,"%.2f %.2f %.2f %.2f\n",angle[0],angle[1],angle[2],angle[3]);
        printf("%.2f %.2f %.2f %.2f\n",angle[0],angle[1],angle[2],angle[3]);
    }
    if(key=='r')
    {
        //printf("%c\n",key);
        if(fin==NULL)
        {
            fin=fopen("file.txt","r");
            fscanf(fin,"%f %f %f %f\n",&newAngle[0],&newAngle[1],&newAngle[2],&newAngle[3]);
        }
        if(alpha>=1.0)

        {
            for(int i=0;i<5;i++) oldAngle[i]=newAngle[i];
            fscanf(fin,"%f %f %f %f\n",&newAngle[0],&newAngle[1],&newAngle[2],&newAngle[3]);
            alpha=0.0;
        }
        for(int i=0;i<5;i++)
        {
            angle[i]=alpha*newAngle[i]+(1-alpha)*oldAngle[i];
        }
        alpha+=0.1;
        /*if(key=='z')
        {
            PlaySound("C:\\Users\\Administrator.20150226-220640\\Desktop\\04160100_hw01\\data\\home.mp3",NULL,SND_ASYNC);
        }*/

        glutPostRedisplay();
    }
}
void mouse(int button,int state,int x,int y)
{
    oldX=x,oldY=y;
    glutPostRedisplay();
}


void motion(int x,int y)
{
    angle[now]+=(x-oldX);
    oldX=x;
    glutPostRedisplay();
}

Week12 三個ㄌ 的 上課筆記

1.播放音樂mp3檔

使用 CMP3_MCI.h 便可以輕鬆地將 MP3檔播放出來。
(1) #include "CMP3_MCI.h"///(1) 放在同一個程式碼專案的目錄中哦

(2) CMP3_MCI myMP3;///(2) 宣告一個變數,叫 myMP3
在 main()函式裡面
(3) myMP3.Load("C:\\dropbox\\yukai.mp3"); /// "C:/dropbox/yukai.mp3"
(4) myMP3.Play();/// (4) Play the file


2.觀察濾鏡

茶壺使用濾鏡


#include <GL/glut.h>
float eyeX=0, eyeY=0, eyeZ=1;
float centerX=0, centerY=0, centerZ=0;
float upX=0, upY=1, upZ=0;
static void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3d(1,0,0);

    //攝影機運鏡的設定
    glPushMatrix();
    gluLookAt(eyeX, eyeY, eyeZ,
    centerX, centerY, centerZ,
    upX, upY, upZ);
    glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}

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 };

/* Program entry point */
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("GLUT Shapes");

    ///glutReshapeFunc(resize);
    glutDisplayFunc(display);
    ///glutKeyboardFunc(key);
    ///glutIdleFunc(idle);
    glClearColor(1,1,1,1);

    ///glEnable(GL_CULL_FACE);
    ///glCullFace(GL_BACK);
    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();
    return EXIT_SUCCESS;
}

3.TRT
#include <GL/glut.h>
#include <math.h>
float eyeX=0, eyeY=0, eyeZ=2;
float centerX=0, centerY=0, centerZ=0;
float upX=0, upY=1, upZ=0;
float angle=0, dir=1;
static void display(void)
{
    glMatrixMode(GL_PROJECTION); 
    gluPerspective(60, 1, 0.001, 10000); 
    glMatrixMode(GL_MODELVIEW);  

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3d(1,0,0);
    angle+=dir;
    if(angle>60)dir = -dir;  
    if(angle<0) dir = -dir;

    glPushMatrix();
    gluLookAt(eyeX, eyeY, eyeZ,
    centerX, centerY, centerZ,
    upX, upY, upZ);

        glutSolidTeapot(0.3);  //head

        glPushMatrix();
            glTranslatef(0, -0.3, 0);
            glColor3f(1,1,1); glutSolidTeapot(0.3);  //body

            glPushMatrix();
                glTranslatef(0.3, 0,0);  
                glRotatef(angle, 0,0,1);  
                glTranslatef(0.3, 0,0);  
                glutSolidTeapot(0.3);  //right upper arm

                glPushMatrix();
                    glTranslatef(0.3, 0,0);  
                    glRotatef(angle, 0,0,1);  
                    glTranslatef(0.3, 0,0);  
                    glutSolidTeapot(0.3);  //right lower arm
                glPopMatrix();
            glPopMatrix();

            glPushMatrix();
                glTranslatef(-0.3, 0,0);  
                glRotatef(-angle, 0,0,1);  
                glTranslatef(-0.3, 0,0);  
                glutSolidTeapot(0.3);  //left upper arm

                glPushMatrix();
                    glTranslatef(-0.3, 0,0);  
                    glRotatef(-angle, 0,0,1);  
                    glTranslatef(-0.3, 0,0);  
                    glutSolidTeapot(0.3);  //left lower arm
                glPopMatrix();
                glPopMatrix();

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

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 };

/* Program entry point */
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");

    ///glutReshapeFunc(resize);
    glutDisplayFunc(display);
    ///glutKeyboardFunc(key);
    ///glutIdleFunc(idle);
    glutIdleFunc(display);

    glClearColor(1,1,1,1);

    ///glEnable(GL_CULL_FACE);
    ///glCullFace(GL_BACK);

    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();
    return EXIT_SUCCESS;
}

Week03 三個ㄌ 的 上課筆記

1.使用範例檔

http://jsyeh.org/3dcg10 下載data,win32,glut32.dll檔案
打開範例檔,自由調整旁邊數值呈現不同的顏色圖形。

2.複習茶壺(改變顏色)
#include <GL/glut.h>
static void display(void)
{
    glColor3f(1,0,0); 
    glutSolidTeapot(0.3); 
    glutSwapBuffers();
}
int main(int argc,chat *argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("GLUT Shapes");
      

    glutDisplayFunc(display);   
    glutMainLoop();
    return EXIT_SUCCESS;

}

3.畫三角形

#include <GL/glut.h>
static void display(void)
{
    glBegin(GL_POLYGON);
    glColor3f(1,0,0); 
    glVertex3f(0,0,0);  


    glColor3f(0,1,0);  
    glVertex3f(1,1,0);  

   
    glColor3f(0,1,0);  
    glVertex3f(-1,1,0);  

    glEnd();

    //glutSolidTeapot(0.3); 
    glutSwapBuffers();
}
int main(int argc,chat *argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("GLUT Shapes");   
    glutDisplayFunc(display);   
    glutMainLoop();
    return EXIT_SUCCESS;


}

2017年5月23日 星期二

Week14 三個ㄌ 的 上課筆記

一.讓自己做的機器人可以在學校開啟
修改Notepad++開include,lib目錄設對working目錄。
程式run我做的花媽就跑出來了~~
使用 glClearColor 函式增加背景顏色~~
套入計時器程式碼和文字
void timer(int t)
{
    printf("大家好我是花媽,現在是%d秒",t);
    glutTimerFunc(100,timer,t+1);
    printf("人見人愛的花媽");
    angle+=3;
    glutPostRedisplay();
    printf("記得天天看我們這一家~~\n");
}

2017年5月16日 星期二

Week13 三個ㄌ 的 上課筆記

一.小考TRT觀念複習

glPushMatrix();
glTranslatef(.....);
//(3)最後把"會照著手肘中心轉動的手"移動,掛到真的世界畫面裡
glRotatef(.....);//(2)旋轉(小心,移動的正負號,要想一下右手定則)
glTranslatef(......);//(1)把手肘(轉動中心)移到畫面的中心(負向,倒過來移哦)
 drawHand();//(0)畫手,位置不能錯,從下往上看
glPopMatrix();


二.讀圖檔
GOOGLE:OPENCV2.1 DOWNLOAD安裝

設定專案 儲存

存圖片至c槽

修改程式
程式碼
#include <opencv/highgui.h>
int main()
{
    IplImage *img = cvLoadImage("C:/Users/user/Desktop/image.jpg");
        
    cvShowImage("my2DImage",img);  //cvShowImage("名稱",img);
    cvWaitKey(0);  
} 

BULID Options 設定




秀出圖片

三.地球運轉

開啟專案中的main.cpp,變可看到地球。

將gluSphere(quad,1,30,30);改成glutSolidTeapot(0.3);
就可以看到變成茶壺的地球了。