2022年5月17日 星期二

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...