2021年3月29日 星期一

是我是我詐騙 Week06

 

🎡🎠階層轉動🎠🎡


1.做一個長方形,用滑鼠讓它旋轉


程式碼如下:


#include <GL/glut.h>
#include <stdio.h>
float vx[2000],vy[2000];  ///準備一個頂點
int N=0;        ///有N個頂點
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();    ///備份矩陣
        glRotatef(angle,0,0,1);   ///旋轉,對Z軸轉
        glScalef(0.5,0.1,0.1);    ///調成細長的
        glColor3f(0.3,0.3,1.0);    ///藍色
        glutSolidCube(1);    ///方塊

    glPopMatrix();
    glutSwapBuffers();
}
void motion (int x,int y)
{
    angle++;       ///只要mouse在motion時,就會增加角度
    display();    ///增加後,重畫畫面
}
void keyboard(unsigned char key,int x,int y)
{

}
int main(int argc,char ** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("08160554");
    glutDisplayFunc(display);
   // glutKeyboardFunc(keyboard);      ///按下去彈起來
    glutMotionFunc(motion);    ///mouse motion 在動
    glutMainLoop();
}





2.做一個正方形,讓它旋轉



程式碼如下:


#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle++,0,0,1);   ///要轉動angle
        glutSolidCube(1);
    glPopMatrix();
    glutSwapBuffers();
}

int main(int argc,char ** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("08160554");
    glutDisplayFunc(display); 
    glutIdleFunc(display);   ///idle很閒的時候,就重畫
    glutMainLoop();
}




3.變成細長方型,並改變中心




程式碼如下:


#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        ///glTranslatef(0,0,0);    ///把正確轉動的手臂掛在肩上
        ///glRotatef(angle++,0,0,1);  ///轉動
        glTranslatef(-0.25,0,0);   ///將旋轉中心放在正中心
        glScalef(0.5,0.1,0.1);   ///細細長長的
        glutSolidCube(1);   ///方塊
    glPopMatrix();
    glutSwapBuffers();
}

int main(int argc,char ** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("08160554");
    glutIdleFunc(display);
    glutDisplayFunc(display);
    glutMainLoop();
}




4.中心是頭,旋轉




程式碼如下:


#include <GL/glut.h>
float angle=0;
void hand()
{
    glPushMatrix();
        glScalef(0.5,0.1,0.1);
        glutSolidCube(1);
    glPopMatrix();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(-0.25,0,0);   ///把正確轉動的手臂掛在肩上
        glRotatef(angle++,0,0,1);   ///轉動
        glTranslatef(-0.25,0,0);
        hand();
    glPopMatrix();
    hand();
    glutSwapBuffers();
}

int main(int argc,char ** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("08160554");
    glutIdleFunc(display);
    glutDisplayFunc(display);
    glutMainLoop();
}



4.做上臂跟下臂,並旋轉💪



程式碼如下:


#include <GL/glut.h>
float angle=0;
void hand()
{
    glPushMatrix();
        glScalef(0.5,0.1,0.1);
        glutSolidCube(1);
    glPopMatrix();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(-0.25,0,0);   ///手臂掛上去肩關節
        glRotatef(angle,0,0,1);    ///轉動(這裡沒有++)
        glTranslatef(-0.25,0,0);   ///將旋轉中心放在正中心
        hand();   ///上手臂
        glPushMatrix();
            glTranslatef(-0.25,0,0);   ///把正確轉動的手掛在肩上
            glRotatef(angle,0,0,1);    ///轉動
            glTranslatef(-0.25,0,0);   ///將旋轉中心放在正中心
            hand();   ///下手臂
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
    angle++;   ///要在外面++
}

int main(int argc,char ** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("08160554");
    glutIdleFunc(display);
    glutDisplayFunc(display);
    glutMainLoop();
}



4.做兩個(改變正負)



程式碼如下:


