2018年3月19日 星期一

week*04小紅帽槓上大野狼

移動/旋轉/放大/縮小

*進入jsyeh.org/3dcg10
  -
  -載[data](解壓縮到win32)、[win32](解壓縮)、glut32.dll(傳送到win32)
  -不能有雙層data
  -

*期中80分
  -

*旋轉茶壺
  -手動旋轉茶壺
  -float angle=0;///旋轉角度
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///備份矩陣
        glRotatef(angle,0,0,1);///旋轉角度(angle度)
        glutSolidTeapot(0.3);///茶壺大小0.3
    glPopMatrix();///還原矩陣
    glutSwapBuffers();
    angle++;///每次角度加1度
}
  -
  -顯示茶壺度數
  -float angle=0;///旋轉角度
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///備份矩陣
        glRotatef(angle,0,0,1);///旋轉角度(angle度)
        glutSolidTeapot(0.3);///茶壺大小0.3
    glPopMatrix();///還原矩陣
    glutSwapBuffers();
    printf("%.1f\n",angle);
    angle++;///每次角度加1度
}
  -
  -自動選轉茶壺
  -int main(int argc , char*argv[])  ///進階的main()參數(個數,字串)

{
    glutInit(&argc,argv);  ///進階參數,初始GLUT
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);  ///開起2種模式
    glutCreateWindow("05160965");  ///盡量不要用中文

    glutIdleFunc(display);///自動旋轉茶壺關鍵句
    glutDisplayFunc(display);  ///等一下準備 void display()
    glutMainLoop();  ///主要迴圈
}
  -
*滑鼠旋轉茶壺
  -void mouse(int button, int state, int x, int y)
{

}

void motion(int x, int y)
{
    angle=x;
    glutPostRedisplay();
}
  -
  -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;///右鍵移動
}

void motion(int x, int y)
{
    if(nowRotate==1) angle=x;///改變angle的值
    else
    {
        teapotX=x;///一次宜太多會失敗,會跑到畫面外面去
        teapotY=y;
    }
    glutPostRedisplay();///請畫面重畫
}
  -
  -不優的設定
  -void motion(int x, int y)
{
    if(nowRotate==1) angle=x;///改變angle的值
    else
    {
        teapotX=(x-150)/150.0;///不好的移動,不好的旋轉,因為每次都從0開始,沒有疊加
        teapotY=(150-y)/150.0;
    }
    glutPostRedisplay();///請畫面重畫
}
  -

*end
  -
*程式碼
  -#include <GL/glut.h>  ///使用GLUT外掛
#include<stdio.h>
float angle=0,teapotX=0,teapotY=0;///旋轉角度 & 移動的座標
int nowRotate=0,oldX=0,oldY=0;

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///備份矩陣
        glTranslatef(teapotX,teapotY,0);
        glRotatef(angle,0,0,1);///旋轉角度(angle度)
        glutSolidTeapot(0.3);///茶壺大小0.3
    glPopMatrix();///還原矩陣
    glutSwapBuffers();
    printf("%.1f\n",angle);
    ///angle++;///每次角度加1度
}

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);///改變angle的值
    else
    {
        teapotX=(x-oldX)/150.0;///不好的移動,不好的旋轉,因為每次都從0開始,沒有疊加
        teapotY=(oldY-y)/150.0;
    }
    oldX=x;
    oldY=y;
    glutPostRedisplay();///請畫面重畫
}

int main(int argc , char*argv[])  ///進階的main()參數(個數,字串)

{
    glutInit(&argc,argv);  ///進階參數,初始GLUT
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);  ///開起2種模式
    glutCreateWindow("05160965");  ///盡量不要用中文
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    ///glutIdleFunc(display);///自動旋轉茶壺關鍵句
    glutDisplayFunc(display);  ///等一下準備 void display()
    glutMainLoop();  ///主要迴圈
}

沒有留言:

張貼留言