顯示具有 10160526_蔣名曜 標籤的文章。 顯示所有文章
顯示具有 10160526_蔣名曜 標籤的文章。 顯示所有文章

2023年6月1日 星期四

week16 蔣

 WEEK16

這禮拜先介紹期末作品,介紹完開始從頭複習,一步一步慢慢教
最後程式碼如下
會跑出剛蛋然後可以操控各部位


#include <opencv/highgui.h> ///使用 OpenCV 2.1 比較簡單, 只要用 High GUI 即可
#include <opencv/cv.h>
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL; ///GLMmodel * gundam = NULL;
GLMmodel * arm1 = NULL, * arm2 = NULL;
GLMmodel * hand1 = NULL, * hand2 = NULL;
GLMmodel * bot = NULL;
GLMmodel * leg1 = NULL, * leg2 = NULL;
GLMmodel * knee1 = NULL, * knee2 = NULL;
GLMmodel * foot1 = NULL, * foot2 = NULL;

float teapotX = 0, teapotY = 0, oldX = 0, oldY = 0;
float angle[20] = {}, angle2[20] = {};///float angle = 0, angle2 = 0;
float NewAngle[20] = {}, NewAngle2[20] = {};
float OldAngle[20] = {}, OldAngle2[20] = {};
int ID = 0;
FILE * fout = NULL;
FILE * fin = NULL;

void timer(int t) {
    printf("現在timer(%d)\n", t);
    glutTimerFunc(20, timer, t+1); ///馬上設定下一個鬧鐘

    float alpha = (t%50) / 50.0; ///0.0 ~ 1.0

    if(t%50==0){
        if(fin == NULL) fin = fopen("motion.txt", "r");
        for(int i=0; i<20; i++){
            OldAngle[i] = NewAngle[i];
            OldAngle2[i] = NewAngle2[i];
            fscanf(fin, "%f", &NewAngle[i] );
            fscanf(fin, "%f", &NewAngle2[i] );
        }
    }
    for(int i=0; i<20; i++){
        angle[i] = NewAngle[i] * alpha + OldAngle[i] * (1-alpha);
        angle2[i] = NewAngle2[i] * alpha + OldAngle2[i] * (1-alpha);
    }

    glutPostRedisplay();
}

