2022年5月17日 星期二

(圖學筆記)電腦圖學Week13

 Week13


step01-1打開codeblocks並將十行程式碼填入

step01-2 glRectf加入程式


step01-3加入紅色手臂,並讓其旋轉


step01-4 加入利用滑鼠轉動手臂

main函式加上這兩行

step01-5 加入綠色手臂,手肘不能斷
step01-6 加入左手

step02-1 加入第二個角度(每個關節都要)
創造陣列,加入ketboard函式

更改裡面的函式

不加入這行不能操作


程式碼
#include <GL/glut.h>
float angle[20],oldx=0;
int angleID=0;
void keyboard(unsigned char key,int x,int y)
{
    if(key=='0')angleID=0;
    if(key=='1')angleID=1;
    if(key=='2')angleID=2;
    if(key=='3')angleID=3;

}
void mouse(int button,int state,int x,int y)
{
    oldx=x;
}
void motion(int x,int y)
{
    angle[angleID]+=(x-oldx);
    oldx=x;
    glutPostRedisplay();///請glut重畫畫面
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); ///畫圖前,清畫面
    glColor3f(1,1,1);///白色方塊
    glRectf(0.3,0.5,-0.3,-0.5);///四邊形
    ///右
    glPushMatrix();
        glTranslatef(0.3,0.4,0);///整個手臂接回身體
        glRotatef(angle[0],0,0,1);///對z軸旋轉
        glTranslatef(-0.3,-0.4,0);///把手臂旋轉中心放在世界中心
        glColor3f(1,0,0);///red
        glRectf(0.3,0.5,0.7,0.3);///想像他是手臂
        glPushMatrix();
            glTranslatef(0.7,0.4,0);///整個手臂接回身體
            glRotatef(angle[1],0,0,1);///對z軸旋轉
            glTranslatef(-0.7,-0.4,0);///把手臂旋轉中心放在世界中心
            glColor3f(0,1,0);///red
            glRectf(0.7,0.5,1.0,0.3);///想像他是手臂
        glPopMatrix();
    glPopMatrix();
    ///左
    glPushMatrix();
        glTranslatef(-0.3,0.4,0);///整個手臂接回身體
        glRotatef(-angle[2],0,0,1);///對z軸旋轉
        glTranslatef(0.3,0.4,0);///把手臂旋轉中心放在世界中心
        glColor3f(1,0,0);///red
        glRectf(-0.3,-0.5,-0.7,-0.3);///想像他是手臂
        glPushMatrix();
            glTranslatef(-0.7,-0.4,0);///整個手臂接回身體
            glRotatef(-angle[3],0,0,1);///對z軸旋轉
            glTranslatef(0.7,0.4,0);///把手臂旋轉中心放在世界中心
            glColor3f(0,1,0);///red
            glRectf(-0.7,-0.5,-1.0,-0.3);///想像他是手臂
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("第13的程式trt");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);
    glutMainLoop();
}


沒有留言:

張貼留言

VERY BEAUTIFUL, VERY POWERFUL

一.     一樣先安裝且設定好freeglut,OpecCV, 開啟CodeBlocks建立新專案 week11_gundam,                 把 MyGundam.zip下載解壓縮後的data資料夾放到freeglut/bin裡面 把week09_openc...