2020年4月7日 星期二

week06(2020/4/7)紀錄

今天進度:階層性移動,轉動
複習完上次打光程式後
用滑鼠移動茶壺

float teapotX=0,teapotY=0;

int oldX=0,oldY=0;

void a()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(teapotX/250.0,teapotY/250.0,0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();

}


void m(int b,int s,int x,int y)
{
    oldX=x;

    oldY=y;
}

void motion(int x,int y)

{

    teapotX += x-oldX;
    teapotY += y-oldY;
    oldX=x;
    oldY=y;

    glutPostRedisplay();

記得main中要加入 glutMouseFunc(m); glutMotionFunc(motion);


接著用鍵盤按鍵切換轉動與移動

float teapotRoY=0;

int type=1;(預設模式為1)

改void a()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(teapotX/150.0,-teapotY/150.0,0);
        glRotatef(teapotRoY,0,-1,0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();

}

改void motion(int x,int y)

{
    if(type==1){
        teapotRoY += x-oldX;
    }
    else if(type==2){
    teapotX += x-oldX;
    teapotY += y-oldY;
    }
    oldX=x;
    oldY=y;
    glutPostRedisplay();

}

#include<stdio.h>
void k(unsigned char key, int x,int y)
{
    if(key=='e'||key=='E') type=1;
    if(key=='w'||key=='W') type=2;
    printf("now: %c\n",key);
}

記得main中要加入 glutKeyboardFunc(k);

glutIdleFunc();可以讓程式在閒置時自己跑,()中打要跑的程式

 glTranslatef 跟 glRotatef 的順序有影響!!!

沒有留言:

張貼留言