void keyboard(unsigned char key, int x, int y) {
    if(key=='0') ID = 0;
    if(key=='1') ID = 1;
    if(key=='2') ID = 2;
    if(key=='3') ID = 3;
    if(key=='4') ID = 4;
    if(key=='5') ID = 5;
    if(key=='6') ID = 6;
    if(key=='7') ID = 7;
    if(key=='8') ID = 8;
    if(key=='9') ID = 9;
    if(key=='s'){ ///save存檔 也會動到檔案
        if(fout == NULL) fout = fopen("motion.txt", "w");
        for(int i=0; i<20; i++){
            fprintf(fout, "%.2f ", angle[i] );
            fprintf(fout, "%.2f ", angle2[i] );
        }
        fprintf(fout, "\n");
        printf("寫了一行\n");
    }
    if(key=='r'){ ///read讀檔 也會動到檔案
        if(fin == NULL) fin = fopen("motion.txt", "r");
        for(int i=0; i<20; i++){
            fscanf(fin, "%f", &angle[i] );
            fscanf(fin, "%f", &angle2[i] );
        }
        glutPostRedisplay();
    }
    if(key=='p'){ ///play播放 也會動到檔案
        glutTimerFunc(0, timer, 0);
    }
}

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;
}
void mouse(int button, int state, int x, int y) {
    oldX = x;
    oldY = y;
}
void motion(int x, int y) {
    teapotX += (x - oldX) / 150.0 * 10; ///teapotX = (x-150)/150.0;
    teapotY += (oldY - y) / 150.0 * 10; ///teapotY = (150-y)/150.0;
    angle[ID] += x - oldX;
    angle2[ID] += oldY - y;
    oldX = x;
    oldY = y;
    glutPostRedisplay();
    printf("  glTranslatef( %.2f, %.2f, 0 ); \n", teapotX, teapotY );
}
void display() {
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glScalef(1.6, 1.6, 1.6);
        glTranslatef(0, -0.5, 0);///往下一半
        glPushMatrix();
            glColor3f(1,1,1);
            glScalef(0.04, 0.04, 0.04);
            glRotatef(angle[0], 0, 1, 0); ///身體的轉動
            glmDraw(body, GLM_MATERIAL|GLM_TEXTURE);///glmDraw(gundam, GLM_MATERIAL|GLM_TEXTURE);

            glPushMatrix();///左手
                glTranslatef(-4.07, +21.33, 0 );
                glRotatef(angle[1], 0, 1, 0);
                glRotatef(angle2[1], 1, 0, 0);
                glTranslatef( 4.07, -21.33, 0 );///glTranslatef(teapotX, teapotY, 0);
                glmDraw(arm1, GLM_MATERIAL|GLM_TEXTURE);

                glPushMatrix();
                    glTranslatef(-4.40, +18.53, 0 );
                    glRotatef(angle[2], 0, 1, 0);
                    glRotatef(angle2[2], 1, 0, 0);
                    glTranslatef( 4.40, -18.53, 0 );///glTranslatef(teapotX, teapotY, 0);
                    glmDraw(hand1, GLM_MATERIAL|GLM_TEXTURE);
                glPopMatrix();
            glPopMatrix();


            glPushMatrix();///右手
                glTranslatef(+4.07, +21.33, 0 );
                glRotatef(angle[3], 0, 1, 0);
                glRotatef(angle2[3], 1, 0, 0);
                glTranslatef(-4.07, -21.33, 0 );///glTranslatef(teapotX, teapotY, 0);
                glmDraw(arm2, GLM_MATERIAL|GLM_TEXTURE);

                glPushMatrix();
                    glTranslatef(+4.40, +18.53, 0 );
                    glRotatef(angle[4], 0, 1, 0);
                    glRotatef(angle2[4], 1, 0, 0);
                    glTranslatef(-4.40, -18.53, 0 );///glTranslatef(teapotX, teapotY, 0);
                    glmDraw(hand2, GLM_MATERIAL|GLM_TEXTURE);
                glPopMatrix();
            glPopMatrix();

            glPushMatrix();
                glTranslatef(-0.00, +22.53, 0 );
                glRotatef(angle[5], 0, 1, 0);
                glRotatef(angle2[5], 1, 0, 0);
                glTranslatef( 0.00, -22.53, 0 );///glTranslatef(teapotX, teapotY, 0);
                glmDraw(head, GLM_MATERIAL|GLM_TEXTURE);
            glPopMatrix();


            glPushMatrix();///左腳
                glmDraw(bot, GLM_MATERIAL|GLM_TEXTURE);

                glPushMatrix();
                    glTranslatef(-2.00, +14.27, 0 );
                    glRotatef(angle[6], 0, 1, 0);
                    glRotatef(angle2[6], 1, 0, 0);
                    glTranslatef( 2.00, -14.27, 0 );///glTranslatef(teapotX, teapotY, 0);
                    glmDraw(leg1, GLM_MATERIAL|GLM_TEXTURE);

                    glPushMatrix();
                        glTranslatef(-2.00, +9.87, 0 );
                        glRotatef(angle[7], 0, 1, 0);
                        glRotatef(angle2[7], 1, 0, 0);
                        glTranslatef( 2.00, -9.87, 0 );///glTranslatef(teapotX, teapotY, 0);
                        glmDraw(knee1, GLM_MATERIAL|GLM_TEXTURE);

                        glPushMatrix();
                            glTranslatef(-2.13, +2.40, 0 );
                            glRotatef(angle[8], 0, 1, 0);
                            glRotatef(angle2[8], 1, 0, 0);
                            glTranslatef( 2.13, -2.40, 0 );///glTranslatef(teapotX, teapotY, 0);
                            glmDraw(foot1, GLM_MATERIAL|GLM_TEXTURE);
                        glPopMatrix();
                    glPopMatrix();
                glPopMatrix();
            glPopMatrix();

            glPushMatrix();///右腳
                glmDraw(bot, GLM_MATERIAL|GLM_TEXTURE);

                glPushMatrix();
                    glTranslatef(+2.00, +14.27, 0 );
                    glRotatef(angle[9], 0, 1, 0);
                    glRotatef(angle2[9], 1, 0, 0);
                    glTranslatef(-2.00, -14.27, 0 );///glTranslatef(teapotX, teapotY, 0);
                    glmDraw(leg2, GLM_MATERIAL|GLM_TEXTURE);

                    glPushMatrix();
                        glTranslatef(+2.00, +9.87, 0 );
                        glRotatef(angle[10], 0, 1, 0);
                        glRotatef(angle2[10], 1, 0, 0);
                        glTranslatef(-2.00, -9.87, 0 );///glTranslatef(teapotX, teapotY, 0);
                        glmDraw(knee2, GLM_MATERIAL|GLM_TEXTURE);

                        glPushMatrix();
                            glTranslatef(+2.13, +2.40, 0 );
                            glRotatef(angle[11], 0, 1, 0);
                            glRotatef(angle2[11], 1, 0, 0);
                            glTranslatef(-2.13, -2.40, 0 );///glTranslatef(teapotX, teapotY, 0);
                            glmDraw(foot2, GLM_MATERIAL|GLM_TEXTURE);
                        glPopMatrix();
                    glPopMatrix();
                glPopMatrix();
            glPopMatrix();

        glPopMatrix();




        glColor3f(0,1,0);
        glutSolidTeapot( 0.02 );

    glPopMatrix();
    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutInitWindowSize(500,500);
    glutCreateWindow("week16");

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

    head = glmReadOBJ("model/head.obj");
    body = glmReadOBJ("model/body.obj"); ///gundam = glmReadOBJ("model/Gundam.obj");
    arm1 = glmReadOBJ("model/arm1.obj");
    arm2 = glmReadOBJ("model/arm2.obj");
    hand1 = glmReadOBJ("model/hand1.obj");
    hand2 = glmReadOBJ("model/hand2.obj");
    bot = glmReadOBJ("model/bot.obj");
    leg1 = glmReadOBJ("model/leg1.obj");
    leg2 = glmReadOBJ("model/leg2.obj");
    knee1 = glmReadOBJ("model/knee1.obj");
    knee2 = glmReadOBJ("model/knee2.obj");
    foot1 = glmReadOBJ("model/foot1.obj");
    foot2 = glmReadOBJ("model/foot2.obj");

    myTexture("model/Diffuse.jpg");
    glEnable(GL_DEPTH_TEST);

    glutMainLoop();
}



