2022年3月28日 星期一

Zfish week06

一:    

    1.電腦圖學之父:Ivan Sutherland (Sketchpad)


二:

1-1.根據week04第一點,執行Transformation.exe。

1-2.

二:鍵盤事件
1.鍵盤
#include <GL/glut.h>
#include <stdio.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glColor3f(1, 1, 0);
        glutSolidTeapot(0.3);
    glutSwapBuffers();
}

void Keyboard(unsigned char key, int x, int y)
{
    printf("現在按下:%c 座標在:%d %d\n",key, x, y);
}
int main(int argc, char *argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week06 keyboard");

    glutDisplayFunc(display);
    glutKeyboardFunc(Keyboard);
    glutMainLoop();
}

2.鍵盤+滑鼠
#include <GL/glut.h>
#include <stdio.h>
float x=0, y=0, z=0, oldX, oldY;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glTranslatef( (x-150)/150.0, -(y-150)/150.0, z);
        glColor3f(1, 1, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void Keyboard(unsigned char key, int x, int y)
{
    printf("現在按下:%c 座標在:%d %d\n",key, x, y);
}
void mouse(int button, int state, int mouseX, int mouseY)
{
    oldX= mouseX; oldY= mouseY;
}
void motion(int mouseX, int mouseY)
{
    x += (mouseX-oldX);
    y += (mouseY-oldY);
    oldX= mouseX; oldY= mouseY;
    display();
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week06 keyboard");
    glutDisplayFunc(display);
    glutKeyboardFunc(Keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}

3.滑鼠縮放
#include <GL/glut.h>
#include <stdio.h>
float x=0, y=0, z=0, oldX, oldY;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glTranslatef( (x-150)/150.0, -(y-150)/150.0, z);
        glColor3f(1, 1, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void Keyboard(unsigned char key, int x, int y)
{
    printf("現在按下:%c 座標在:%d %d\n",key, x, y);
}
void mouse(int button, int state, int mouseX, int mouseY)
{
    oldX= mouseX; oldY= mouseY;
}
void motion(int mouseX, int mouseY)
{
    if(mouseX>oldX) scale=scale*1.01;
    if(mouseX<oldX) scale=scale*0.99;
    //x += (mouseX-oldX);
    //y += (mouseY-oldY);
    oldX= mouseX; oldY= mouseY;
    display();
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week06 keyboard");
    glutDisplayFunc(display);
    glutKeyboardFunc(Keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}


4.鍵盤選擇要移動(w)、旋轉(e)、縮放(r)。
#include <GL/glut.h>
#include <stdio.h>
float x=0, y=0, z=0, scale=1.0, angle=0.0, oldX, oldY;
int now=1;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glTranslatef( (x-150)/150.0, -(y-150)/150.0, z); 
    glRotatef(angle, 0, 0, 1);
    glScalef(scale, scale, scale);
        glColor3f(1, 1, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void Keyboard(unsigned char key, int x, int y)///像MAYA
{
    if(key == 'W' || key == 'w') now=1; ///移動
    if(key == 'E' || key == 'e') now=2; ///旋轉
    if(key == 'R' || key == 'r') now=3; ///縮放
}
void mouse(int button, int state, int mouseX, int mouseY)
{
    oldX= mouseX; oldY= mouseY;
}
void motion(int mouseX, int mouseY)
{
    if(now == 1){ ///移動
        x += (mouseX-oldX);
        y += (mouseY-oldY);
    }else if(now == 3){ ///縮放
        if(mouseX>oldX) scale= scale * 1.01;
        if(mouseX<oldX) scale= scale * 0.99;
    }

    oldX= mouseX; oldY= mouseY;
    display();
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week06 keyboard");
    glutDisplayFunc(display);
    glutKeyboardFunc(Keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}

5.
#include <GL/glut.h>
#include <stdio.h>
float x=250, y=250, z=0, scale=1.0, angle=0.0, oldX, oldY;
int now=1;
void display()
{
    glClearColor(0.8, 0.8, 0.8, 1);///顏色R,G,B,A A->Alpha值
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glTranslatef( (x-150)/150.0, -(y-150)/150.0, z); ///w
    glRotatef(angle, 0, 0, 1);///e
    glScalef(scale, scale, scale);///r
        glColor3f(1, 1, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void Keyboard(unsigned char key, int x, int y)///像MAYA
{
    if(key == 'W' || key == 'w') now=1; ///移動
    if(key == 'E' || key == 'e') now=2; ///旋轉
    if(key == 'R' || key == 'r') now=3; ///縮放
}
void mouse(int button, int state, int mouseX, int mouseY)
{
    oldX= mouseX; oldY= mouseY;
}
void motion(int mouseX, int mouseY)
{
    if(now == 1){ ///移動
        x += (mouseX-oldX);
        y += (mouseY-oldY);
    }else if(now==2){///旋轉
        angle+=(mouseX-oldX);
    }else if(now == 3){ ///縮放
        if(mouseX>oldX) scale= scale * 1.01;
        if(mouseX<oldX) scale= scale * 0.99;
    }

    oldX= mouseX; oldY= mouseY;
    display();
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(500,500);///視窗大小
    glutCreateWindow("week06 keyboard");
    glutDisplayFunc(display);
    glutKeyboardFunc(Keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}


沒有留言:

張貼留言