2023年5月18日 星期四

91 week14

 step01-1:

安裝git,輸入指令如下:

cd desktop       clone網址      cd 你的資料庫    start .

freeglut直接從上周的檔案路徑設置
複製貼上week02的程式碼:
#include<GL/glut.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
#include <stdio.h>
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week14");
    glutDisplayFunc(display);
    glutMainLoop();
}

week14-1_timer
自動旋轉茶壺程式碼:
#include<GL/glut.h>
float angle=0;
void timer(int t)
{
    glutTimerFunc(500,timer,t+1);
    angle+=90;
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glRotatef(angle,0,0,1);
    glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
#include <stdio.h>
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week14");
    glutTimerFunc(3000,timer,0);
    glutDisplayFunc(display);
    glutMainLoop();
}

week14-2_timer_play:
鍵盤啟動旋轉茶壺
程式碼:
#include<GL/glut.h>
float angle=0;
void timer(int t)
{
    glutTimerFunc(33,timer,t+1);
    angle+=3;
    glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y)//step01-2
{
    glutTimerFunc(0,timer,0);//step01-2
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glRotatef(angle,0,0,1);
    glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
#include <stdio.h>
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week14");
    glutKeyboardFunc(keyboard);//step01-2
    ///glutTimerFunc(3000,timer,0);//註解掉
    glutDisplayFunc(display);
    glutMainLoop();
}
EXCEL寫公式:
alpha(0..1.0) 公式:a2/20
角度alpha*新+(1-alpha)*舊 公式:B2*D2+(1-B2)*C2


git備份設定:
git config  --global user.email 信箱
git config  --global user.name 名子git config  --global user.name SeanC91
備份:
git add .
git commit  -m week14
git push

week14-3:
滑鼠拖移橫軸左右轉,空白鍵根據滑鼠動作旋轉。
程式碼:
#include<GL/glut.h>
float angle=0,newAngle=0,oldAngle=0;
float oldX=0;
void timer(int t)
{
    if(t<100) glutTimerFunc(33,timer,t+1);
    float alpha=t/100.0;
    angle=alpha*newAngle+(1-alpha)*oldAngle;
    glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y)
{
    glutTimerFunc(0,timer,0);
}
void mouse(int button,int state,int x,int y){
    if(state==GLUT_DOWN) oldAngle=angle;
    if(state==GLUT_UP) newAngle=angle;
    oldX=x;
    glutPostRedisplay();
}
void motion(int x,int y){
    angle+=x-oldX;
    oldX=x;
    glutPostRedisplay();
}
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("week14");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    ///glutTimerFunc(3000,timer,0);
    glutDisplayFunc(display);
    glutMainLoop();
}
final_project week14-3:
程式碼:
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel*head=NULL;
GLMmodel*body=NULL;
GLMmodel*uphandr=NULL;
GLMmodel*downhandr=NULL;
int show[4]={1,1,1,1};///week14_step03_1
int ID =2;///week14_step03_1
float teapotX=0,teapotY=0;
FILE*fout=NULL;
FILE*fin=NULL;
void keyboard(unsigned char key,int x,int y)
{
    if(key=='0') ID=0;///show[0]=!show[0];
    if(key=='1') ID=1;///show[1]=!show[1];
    if(key=='2') ID=2;///show[2]=!show[2];
    if(key=='3') ID=3;///show[3]=!show[3];
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    if(head==NULL){
        head=glmReadOBJ("model/head.obj");
        body=glmReadOBJ("model/body.obj");
        uphandr=glmReadOBJ("model/uphandr.obj");
        downhandr=glmReadOBJ("model/downhandr.obj");
    }
    glPushMatrix();
        glScalef(0.3,0.3,0.3);
        glPushMatrix();
            glTranslatef(teapotX,teapotY,0);
            if(ID==0)glColor3f(1,0,0);
            else glColor3f(1,1,1);
            if(show[0]) glmDraw(head,GLM_MATERIAL);
    glPopMatrix();
     if(ID==1)glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if(show[1]) glmDraw(body,GLM_MATERIAL);
        if(ID==2)glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if(show[2]) glmDraw(uphandr,GLM_MATERIAL);
        if(ID==3)glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if(show[3]) glmDraw(downhandr,GLM_MATERIAL);
    glPopMatrix();
    glutSwapBuffers();
}
int oldX=0,oldY=0;
void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN){
        oldX=x;
        oldY=y;
    }
    display();
}
///void keyboard(unsigned char key,int x,int y)
///{
///    if(fin==NULL){
///        fclose(fout);
///       fin=fopen("file4.txt","r");
///    }
///    fscanf(fin,"%f%f",&teapotX,&teapotY);
///   display();
///}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week12");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMainLoop();
}

滑鼠控制旋轉:
week14-3FINAL程式碼:
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel*head=NULL;
GLMmodel*body=NULL;
GLMmodel*uphandr=NULL;
GLMmodel*downhandr=NULL;
int show[4]={1,1,1,1};///week14_step03_1
int ID =2;///week14_step03_1
float teapotX=0,teapotY=0;
float angle=0;
FILE*fout=NULL;
FILE*fin=NULL;
void keyboard(unsigned char key,int x,int y)
{
    if(key=='0') ID=0;///show[0]=!show[0];
    if(key=='1') ID=1;///show[1]=!show[1];
    if(key=='2') ID=2;///show[2]=!show[2];
    if(key=='3') ID=3;///show[3]=!show[3];
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    if(head==NULL){
        head=glmReadOBJ("model/head.obj");
        body=glmReadOBJ("model/body.obj");
        uphandr=glmReadOBJ("model/uphandr.obj");
        downhandr=glmReadOBJ("model/downhandr.obj");
    }
    glPushMatrix();
        glScalef(0.3,0.3,0.3);
        glPushMatrix();
            ///glTranslatef(teapotX,teapotY,0);
            if(ID==0)glColor3f(1,0,0);
            else glColor3f(1,1,1);
            if(show[0]) glmDraw(head,GLM_MATERIAL);
    glPopMatrix();
     if(ID==1)glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if(show[1]) glmDraw(body,GLM_MATERIAL);
        glPushMatrix();
        glTranslatef(-1.360000,+0.360000,0);
        glRotatef(angle,0,0,1);
        glTranslatef(1.360000,-0.360000,0);
        if(ID==2)glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if(show[2]) glmDraw(uphandr,GLM_MATERIAL);
        glPushMatrix();
        glTranslatef(-1.959999,+0.080000,0);
        glRotatef(angle,0,0,1);
        glTranslatef(1.959999,-0.080000,0);
        if(ID==3)glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if(show[3]) glmDraw(downhandr,GLM_MATERIAL);
        glPopMatrix();
        glPopMatrix();
        glPopMatrix();
        glColor3f(0,1,0);
        glutSolidTeapot(0.02);
    glutSwapBuffers();
}
int oldX=0,oldY=0;
void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN){
        oldX=x;
        oldY=y;
    }
}
void motion(int x,int y){
    teapotX+=(x-oldX)/150.0*3;
    teapotY-=(y-oldY)/150.0*3;
    printf("glTranslatef(%f,%f,0);\n",teapotX,teapotY);
    angle+=x-oldX;
    oldX=x;
    oldY=y;
    glutPostRedisplay();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week12");
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMainLoop();
}
















沒有留言:

張貼留言