2020年3月17日 星期二

week03 polygon


接著,我們要利用座標創造圖形

#include <GL/glut.h>
#include <stdio.h>
void display()
{
    glClearColor(255,0,0,1)///可以設定背景顏色
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//一樣,先清除整個螢幕
    glBegin(GL_POLYGON);
    glVertex2f((180-150)/150.0, - (120-150)/150.0); ///   (x-150)/150.0   -(y-150)/150.0 x和y指座標
    glVertex2f((120-150)/150.0, - (180-150)/150.0);///其餘的也一樣
    glVertex2f((60-150)/150.0, - (120-150)/150.0);
    glVertex2f((120-150)/150.0, - (60-150)/150.0);


glEnd();///結束筆畫
glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
    if(state==GLUT_DOWN)///為了避免彈跳,必須以按下為主
    {
        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();
}

--------(BONUS)--------
作業成果

#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
void square() ///一個正方形
{
    glBegin(GL_POLYGON);
    glVertex2f(0.2,0.2);
    glVertex2f(0.2,0.5);
    glVertex2f(0.5,0.5);
    glVertex2f(0.5,0.2);
    glEnd();
}
void display()
{
    glClearColor(0/255.0,0/255.0,255/255.0,1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBegin(GL_POLYGON);
    float i=0; ///亮度係數
    for(float angle=0; angle<=360; angle+=12) ///角度設定,此為30次
    {
        glColor3f(i/255.0, i/255.0, i/255.0); ///顏色設定
        glPushMatrix();
        glRotatef(angle,0,0,1); ///旋轉
        square(); ///製圖
        glPopMatrix();
        i+=(10); ///逐漸增加
    }
    glEnd();
    glutSwapBuffers();
}
///從這裡之後都沒改///
void mouse(int button, int state, int x,int y)
{
    if(state==GLUT_DOWN)
    {
        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();
}

沒有留言:

張貼留言