2020年3月18日 星期三

泳裝莉瑪莉歐巴馬英九把刀削麵疙瘩


今天一開始用ZUVIO先複習上禮拜的進度




再來複習上次的茶壺




給小黑新增一個滑鼠

         botton                          state                            y
鍵: 0左 、1中、2右鍵 ,   0下、1上 ,   x:0~300 ,   y:0~300



程式碼:

#include <GL/glut.h>
#include <stdio.h>
void display(){
    glutSolidTeapot( 0.3 );
    glutSwapBuffers();
}
void mouse(int button, int state , int x, int y){
    printf("%d %d %d %d\n", button , state, x, y);
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("haha");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMainLoop();
}


再讓茶壺可以移動

程式碼:

#include <GL/glut.h>
#include <stdio.h>
float teapotX=0, teapotY=0; ///用變數 -1...+1表示座標
void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix(); //備份矩陣(大二上資料結構教過)
        //glTranslatef(x, y, z); 移動(x, y, z)
        glTranslatef(teapotX, teapotY, 0);//移動(X,Y,0)
        glutSolidTeapot( 0.3 );
    glPopMatrix(); //備份矩陣(大二上資料結構教過)
    glutSwapBuffers();
}
void mouse(int button, int state , int x, int y){
  ///                x:   0  ~   150   ~  300
  ///減150         -150       0         150  先把mouse的座標,減150
  ///除150.0        -1         0         +1    再把結果,再除以   150.0
  /// 改成            -1         0         +1    就會得到我們要的-1...+1的範例

    teapotX= (x-150)/150.0; ///換算一下座標
    teapotY=-(y-150)/150.0; ///換算一下座標

}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("haha");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMainLoop();
}





再由程式寫出座標來畫出圖形

程式碼:

#include <GL/glut.h>

float teapotX=0, teapotY=0;
void display(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBegin(GL_POLYGON);
        glVertex2f( (227-150)/150.0, -(132-150)/150.0);
        glVertex2f( (151-150)/150.0, -(115-150)/150.0);
        glVertex2f( (85-150)/150.0, -(140-150)/150.0);
        glVertex2f( (118-150)/150.0, -(188-150)/150.0);
        glVertex2f( (180-150)/150.0, -(186-150)/150.0);
    glEnd();
    glutSwapBuffers();
}
void mouse(int button, int state , int x, int y){

}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("haha");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMainLoop();
}



沒有留言:

張貼留言