顯示具有 week13 標籤的文章。 顯示所有文章
顯示具有 week13 標籤的文章。 顯示所有文章

2022年6月25日 星期六

電腦圖學week13課堂筆記

今日課堂內容 : (1) glRectf()函式 和 glRectf()做的手臂
1. glRectf() 四個函數分別為 (1)右上X,右上Y,左上X,左上Y
第一個程式 : 
#include <GL/glut.h>
float angle=45;
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,1);
        glTranslatef(-0.3, -0.4, 0);
        glColor3f(1,0,0);
        glRectf( 0.3, 0.5,  0.7, 0.3);
    glPopMatrix();
    glutSwapBuffers();
}
2. glRectf()加上手臂
第二個程式 :
#include <GL/glut.h>
float angle=0, oldX=0;
void mouse(int button, int state, int x, int y)
{
    oldX = x;
}
void motion(int x, int y)
{
    angle += (x-oldX);
    oldX = x;
    glutPostRedisplay();
}
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,1);
        glTranslatef(-0.3, -0.4, 0);
        glColor3f(1,0,0);
        glRectf( 0.3, 0.5,  0.7, 0.3);
        glPushMatrix();
            glTranslatef( x, y, z);
            glRotatef(angle, 0,0,1);
            glTranslatef(x2,y2,z2);
            glColor3f(0,1,0);
            glRectf( 0.7, 0.5, 1.0, 0.3);
        glPopMatrix();
    glPopMatrix();
    glutSwapBuffers();
}
 最後的程式碼 : 
#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();
}
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);
        glTranslatef(-0.3,-0.4,0);
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);
        glPushMatrix();
            glTranslatef(0.7,0.4,0);
            glRotatef(angle[1],0,0,1);
            glTranslatef(-0.7,-0.4,0);
            glColor3f(0,1,0);
            glRectf(0.7,0.5,1.0,0.3);
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.3,0.4,0);
        glRotatef(-angle[2],0,0,1);
        glTranslatef(0.3,0.4,0);
        glColor3f(1,0,0);
        glRectf(-0.3,-0.5,-0.7,-0.3);
        glPushMatrix();
            glTranslatef(-0.7,-0.4,0);
            glRotatef(-angle[3],0,0,1);
            glTranslatef(0.7,0.4,0);
            glColor3f(0,1,0);
            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("week13");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);
    glutMainLoop();
}







2022年6月8日 星期三

Ru的電腦圖學筆記>

 

Step01

    1.利用 glRectf(x1,y1, x2,y2) 方塊,簡化程式,示範TRT的觀念

        -新增 GLUT 專案,檔名:week13_rect_TRT

        -貼上 GLUT 十行程式,不用茶壺,改 glRectf()

    2.把紅色的手加上去,讓他可以轉動


    3.依序把 T-R-T 寫出來

        -先寫最下面的 Translate 把手臂的旋轉中心放到世界的中心

        -加上 float angle=45 可以看到手臂在肚臍的地方轉45度

        -最後調整最下面的 Translate 把手臂的位置移到原來的地方

Step02

    1.用 mouse motion 來改變手臂的角度

    2.新增 GLUT 專案,檔名:week13_rect_TRT_TRT

        -沿用前面的程式

        -增加第二個關節

    3.照著把下手肘的T-R-T依序做出來

Step03

    1.新增 GLUT 專案,檔名:week13_rect_many_TRT

        -做出另一隻手的關節

    2.使用鍵盤keyboard()來切換 angleID


 完成!!💝💝💝💝💝💝💝

2022年5月17日 星期二

A

 WEEK13

1.先寫入

void display(){

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glutSwapBuffers();

}


int main(int argc,char** argv){

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);

    glutInitWindowSize(600,600);

    glutCreateWindow("week13_RECT_TRT");


    glutDisplayFunc(display);

    glutMainLoop();

}

2.在display()加    glRectf(0.3,0.5,-0.3,-0.5);//矩形