2023年5月25日 星期四

week15 蔣

 WEEK15

week15-0

https://jsyeh.org/3dcg10/網頁,下載win32和data,把他們解壓縮,將data放入window,開啟Transformation.exe檔
下載git ,把上禮拜的檔案雲端下載下來
先下載git,把之前的檔案雲端下載下來,建立一個project專案



打上程式碼


float eyeX = 0, eyeY = 0;

void motion(int x, int y){

    eyeX = 3*(x-320)/320.0;

    eyeY = 3*(240-y)/240.0;

    glLoadIdentity();

    gluLookAt(eyeX, eyeY, 3, 0, 0, -6, 0, 1, 0);


    glutPostRedisplay();

}

底下還有

    glutMotionFunc(motion);



新增程式碼    glOrtho(-ar*3, ar*3, -1.0*3, 1.0*3, 2.0, 100.0);

註解掉glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);,會有一個對稱圖案

加上gluPerspective(60, ar, 0.01, 1000);


加上新的程式


#include <GL/glut.h>
void display()
{
    glClear(  GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glutSolidTeapot(0.3);
    glutSwapBuffers();
}
void motion(int x, int y){
    glLoadIdentity();
    float eyeX = (x-150)/150.0, eyeY = (150-y)/150.0;
    gluLookAt(eyeX,eyeY,1, 0,0,0, 0,1,0);
    glutPostRedisplay();
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week15");
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutMainLoop();
}


最後完整的程式碼


#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * right_arm = NULL;
GLMmodel * left_arm = NULL;
int show[4] ={1,1,1,1};
int ID = 2;
float teapotX = 0, teapotY = 0;
float angle[20] = {};
FILE * fout = NULL;
FILE * fin = NULL;
void keyboard(unsigned char key,int x, int y)
{
    if( key=='0' ) ID=0;
    if( key=='1' ) ID=1;
    if( key=='2' ) ID=2;
    if( key=='3' ) ID=3;
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    if(head==NULL)
    {
        head = glmReadOBJ("model/head.obj");
        body = glmReadOBJ("model/body.obj");
        right_arm = glmReadOBJ("model/uparmR.obj");
        left_arm = glmReadOBJ("model/lowarmR.obj");
    }
    glPushMatrix();
        glScalef(0.3,0.3,0.3);
        glPushMatrix();
            ///glTranslatef(teapotX,teapotY,0);
            if(ID==0) glColor3f(1,0,0);
            else glColor3f(1,1,1);
            if(show[0]) glmDraw(head, GLM_MATERIAL);
        glPopMatrix();

        if(ID==1) glColor3f(1,0,0);
        else glColor3f(1,1,1);
        if(show[1]) glmDraw(body, GLM_MATERIAL);

        glPushMatrix();
            glTranslatef(-1.360000, +0.360000, 0);
            glRotatef(angle[2], 0, 0, 1);
            glTranslatef(1.360000, -0.360000, 0);

            if(ID==2) glColor3f(1,0,0);
            else glColor3f(1,1,1);
            if(show[2]) glmDraw(right_arm, GLM_MATERIAL);

            glPushMatrix();
                ///glTranslatef(teapotX,teapotY,0);
                glTranslatef(-1.959999, +0.080000, 0);
                glRotatef(angle[3], 0, 0, 1);
                glTranslatef(1.959999, -0.080000, 0);

                if(ID==3) glColor3f(1,0,0);
                else glColor3f(1,1,1);
                if(show[3]) glmDraw(left_arm, GLM_MATERIAL);
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();
    glColor3f(0,1,0);
    glutSolidTeapot( 0.02 );
    glutSwapBuffers();
}
int oldX=0, oldY=0;
void mouse(int button, int state, int x, int y)
{
    if(state==GLUT_DOWN)
    {
        oldX = x;
        oldY = y;
    }
}
void motion(int x, int y)
{
    teapotX += (x-oldX)/150.0*3;
    teapotY -= (y-oldY)/150.0*3;
    printf("glTranslatef(%f, %f, 0);\n",teapotX, teapotY);
    angle[ID] += x-oldX;
    oldX = x;
    oldY = y;
    glutPostRedisplay();
}
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week13");
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);

    glutMainLoop();
}




