2024年3月11日 星期一

week04

#week04


1.開啟網址   https://jsyeh.org/3dcg10/
2.下載 windows data 檔案 解壓縮  
3.試glScalef(x,y,z)




##week04_mouse_glScalef
1.桌面上課用軟體 freeglut 安裝 更改lib檔名
2.開啟CodeBlocks  新增專案 week04_mouse_glScalef
3.貼上10行程是碼 更改程式











#程式碼
#include <GL/glut.h>///使用GLUT外掛
float s=1;
 void display()
 {
     glClearColor(1,1,0.9,1);
     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
     glColor3f(0,1,0);
     glPushMatrix();
        glScalef(s,s,s);
        glutSolidTeapot(0.3);
     glPopMatrix();
     glutSwapBuffers();
 }

 void motion(int x,int y)
 {
     s= 1 +(x-150)/150.0;///變大變小
     display();

 }

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);///初始化 GLUT 140
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);///143

    glutCreateWindow("GLUT Shapes");///145
    glutDisplayFunc(display);///148 display函式
    glutMotionFunc(motion);
    glutMainLoop();///174 迴圈
}


#試著交換 Translate/rotate 程式
1.課本範例 Transformation
2.先把 translate移到右方 rotate 看看
3.下方程式 按右鍵可交換
4.(公轉 自轉) 旋轉不同







#week04_translate_rotate_scale
1.新增專案
2.剛剛程式貼上 修改






#程式碼
#include <GL/glut.h>///使用GLUT外掛
float teapotX=0.5, teapotY=0;
float angle=0;
float s=0.3;
 void display()
 {
     glClearColor(1,1,0.9,1);
     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
     glColor3f(0,1,0);
     glPushMatrix();
        glTranslatef(teapotX,teapotY,0);
        glRotatef(angle++,0,0,1);
        glScalef(s,s,s);
        glutSolidTeapot(0.3);
     glPopMatrix();
     glutSwapBuffers();
 }

 void motion(int x,int y)
 {
     s= 1 +(x-150)/150.0;///變大變小
     display();

 }

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);///初始化 GLUT 140
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);///143

    glutCreateWindow("GLUT Shapes");///145
    glutDisplayFunc(display);///148 display函式
    glutMotionFunc(motion);
    glutMainLoop();///174 迴圈
}

#week04_rotate_translate_scale
1.新稱glut專案 week04_rotate_translate_scale
2.貼上上一個程式 交換兩行






#程式碼
#include <GL/glut.h>///使用GLUT外掛
float teapotX=0.5, teapotY=0;
float angle=0;
float s=0.3;
 void display()
 {
     glClearColor(1,1,0.9,1);
     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
     glColor3f(0,1,0);
     glPushMatrix();
        glRotatef(angle++,0,0,1);
         glTranslatef(teapotX,teapotY,0);
        glScalef(s,s,s);
        glutSolidTeapot(0.3);
     glPopMatrix();
     glutSwapBuffers();
 }

 void motion(int x,int y)
 {
     s= 1 +(x-150)/150.0;///變大變小
     display();

 }

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);///初始化 GLUT 140
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);///143

    glutCreateWindow("GLUT Shapes");///145
    glutDisplayFunc(display);///148 display函式
    glutMotionFunc(motion);
    glutMainLoop();///174 迴圈
}


#week04_keyboard_mouse_motion
1.新增專案
2.10行程式貼上
3.修改程式











#程式碼
#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_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week04_keyboard_mouse_motion");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);

    glutMainLoop();
}

沒有留言:

張貼留言