3.再加(畫手臂)glPushMatrix();glColor3f(1,0,0);///紅 glRectf(0.3,0.5,0.7,0.3);///手臂 glPopMatrix();
4.把手臂旋轉中心放到身體中心 glTranslatef(-0.3,-0.4,0);///把手臂旋轉中心放到中心
5.旋轉手臂角度float angle=45;glRotatef(angle,0,0,1);///旋轉
6.手臂掛回身體(右上) glTranslatef(0.3,0.4,0);///手臂掛到身體
7.讓手臂可以轉動(滑鼠控制)float oldX=0;void mouse(int button,int state,int x,int y){ oldX=x;}
void motion(int x,int y){ angle+=(x-oldX); oldX=x; glutPostRedisplay();///重畫畫面}
main加入glutMouseFunc(mouse);glutMotionFunc(motion);



1.加在上個glutPushMatrix()裡 glPushMatrix(); glRotatef(angle,0,0,1);///旋轉  glTranslatef(-0.3,-0.4,0);///把手臂旋轉中心放到中心  glColor3f(0,1,0);  glRectf(0.7,0.5,1.0,0.3);///手臂 glPopMatrix();
2.加入glTranslatef(0.7,.4,0);




1.複製glPushMatrix()程式,把X值數值設為負數(因為對稱在反方向)
glPushMatrix();
        glTranslatef(-0.3,0.4,0);///手臂掛到身體
        glRotatef(angle,0,0,1);///旋轉
        glTranslatef(0.3,-0.4,0);///把手臂旋轉中心放到中心
        glColor3f(1,0,0);///紅
        glRectf(-0.3,0.5,-0.7,0.3);///上手臂

        glPushMatrix();
            glTranslatef(-0.7,.4,0);
            glRotatef(angle,0,0,1);///旋轉
            glTranslatef(0.7,-0.4,0);///把手臂旋轉中心放到中心
            glColor3f(0,1,0);///紅
            glRectf(-0.7,0.5,-1.0,0.3);///下手臂
        glPopMatrix();
    glPopMatrix();
2.利用鍵盤和滑鼠搭配指定關節
float angle[20];
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;
}
0對應上右臂;1對應下右臂;2對應上左臂;3對應下左臂;
motion裡面angle+=(x-oldX);改成angle[angleID]+=(x-oldX);
glPushMatrix()的angle改成angle[0],angle[1],angle[2],angle[3]
main()加入glutKeyboardFunc(keyboard);

程式碼

#include <GL/glut.h>
float angle=0; void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,1); ///白色
glutSolidTeapot(0.3);///茶壺身體
glPushMatrix();
glTranslatef(0.2,0,0);///3.將茶壺手臂往右邊移動
glRotatef(angle,0,0,1); ///2.旋轉
glTranslatef(0.2,0,0);/// 1.把茶壺旋轉中心放到世界中心
glColor3f(1,0,0);///紅色
glutSolidTeapot(0.2);///茶壺手臂
glPushMatrix();
glTranslatef(0.2,0,0);///3.將茶壺手臂往右邊移動
glRotatef(angle,0,0,1); ///2.旋轉
glTranslatef(0.2,0,0);/// 1.把茶壺旋轉中心放到世界中心
glColor3f(1,0,0);///紅色
glutSolidTeapot(0.2);///茶壺手臂
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2,0,0);///3.將茶壺手臂往右邊移動
glRotatef(-angle,0,0,1); ///2.旋轉
glTranslatef(-0.2,0,0);/// 1.把茶壺旋轉中心放到世界中心
glColor3f(1,0,0);///紅色
glutSolidTeapot(0.2);///茶壺手臂
glPushMatrix();
glTranslatef(-0.2,0,0);///3.將茶壺手臂往右邊移動
glRotatef(-angle,0,0,1); ///2.旋轉
glTranslatef(-0.2,0,0);/// 1.把茶壺旋轉中心放到世界中心
glColor3f(1,0,0);///紅色
glutSolidTeapot(0.2);///茶壺手臂
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
angle+=0.05;
}
int main(int argc, char**argv)
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week12 TRT");
glutIdleFunc(display);
glutDisplayFunc(display);
glutMainLoop();
}





AAA的圖學筆記

 Week13 TRT

1. 示範複習作業/考試的 TRT(實際例子)

2. TRT 的R角度

3. 利用 keyboard/mouse 來改變

4. 如何轉正模型、如何調整模型大小
STEP01
先新增一個四邊形

#include <GL/glut.h>


void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glRectf( 0.3,0.5,-0.3,-0.5);///四邊形

    glutSwapBuffers();

}

int main(int argc, char**argv)

{

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutInitWindowSize(600,600);///改視窗大小

    glutCreateWindow("week13_rect_TRT");

    glutDisplayFunc(display);

    glutMainLoop();

}


之後再增加手臂
#include <GL/glut.h>

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();
        glColor3f(1,0,0);///紅色
        glRectf(0.3,0.5,0.7,0.3);///右邊手臂
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("week13_rect_TRT");
    glutDisplayFunc(display);
    glutMainLoop();
}
讓手臂在正確的位置
#include <GL/glut.h>

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);///(3)手臂調整到要的位置
        glRotatef(45,0,0,1);///(2)旋轉 對Z軸轉
        glTranslatef(-0.3,-0.4,0);///(1)把手臂選轉中心放世界中心
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("week13_rect_TRT");
    glutDisplayFunc(display);
    glutMainLoop();
}