2023年5月18日 星期四

week14 蔣

 WEEK14

week14-1_timer

 開始先用GIT指令下載下來2023graphicsb,開啟新的GLUT專案,放在桌面2023graphicsb裡,開啟後打程式,打完後茶壺會半秒轉90度一次

#include <GL/glut.h>
float angle=0;
void timer(int t)
{
    glutTimerFunc(500,timer,t+1);
    angle+=90;
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,0,1);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week14");

    glutTimerFunc(3000,timer,0);
    glutDisplayFunc(display);

    glutMainLoop();
}


week14-2_timer_play

從上個程式修改成可以旋轉,修改完後可以按數字鍵,會越轉越快



#include <GL/glut.h>
float angle=0;
void timer(int t)
{
    glutTimerFunc(33,timer,t+1);
    angle += 3;
    glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y)
{
    glutTimerFunc(0,timer,0);
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,0,1);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week14");

    glutKeyboardFunc(keyboard);
    ///glutTimerFunc(3000,timer,0);
    glutDisplayFunc(display);

    glutMainLoop();
}

week14-3_timer_alpha_interpolation

從上個程式修改,可以自己旋轉他,旋轉完按空白鍵就會撥放


#include <GL/glut.h>
float angle=0,newAngle = 0,oldAngle = 0;
float oldX = 0;
void timer(int t)
{
    if(t<100) glutTimerFunc(33,timer,t+1);
    float alpha = t/100.0;
    angle = alpha*newAngle + (1-alpha)*oldAngle;
    glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y)
{
    glutTimerFunc(0,timer,0);
}
void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN) oldAngle = angle;
    if(state==GLUT_UP) newAngle = angle;
    oldX = x;
    glutPostRedisplay();
}
void motion(int x,int y)
{
    angle += x-oldX;
    oldX = x;
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,0,1);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week14");

    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    ///glutTimerFunc(3000,timer,0);
    glutDisplayFunc(display);

    glutMainLoop();
}





