2020年3月17日 星期二

week03畫出小小兵

W03---開啟3D圖學網址

下載圈起來的三個資料夾

解壓縮Windows後把壓縮後的data放進去
 再把glut32.dll一起放進Windows資料夾裡面~~~

打開Shapes就能看到點線面了

W03-1---顯示茶壺的座標位置

利用x,y軸" -1 ~ 1 "找出座標
#include <GL/glut.h>///為了glut()
#include <stdio.h>///為了printf()
void display()
{
    glColor3f(0.5,0.1,0.2);

    glutSolidTeapot( 0.3 );


    glutSwapBuffers();


}

void mouse(int button,int state,int x,int y)
{ ///mouse函式: 左0 中1 右2 , 下0 上1 , x座標 , 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("Week03");

    glutDisplayFunc(display); ///display顯示函式

    glutMouseFunc(mouse);
    glutMainLoop(); ///mouse的函式
}

W03-01---移動茶壺 (x左右,y上下,z前後)

glTranslatef讓座標從-1.0 ~ +1.0

#include <GL/glut.h>
#include <stdio.h>
float teapotX=0, teapotY=0; ///這裡是-1.0~+1.0的座標
void display()
{
    glColor3f(0.5,0.1,0.2);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///清背景
    glPushMatrix(); ///備分矩陣
        glTranslatef(teapotX, teapotY ,0); ///移動
        glutSolidTeapot( 0.3 );
    glPopMatrix(); ///備分矩陣
    glutSwapBuffers();

}
void mouse(int button,int state,int x,int y)
{
    printf("%f %f\n",(x-150)/150.0, (y-150)/150.0);
    teapotX =  (x-150)/150.0; ///任務 : 讓 x 從0...300變成-1...+1
    teapotY = -(y-150)/150.0; ///任務 : 讓 x 從300...0變成-1...+1
}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week03");

    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMainLoop();
}

 茶壺移到左邊顯示的位置

 W03-02---畫出喜歡的圖案

畫一個小圓形   

#include <GL/glut.h>
#include <stdio.h>
#include <math.h> ///為了 cos() sin()
float teapotX=0, teapotY=0; ///這裡是-1.0 ~ +1.0的座標
void display()
{
    glClearColor(180/255.0, 203/255.0, 87/255.0, 1);///背景顏色
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///清背景
    glBegin(GL_POLYGON); ///開始畫
        for(float angle=0;angle<=2*3.1415;angle+=0.01) {
            glVertex2f( 0.3*cos(angle), 0.3*sin(angle) );
        }
        //glVertex2f((-150)/150.2,-(-150)/150.0);///畫圖 : 設定數值
    glEnd(); ///結束畫
    glutSwapBuffers();
}
void mouse(int button,int state,int x,int y){
    if(state==GLUT_DOWN){ ///座標:減一半 ,除一半,y加負號
    printf("glVertex2f( (%d-150)/150.0, (%d-150)/150.0);\n",x,y);
    }
}
int main(int argc, char**argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week03");

    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMainLoop();
}




畫出一個小小兵

找一張小小兵的圖片放入小畫家
滑鼠游標對照左下角顯示的x,y位置寫進程式碼

對照色彩也要使用小畫家來找出對應的R,G,B 
~~~程式碼~~~
#include <GL/glut.h>
#include <stdio.h>
#include <math.h> ///為了 cos() sin()
float teapotX=0, teapotY=0; ///這裡是-1.0 ~ +1.0的座標

