2024年2月26日 星期一

week02 這個缺標題補打之圖學

 1. 下載課本範例 https://jsyeh/3cdg10/windows.zip和data.zip

2-1. 解壓縮後把data放進window裡

2-2. shapes.exe 執行今天的作業

3. 右鍵,可換點線面

4.左鍵,綠色數字上下調整

5-1.glColor3f (r , g , b) 顏色

5-2.glVertex2f (x , y)座標

如要使用其他顏色 可使用小畫家的滴管!

<<茶杯程式碼>>

#include <GL/glut.h>


void display()

{

    glColor3f(247/255.0,180/255.0,171/255.0);

    glutSolidTeapot(0.3);


    glColor3f(251/255.0,233/255.0,219/255.0);

    glutSolidTeapot(0.1);


    glColor3f(244/255.0,114/255.0,45/255.0);

    glutSolidTeapot(0.05);

    glutSwapBuffers();

}

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

{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE);

    glutCreateWindow("week02_glut_colar_teapot");

    glutDisplayFunc(display);

    glutMainLoop();

}


<<三角形程式碼>>

#include <GL/glut.h>


void display()

{

    glBegin(GL_POLYGON);

    glColor3f(1,0,0);glVertex2f(0,1);

    glColor3f(0,1,0);glVertex2f(1,-0.6);

    glColor3f(0,0,1);glVertex2f(-1,-0.6);

    glEnd();

    glutSwapBuffers();

}

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

{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE);

    glutCreateWindow("week02_glut_traingle");

    glutDisplayFunc(display);

    glutMainLoop();

}


6.用小畫家 找任意形狀頂點座標!
New file GLUT專案 名稱:week02_glut_mspaint_help_draw
step1:開小畫家 圖片放上去
step2:在小畫家的座標中 左上->0.0,右下->300.300
step3:座標中OpenGL預設正中間為 (0.0),右 上+1,左 下-1
step4:y座標(Y座標-中心點200)/200.0

程式碼:
#include <GL/glut.h>

void display()
{
    glBegin(GL_POLYGON);
    glColor3f(1,1,1);
    glVertex2f((30-200)/200.0,-(45-200)/200.0);
    glVertex2f((46-200)/200.0,-(102-200)/200.0);
    glVertex2f((84-200)/200.0,-(23-200)/200.0);
    glEnd();
    glutSwapBuffers();
}
int main(int argc,char*argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE);
    glutCreateWindow("week02_glut_traingle");
    glutDisplayFunc(display);
    glutMainLoop();
}

7.Drawing Circle
程式碼
#include <GL/glut.h>
#include <math.h>
void display()
{
    glBegin(GL_POLYGON);
    for(float a=0;a<=3.14*2;a+=0.1){
        glVertex2f(cos(a),sin(a));
    }

    glEnd();
    glutSwapBuffers();
}
int main(int argc,char*argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE);
    glutCreateWindow("week02_glut_traingle");
    glutDisplayFunc(display);
    glutMainLoop();
}

8.米老鼠程式碼:
#include <GL/glut.h>
#include <math.h>
void myCircle(float cx,float cy,float r)
{
    glBegin(GL_POLYGON);
    for(float a=0;a<=3.14*2;a+=0.1){
        glVertex2f(r*cos(a)+cx,r*sin(a)+cy);
    }

    glEnd();
}
void display()
{
    glColor3f(1,1,1);myCircle(0,0,1);
    glColor3f(0,0,0);myCircle(-0.5,+0.5,0.3);
    glColor3f(0,0,0);myCircle(+0.5,+0.5,0.3);
    glColor3f(0,0,0);myCircle(0,-0.3,0.7);
    glutSwapBuffers();
}
int main(int argc,char*argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE);
    glutCreateWindow("week02_glut_traingle");
    glutDisplayFunc(display);
    glutMainLoop();
}

沒有留言:

張貼留言