2022年3月28日 星期一

Hank Week06

 1. 用每週看模型的程式,看自轉 ( Rotate ) V.S. 公轉 ( Translate ) 

    

   2. 用上週的 Main 函式程式碼,在上方寫入 keyboard 函式,讓程式可以偵測按下什麼按鍵,
       且按鍵座標在哪裡
     程式碼:
     #include <GL/glut.h>

    #include <stdio.h>
    void display()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glColor3f(1,1,0);
            glutSolidTeapot(0.3);
            glutSwapBuffers();
    }
    void keyboard( unsigned char key, int x, int y)
    {
        printf("現在按下:%c 座標在:%d %d\n",key, x, y);
    }
    int main(int argc ,char**argv)
    {
        glutInit(&argc ,argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
        glutCreateWindow("Week05 Rotate");
    
        glutDisplayFunc(display);
        glutKeyboardFunc(keyboard);
        glutMainLoop();
    }
3. 修改剛剛的程式,讓滑鼠可以拖曳那個茶壺,然後可以看茶壺的位子

     程式碼:
    #include <GL/glut.h>
    #include <stdio.h>
    float x=0,y=0,z=0,oldX,oldY;
    void display()
    {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glPushMatrix();
                glTranslatef((x-150)/150.0,-(y-150)/150.0,z);
                ///glRotatef( angle,0,0,1);
                ///glScalef( scale ,scale ,scale );
                glColor3f(1,1,0);
                glutSolidTeapot(0.3);
                glPopMatrix();
                glutSwapBuffers();
     }
    void keyboard( unsigned char key , int mouseX, int mouseY)
    {
            printf("現在按下:%c 座標在:%d %d\n",key, mouseX, mouseY);
    }
    void mouse(int button, int state ,int mouseX, int mouseY)
    {
            oldX = mouseX; oldY = mouseY;
    }
    void motion(int mouseX, int mouseY)
    {
            x += (mouseX-oldX);
            y += (mouseY-oldY);
            oldX = mouseX; oldY = mouseY;
            display();
    }
    int main(int argc ,char**argv)
    {
            glutInit(&argc ,argv);
            glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
            glutCreateWindow("Week05 Rotate");

            glutDisplayFunc(display);
            glutKeyboardFunc(keyboard);
            glutMouseFunc(mouse);
            glutMotionFunc(motion);
            glutMainLoop();
    }

4. 將剛剛的程式碼,把用滑鼠拖曳茶壺的程式碼修改成用滑鼠拖曳放大縮小茶壺的程式碼
    程式碼:
    #include <GL/glut.h>
    #include <stdio.h>
    float x=0,y=0,z=0,scale=1.0,oldX,oldY;
    void display()
    {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glPushMatrix();
                //glTranslatef((x-150)/150.0,-(y-150)/150.0,z);
                ///glRotatef( angle,0,0,1);
                glScalef( scale ,scale ,scale );
                glColor3f(1,1,0);
                glutSolidTeapot(0.3);
                glPopMatrix();
                glutSwapBuffers();
    }
    void keyboard( unsigned char key , int mouseX, int mouseY)
    {
            printf("現在按下:%c 座標在:%d %d\n",key, mouseX, mouseY);
    }
    void mouse(int button, int state ,int mouseX, int mouseY)
    {
            oldX = mouseX; oldY = mouseY;
    }
    void motion(int mouseX, int mouseY)
    {

            if(mouseX>oldX) scale = scale *1.01;
            if(mouseX<oldX) scale = scale *0.99;
            //x += (mouseX-oldX);
            //y += (mouseY-oldY);
            oldX = mouseX; oldY = mouseY;
            display();
    }
    int main(int argc ,char**argv)
    {
            glutInit(&argc ,argv);
            glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
            glutCreateWindow("Week05 Rotate");

            glutDisplayFunc(display);
            glutKeyboardFunc(keyboard);
            glutMouseFunc(mouse);
            glutMotionFunc(motion);
            glutMainLoop();
    }

5. 接下來將移動,旋轉,縮放全部合在一起
    程式碼:
    #include <GL/glut.h>
#include <stdio.h>
float x=0,y=0,z=0/*移動*/,scale=1.0/*縮放*/,angle=0.0/*旋轉*/,oldX,oldY;
int now=1;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef((x-150)/150.0,-(y-150)/150.0,z);
        glRotatef( angle, 0, 0, 1);
        glScalef( scale ,scale ,scale );
        glColor3f(1,1,0);
        glutSolidTeapot(0.3);
        glPopMatrix();
        glutSwapBuffers();
}
void keyboard( unsigned char key , int mouseX, int mouseY)
{
    if(key=='w' || key=='W')now=1;
    if(key=='e' || key=='E')now=2;
    if(key=='r' || key=='R')now=3;
    //printf("現在按下:%c 座標在:%d %d\n",key, mouseX, mouseY);
}
void mouse(int button, int state ,int mouseX, int mouseY)
{
    oldX = mouseX; oldY = mouseY;
}
void motion(int mouseX, int mouseY)
{
    if(now==1){
        x += (mouseX - oldX);
        y += (mouseY - oldY);
    }else if(now==3){
        if(mouseX>oldX) scale = scale *1.01;
        if(mouseX<oldX) scale = scale *0.99;
    }
    oldX = mouseX; oldY = mouseY;
    display();
}
int main(int argc ,char**argv)
{
    glutInit(&argc ,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week05 Rotate");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}
6. 剛剛沒有把旋轉的程式碼寫進去,現在加進去

    程式碼:
    #include <GL/glut.h>
    #include <stdio.h>
    float x=250,y=250,z=0/*移動*/,scale=1.0/*縮放*/,angle=0.0/*旋轉*/,oldX,oldY;
    int now=1;
    void display()
    {
            glClearColor(0.5,0.5,0.5,1);
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glPushMatrix();
                glTranslatef((x-250)/250.0,-(y-250)/250.0,z);
                glRotatef( angle, 0, 0, 1);
                glScalef( scale ,scale ,scale );
                glColor3f(1,1,0);
                glutSolidTeapot(0.3);
                glPopMatrix();
                glutSwapBuffers();
    }
    void keyboard( unsigned char key , int mouseX, int mouseY)
    {
            if(key=='w' || key=='W')now=1;
            if(key=='e' || key=='E')now=2;
            if(key=='r' || key=='R')now=3;
            //printf("現在按下:%c 座標在:%d %d\n",key, mouseX, mouseY);
    }
    void mouse(int button, int state ,int mouseX, int mouseY)
    {
            oldX = mouseX; oldY = mouseY;
    }
    void motion(int mouseX, int mouseY)
    {
            if(now==1){
                x += (mouseX - oldX);
                y += (mouseY - oldY);
            }else if(now==2){
                angle += (mouseX-oldX);
            }else if(now==3){
                if(mouseX > oldX) scale = scale *1.01;
                if(mouseX < oldX) scale = scale *0.99;
            }
            oldX = mouseX; oldY = mouseY;
            display();
    }
    int main(int argc ,char**argv)
    {
            glutInit(&argc ,argv);
            glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
            glutInitWindowSize(500,500);
            glutCreateWindow("Week05 Rotate");

            glutDisplayFunc(display);
            glutKeyboardFunc(keyboard);
            glutMouseFunc(mouse);
            glutMotionFunc(motion);
            glutMainLoop();
    }









沒有留言:

張貼留言