2024年5月16日 星期四

week13電腦圖學

 

 Week13

week13-1_glm_gundam_keyboard_angleID_mouse_motion_angle

1. 先設定好環境

2. 安裝 freeglut 再把 lib\libfreeglut.a 複製成 libglut32.a

3. 安裝 opencv 2.1, 記得要 Add PATH, 不要設目錄

4. 關閉重開 CodeBlocks (才會把 PATH 設定設好 要記得存檔)

5. -Search directories 的 Compiler 對應 C:\OpenCV2.1\include

6. -Search directories 的 Linker 對應 C:\OpenCV2.1\lib

7. -Linker setting 要加 cv210 cxcore210 highgui210

8. 再把 GitHub 的程式下載, 再把 week12-3_glm_gundam_keyboard_angleID_mouse_motion_angle 拿來改

9. 把float angle = 0; 改成
    float angle[20] = {};
    int angleID = 0; 


10. 加上Keyboard


11. 固定mouse新的位置


week13-2_FILE_fopen_fprintf_fscanf

1. 把 week13-1 複製整個目錄, 改目錄名,  改 .cbp 名, Notepad++ 改裡面的 Title

2. 在 week13-2 裡, 要加 File

    FILE * fin = NULL;
    FILE * fout = NULL;

3. 在motion裡, 要加一段程式
    
    void motion (int x, int y)
    {
        angle[angleID] += y-oldY;
        oldX = x;
        oldY = y;
        glutPostRedisplay();
        if(fout==NULL) fout = fopen("angle.txt", "w+" );
        for (int i=0; i<20; i++)
        {
            printf("%.1f ", angle[i] );
            fprintf(fout, "%.1f ", angle[i] );
        }
        printf("\n");
        fprintf(fout,  "\n");
    }

4. 執行程式時, 用  mouse motion 一邊改變角度, 一邊大量產生角度值小黑關掉後, 可在目錄裡, 看到angle.txt 裡面就是剛剛印的一堆數值


5. 小心, angle.txt 裡面要有空格, 不然就錯了, printf() 及 fprintf() 要加 "%.1f "

6. 現在要讀 (按 r 結果會變成剛剛動的軌跡)

    void keyboard(unsigned char key, int x, int y)
    {
        if(key=='r')
        {
            if(fin==NULL) fin = fopen("angle.txt", "r");
            for(int i=0; i<20; i++)
            {
                fscanf(fin, "%f", & angle[i] );
            }
            glutPostRedisplay();
        }
        if(key=='0') angleID = 0;
        if(key=='1') angleID = 1;
        if(key=='2') angleID = 2;
        if(key=='3') angleID = 3;
    }


7. 新增身體, 把老師傳的 body.obj body.mtl 傳到 data 裡面

8. 把 drawmodel 複製貼上 把model 改成body

9. 新增一行 GLMmodel * body = NULL;



10. 新增多個關節(作業)


#include <opencv/highgui.h> ///使用 OpenCV 2.1 比較簡單, 只要用 High GUI 即可
#include <opencv/cv.h>
#include <GL/glut.h>
int myTexture(char * filename)
{
    IplImage * img = cvLoadImage(filename); ///OpenCV讀圖
    cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
    glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
    GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
    glGenTextures(1, &id); /// 產生Generate 貼圖ID
    glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
    return id;
}
#include <GL/glut.h>
#include "glm.h"
GLMmodel * pmodel = NULL;
GLMmodel * handA = NULL;
GLMmodel * handB = NULL;
GLMmodel * upperA = NULL;
GLMmodel * lowerA = NULL;
GLMmodel * body = NULL;

void drawBody(void)
{
    if (!body) {
body = glmReadOBJ("data/body.obj");
if (!body) exit(0);
glmUnitize(body);
glmFacetNormals(body);
glmVertexNormals(body, 90.0);
    }

    glmDraw(body, GLM_SMOOTH | GLM_TEXTURE);
}

void drawupperA(void)
{
    if (!upperA) {
upperA = glmReadOBJ("data/upperA.obj");
if (!upperA) exit(0);
glmUnitize(upperA);
glmFacetNormals(upperA);
glmVertexNormals(upperA, 90.0);
    }

    glmDraw(upperA, GLM_SMOOTH | GLM_TEXTURE);
}

void drawlowerA(void)
{
    if (!lowerA) {
lowerA = glmReadOBJ("data/lowerA.obj");
if (!lowerA) exit(0);
glmUnitize(lowerA);
glmFacetNormals(lowerA);
glmVertexNormals(lowerA, 90.0);
    }

    glmDraw(lowerA, GLM_SMOOTH | GLM_TEXTURE);
}

