2023年5月17日 星期三

chiche_week14

 Week14

Step01

開啟程式Git-2.36.1-64-bit.exe

1.git指令
2. cd desktop
3. git clone https://github.com/chiche/2023graphicsa
4. cd 2023graphicsa
5. start .
6. git add . 

Step02

複製資料夾 final_project到桌面
開啟codeblocks

Step03

project week14-1_timer

程式碼:

#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,0,1);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void timer(int t)
{
    glutTimerFunc(500,timer,t+1);
    angle += 90;
    glutPostRedisplay();
}
int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE| GLUT_DEPTH);
    glutCreateWindow("week14");

    glutDisplayFunc(display);
    glutTimerFunc(2000,timer, 0);

    glutMainLoop();
}

Step04

新建新project week14-2_timer_play

程式碼:

#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,0,1);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void timer(int t)
{
    glutTimerFunc(500,timer,t+1);
    angle += 90;
    glutPostRedisplay();
}
void keyboard(unsigned char key, int x,int y)
{
    glutTimerFunc(0, timer, 0);
}
int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE| GLUT_DEPTH);
    glutCreateWindow("week14");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    //glutTimerFunc(2000,timer, 0);

    glutMainLoop();
}


Step05

打開Excel輸入資料

Step06

新建新project week14-3_timer_alpha_interpolation

程式碼:

#include <GL/glut.h>
float angle=0,oldAngle=0, newAngle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,0,1);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void timer(int t)
{
    if (t<100) glutTimerFunc(50,timer,t+1);
    float alpha = t/100.0;
    angle = newAngle*alpha + (1-alpha) * oldAngle;
    glutPostRedisplay();
}
void motion(int x, int y)
{
    angle = x;
    glutPostRedisplay();
}
void mouse (int button, int state,int x,int y)
{
    if(state==GLUT_DOWN) oldAngle = x;
    if(state==GLUT_UP) newAngle = x;
    glutPostRedisplay();
}
void keyboard(unsigned char key, int x,int y)
{
    glutTimerFunc(0, timer, 0);
}
int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE| GLUT_DEPTH);
    glutCreateWindow("week14");

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

    glutMainLoop();
}

Step07

備份到github

Step08

開啟上禮拜的final project

程式碼:


#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * ass = NULL;
GLMmodel * uparmR = NULL;
GLMmodel * armR = NULL;
int show[5] = {1,1,1,1,1}; ///show[1]來決定要不要顯示
int ID=0;
void keyboard(unsigned char key,int x,int y)
{
    if(key=='0') ID = 0;
    if(key=='1') ID = 1;
    if(key=='2') ID = 2;
    if(key=='3') ID = 3;
    if(key=='4') ID = 4;
    ///if(key=='0') show[0] = !show[0];
    ///if(key=='1') show[1] = !show[1];
    ///if(key=='2') show[2] = !show[2];
    ///if(key=='3') show[3] = !show[3];
    ///if(key=='4') show[4] = !show[4];
    glutPostRedisplay();
}
FILE * fout = NULL;
FILE * fin = NULL;
float teapotX=0 , teapotY=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glScalef(0.2,0.2,0.2);
        if(body==NULL){
            head = glmReadOBJ("model/head.obj");
            body = glmReadOBJ("model/body.obj");
            ass = glmReadOBJ("model/ass.obj");
            uparmR = glmReadOBJ("model/uparmR.obj");
            armR = glmReadOBJ("model/armR.obj");
        }
        if (ID==0) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if (show[0]) glmDraw(head, GLM_MATERIAL);

        if (ID==1) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if (show[1]) glmDraw(body, GLM_MATERIAL);

        glPushMatrix();
            glTranslatef(teapotX,teapotY,0);
            if (ID==2) glColor3f(1,0,0);
            else glColor3f(1,1,1);
            if (show[2]) glmDraw(ass, GLM_MATERIAL);
        glPopMatrix();

        if (ID==3) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if (show[3]) glmDraw(uparmR, GLM_MATERIAL);

        if (ID==4) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if (show[4]) glmDraw(armR, GLM_MATERIAL);
    glPopMatrix();
    glutSwapBuffers();
}
int oldX = 0 ,oldY = 0;
void motion(int x, int  y)
{
    teapotX += (x - oldX)/150.0;
    teapotY -= (y - oldY)/150.0;
    oldX = x;
    oldY = y;
    printf("glTranslatef(%f, %f, 0);\n",teapotX,teapotY);
    glutPostRedisplay();
}
void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN)
    {
        teapotX = (x-150)/150.0;
        teapotY = (150-y)/150.0;
        if(fout==NULL) fout = fopen("file4.txt","w");
        fprintf(fout, "%f %f\n",teapotX , teapotY);
    }
    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);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);

    glutMainLoop();
}


