2017年3月14日 星期二

Week04 劉育榕

今日課堂內容 :
  1. 作業互評
  2. 主題: Mouse互動
  3. 實作: glutMouseFunc()    glutMotionFunc()
  4. 圓 , sin() ,cos(),Shape
  5. 作業 (Blog v.s. Moodle )
------------------------------------------------------------------
課堂作業 一 :作業互評
調大小 : Ctrl- +  Ctrl- -
→挑出3個最好作品上傳Moodle


課堂作業 二 :    畫正圓形

glVertex2f(   cos(angle) , sin(angle)   )  ;


  1. 畫出圓形
程式碼  ↓ 

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

   glBegin(GL_POLYGON);
   for( float angle=0 ; angle<=3.1415926*2 ; angle+=0.01) 
   {
       glVertex2f(cos(angle),sin(angle));   
   }

 glEnd();
    glutSwapBuffers();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("04160533 Number 1");
    glutDisplayFunc(display);

    glutMainLoop();
}



                 
完成圖


        2.滑鼠按鍵改變顏色
 
加上Mouse函式
↓↓
 
void mouse(int button ,int state ,int x,int y)   // button : 為滑鼠的案鍵(右鍵左鍵中鍵) 
                                                                                //  state: 為滑鼠狀態(按下或放開)
{
    printf("%d %d %d %d \n",button ,state ,x ,y);   //印數值
    glColor3f(1,x/300.0,y/300.0);                            //改變任意顏色
}
 
 


完成圖
 
     3.使用滑鼠拖曳改變顏色
 
    加入glutMotionFunc(motion);
 motion拖曳函式
    ↓↓
 
   void motion(int x,int y)     // 拖曳
  {
      glColor3f(1,x/300.0,y/300.0);
      glutPostRedisplay();    // Post 貼便利貼告訴電腦 有空時候要重複display
  }
 
 
 

完成圖
 
          4.做出小精靈
 
        使用扇形TRIANGLE_FAN 來繪圖
        (注意 :  每次繪圖前都必須要有glClear()程式碼 作為清畫面)
         ↓↓   
    
程式碼 :
 
 
#include <GL/glut.h>
#include <math.h>
#include<stdio.h>
   //printf()
float mouth =0;
static void display(void)
{
   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   //清畫面
   glBegin(GL_TRIANGLE_FAN);                                                            //使用扇形繪圖
 
  glVertex2f(0,0);      //扇形起點
   for( float angle=0+mouth ; angle<=3.1415926*2-mouth ; angle+=3.1415926/100
   {
       glVertex2f( cos(angle) , sin(angle) );
   }
    glEnd();
    glutSwapBuffers();
}
void mouse(int button ,int state ,int x,int y)  //滑鼠事件
{
   // printf("%d %d %d %d \n",button ,state ,x ,y);
}
void motion(int x,int y)     // 拖曳
{    
      mouth=x/300.0;
      glColor3f(1,x/300.0,y/300.0);
      glutPostRedisplay();    // Post 貼便利貼告訴電腦 有空時候要重複display
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("04160533 Number 1");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}

 

 
完成圖
 


沒有留言:

張貼留言