2023年5月11日 星期四

week13 蔣

 WEEK13

week13-1

從Git指令下載整個倉庫,用CodeBlocks開啟Final_Project.cbp專案檔,以後不用再裝freeglut了


week13-2

利用MAYA把上課範例的Al.obj裁切成很多部位,要放入Final_Project裡面,創一個叫model的資料夾把剛剛裁切的東西放進去,再把model資料夾放入Final_Project裡,再把week10裡的glm.cpp及glm.h複製貼上到Final_Project裡,用CodeBlocks開啟Final_Project.cbp,修改程式。如下:



///之後都用同一個程式來進行Final_project
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * uparmR = NULL;
GLMmodel * lowarmR = NULL;

float teapotX = 0, teapotY = 0;
FILE * fout = NULL;
FILE * fin = NULL;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    if(head==NULL){
        head = glmReadOBJ("model/head.obj");
        body = glmReadOBJ("model/body.obj");
        uparmR = glmReadOBJ("model/uparmR.obj");
        lowarmR = glmReadOBJ("model/lowarmR.obj");
    }
    glPushMatrix();
        glScalef(0.3,0.3,0.3);
        glmDraw(head, GLM_MATERIAL);
        glmDraw(body, GLM_MATERIAL);
        glmDraw(uparmR, GLM_MATERIAL);
        glmDraw(lowarmR, GLM_MATERIAL);
    glPopMatrix();
    glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
    teapotX = (x-150)/150.0;
    teapotY = (150-y)/150.0;
    if(state==GLUT_DOWN){
        if(fout==NULL) fout = fopen("file4.txt", "w");

        fprintf(fout, "%f %f\n", teapotX, teapotY);
    }
    display();
}
void keyboard(unsigned char key, int x, int y)
{
    if(fin==NULL){
        fclose(fout);
        fin = fopen("file4.txt", "r");
    }
    fscanf(fin, "%f%f", &teapotX, &teapotY);
    display();
}
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);\
    glutCreateWindow("week12");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);

    glutMainLoop();
}

現在再修改程式,可以按0,1,2,3秀出頭 身體 上手臂 下手臂



///之後都用同一個程式來進行Final_project
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * uparmR = NULL;
GLMmodel * lowarmR = NULL;
int show[4] = {1,0,0,0};
float teapotX = 0, teapotY = 0;
FILE * fout = NULL;
FILE * fin = NULL;
void keyboard(unsigned char key, int x, int y){
    if(key=='0') show[0] = ! show[0];
    if(key=='1') show[1] = ! show[1];
    if(key=='2') show[2] = ! show[2];
    if(key=='3') show[3] = ! show[3];
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    if(head==NULL){
        head = glmReadOBJ("model/head.obj");
        body = glmReadOBJ("model/body.obj");
        uparmR = glmReadOBJ("model/uparmR.obj");
        lowarmR = glmReadOBJ("model/lowarmR.obj");
    }
    glPushMatrix();
        glScalef(0.3,0.3,0.3);
        if(show[0]) glmDraw(head, GLM_MATERIAL);
        if(show[1]) glmDraw(body, GLM_MATERIAL);
        if(show[2]) glmDraw(uparmR, GLM_MATERIAL);
        if(show[3]) glmDraw(lowarmR, GLM_MATERIAL);
    glPopMatrix();
    glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{
    teapotX = (x-150)/150.0;
    teapotY = (150-y)/150.0;
    if(state==GLUT_DOWN){
        if(fout==NULL) fout = fopen("file4.txt", "w");

        fprintf(fout, "%f %f\n", teapotX, teapotY);
    }
    display();
}
///void keyboard(unsigned char key, int x, int y)
///{
    ///if(fin==NULL){
        ///fclose(fout);
        ///fin = fopen("file4.txt", "r");
    ///}
    ///fscanf(fin, "%f%f", &teapotX, &teapotY);
    ///display();
///}
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);\
    glutCreateWindow("week12");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);

    glutMainLoop();
}



