2023年3月23日 星期四

Cid Week06

  Step01

先至moodle下載freeglut並解壓縮至桌面把lib資料夾內的libfreeglut.a檔案複製貼上並重新命名libglut32.a,開啟codeblocks開新專案,並複製上週程式碼後將方塊程式碼刪除,換成茶壺程式碼利用小球體讓茶壺以手柄為中心旋轉旋轉

程式碼  茶壺程式碼 小球體 以手柄為中心

#include <GL/glut.h>

float angle = 0;

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glutSolidSphere(0.01,30,30);

    glPushMatrix();

        glRotatef(angle, 0,0,1 );

        glTranslatef(0.45,0,0);

        glutSolidTeapot(0.3);

    glPopMatrix();

    glutSwapBuffers();

    angle++;

}

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

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week06");

    glutDisplayFunc(display);

    glutIdleFunc(display);

    glutMainLoop();

}

程式碼  更改掛的地方

#include <GL/glut.h>
float angle = 0;

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSolidSphere(0.01,30,30);
    glPushMatrix();
        glTranslatef(0.5,0.5,0);
        glRotatef(angle, 0,0,1 );
        glTranslatef(0.45,0,0);
        glutSolidTeapot(0.3);
    glPopMatrix();


    glutSwapBuffers();
    angle++;
}
int main(int argc, char* argv[] )
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week06");

    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop();
}

Step02

先劃出一個方塊

程式碼  方塊
#include <GL/glut.h>
float angle = 0;
void myCube()
{
    glPushMatrix();
        glScalef(1,0.3,0.3); ///大小
        glutSolidCube(0.5); ///正方形
    glPopMatrix();

}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSolidSphere(0.01,30,30);
    glPushMatrix();
        ///glTranslatef(0.5,0.5,0);
        ///glRotatef(angle, 0,0,1 );
        ///glTranslatef(0.25,0,0);
        myCube();
    glPopMatrix();


    glutSwapBuffers();
    angle++;
}
int main(int argc, char* argv[] )
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week06");

    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop();
}

讓方塊旋轉

程式碼  旋轉

#include <GL/glut.h>

float angle = 0;

void myCube()

{

    glPushMatrix();

        glScalef(1,0.3,0.3); ///大小

        glutSolidCube(0.5); ///正方形

    glPopMatrix();

}

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glutSolidSphere(0.01,30,30);

    glPushMatrix();

        ///glTranslatef(0.25,0,0);

        glRotatef(angle, 0,0,1 );

        glTranslatef(0.25,0,0);

        myCube();

    glPopMatrix();

    glutSwapBuffers();

    angle++;

}

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

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week06");

    glutDisplayFunc(display);

    glutIdleFunc(display);

    glutMainLoop();

}


 程式碼 右移

#include <GL/glut.h>

float angle = 0;

void myCube()

{

    glPushMatrix();

        glScalef(1,0.3,0.3); ///大小

        glutSolidCube(0.5); ///正方形

    glPopMatrix();

}

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glutSolidSphere(0.01,30,30);

    glPushMatrix();

        glTranslatef(0.25,0,0);

        glRotatef(angle, 0,0,1 );

        glTranslatef(0.25,0,0);

        myCube();

    glPopMatrix();

    glutSwapBuffers();

    angle++;

}

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

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week06");

    glutDisplayFunc(display);

    glutIdleFunc(display);

    glutMainLoop();

}


連接另一個方塊讓她看起來像手臂
程式碼 連接另一個方塊
#include <GL/glut.h>
float angle = 0;
void myCube()
{
    glPushMatrix();
        glScalef(1,0.3,0.3); ///大小
        glutSolidCube(0.5); ///正方形
    glPopMatrix();

}    
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSolidSphere(0.01,30,30);
    glPushMatrix();
        glTranslatef(0.25,0,0);
        glRotatef(angle, 0,0,1 );
        glTranslatef(0.25,0,0);
        myCube();
        glPushMatrix();
            glTranslatef(0.25,0,0);
                glRotatef(angle, 0,0,1 );
                glTranslatef(0.25,0,0);
                myCube();
            glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
    angle++;
}
int main(int argc, char* argv[] )
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week06");

    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop();
}


