2023年2月23日 星期四

week02 點線面色彩

 


GLUT

step01-1 從codeblocks打開專案並打開GLU
複製以下程式碼後刪除原本的程式碼並輸入
#include <GL/glut.h>
void display()
{
    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();
}

step01-2增加glColor3f(1,1,0);變黃色茶壺
再改成glColor3f(0,1,0);變綠色茶壺
同時輸入
glColor3f(1,1,0);///黃色
glutSolidTeapot( 0.5 );///大茶壺

glColor3f(0,1,0);///綠色
glutSolidTeapot( 0.3 );///小茶壺
變成黃色大茶壺加上綠色小茶壺
最後把glutCreateWindow("GLUT Shapes");改成glutCreateWindow("week02");
step02-1
開新的專案
輸入
#include <GL/glut.h>
void display()
{
    glColor3f(0,1,0);///綠色
    glBegin(GL_POLYGON);
    glVertex2f(0,1);///上面
    glVertex2f(-1,-1);///左下角
    glVertex2f(1,-1);///右下角
    glEnd();

    glColor3f(1,1,0);///黃色
    glutSolidTeapot( 0.3 );///茶壺
    glutSwapBuffers();
}

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




step02-2加入程式碼
glColor3f(1,0,0);
glColor3f(0,1,0);
glColor3f(0,0,1);
step02-3用作標劃AI的字
開新的檔案
複製之前的程式碼
改成
#include <GL/glut.h>
void display()
{

    glBegin(GL_POLYGON);
    glVertex2f( (65-100)/100.0, -(54-100)/100.0);
    glVertex2f( (34-100)/100.0, -(138-100)/100.0);
    glVertex2f( (59-100)/100.0, -(138-100)/100.0);
    glVertex2f( (87-100)/100.0, -(53-100)/100.0);
    glEnd();


    glutSwapBuffers();
}

int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week02");
glutDisplayFunc(display);
glutMainLoop();
}
用小畫家計算座標或顏色
座標要除以長寬(200)的一半(如:100.0)
顏色要除以255.0

畫出圓形
程式碼
#include <GL/glut.h>
#include <math.h>
void myCircle(float r, float x, float y)///r半徑
{
    glBegin(GL_POLYGON);
    for(float a=0; a<=2*3.141592; a+=0.01){///圓=2*3.14159
        glVertex2f( r*cos(a)+x, r*sin(a)+y);
    }
    glEnd();

}
void display(){
glColor3f(234/255.0,141/255.0,128/255.0);
myCircle(0.5, 0, 0);///r,x,y

glColor3f(1,1,0);
myCircle(0.3, 0.5, 0.5);

glColor3f(1,0,0);
myCircle(0.3, -0.5, 0.5);

glColor3f(0,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("week02");
glutDisplayFunc(display);
glutMainLoop();
}
homework
劃出田字
程式碼
#include <GL/glut.h>
void display()
{
    glColor3f(234/255.0,141/255.0,128/255.0);
    glBegin(GL_POLYGON);
    glVertex2f( (30-100)/100.0, -(68-100)/100.0);
    glVertex2f( (45-100)/100.0, -(153-100)/100.0);
    glVertex2f( (56-100)/100.0, -(164-100)/100.0);
    glVertex2f( (49-100)/100.0, -(75-100)/100.0);
    glEnd();

    glColor3f(254/255.0,238/255.0,116/255.0);
    glBegin(GL_POLYGON);
    glVertex2f( (37-100)/100.0, -(72-100)/100.0);
    glVertex2f( (49-100)/100.0, -(80-100)/100.0);
    glVertex2f( (144-100)/100.0, -(72-100)/100.0);
    glVertex2f( (148-100)/100.0, -(56-100)/100.0);
    glEnd();

    glColor3f(172/255.0,202/255.0,192/255.0);
    glBegin(GL_POLYGON);
    glVertex2f( (147-100)/100.0, -(56-100)/100.0);
    glVertex2f( (130-100)/100.0, -(148-100)/100.0);
    glVertex2f( (139-100)/100.0, -(167-100)/100.0);
    glVertex2f( (164-100)/100.0, -(66-100)/100.0);
    glEnd();

    glColor3f(254/255.0,52/255.0,16/255.0);
    glBegin(GL_POLYGON);
    glVertex2f( (53-100)/100.0, -(148-100)/100.0);
    glVertex2f( (53-100)/100.0, -(155-100)/100.0);
    glVertex2f( (124-100)/100.0, -(149-100)/100.0);
    glVertex2f( (126-100)/100.0, -(142-100)/100.0);
    glEnd();

    glColor3f(172/255.0,202/255.0,192/255.0);
    glBegin(GL_POLYGON);
    glVertex2f( (124-100)/100.0, -(140-100)/100.0);
    glVertex2f( (141-100)/100.0, -(162-100)/100.0);
    glVertex2f( (136-100)/100.0, -(170-100)/100.0);
    glVertex2f( (124-100)/100.0, -(148-100)/100.0);
    glEnd();

    glColor3f(219/255.0,163/255.0,116/255.0);
    glBegin(GL_POLYGON);
    glVertex2f( (85-100)/100.0, -(74-100)/100.0);
    glVertex2f( (100-100)/100.0, -(69-100)/100.0);
    glVertex2f( (89-100)/100.0, -(146-100)/100.0);
    glVertex2f( (98-100)/100.0, -(145-100)/100.0);
    glEnd();

    glColor3f(88/255.0,130255.0,30/255.0);
    glBegin(GL_POLYGON);
    glVertex2f( (63-100)/100.0, -(109-100)/100.0);
    glVertex2f( (72-100)/100.0, -(116-100)/100.0);
    glVertex2f( (126-100)/100.0, -(107-100)/100.0);
    glVertex2f( (121-100)/100.0, -(101-100)/100.0);
    glEnd();


    glutSwapBuffers();
}

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
















沒有留言:

張貼留言