2023年5月17日 星期三

電腦圖學課程week14 叡

 

Week14-1_timer

開啟GLUT專案
打下列程式碼

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

可以用鍵盤去操控



Week14-2_timer_alpha_interpolation


#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);///用keyboard()
    ///glutTimerFunc(2000,timer,0);
    glutMainLoop();
}


Week14-3



#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)///timer()函式,做對應動作
{
    if(t<100) glutTimerFunc(50,timer,t+1);
    float alpha = t/100.0;///變成float值介於0.00~1.00之間
    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);///設定timer函式

}
int main(int argc,char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
    glutCreateWindow("week14");

    glutDisplayFunc(display);
    glutMouseFunc(mouse);///按下去表示起點
    glutMotionFunc(motion);///當mouse在motion時,即時更新畫面
    glutKeyboardFunc(keyboard);///keyboard()

    glutMainLoop();
}

可以用滑鼠去操控


#include <stdio.h>

#include <GL/glut.h>

#include "glm.h"///week13-1

GLMmodel * head =NULL;///week13-1

GLMmodel * uparmR =NULL;///week13-1

GLMmodel * lowarmR =NULL;///week13-1

GLMmodel * body =NULL;///week13-1

int show[4]={1,1,1,1};///week13-3///week14 3-1

int ID = 0;///week14 3-1 //0頭 1身體 2上手臂 3下手臂

void keyboard(unsigned char key,int x, int y){///week13-3

    if(key=='0') ID = 0;///week14 3-1

    if(key=='1') ID = 1;///week14 3-1

    if(key=='2') ID = 2;///week14 3-1

    if(key=='3') ID = 3;///week14 3-1

    ///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];

    glutPostRedisplay();

}///原來的keyboard先註解

FILE * fout=NULL;///step2-2

FILE * fin =NULL;///2

float teapotX=0,teapotY=0;

float angle=0,angle2=0,angle3=0;

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();

        glScalef(0.2,0.2,0.2);///week13-2

        if(body==NULL){///week13-1

            head = glmReadOBJ("model/head.obj");///week13-2

            body = glmReadOBJ("model/body.obj");///week13-1

            uparmR = glmReadOBJ("model/uparmR.obj");///week13-3

            lowarmR = glmReadOBJ("model/lowarmR.obj");///week13-3

            ///glmUnitize(body);///week13-1

        }

        if(ID==0)glColor3f(1,0,0);///week14 3-1

        else glColor3f(1,1,1);///week14 3-1

        if(show[0]) glmDraw(body, GLM_MATERIAL);///week13-1


        if(ID==1)glColor3f(1,0,0);///week14 3-1

        else glColor3f(1,1,1);///week14 3-1

        if(show[1]) glmDraw(head, GLM_MATERIAL);///week13-2


        glPushMatrix();///week13-4

            glTranslatef(teapotX,teapotY,0);///week13-4


            if(ID==2)glColor3f(1,0,0);///week14 3-1

            else glColor3f(1,1,1);///week14 3-1

            if(show[2]) glmDraw(uparmR, GLM_MATERIAL);///week13-3

        glPopMatrix();///week13-4


        if(ID==3)glColor3f(1,0,0);///week14 3-1

        else glColor3f(1,1,1);///week14 3-1

        if(show[3]) glmDraw(lowarmR, GLM_MATERIAL);///week13-3

    glPopMatrix();

    glutSwapBuffers();

}

int oldX=0,oldY=0;///week13-4

void motion (int x,int y){///week13-4

    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){

        oldX = x;///week13-4teapotX = (x-150)/150.0;

        oldY = y;///week13-4teapotY = (150-y)/150.0;

        angle =x;///week13-4

        ///if(fout==NULL)fout =fopen("file4.txt","w");week12

        ///fprintf(fout,"%f %f\n",teapotX,teapotY);

    }

    display();

}

int main(int argc, char *argv[])

{

glutInit(&argc, argv);

glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH);

glutCreateWindow("week13");

glutDisplayFunc(display);

glutMouseFunc(mouse);

glutMotionFunc(motion);///week13-4

glutKeyboardFunc(keyboard);///2keboard要做事了

        glutMainLoop();

}











沒有留言:

張貼留言