void drawmodel(void)
{
    if (!pmodel) {
pmodel = glmReadOBJ("data/Gundam.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
    }

    glmDraw(pmodel, GLM_SMOOTH | GLM_TEXTURE);
}
void drawHandA(void)
{
    if (!handA) {
handA = glmReadOBJ("data/handA.obj");
if (!handA) exit(0);
glmUnitize(handA);
glmFacetNormals(handA);
glmVertexNormals(handA, 90.0);
    }

    glmDraw(handA, GLM_SMOOTH | GLM_TEXTURE);
}
void drawHandB(void)
{
    if (!handB) {
handB = glmReadOBJ("data/handB.obj");
if (!handB) exit(0);
glmUnitize(handB);
glmFacetNormals(handB);
glmVertexNormals(handB, 90.0);
    }

    glmDraw(handB, GLM_SMOOTH | GLM_TEXTURE);
}
void myBody() { ///我的身體
glPushMatrix(); ///備份矩陣
        glColor3f(1, 0, 0);///紅色的
        glutSolidCube(0.6);///myBody();
glPopMatrix(); ///還原矩陣
}
///float angle = 0, da = 1; ///加這行, 讓它轉動
float angle[20] = {}; ///20個角度 , 都設成0
int angleID = 0; ///可以是角度0, 角度1, 角度2, .....
int oldX = 0, oldY = 0;

#include <stdio.h>
FILE * fin = NULL;
FILE * fout = NULL;
void motion (int x, int y)
{
    angle[angleID] += y-oldY;
    oldX = x;
    oldY = y;
    glutPostRedisplay();
    if(fout==NULL) fout = fopen("angle.txt", "w+" );
    for (int i=0; i<20; i++)
    {
        printf("%.1f ", angle[i] );
        fprintf(fout, "%.1f ", angle[i] );
    }
    printf("\n");
    fprintf(fout,  "\n");
}


void mouse (int button, int state, int x, int y)
{
    oldX = x;
    oldY = y;
}

void keyboard(unsigned char key, int x, int y)
{
    if(key=='r')
    {
        if(fin==NULL) fin = fopen("angle.txt", "r");
        for(int i=0; i<20; i++)
        {
            fscanf(fin, "%f", & angle[i] );
        }
        glutPostRedisplay();
    }
    if(key=='0') angleID = 0;
    if(key=='1') angleID = 1;
    if(key=='2') angleID = 2;
    if(key=='3') angleID = 3;
}///記得在 int main()裡面, 加 glutKeyboardFunc(keyboard)

void display()
{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glDisable(GL_TEXTURE_2D);

    glPushMatrix();
        glRotatef(angle[0], 0, 1, 0);
        drawBody();
        ///glutSolidSphere(0.1, 30, 30); ///圓球
        glEnable(GL_TEXTURE_2D);
        glColor3f(1,1,1);

        glPushMatrix();
            glTranslatef(-0.1, 0, 0);
            glRotatef(angle[1], 0, 0, 1);
            glRotatef(angle[2], 1, 0, 0);
            glTranslatef(0, -0.07, 0);
            drawupperA(); ///(3)上手臂放好
            
            glPushMatrix(); ///(0)
                glTranslatef(-0.02, -0.09, 0); ///(3) 再掛到上手臂的位置
                glRotatef(angle[3], 1, 0, 0); ///(2) x軸轉
                glTranslatef(0, -0.21, 0); ///(1)
                drawlowerA(); ///(0)
            glPopMatrix(); ///(0)
            
        glPopMatrix();

    glPopMatrix();
    glutSwapBuffers();
}

const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };///加這行, 讓它轉動

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

int main(int argc, char*argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week13-1 Gundam");
    glutDisplayFunc(display);
    glutIdleFunc(display); ///加這行, 讓它轉動
    glutMotionFunc(motion);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard); ///week13-1新加的

    myTexture("data/Diffuse.jpg");

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_COLOR_MATERIAL);
    glEnable(GL_LIGHTING);

    glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv(GL_FRONT, GL_AMBIENT,   mat_ambient);
    glMaterialfv(GL_FRONT, GL_DIFFUSE,   mat_diffuse);
    glMaterialfv(GL_FRONT, GL_SPECULAR,  mat_specular);
    glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);

    glutMainLoop();
}

沒有留言:

張貼留言