2022年3月21日 星期一

ander week05的筆記

 下載 windows .zip  data.zip 放到相對應的目錄去執行,執行transform.exe這個課本範例,是跑一下程式

右上角可切換模式  角度xyz
gist.github.com

關於旋轉軸,就像買鹹酥雞的竹籤,我們研究了010的轉動,研究100的轉動也研究了00利用神奇的安培右手,就可以知道東西怎麼繞的


第二節課 旋轉的黃色茶壺
#include <GL/glut.h>void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix(); ///口訣:減一半、除一半, y還倒過來
  glRotatef(90,0,0,1);///旋轉的角度90
    glColor3f(1,1,0);///黃色的
        glutSolidTeapot(0.3);///茶壼
    glPopMatrix();
    glutSwapBuffers();

}
int main( int argc, char**argv )
{
    glutInit( &argc, argv);
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow("week05 rotate");

    glutDisplayFunc(display);///Display顯示
    glutMainLoop();
}



用滑鼠轉角度
#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix(); ///備份矩陣
    glRotatef(angle,0,0,1);///旋轉的角度
    glColor3f(1,1,0);///黃色的
        glutSolidTeapot(0.3);///茶壼
    glPopMatrix();
    glutSwapBuffers();

}
void motion (int x,int y)
{
    angle=x;
    display();

}
int main( int argc, char**argv )
{
    glutInit( &argc, argv);
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow("week05 rotate");

    glutDisplayFunc(display);///Display顯示
    glutMotionFunc(motion);
    glutMainLoop();
}

#include <GL/glut.h>
float angle=0 ,oldX=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix(); ///口訣:減一半、除一半, y還倒過來
    glRotatef(angle,0,0,1);///旋轉的角度
    glColor3f(1,1,0);///黃色的
        glutSolidTeapot(0.3);///茶壼
    glPopMatrix();
    glutSwapBuffers();

}
void mouse(int button,int state,int x,int y)
{
    oldX =x ;
}
void motion (int x,int y)
{
    angle+=(x-oldX);
    oldX=x;
    display();

}
int main( int argc, char**argv )
{
    glutInit( &argc, argv);
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
    glutCreateWindow("week05 rotate");

    glutDisplayFunc(display);///Display顯示
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}
改完後的程式,旋轉起來順暢許多


拉點做出可愛的圖形
#include <stdio.h>
#include <GL/glut.h>
int N=0;
int x[1000],y[1000];
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBegin(GL_LINE_LOOP);
    for(int i=0; i<N;i++){
        glVertex2f((x[i]-150)/150.0,-(y[i]-150)/150.0);
    }
    glEnd();
    glutSwapBuffers();
}
void motion(int mouseX, int mouseY)
{
   // if(state==GLUT_DOWN){
        N++;
        x[N-1]=mouseX;
        y[N-1]=mouseY;
        printf("現在按下滑鼠,得到一個座標 %d %d\n",x[N-1],y[N-1]);
   // }
    display();
}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week05");

    glutDisplayFunc(display);
    glutMotionFunc(motion);///
    glutMainLoop();
}





沒有留言:

張貼留言