void display()
{
    glClearColor(252/255.0, 222/255.0, 98/255.0, 1);///顏色

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///清背景

        glBegin(GL_POLYGON); ///開始畫右眼鏡
        glColor3f((131/255.0),(134/255.0) , (132/255.0));
        for(float angle=0;angle<=2*3.1415;angle+=0.01){
            glVertex2f( 0.48*cos(angle)+0.4, 0.48*sin(angle)+0.3 );
        }
        //glVertex2f((-150)/150.2,-(-150)/150.0);///畫圖 : 設定數值
        glEnd(); ///結束畫
        glBegin(GL_POLYGON); ///開始畫右眼白
        glColor3f((252/255.0),(252/255.0) , (242/255.0));
        for(float angle=0;angle<=2*3.1415;angle+=0.01){
            glVertex2f( 0.32*cos(angle)+0.4, 0.32*sin(angle)+0.3 );
        }
        glEnd(); ///結束畫
        glBegin(GL_POLYGON); ///開始畫右眼珠
        glColor3f((139/255.0),(92/255.0) , (69/255.0));
        for(float angle=0;angle<=2*3.1415;angle+=0.01){
            glVertex2f( 0.13*cos(angle)+0.3, 0.13*sin(angle)+0.26 );
        }
        glEnd(); ///結束畫
        glBegin(GL_POLYGON); ///開始畫右眼黑
        glColor3f((43/255.0),(17/255.0) , (11/255.0));
        for(float angle=0;angle<=2*3.1415;angle+=0.01){
            glVertex2f( 0.08*cos(angle)+0.3, 0.08*sin(angle)+0.26 );
        }
        glEnd(); ///結束畫
        glBegin(GL_POLYGON); ///開始畫左眼鏡
        glColor3f((131/255.0),(134/255.0) , (132/255.0));
        for(float angle=0;angle<=2*3.1415;angle+=0.01){
            glVertex2f( 0.48*cos(angle)-0.4, 0.48*sin(angle)+0.3 );
        }
        glEnd(); ///結束畫
        glBegin(GL_POLYGON); ///開始畫左眼白
        glColor3f((252/255.0),(252/255.0) , (242/255.0));
        for(float angle=0;angle<=2*3.1415;angle+=0.01){
            glVertex2f( 0.32*cos(angle)-0.4, 0.32*sin(angle)+0.3 );
        }
        glEnd(); ///結束畫
                glBegin(GL_POLYGON); ///開始畫左眼珠
        glColor3f((139/255.0),(92/255.0) , (69/255.0));
        for(float angle=0;angle<=2*3.1415;angle+=0.01){
            glVertex2f( 0.13*cos(angle)-0.3, 0.13*sin(angle)+0.26 );
        }
        glEnd(); ///結束畫
        glBegin(GL_POLYGON); ///開始畫左眼黑
        glColor3f((43/255.0),(17/255.0) , (11/255.0));
        for(float angle=0;angle<=2*3.1415;angle+=0.01){
            glVertex2f( 0.08*cos(angle)-0.3, 0.08*sin(angle)+0.26 );
        }
        glEnd(); ///結束畫
        glBegin(GL_POLYGON); ///開始畫眼鏡框
        glColor3f((75/255.0),(71/255.0) , (66/255.0));
glVertex2f( (4-150)/150.0, (178-150)/150.0);
glVertex2f( (23-150)/150.0, (176-150)/150.0);
glVertex2f( (23-150)/150.0, (210-150)/150.0);
glVertex2f( (3-150)/150.0, (209-150)/150.0);
glVertex2f( (1-150)/150.0, (168-150)/150.0);
glVertex2f( (21-150)/150.0, (172-150)/150.0);
glVertex2f( (21-150)/150.0, (207-150)/150.0);
glVertex2f( (3-150)/150.0, (208-150)/150.0);
        glEnd(); ///結束畫
        glBegin(GL_POLYGON); ///開始畫眼鏡框
        glColor3f((75/255.0),(71/255.0) , (66/255.0));
glVertex2f( (275-150)/150.0, (170-150)/150.0);
glVertex2f( (298-150)/150.0, (173-150)/150.0);
glVertex2f( (298-150)/150.0, (212-150)/150.0);
glVertex2f( (276-150)/150.0, (212-150)/150.0);
        glEnd(); ///結束畫
        glBegin(GL_POLYGON); ///開始畫愛心嘴唇
        glColor3f((205/255.0),(16/255.0) , (76/255.0));
glVertex2f( (148-150)/150.0, (32-150)/150.0);
glVertex2f( (143-150)/150.0, (38-150)/150.0);
glVertex2f( (135-150)/150.0, (45-150)/150.0);
glVertex2f( (131-150)/150.0, (51-150)/150.0);
glVertex2f( (133-150)/150.0, (54-150)/150.0);
glVertex2f( (135-150)/150.0, (61-150)/150.0);
glVertex2f( (137-150)/150.0, (64-150)/150.0);
glVertex2f( (140-150)/150.0, (64-150)/150.0);
glVertex2f( (144-150)/150.0, (63-150)/150.0);
glVertex2f( (149-150)/150.0, (59-150)/150.0);
glVertex2f( (150-150)/150.0, (56-150)/150.0);
glVertex2f( (150-150)/150.0, (55-150)/150.0);
glVertex2f( (151-150)/150.0, (57-150)/150.0);
glVertex2f( (153-150)/150.0, (63-150)/150.0);
glVertex2f( (155-150)/150.0, (66-150)/150.0);
glVertex2f( (161-150)/150.0, (70-150)/150.0);
glVertex2f( (164-150)/150.0, (70-150)/150.0);
glVertex2f( (169-150)/150.0, (68-150)/150.0);
glVertex2f( (174-150)/150.0, (66-150)/150.0);
glVertex2f( (177-150)/150.0, (61-150)/150.0);
glVertex2f( (177-150)/150.0, (55-150)/150.0);
glVertex2f( (173-150)/150.0, (49-150)/150.0);
glVertex2f( (169-150)/150.0, (43-150)/150.0);
glVertex2f( (166-150)/150.0, (38-150)/150.0);
glVertex2f( (163-150)/150.0, (34-150)/150.0);
glVertex2f( (159-150)/150.0, (29-150)/150.0);
glVertex2f( (155-150)/150.0, (26-150)/150.0);
glVertex2f( (152-150)/150.0, (24-150)/150.0);
glVertex2f( (152-150)/150.0, (24-150)/150.0);
        glEnd(); ///結束畫
    glutSwapBuffers();
}
void mouse(int button,int state,int x,int y){
    if(state==GLUT_DOWN){ ///座標:減一半 ,除一半,y加負號
        printf("glVertex2f( (%d-150)/150.0, (%d-150)/150.0);\n",x,y);
    }
}
int main(int argc, char**argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week03");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMainLoop();
}
成果 :  畫出一個小小兵了<333



沒有留言:

張貼留言