///之後都用同一個程式來進行Final_project
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL;
GLMmodel * uparmR = NULL;
GLMmodel * lowarmR = NULL;
int show[4] = {1,0,0,0};
float teapotX = 0, teapotY = 0;
FILE * fout = NULL;
FILE * fin = NULL;
void keyboard(unsigned char key, int x, int y){
    if(key=='0') show[0] = ! show[0];
    if(key=='1') show[1] = ! show[1];
    if(key=='2') show[2] = ! show[2];
    if(key=='3') show[3] = ! show[3];
    glutPostRedisplay();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    if(head==NULL){
        head = glmReadOBJ("model/head.obj");
        body = glmReadOBJ("model/body.obj");
        uparmR = glmReadOBJ("model/uparmR.obj");
        lowarmR = glmReadOBJ("model/lowarmR.obj");
    }
    glPushMatrix();
        glScalef(0.3,0.3,0.3);
        glPushMatrix();
            glTranslatef(teapotX,teapotY,0);
            if(show[0]) glmDraw(head,GLM_MATERIAL);
        glPopMatrix();
        if(show[1]) glmDraw(body, GLM_MATERIAL);
        if(show[2]) glmDraw(uparmR, GLM_MATERIAL);
        if(show[3]) glmDraw(lowarmR, GLM_MATERIAL);
    glPopMatrix();
    glutSwapBuffers();
}
int oldX=0,oldY=0;
void mouse(int button, int state, int x, int y){
    if(state==GLUT_DOWN){
            oldX=x;
            oldY=y;
    }
}
void motion(int x,int y){
    teapotX += (x-oldX)/150.0*3;
    teapotY -= (y-oldY)/150.0*3;
    oldX=x;
    oldY=y;
    glutPostRedisplay();
}
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);\
    glutCreateWindow("week12");

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

    glutMainLoop();
}




2023年5月4日 星期四

week12 蔣

 WEEK12

week12-1_printf_fprintf



#include <stdio.h>
int main()
{
    FILE*fout=fopen("file.txt","w");
    printf("Hello World\n");
    fprintf(fout,"Hello World在檔案裡\n");
}


week12-2_scanf


#include <stdio.h>
int main()
{
    char line[20];
    FILE*fin=fopen("file.txt","r");
    fscanf(fin,"%s",line);
    printf("讀到了:%s\n",line);
    fscanf(fin,"%s",line);
    printf("讀到了:%s\n",line);
}


week12-3_fopen_fdose_fprintf



#include <stdio.h>
int main()
{
    int a[10]={10,20,30,40,50,60,70,80,90,100};
    FILE*fout=fopen("file3.txt","w");
    for(int i=0;i<10;i++){
        fprintf(fout,"%d",a[i]);
        printf("%d ",a[i]);
    }
    fclose(fout);
    int b[10];
    FILE*fin=fopen("file3.txt","r");
    for(int i=0;i<10;i++){
        fscanf(fin,"%d",&b[i]);
        printf("%d ",b[i]);
    }
    fclose(fin);

}

week12-4_keyboard_mouse



#include <stdio.h>
#include <GL/glut.h>
float teapotX=0,teapotY=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(teapotX,teapotY,0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
    teapotX = (x-150)/150.0;
    teapotY = (150-y)/150.0;
    display();
}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week12");

    glutDisplayFunc(display);
    glutMouseFunc(mouse);

    glutMainLoop();
}

再改一下程式碼


