2019年5月23日 星期四

嗶啵啵嗶啵2019/0321_W5

Week05
主題:旋轉

---------------------------------------------------------------------------------------------------
~開啟範例~
      載data、win32、glut32.dll 

2.先將window解壓縮、data移至window裡、glut32.dll 也移到window裡



~開啟GLIT專案~
1.開啟GLUT專案
(記得載freeglut的資料夾、lib資料夾要有libglut32.a)


2.讓茶壺自動旋轉
#include <GL/glut.h>
float angle=0;///宣告
 void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///沒有clear()清除畫面會留殘影
    glPushMatrix();///備份矩陣
        glRotatef(angle, 0 ,1 ,0);///對Y軸轉angle角度
        glutSolidTeapot(0.3);
    glPopMatrix();///還原矩陣,不然會跑掉!!
    glutSwapBuffers();
    angle+=3;///每次執行加一點點
}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("06160531_Week05!!");
    glutDisplayFunc(display);
    glutIdleFunc(display);/// Idle:重畫
    glutMainLoop();

}


3.按滑鼠後就可以用滑鼠旋轉
#include <GL/glut.h>
float angle=0;
 void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///沒有clear()清除畫面會留殘影
    glPushMatrix();///備份矩陣
        glRotatef(angle, 0 ,1 ,0);///對Y軸轉angle角度
        glutSolidTeapot(0.3);
    glPopMatrix();///還原矩陣,不然會跑掉!!
    glutSwapBuffers();
    ///要刪掉!!不然會自動旋轉 angle+=3;///每次執行加一點點
}
void motion(int x,int y)///滑鼠控制

{
     angle=x;///2.mouse motion移動
    display();
}
#include <stdio.h>
void mouse(int button, int state, int x, int y)///1.mouse按下去  3.mouse放開
{
    printf("%d %d %d %d\n",button,state,x,y);
}

int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("06160531_Week05!!");
    glutDisplayFunc(display);
    glutIdleFunc(display);/// Idle:重畫
    glutMotionFunc(motion);///滑鼠控制
    glutMouseFunc(mouse);///mouse
    glutMainLoop();
}




沒有留言:

張貼留言