做出另一條手臂 並讓它旋轉另一個方向
程式碼 另一條手臂

#include <GL/glut.h>

float angle = 0;

void myCube()

{

    glPushMatrix();

        glScalef(1,0.3,0.3); ///大小

        glutSolidCube(0.5); ///正方形

    glPopMatrix();

}

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glutSolidSphere(0.01,30,30);

    glPushMatrix();

        glTranslatef(0.25,0,0);

        glRotatef(angle, 0,0,1 );

        glTranslatef(0.25,0,0);

        myCube();

        glPushMatrix();

            glTranslatef(0.25,0,0);

                glRotatef(angle, 0,0,1 );

                glTranslatef(0.25,0,0);

                myCube();

            glPopMatrix();

    glPopMatrix();


    glPushMatrix();

        glTranslatef(-0.25,0,0);

        glRotatef(angle, 0,0,1 );

        glTranslatef(-0.25,0,0);

        myCube();

        glPushMatrix();

            glTranslatef(-0.25,0,0);

                glRotatef(angle, 0,0,1 );

                glTranslatef(-0.25,0,0);

                myCube();

            glPopMatrix();

    glPopMatrix();

    glutSwapBuffers();

    angle++;

}

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

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week06");

    glutDisplayFunc(display);

    glutIdleFunc(display);

    glutMainLoop();

}

Step03

讓手臂能夠使用滑鼠來操控上下


程式碼  滑鼠操控 記得註解

#include <GL/glut.h>

float angle = 0;

void myCube()

{

    glPushMatrix();

        glScalef(1,0.3,0.3); ///大小

        glutSolidCube(0.5); ///正方形

    glPopMatrix();


}

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glutSolidSphere(0.01,30,30);

    glPushMatrix();

        glTranslatef(0.25,0,0);

        glRotatef(angle, 0,0,1 );

        glTranslatef(0.25,0,0);

        myCube();

        glPushMatrix();

            glTranslatef(0.25,0,0);

            glRotatef(angle, 0,0,1 );

            glTranslatef(0.25,0,0);

            myCube();

        glPopMatrix();

    glPopMatrix();


    glPushMatrix();

        glTranslatef(-0.25,0,0);

        glRotatef(-angle, 0,0,1 );

        glTranslatef(-0.25,0,0);

        myCube();

        glPushMatrix();

            glTranslatef(-0.25,0,0);

            glRotatef(-angle, 0,0,1 );

            glTranslatef(-0.25,0,0);

            myCube();

        glPopMatrix();

    glPopMatrix();

    glutSwapBuffers();

    ///angle++;

}

void motion(int x, int y)

{

    angle = x;///滑鼠motion

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

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);

    glutCreateWindow("week06");

    glutMotionFunc(motion); ///滑鼠motion

    glutDisplayFunc(display);

    glutIdleFunc(display);

    glutMainLoop();

}

用老師的網站  https://jsyeh.org/gl/   練習期中的題目

程式碼
1.  glPushMatrix();//備份矩陣
2.  glTranslatef(x,y,z);//移動  
3.    glRotatef(angle,x,y,z);//轉動  
4.    glScalef(x,y,z);//縮放  
5.    glBegin(GL_POLYGON);//開始畫  
6     glColor3f(r,g,b);//色彩.  
7.    glTexCoord2f(tx,ty);//貼圖座標  
8.    glNormal3f(nx,ny,nz);//打光的法向量  
9.    glVertex2f(x,y);//頂點  
10.   glEnd();//結束畫
11. glPopMatrix();//還原矩陣




沒有留言:

張貼留言