Step09

程式碼:


#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * ass = NULL;
GLMmodel * uparmR = NULL;
GLMmodel * armR = NULL;
int show[5] = {0,0,1,0,0}; ///show[1]來決定要不要顯示
int ID = 2;
void keyboard(unsigned char key,int x,int y)
{
    if(key=='0') ID = 0;
    if(key=='1') ID = 1;
    if(key=='2') ID = 2;
    if(key=='3') ID = 3;
    if(key=='4') ID = 4;
    ///if(key=='0') show[0] = !show[0];
    ///if(key=='1') show[1] = !show[1];
    ///if(key=='2') show[2] = !show[2];
    ///if(key=='3') show[3] = !show[3];
    ///if(key=='4') show[4] = !show[4];
    glutPostRedisplay();
}
FILE * fout = NULL;
FILE * fin = NULL;
float teapotX=0 , teapotY=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glScalef(0.2,0.2,0.2);
        if(body==NULL){
            head = glmReadOBJ("model/head.obj");
            body = glmReadOBJ("model/body.obj");
            ass = glmReadOBJ("model/ass.obj");
            uparmR = glmReadOBJ("model/uparmR.obj");
            armR = glmReadOBJ("model/armR.obj");
        }
        if (ID==0) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if (show[0]) glmDraw(head, GLM_MATERIAL);

        if (ID==1) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if (show[1]) glmDraw(body, GLM_MATERIAL);

        glPushMatrix();
            ///glTranslatef(teapotX,-teapotY,0);
            glTranslatef(teapotX,teapotY,0);

            if (ID==2) glColor3f(1,0,0);
            else glColor3f(1,1,1);
            if (show[2]) glmDraw(ass, GLM_MATERIAL);
        glPopMatrix();

        if (ID==3) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if (show[3]) glmDraw(uparmR, GLM_MATERIAL);

        if (ID==4) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if (show[4]) glmDraw(armR, GLM_MATERIAL);
    glPopMatrix();
    glColor3f(0, 1, 0);
    glutSolidTeapot(0.02);
    glutSwapBuffers();
}
int oldX = 0 ,oldY = 0;
void motion(int x, int  y)
{
    teapotX += (x - oldX)/150.0;
    teapotY -= (y - oldY)/150.0;
    oldX = x;
    oldY = y;
    printf("glTranslatef(%f, %f, 0);\n",teapotX,teapotY);
    glutPostRedisplay();
}
void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN)
    {
        teapotX = (x-150)/150.0;
        teapotY = (150-y)/150.0;
        if(fout==NULL) fout = fopen("file4.txt","w");
        fprintf(fout, "%f %f\n",teapotX , teapotY);
    }
    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);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);

    glutMainLoop();
}


Step10

程式碼:

#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * ass = NULL;
GLMmodel * uparmR = NULL;
GLMmodel * armR = NULL;
int show[5] = {0,0,1,0,0}; ///show[1]來決定要不要顯示
int ID = 2;
void keyboard(unsigned char key,int x,int y)
{
    if(key=='0') ID = 0;
    if(key=='1') ID = 1;
    if(key=='2') ID = 2;
    if(key=='3') ID = 3;
    if(key=='4') ID = 4;
    ///if(key=='0') show[0] = !show[0];
    ///if(key=='1') show[1] = !show[1];
    ///if(key=='2') show[2] = !show[2];
    ///if(key=='3') show[3] = !show[3];
    ///if(key=='4') show[4] = !show[4];
    glutPostRedisplay();
}
FILE * fout = NULL;
FILE * fin = NULL;
float angle=0;
float teapotX=0 , teapotY=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glScalef(0.2,0.2,0.2);
        if(body==NULL){
            head = glmReadOBJ("model/head.obj");
            body = glmReadOBJ("model/body.obj");
            ass = glmReadOBJ("model/ass.obj");
            uparmR = glmReadOBJ("model/uparmR.obj");
            armR = glmReadOBJ("model/armR.obj");
        }
        if (ID==0) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if (show[0]) glmDraw(head, GLM_MATERIAL);

        if (ID==1) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if (show[1]) glmDraw(body, GLM_MATERIAL);

        glPushMatrix();
            ///glTranslatef(-0.006667, 1.093333, 0);
            glRotatef(angle, 0, 0, 1);
            glTranslatef(0.013333, 0.646666, 0);
            ///glTranslatef(teapotX,-teapotY,0);
            ///glTranslatef(teapotX,teapotY,0);

            if (ID==2) glColor3f(1,0,0);
            else glColor3f(1,1,1);
            if (show[2]) glmDraw(ass, GLM_MATERIAL);
        glPopMatrix();

        if (ID==3) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if (show[3]) glmDraw(uparmR, GLM_MATERIAL);

        if (ID==4) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if (show[4]) glmDraw(armR, GLM_MATERIAL);
    glPopMatrix();
    glColor3f(0, 1, 0);
    glutSolidTeapot(0.02);
    glutSwapBuffers();
}
int oldX = 0 ,oldY = 0;
void motion(int x, int  y)
{
    teapotX += (x - oldX)/150.0;
    teapotY -= (y - oldY)/150.0;
    oldX = x;
    oldY = y;
    angle = x;
    printf("glTranslatef(%f, %f, 0);\n",teapotX,teapotY);
    glutPostRedisplay();
}
void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN)
    {
        teapotX = (x-150)/150.0;
        teapotY = (150-y)/150.0;
        if(fout==NULL) fout = fopen("file4.txt","w");
        fprintf(fout, "%f %f\n",teapotX , teapotY);
    }
    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);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);

    glutMainLoop();
}