STEP02



加上mouse指令
#include <GL/glut.h>
float angle=45,oldX=0;
void mouse(int Button, int state, int x, int y){
    oldX= x;
}
void motion(int x,int y){
    angle += (x-oldX);
    oldX = x;
    glutPostRedisplay();
}
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,1);    ///旋轉
        glTranslatef(-0.3,-0.4,0); ///手臂的旋轉中心

        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13 rect TRT");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}
再加上motion指令

#include <GL/glut.h>
float angle=45 , oldx=0;
void mouse(int button , int state , int x ,int y)
{
    oldx=x;
}
void motion (int x, int y)
{
    angle+=(x-oldx); ///每次轉換要減去原本角度
    oldx=x;///舊的變新的
    glutPostRedisplay();///請glut重新re display
}
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,1);///旋轉 對Z軸轉
        glTranslatef(-0.3,-0.4,0);///把手臂選轉中心放世界中心
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(600,600);
    glutCreateWindow("week13_rect_TRT");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);///motion用
    glutDisplayFunc(display);
    glutMainLoop();

新增一個左手
#include <GL/glut.h>
float angle=0,oldX=0;
void mouse(int Button, int state, int x, int y){
    oldX= x;
}
void motion(int x,int y){
    angle += (x-oldX);
    oldX = x;
    glutPostRedisplay();
}
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,1);    ///旋轉
        glTranslatef(-0.3,-0.4,0); ///手臂的旋轉中心
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);

        glPushMatrix();
            glTranslatef(0.7,0.4,0); ///把手臂放回身體
            glRotatef(angle,0,0,1);    ///旋轉
            glTranslatef(-0.7,-0.4,0); ///手臂的旋轉中心
            glColor3f(0,1,0);
            glRectf(0.7,0.5,1,0.3);
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-0.3,0.4,0); ///把手臂放回身體
        glRotatef(angle,0,0,1);    ///旋轉
        glTranslatef(0.3,-0.4,0); ///手臂的旋轉中心
        glColor3f(1,0,0);
        glRectf(-0.3,0.5,-0.7,0.3);

        glPushMatrix();
            glTranslatef(-0.7,0.4,0); ///把手臂放回身體
            glRotatef(angle,0,0,1);    ///旋轉
            glTranslatef(0.7,-0.4,0); ///手臂的旋轉中心
            glColor3f(0,1,0);
            glRectf(-0.7,0.5,-1,0.3);
        glPopMatrix();
    glPopMatrix();


    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13 rect TRT");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}

更改角度

#include <GL/glut.h>
float angle[20],oldX=0;
int angleID=0;
void mouse(int Button, int state, int x, int y){
    oldX= x;
}
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 motion(int x,int y){
    angle[angleID] += (x-oldX);
    oldX = x;
    glutPostRedisplay();
}
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);    ///旋轉
        glTranslatef(-0.3,-0.4,0); ///手臂的旋轉中心
        glColor3f(1,0,0);
        glRectf(0.3,0.5,0.7,0.3);

        glPushMatrix();
            glTranslatef(0.7,0.4,0); ///把手臂放回身體
            glRotatef(angle[1],0,0,1);    ///旋轉
            glTranslatef(-0.7,-0.4,0); ///手臂的旋轉中心
            glColor3f(0,1,0);
            glRectf(0.7,0.5,1,0.3);
        glPopMatrix();
    glPopMatrix();

    glPushMatrix();
        glTranslatef(-0.3,0.4,0); ///把手臂放回身體
        glRotatef(angle[2],0,0,1);    ///旋轉
        glTranslatef(0.3,-0.4,0); ///手臂的旋轉中心
        glColor3f(1,0,0);
        glRectf(-0.3,0.5,-0.7,0.3);

        glPushMatrix();
            glTranslatef(-0.7,0.4,0); ///把手臂放回身體
            glRotatef(angle[3],0,0,1);    ///旋轉
            glTranslatef(0.7,-0.4,0); ///手臂的旋轉中心
            glColor3f(0,1,0);
            glRectf(-0.7,0.5,-1,0.3);
        glPopMatrix();
    glPopMatrix();


    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    ///glutInitWindowSize(600,600);
    glutCreateWindow("week13 rect TRT");

    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}


VERY BEAUTIFUL, VERY POWERFUL

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