#include <stdio.h>
#include <GL/glut.h>
float teapotX=0,teapotY=0;
FILE*fout = NULL;
FILE*fin = NULL;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(teapotX,teapotY,0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
    teapotX = (x-150)/150.0;
    teapotY = (150-y)/150.0;
    if(state==GLUT_DOWN){
        if(fout==NULL) fout = fopen("file4.txt","w");

        fprintf(fout,"%f %f\n",teapotX,teapotY);
    }
    display();
}
void keyboard(unsigned char key,int x,int y)
{
    if(fin==NULL){
        fclose(fout);
        fin=fopen("file4.txt","r");
    }
    fscanf(fin,"%f%f",&teapotX,&teapotY);
    display();
}
int main(int argc, char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week12");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);

    glutMainLoop();
}
///CodeBlocks的專案設定 Project-Properties裡第二個Build Target
///Executing working dir工作執行目錄
///原本是C:\Users\Administrator\Desktop\freeglut\bin
///改成  .  (小數點)再File-Save Everything便能將專案檔設好、存檔
///之後你的工作執行目錄就在你的程式專案的那一目錄裡
///但執行後,就會少了freeglut.dll檔,你再手動copy到你的專案檔裡
///就可以看到  .cbp CodeBlocks Projects專案裡 working dir有變動


2023年4月27日 星期四

week11 蔣

 WEEK11

week11-1 keyboard

新增一個glut專案,打上程式後,會顯示茶壺


#include <GL/glut.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
void keyboard(unsigned char key,int x,int y)
{
    if(key==27) exit(1234);
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week11");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);

    glutMainLoop();
}


week11-2 keyboard_PlaySound

老師先傳一個聲音檔給我們,桌面上會有do-re-mi的檔案,再來複製貼上第一個程式後修改程式,按1 2 3 就會有do-re-mi的聲音



#include <windows.h>
#include <GL/glut.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='1') PlaySound("C:\\Users\\Administrator\\Desktop\\do-re-mi\\do.wav",NULL,SND_ASYNC);
    if(key=='2') PlaySound("C:\\Users\\Administrator\\Desktop\\do-re-mi\\re.wav",NULL,SND_ASYNC);
    if(key=='3') PlaySound("C:\\Users\\Administrator\\Desktop\\do-re-mi\\mi.wav",NULL,SND_ASYNC);
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week11");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);

    glutMainLoop();
}

week11-3 PlaySound.cpp

開啟一個CPP檔,用Hello World播放do-re-mi,到Settings-Compiler裡設定Linker settings打上winmm

打上新的程式,執行後可以聽到do-re-mi的聲音後跑出Hello World。


#include <windows.h>
#include <stdio.h>
int main()
{
    PlaySound("C:\\Users\\Administrator\\Desktop\\do-re-mi\\do.wav",NULL,SND_SYNC);
    PlaySound("C:\\Users\\Administrator\\Desktop\\do-re-mi\\re.wav",NULL,SND_SYNC);
    PlaySound("C:\\Users\\Administrator\\Desktop\\do-re-mi\\mi.wav",NULL,SND_SYNC);
    printf("Hello World\n");
}

week11-4_CMP3_MCI_Load_Play

老師先傳MP3檔給我們,開一個的glut檔,裡面程式不用刪,然後丟進week11-4目錄裡,更改137行的程式,如下。