#include <GL/glut.h>
float angle=0;
void hand()
{
    glPushMatrix();
        glScalef(0.5,0.1,0.1);
        glutSolidCube(1);
    glPopMatrix();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(-0.25,0,0);
        glRotatef(angle,0,0,1);
        glTranslatef(-0.25,0,0);
        hand();
        glPushMatrix();
            glTranslatef(-0.25,0,0);
            glRotatef(angle,0,0,1);
            glTranslatef(-0.25,0,0);
            hand();
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glTranslatef(+0.25,0,0);
        glRotatef(-angle,0,0,1);
        glTranslatef(+0.25,0,0);
        hand();
        glPushMatrix();
            glTranslatef(+0.25,0,0);
            glRotatef(-angle,0,0,1);
            glTranslatef(+0.25,0,0);
            hand();
        glPopMatrix();
    glPopMatrix();

    glutSwapBuffers();
    angle++;
}

int main(int argc,char ** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("08160554");
    glutIdleFunc(display);
    glutDisplayFunc(display);
    glutMainLoop();
}


5.作業


程式碼如下:

#include <GL/glut.h>
#include <math.h>
float angle=0;
void hand()
{
    glPushMatrix();
        glScalef(0.2,0.2,0.2);
        glutSolidCube(1);
    glPopMatrix();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1,1,1);
    glPushMatrix();
        glTranslatef(-0.2,0,0);
        glRotatef(angle,0,0,1);
        glTranslatef(-0.2,0,0);
        hand();
        glPushMatrix();
            glTranslatef(-0.1,0,0);
            glRotatef(angle,0,0,1);
            glTranslatef(-0.1,0,0);
            hand();

            glTranslatef(-0.11,0,0);
            glRotatef(angle,0,0,1);
            glTranslatef(-0.11,0,0);
            hand();
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glTranslatef(+0.2,0,0);
        glRotatef(angle,0,0,1);
        glTranslatef(+0.2,0,0);
        hand();
        glPushMatrix();
            glTranslatef(+0.1,0,0);
            glRotatef(angle,0,0,1);
            glTranslatef(+0.1,0,0);
            hand();

            glTranslatef(+0.11,0,0);
            glRotatef(angle,0,0,1);
            glTranslatef(+0.11,0,0);
            hand();
        glPopMatrix();
    glPopMatrix();

    glColor3f(0,1,1);
    glBegin(GL_POLYGON);
        glVertex3f( 0.3, 0.2,-0.6);
        glVertex3f( 0.3,-0.9,-0.6);
        glVertex3f(-0.3,-0.9,-0.6);
        glVertex3f(-0.3, 0.2,-0.6);

    glEnd();
    glColor3f(0,1,1);
    glBegin(GL_POLYGON);
        glVertex3f( 0.2, 0.5,-0.6);
        glVertex3f( 0.2,-0.2,-0.6);
        glVertex3f(-0.2,-0.2,-0.6);
        glVertex3f(-0.2, 0.5,-0.6);
    glEnd();

    glColor3f(0,0,0);
    glBegin(GL_POLYGON);
        glVertex3f( -0.05, 0.3,-0.2);
        glVertex3f(-0.05,0.4,-0.2);
        glVertex3f(-0.15,0.4,-0.2);
        glVertex3f(-0.15, 0.3,-0.2);
    glEnd();
    glColor3f(0,0,0);
    glBegin(GL_POLYGON);
        glVertex3f( +0.05, 0.3,-0.2);
        glVertex3f(+0.05,0.4,-0.2);
        glVertex3f(+0.15,0.4,-0.2);
        glVertex3f(+0.15, 0.3,-0.2);
    glEnd();

    glutSwapBuffers();
    angle++;
}

int main(int argc,char ** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("08160554");
    glutIdleFunc(display);
    glutDisplayFunc(display);
    glutMainLoop();
}










沒有留言:

張貼留言

Week18期末作業(橘貓的跳舞熊熊)

 期末作業(橘貓的跳舞熊熊) 影片: https://youtu.be/R89tptMaQZw 程式碼: #include <opencv/highgui.h> #include <opencv/cv.h> #include <GL/glut.h...