2024年3月11日 星期一

week04_mouse_glScalef 凱

 week04

## glscalef

1.下載課本範例 https://jsyeh.org/3dcg10/

2. data,win32

3. windows.zip = >

4.data.zip =>

5.試 glscalef(x,y,z) 


## week04_mouse_glscalef  (綠色茶壺)

1.安裝freeglut,同時把lib\libfreeglut.a 複製成 libglut32.a

2.flie-new-project, glut 專案



## week04_mouse_glscalef  (按著活數可以移動,變大變小)

1. 程式碼

#include <GL/glut.h>
float s = 1;///一開始是原本的1倍
void display()
{///設定 "清背景" 顏色
    glClearColor(1,1,0.9,1);///顏色R,G,B,A
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///清背景
    glColor3f(0,1,0);///3F就是R,G,B
    glPushMatrix();///備份矩陣
            glScalef(s,s,s);///縮放s倍
            glutSolidTeapot( 0.3 );
    glPopMatrix();
    glutSwapBuffers();
}
void motion(int x,int y)
{
    s = 1 + (x-150)/150.0;///會變大變小的變數 0-1
    display();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week04_mouse_glScalef");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}

##試著交換 translate/rotate 兩航程式 i
1.試一下課本範例 transformation
2.先把 translate 移到右方,再rotate看看
3.下方程式,按右鍵,可 swap translate/rotate交換
4. translate是公轉 rotate是自轉

##理解的技巧 
1.把左耳 "靠著" 左肩,在看程式碼(從下往上看)
2.把 "旋轉中,長胖的藍色的車子" 移到右邊去


##week04_translate_rotate_scale
1.file_new_ project, GLUT專案  week04_translate_rotate_scale
2.把剛剛的week04_mouse_glScalef 程式
3.整個 "旋轉中的縮很小的綠色茶壺" 
##week04_rotate_translate_scale (茶壺轉圈圈) => 逆時針 

1.程式碼

#include <GL/glut.h>
float teapotX = 0.5,teapotY = 0;
float angle = 0;
float s = 0.3;///一開始是原本的1倍
void display()
{///設定 "清背景" 顏色
    glClearColor(1,1,0.9,1);///顏色R,G,B,A
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///清背景
    glColor3f(0,1,0);///3F就是R,G,B
    glPushMatrix();///備份矩陣
        glRotatef(angle++,0,0,1);
        glTranslatef(teapotX,teapotY,0);
        glScalef(s,s,s);///縮放s倍
        glutSolidTeapot( 0.3 );
    glPopMatrix();
    glutSwapBuffers();
}
void motion(int x,int y)
{
    s = 1 + (x-150)/150.0;///會變大變小的變數 0-1
    display();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week04_mouse_glScalef");
    glutIdleFunc(display);
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}




##week04_keyboard_mouse_motion 
1.file-new-project, GLUT專案  week04_keyboard_mouse_motion
2. 程式碼

#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();
}


1-2 (按滑鼠自動貼上茶壺)
2. 程式碼
#include <GL/glut.h>
#include <stdio.h>
int N = 0;
float teapotX[1000],teapotY[1000];
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    for(int i=0; i<N; i++){
        glPushMatrix();
            glTranslatef(teapotX[i],teapotY[i],0);
            glutSolidTeapot( 0.1 );
        glPopMatrix();
    }
    glutSwapBuffers();
}
void keyboard(unsigned char key,int x,int y){
    printf("key:%c  x:%d  y:%d\n",key,x,y);
}
void mouse(int button,int state,int x,int y){
    teapotX[N] = (x-150)/150.0;
    teapotY[N] = -(y-150)/150.0;
    N++;
}
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);
    glutMainLoop();
}



沒有留言:

張貼留言