2018年3月19日 星期一

Week04 小葉非常認真~報導

先行下載[data][win32] glut32.dll
接下來把[data][win32]解壓縮
把data資料夾與 glut32.dll檔案放入windows資料夾中
開啟Transformation檔 即開啟以下檔

--------------------------------------------------------------分隔線

(教學)可以用安培右手定則了解旋轉方向的運行




上課範例程式碼
glPushMatrix();
        glTranslated(x,y,z);
        glRotated(angle,x,y,z);
        glScalef(x,y,z);
        glBegin(GL_POLYGON);
             glColor3f(r,g,b);
             glVertex3f(x,y,z);
        glEnd();
glPopMatrix();


--------------------------------------------------------------分隔線
旋轉的茶壺(並且顯示增加的角度)


#include <stdio.h>
#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,0,1);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
    printf("%.1f\n",angle);
    angle++;
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week04 transformation");

    glutIdleFunc(display);
    glutDisplayFunc(display);

    glutMainLoop();
}

--------------------------------------------------------------分隔線

能有辦法用滑鼠旋轉茶壺轉動
#include <stdio.h>
#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,0,1);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
    ///printf("%.1f\n",angle);
    ///angle++;
}
void mouse(int button,int state,int x,int y)
{

}
void motion(int x,int y)
{
    angle=x;
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week04 transformation");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);

    glutMainLoop();
}

--------------------------------------------------------------分隔線
可以利用滑鼠左鍵轉動以及右鍵移動

#include <stdio.h>
#include <GL/glut.h>
float angle=0,teapotX=0,teapotY=0;
int oldX=0, oldY=0, nowRotate=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(teapotX,teapotY,0);
        glRotatef(angle,0,0,1);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
    ///printf("%.1f\n",angle);
    ///angle++;
}
void mouse(int button,int state,int x,int y)
{
    if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN) nowRotate=1;
    if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN) nowRotate=0;
    oldX=x; oldY=y;
}
void motion(int x,int y)
{
    if(nowRotate==1) angle += (x-oldX);
    else{
        teapotX +=(x-oldX)/150.0;
        teapotX +=(oldY-y)/150.0;
    }
    oldX=x; oldY=y;
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week04 transformation");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);

    glutMainLoop();
}


沒有留言:

張貼留言