2022年3月28日 星期一

week 06電腦圖學之父

今天 小葉上課開頭所要介紹的是一位電腦圖學之父

名為Ivan Sutherland

https://jsyeh.org/3dcg10/download
windows.zip.data.zip


然後至windows.zip download/windows\Transformation.exe.
data.zip至 download\windows\data\3D模型
如圖
           
接下來打開後可以看到一台車的畫面
點一下下方黑色的框框案右鍵的
[s]Swap translate/rotate
如下圖

車子移動可以有公轉和自轉的功能
打開CODEBLOCKS將上禮拜的程式碼複製貼上到今天的內容
#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();
}
如圖下
在茶壺上面按下A到Z的任何一個字母就會產生不同的座標出來
在新創一個freeglut:
將剛剛的程式碼複製過來做修改:
#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);
}
void mouse( int button, int state, int x ,int y)
{
}
void motion( int x, int y)
{
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week06 keyboard mouse motion");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
}
接下來再修改一些數值 讓茶壺的位置改變


#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 mouseX ,int mouseY )
{
    printf("現在按下:%c 座標在:%d %d\n", key, mouseX ,mouseX);
}
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 mouse motion");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
}
此圖為茶壺的變更位置
將屬標點至茶壺可以拉到不同的位置


另一種程式碼可以讓茶壺變大變小
#include <GL/glut.h>
#include <stdio.h>
float x=0, y=0, z=0, scale=1.0, oldX, oldY;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glScalef( scale, scale, scale);
        glColor3f(1, 1 ,0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void keyboard(unsigned char key, int mouseX ,int mouseY )
{
    printf("現在按下:%c 座標在:%d %d\n", key, mouseX ,mouseX);
}
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;
    oldX=mouseX; oldY =mouseY;
    display();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week06 keyboard mouse motion");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
}
大的茶壺
小的茶壺
other else:
#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 mouseX ,int mouseY )
{
    //printf("現在按下:%c 座標在:%d %d\n", key, mouseX ,mouseX);
    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 mouse motion");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
}



茶壺最後的程式碼:



#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.5, 0.5, 0.5, 1);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef( (x-250)/250.0 , -(y-250)/250.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 mouseX ,int mouseY )
{   //printf("現在按下:%c 座標在:%d %d\n", key, mouseX ,mouseX);
    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;
    dispaly();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE || GLUT_DEPTH);
    glutInitWindowSize(500, 500);
    glutCreateWindow("week06 keyboard mouse motion");

    glutDisplayFunc(display);
    glutKeyboardFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
}


















沒有留言:

張貼留言