include "CMP3_MCI.h"///雙引號表示是同個目錄裡的
CMP3_MCI myMP3;
int main(int argc, char *argv[])
{
    char filename[]="C:/Users/Administrator/Desktop/do-re-mi/suzume.mp3";
    myMP3.Load(filename);
    myMP3.Play();

    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

執行後就會有suzume的音樂。


把freeglut-bin裡的freeglut.dll跟老師給的資料夾裡的suzume.mp3複製貼上到week11-4的目錄裡,
更改程式讓程式變簡單,再到codeblocks裡的project-Properties打開然後照圖片更改成"."。



再更改一些程式,如下



#include "CMP3_MCI.h"///雙引號表示是同個目錄裡的
CMP3_MCI myMP3;
int main(int argc, char *argv[])
{
    ///char filename[]="C:/Users/Administrator/Desktop/do-re-mi/suzume.mp3";
    ///myMP3.Load(filename);
    myMP3.Load("suzume.mp3");
    myMP3.Play();

    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);


執行後一樣可以聽到音樂。這修改是讓東西都放在同個工作目錄下,比較好找。





2023年4月20日 星期四

week10 蔣

 WEEK10

week10-0

先考期中考11行程式碼

week10-1 glm gundam

先把 opencv 跟 freeglut用好
開啟新的freeglut檔案然後命名
Computer Graphics (jsyeh.org)的source下載解壓縮然後把glm.c跟glm.h拉進桌面的week10-1檔案裡
再把老師給的鋼蛋三張圖片放進桌面的freeglut/bin裡,把Diffuse圖片用小畫家打開然後垂直翻轉再打上程式,執行就可以跑出鋼蛋模型了

#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"///把source.zip裡的glm.h放在同目錄
float angle=0;
GLMmodel * pmodel =NULL;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    if(pmodel==NULL){
        pmodel = glmReadOBJ("Gundam.obj");
        glmUnitize(pmodel);
        glmFacetNormals(pmodel);
        glmVertexNormals(pmodel, 90);

    }
    glPushMatrix();
    glRotated(angle, 0, 1, 0);
    glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL | GLM_TEXTURE);
    glPopMatrix();
    ///glutSolidTeapot( 0.3 );
    angle++;
    glutSwapBuffers();
}
int main(int argc, char**argv)
{
    glutInit( &argc, argv );
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week10");

    myTexture("Diffuse.jpg");
    glEnable(GL_DEPTH_TEST);
    glutDisplayFunc(display);
    glutIdleFunc(display);

    glutMainLoop();
}


到桌面week10-1目錄裡把week10-1 glm gundamCBD檔用Notepad++開啟然後更改東西,再把freeglut/bin裡的四個檔案複製貼上到week10-1裡,執行目錄就會更改了,成功的話鋼蛋就可以執行出來





2023年4月6日 星期四

week08 蔣

 WEEK08

week08-0

到https://jsyeh.org/3dcg10/網頁,下載source,data,windows全部解壓縮,data放入window,
了解3D模型是如何產生的
v 對應 vertex頂點
vt對應貼圖座標
vn對應vertex normal法向量
f對應 面facet

去may匯出obj檔,取名叫F-16

從桌面把兩個檔案丟入data裡

打開Transformation換F-16,就有自己的模型了

week08-1 glm model

開啟新的glut專案,把範例全刪,先把上周的程式拿來用,改成如下



#include <GL/glut.h>
#include "glm.h"
GLMmodel * pmodel = NULL;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    if(pmodel==NULL){
        pmodel = glmReadOBJ("F-16.obj");
        glmUnitize(pmodel);
    }
    glmDraw(pmodel,GLM_SMOOTH | GLM_MATERIAL);

    glutSwapBuffers( );
}
int main(int argc, char *argv[ ])

{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week08");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop( );
}

week08-2

#include <GL/glut.h>
#include "glm.h"
GLMmodel * pmodel = NULL;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    if(pmodel==NULL){
        pmodel = glmReadOBJ("Gundam.obj");
        glmUnitize(pmodel);
        glmFacetNormals(pmodel);
        glmVertexNormals(pmodel,90);
    }
    glmDraw(pmodel,GLM_SMOOTH | GLM_MATERIAL);

    glutSwapBuffers( );
}
int main(int argc, char *argv[ ])

{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week08");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop( );
}




#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;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    if(pmodel==NULL){
        pmodel = glmReadOBJ("Gundam.obj");
        glmUnitize(pmodel);
        glmFacetNormals(pmodel);
        glmVertexNormals(pmodel,90);
    }
    glmDraw(pmodel,GLM_SMOOTH | GLM_MATERIAL | GLM_TEXTURE);

    glutSwapBuffers( );
}
int main(int argc, char *argv[ ])

{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week08");

    myTexture("Diffuse.jpg");
    glutDisplayFunc(display);
    glutMainLoop( );
}