2024年3月14日 星期四

WEEK04電腦圖學

week04

1.桌面安裝 freeglut , 把 lib 的 libfreeglut.a 複製成 libglut32.a

2. File-New-Project , GLUT 專案 week03_mouse 記得目錄(桌面) , GLUT目錄(桌面/freeglut)

3. 貼上10行程式碼

 week04_mouse_glScalef

1. 加上週的week03_mouse_glRotatef的部分程式

2.複習上禮拜旋轉茶壺


3. 用滑鼠縮放茶壺

///手動輸入 10程式
#include <GL/glut.h>
///float angle=0;///旋轉角度
float s = 1;
void display()
{
    glClearColor(1.0,0.0,0.9,1.0);///清背景的淡黃色
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);///清背景
    glPushMatrix();
        ///glRotatef(angle, 0, 0, 1);
        glScalef(s, s, s);///縮放
        glutSolidTeapot( 0.3 );
    glPopMatrix();
    glutSwapBuffers();
}

void motion(int x, int y)
{
    ///angle = x;
    s = 1+(x-150)/150.0;
    display();
}

int main(int argc, char*argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week04_mouse_glScalef");
    glutDisplayFunc(display);
    glutMotionFunc(motion);

    glutMainLoop();
}


week04_keyboard_mouse_motion

1. 新增專案

2. 貼上11行程式碼

3.修改程式碼

4.在鍵盤上按鍵可以顯示滑鼠在小正方形當前的座標

#include <GL/glut.h>
#include <stdio.h>

void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot( 0.3 );
    glutSwapBuffers();
}

void keyboard(unsigned char key, int x, int y)
{
    printf("key: %c x: %d y: %d\n", key, x, y);
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week04_keyboard_mouse_motion");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);

    glutMainLoop();
}



5. 結合旋轉和鍵盤

#include <GL/glut.h>
#include <stdio.h>

float angle = 0, oldX = 0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle, 0, 0, 1);
        glutSolidTeapot( 0.3 );
    glPopMatrix();
    glutSwapBuffers();
}

void mouse(int button, int state, int x, int y)
{
    oldX = x;
}

void motion(int x, int y)
{
    angle += (x-oldX);
    oldX = x;
    display();
}

void keyboard(unsigned char key, int x, int y)
{
    printf("key: %c x: %d y: %d\n", key, x, y);
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week04_keyboard_mouse_motion");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
}


week04_translate_rotate_scale

1. 貼上前一個程式碼 , 再刪掉一些內容

2.修改程式碼 , 就能讓茶壺自行轉動了

#include <GL/glut.h>
#include <stdio.h>

float angle = 0, oldX = 0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle++, 0, 0, 1);
        glutSolidTeapot( 0.3 );
    glPopMatrix();
    glutSwapBuffers();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week04_mouse_glScalef");
    glutDisplayFunc(display);
    glutIdleFunc(display);

    glutMainLoop();
}


3.再修改程式碼 , 能讓茶壺往右邊再變顏色

#include <GL/glut.h>
#include <stdio.h>

float angle = 0, oldX = 0;
void display()
{
    glClearColor(1.0, 1.0, 0.9, 1.0);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(0.8, 0, 0);///放到右邊去
        glRotatef(angle++, 0, 0, 1);///轉動中的
        glScalef(0.3, 0.3, 0.3);///小小的
        glColor3f(0, 1, 0);///綠色的
        glutSolidTeapot( 0.3 );///茶壺
    glPopMatrix();
    glutSwapBuffers();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week04_mouse_glScalef");
    glutDisplayFunc(display);
    glutIdleFunc(display);

    glutMainLoop();
}


week04_rotate_translate_scale


1.交換程式碼 translate/rotate

2.看顏色對應圖


3.複製上一個程式碼 , 新增專案

4.將10跟11行交換就可從自轉變公轉了

#include <GL/glut.h>
#include <stdio.h>

float angle = 0, oldX = 0;
void display()
{
    glClearColor(1.0, 1.0, 0.9, 1.0);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle++, 0, 0, 1);///轉動中的
        glTranslatef(0.8, 0, 0);///放到右邊去
        glScalef(0.3, 0.3, 0.3);///小小的
        glColor3f(0, 1, 0);///綠色的
        glutSolidTeapot( 0.3 );///茶壺
    glPopMatrix();
    glutSwapBuffers();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week04_mouse_glScalef");
    glutDisplayFunc(display);
    glutIdleFunc(display);

    glutMainLoop();
}


















沒有留言:

張貼留言