2023年2月22日 星期三

ww-week02-點_線_面_色彩

一、

1.同上週第二個Project:開New Project / GLUT專案 / 存放位置:桌面 / 專案名week02_GLUT_first / codeBlocks中開啟Project的Sources下拉main.cpp / 開啟notepad++ / 寫出下圖的程式碼 / 貼回Codeblocks / 執行

2.更改顏色: 使用glColor3f( 0,0,0) / 調整大小 / 綠色0.5藍色0.3


3.畫多邊形:用glBegin(GL_POLYGON)畫多邊形 / 用glVertex2f( 0, 1)頂點座標 / glEnd()結束畫



#include <GL/glut.h>
void display()
{
        glColor3f(0,1,0);///開始畫
glBegin(GL_POLYGON);///畫多邊形
        glVertex2f( 0, 1);///頂點Vertex
        glVertex2f(-1,-1);
        glVertex2f(+1,-1);
        glEnd();///結束畫

        glColor3f(0,1,1);///設定顏色:藍色
        glutSolidTeapot( 0.3 );///劃出一個實心的茶壺,它的大小0.3
        glutSwapBuffers();///請GLUT把畫面swap送到顯示的地方
}
int main(int argc, char *argv[])///特別的main()函式,有很多參數//138
{
glutInit(&argc, argv);///把GLUT開起來//140
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
///顯示模式設定//143
glutCreateWindow("GLUT Shapes");///要開視窗//145
glutDisplayFunc(display);///要顯示的對應函式//148
glutMainLoop();///最後用main迴圈//174
}

 

4. 漸層,多色:glColor3f(1,0,1); glVertex2f( 0, 1);///寫在同一行
 

#include <GL/glut.h>
void display()
{

     glBegin(GL_POLYGON);
        glColor3f(1,0,1); glVertex2f( 0, 1);///顏色;從什麼頂點開始畫
        glColor3f(0,1,1); glVertex2f(-1,-1);
        glColor3f(1,1,0); glVertex2f(+1,-1);
     glEnd();

     glColor3f(0,1,1);
     glutSolidTeapot( 0.3 );
     glutSwapBuffers();
}
int main(int argc, char *argv[])
{
     glutInit(&argc, argv);
     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
     glutCreateWindow("GLUT Shapes");
     glutDisplayFunc(display);
  glutMainLoop();
}

 

二、畫圖:

1.畫圖案的方法 : 用小畫家來找顏色和座標 / 將小畫家框改成200*200 / 用顏色吸取器提顏色 :(顏色/255.0) 確認頂點,在程式中換算位值((x-一半)/一半,-(y-一半)/一半

 #include <GL/glut.h>

void display()

{

    glColor3f(247/255.0,247/255.0,247/255.0);///背景顏色

    glBegin(GL_POLYGON);

        glVertex2f( 1, 1);

        glVertex2f(-1, 1);

        glVertex2f(-1,-1);

        glVertex2f( 1,-1);

    glEnd();

 

    glColor3f(244/255.0,209/255.0,33/255.0);///皮卡丘顏色

    glBegin(GL_POLYGON);

        glVertex2f((45-100)/100.0, -(134-100)/100.0);

        glVertex2f((36-100)/100.0, -(171-100)/100.0);

        glVertex2f((108-100)/100.0, -(179-100)/100.0);

        glVertex2f((104-100)/100.0, -(117-100)/100.0);///減一半,除一半,Y變負號

    glEnd();

    glutSwapBuffers();

}

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

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT Shapes");

    glutDisplayFunc(display);

    glutMainLoop();

}


2.畫圓:用迴圈,cos(),sin()

#include <GL/glut.h>
#include <math.h>
void myCircle(float r,float x,float y)///畫圓的函式 
{
    glBegin(GL_POLYGON);
        for(float a=0;a<2*3.141592653589793238462632383279;a+=0.01){
            glVertex2f( r*cos(a)+x, r*sin(a)+y);
        }
    glEnd();
}
void display()
{
     glColor3f(0,1,1); myCircle(0.6,0,0);
     glColor3f(1,0,0); myCircle( 0.3, 0.5, 0.5);///右上角
     glColor3f(0,1,0); myCircle( 0.3,-0.5, 0.5);///左上角
     glColor3f(1,1,0); myCircle( 0.3,-0.5,-0.5);///左下角
     glColor3f(0,0,1); myCircle( 0.3, 0.5,-0.5);///右下角

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

 

git指令
cd desktop
git clone https://github.com/你的帳號/2023graphicsa
cd 2023graphicsa
加入檔案
git add . 
git status

git config --global user.email "jsyeh@mail.mcu.edu.tw"

git config --global user.name "jsyeh"

git commit -m "week10"

git push

沒有留言:

張貼留言