2021年5月26日 星期三

電腦圖學week14

1.備份main函式到notepad++


2.程式碼:

#include <GL/glut.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidSphere(0.3,30,30);///實心圓形(半徑,縱切數,橫切數)
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week14 timer");
    glutDisplayFunc(display);
    glutMainLoop();

}
glutSolidSphere(0.3,30,30);///實心圓形(半徑,縱切數,橫切數)

glueWireSphere(0.3,30,30);///線框圓形(半徑,縱切數,橫切數)


3.程式碼:
#include <GL/glut.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    ///glutSolidSphere(0.3,30,30);
    glutWireSphere(0.3,30,30);
    glutSwapBuffers();
}
void timer(int t){
    glClearColor(1,0,0,0);///清背景,紅色
    display();///重畫畫面
}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week14 timer");
    glutDisplayFunc(display);
    glutTimerFunc(3000,timer,0);
    glutMainLoop();

}

glutTimerFunc(3000,timer,0);///時間函式(等的時間,函式名稱,變數值)


4.程式碼:
#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,0,1);
        glutSolidCube(0.3);
    glPopMatrix();

    glutSwapBuffers();
}
void timer(int t)
{
    glutTimerFunc(30,timer,t+1);///設定新timer
    glClearColor(1,0,0,0);
    angle++;///逆時鐘轉動
    display();
}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week14 timer");

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

    glutMainLoop();

}
逆時鐘旋轉

5.程式碼:
#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,0,1);
        glutSolidCube(0.3);
    glPopMatrix();

    glutSwapBuffers();
}
int diff=2;
void timer(int t)
{
    glutTimerFunc(30,timer,t+1);
    glClearColor(1,0,0,0);
    if(angle>90)diff=-2;
    if(angle<0)diff=2;
    angle+=diff;
    display();
}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week14 timer");

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

    glutMainLoop();

}
正90度、負90度來回旋轉
6.程式碼:
#include <GL/glut.h>
float angle=0;
void drawArml(){
    glPushMatrix();
        glScalef(1,0.5,0.5);///細長的手臂
        glColor3f(0,1,0);glutSolidCube(0.2);///綠色手臂
    glPopMatrix();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glColor3f(1,1,1);glutSolidCube(0.4);
        glPushMatrix();
            glTranslatef(-0.2,0.2,0);///掛上肩膀
            glRotatef(angle,0,0,1);///轉動
            glTranslatef(-0.1,0,0);///旋轉中心(關節)移到畫面中心
            drawArml();///畫手臂
        glPopMatrix();
    glPopMatrix();
    ///glutSolidSphere(0.3,30,30);
    ///glutWireSphere(0.3,30,30);
    glutSwapBuffers();
}
int diff=2;
void timer(int t)
{
    glutTimerFunc(30,timer,t+1);
    glClearColor(1,0,0,0);
    if(angle>90)diff=-2;
    if(angle<0)diff=2;
    angle+=diff;
    display();
}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week14 timer");

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

    glutMainLoop();

}

















沒有留言:

張貼留言

距地表面160 Week11

 #include "glm.h" GLMmodel* pmodel = NULL; void drawmodel(void) {     if (!pmodel) { pmodel = glmReadOBJ("data/porsche.obj...