2024年3月11日 星期一

Week04    

       
***縮放茶杯***
   
程式碼:     
 
 #include <GL/glut.h> ///貼上10行GLUT程式碼
float s = 1;         ///一開始是原本一倍的大小


void display()

{ ///設定"清背景"的顏色

    glClearColor(1,1, 0.9 ,1); ///R, G , B , A
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glColor3f(0, 1, 0); ///3f 就是R,G,B
    glPushMatrix();    ///備份矩陣
        glScalef(s,s,s);    ///縮放s倍
            glutSolidTeapot( 0.3 );
    glPopMatrix();
    glutSwapBuffers();
}
void motion(int x,int y)
{
        s = 1 + (x-150)/150.0;    ///變大變小的變數 0~1~2
        display(); ///重畫畫面
}

int main(int argc,char*argv[])

{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week04 mouse glScalef");

    glutDisplayFunc(display);
    glutMotionFunc(motion); ///拖動motion的時候

    glutMainLoop();

}


*** 試著交換Translate/Rotate兩行程式***

1 試課本 Transformation
2 先把translate移到右方,再rotate看看
3 下方程式按右鍵,可swap translate/rotate交換
4 再rotate看看差在哪裡
5 觀察公轉 自轉 ///旋轉軸不一樣
   
#差異在於更改旋轉軸 (一個在物體外 一個在物體內)
  
程式碼:
 
#include <GL/glut.h>
float teapotX = 0.5,teapotY = 0; ///放右邊
float angle = 0; ///旋轉角度
float s = 0.3;


void display()

{

    glClearColor(1,1, 0.9 ,1);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glColor3f(0, 1, 0);
    glPushMatrix(); ///輩分矩陣
        glTranslatef(teapotX,teapotY,0);
        glRotatef(angle++,0,0,1); ///旋轉矩陣
        glScalef(s,s,s);
            glutSolidTeapot( 0.3 );
    glPopMatrix(); ///還原矩陣
    glutSwapBuffers();
}
void motion(int x,int y)
{
        s = 1 + (x-150)/150.0;
        display();
}

int main(int argc,char*argv[])

{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week04 mouse glScalef");
    glutIdleFunc(display);
    glutDisplayFunc(display); ///沒事就display    
    glutMotionFunc(motion);

    glutMainLoop();

}

***公轉程式***

程式碼:

#include <GL/glut.h>
float teapotX = 0.5,teapotY = 0;
float angle = 0;
float s = 0.3;


void display()

{

    glClearColor(1,1, 0.9 ,1);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glColor3f(0, 1, 0);
    glPushMatrix();
        glRotatef(angle++,0,0,1);  ///交換這兩行
        glTranslatef(teapotX,teapotY,0); 
        glScalef(s,s,s);
            glutSolidTeapot( 0.3 );
    glPopMatrix();
    glutSwapBuffers();
}
void motion(int x,int y)
{
        s = 1 + (x-150)/150.0;
        display();
}

int main(int argc,char*argv[])

{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week04 mouse glScalef");
    glutIdleFunc(display);
    glutDisplayFunc(display);
    glutMotionFunc(motion);

    glutMainLoop();

}



***點按畫面出現茶壺***

程式碼:

#include <GL/glut.h>
#include <stdio.h>
int N=0;
float teapotX[1000], teapotY[1000]; ///要存座標
float teapot;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    for(int i=0;i<N;i++){
        glPushMatrix();
            glTranslatef( teapotX[i] , teapotY[i],0);
            glutSolidTeapot( 0.1 );
        glPopMatrix();
    }
    glutSwapBuffers();

}
void keyboard (unsigned char key , int x, int y){
    printf("key:%c x:%d y:%d\n", key,x,y);
}
void mouse(int button, int state, int x, int y){
    teapotX[N] = (x-150)/150.0;
    teapotY[N] = -(-150)/150.0;
    N++;
}

int main(int argc,char*argv[])

{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week04 keyboard mouse motion");

    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard); ///鍵盤事件的函式

    glutMouseFunc(mouse); ///滑鼠事件的函式

    glutMainLoop();

}

沒有留言:

張貼留言