Step11

程式碼:

#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * ass = NULL;
GLMmodel * uparmR = NULL;
GLMmodel * armR = NULL;
int show[5] = {0,0,0,0,1}; ///show[1]來決定要不要顯示
int ID = 4;
void keyboard(unsigned char key,int x,int y)
{
    if(key=='0') ID = 0;
    if(key=='1') ID = 1;
    if(key=='2') ID = 2;
    if(key=='3') ID = 3;
    if(key=='4') ID = 4;
    ///if(key=='0') show[0] = !show[0];
    ///if(key=='1') show[1] = !show[1];
    ///if(key=='2') show[2] = !show[2];
    ///if(key=='3') show[3] = !show[3];
    ///if(key=='4') show[4] = !show[4];
    glutPostRedisplay();
}
FILE * fout = NULL;
FILE * fin = NULL;
float angle=0;
float teapotX=0 , teapotY=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glScalef(0.2,0.2,0.2);
        if(body==NULL){
            head = glmReadOBJ("model/head.obj");
            body = glmReadOBJ("model/body.obj");
            ass = glmReadOBJ("model/ass.obj");
            uparmR = glmReadOBJ("model/uparmR.obj");
            armR = glmReadOBJ("model/armR.obj");
        }
        if (ID==0) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if (show[0]) glmDraw(head, GLM_MATERIAL);

        if (ID==1) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if (show[1]) glmDraw(body, GLM_MATERIAL);

        glPushMatrix();///屁股
            ///glTranslatef(-0.013333, 0.646666, 0);
            glRotatef(angle, 0, 0, 1);
            glTranslatef(0.013333, -0.646666, 0);
            ///glTranslatef(teapotX,-teapotY,0);
            ///glTranslatef(teapotX,teapotY,0);

            if (ID==2) glColor3f(1,0,0);
            else glColor3f(1,1,1);
            if (show[2]) glmDraw(ass, GLM_MATERIAL);
        glPopMatrix();

            if (ID==3) glColor3f(1,0,0);
            else glColor3f(1,1,1);
            if (show[3]) glmDraw(uparmR, GLM_MATERIAL);

        glPushMatrix(); ///下手臂
            glTranslatef(-1.946665, +0.120001, 0);
            glRotatef(angle, 0, 0, 1);
            glTranslatef(1.946665, -0.120001, 0);
            ///glTranslatef(teapotX,teapotY,0);
            if (ID==4) glColor3f(1,0,0);
            else glColor3f(1,1,1);
            if (show[4]) glmDraw(armR, GLM_MATERIAL);
        glPopMatrix();

    glPopMatrix();
    glColor3f(0, 1, 0);
    glutSolidTeapot(0.02);
    glutSwapBuffers();
}
int oldX = 0 ,oldY = 0;
void motion(int x, int  y)
{
    teapotX += (x - oldX)/150.0;
    teapotY -= (y - oldY)/150.0;
    oldX = x;
    oldY = y;
    angle = x;
    printf("glTranslatef(%f, %f, 0);\n",teapotX,teapotY);
    glutPostRedisplay();
}
void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN)
    {
        teapotX = (x-150)/150.0;
        teapotY = (150-y)/150.0;
        if(fout==NULL) fout = fopen("file4.txt","w");
        fprintf(fout, "%f %f\n",teapotX , teapotY);
    }
    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);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);

    glutMainLoop();
}


Step12

上傳到github








沒有留言:

張貼留言