顯示具有 03160294_潘俊凱 標籤的文章。 顯示所有文章
顯示具有 03160294_潘俊凱 標籤的文章。 顯示所有文章

2017年5月23日 星期二

WEEK14,跌雷嘻嘻嘻嘻..

讓機器人旋轉

在main()裡

 #include <stdio.h>
void timer(int t)
{
    glutTimerFunc(100,timer,t+1);
    angle++;
    glutPostRedisplay();
}

 glutTimerFunc(2000,timer,0);



把函式拉出去

讓程式變清爽
drawBody()
drawArm0()
drawArm1()


例子:
void drawBody()
{
    glPushMatrix( );  ///body
        glTranslatef(0,0.1,0);///T
        glRotatef(90,0,0,1);///R
        glRotatef(90,0,1,0);
        glRotatef(90,0,0,1);
        glRotatef(180,1,0,0);
        glTranslatef(0,0.5,0);///T
        glScalef(0.3,0.3,0.3);
        glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix();
}
void drawArm0()
{
    glPushMatrix( );  ///left hand arm
        glTranslatef(-0.25,0.1,0);///T
        glRotatef(45,0,0,1);///R
        glTranslatef(0,0.2,0);///T
        glScalef(0.25,0.25,0.25);
        glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix();
}
void drawArm1()
{
    glPushMatrix( );  ///left hand
        glTranslatef(-0.25,0,0);///T
        glRotatef(90,0,0,1);///R
        glTranslatef(-0.1,0.25,0);///T
        glScalef(0.25,0.25,0.25);
        glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix();
}
void drawRArm()
{
    glPushMatrix( );  ///Right hand arm
        glTranslatef(0.25,0.1,0);///T
        glRotatef(180,0,0,1);///R
        glRotatef(180,1,0,0);
        glRotatef(45,0,0,1);
        glTranslatef(0,0.2,0);///T
        glScalef(0.25,0.25,0.25);
        glmDraw( pmodel, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix( );
}
void drawRhand()
{
    glPushMatrix( );  ///Right hand
        glTranslatef(0.5,0,0);///T
        glRotatef(180,0,0,1);///R
        glRotatef(180,1,0,0);
        glRotatef(90,0,0,1);
        glTranslatef(-0.1,0,0);///T
        glScalef(0.25,0.25,0.25);
        glmDraw( pmodel2, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix( );
}
void drawHead()
{
    glPushMatrix( );  ///head
        glTranslatef(0,1,0);///T
        glTranslatef(0,-0.3,0);///T
        glScalef(0.3,0.3,0.3);
        glmDraw( pmodel4, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix( );
}
void drawRleg()
{
    glPushMatrix( );  ///Right Leg up
        glTranslatef(-0.15,-0.3,0);///T
        //glTranslatef(0,-0.2,0);///T
        glScalef(0.3,0.5,0.3);
        glmDraw( pmodel5, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix( );
}
void drawLleg()
{
    glPushMatrix( );  ///Left Leg up
        glTranslatef(0.15,-0.3,0);///T
        //glTranslatef(0,-0.2,0);///T
        glScalef(0.3,0.5,0.3);
        glmDraw( pmodel6, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix( );
}
void drawRfoot()
{
    glPushMatrix( );  ///Right Leg low
        glTranslatef(-0.2,-0.65,0);///T
        glRotatef(180,0,1,0);
        //glTranslatef(0,-0.2,0);///T
        glScalef(0.5,0.75,0.5);
        glmDraw( pmodel7, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix( );
}
void drawLfoot()
{
    glPushMatrix( );  ///Left Leg low
        glTranslatef(0.2,-0.65,0);///T
        glRotatef(180,0,1,0);
        //glTranslatef(0,-0.2,0);///T
        glScalef(0.5,0.75,0.5);
        glmDraw( pmodel8, GLM_SMOOTH | GLM_MATERIAL);
    glPopMatrix( );
}
開始旋轉製作



         glPushMatrix();
            //drawBody();
            glutSolidTeapot(0.03);
            glPushMatrix();///Left Part
                glTranslatef(-0.22, +0.35, 0);///(3) 最後,把它掛在對的位置
                glRotatef(-angle, 0,0,1); ///(1) 先轉動
                glTranslatef(0.22, -0.35, 0);///(2) move the rotate center to center
                drawArm0();

                glTranslatef(-0.48, +0.1, 0);///(3)  最後,把它掛在對的位置
                glRotatef(-angle, 0,0,1);///(1) 先轉動
                glTranslatef(0.48, -0.1, 0);///(2) 接著將旋轉中心,放到真的中心, 記得把樓上先註解掉
                drawArm1();
            glPopMatrix();

            glPushMatrix();///Right Part
                glTranslatef(+0.2, +0.36, 0);///3
                glRotatef(angle, 0,0,1); ///(1)
                glTranslatef(-0.2, -0.36, 0);///(2)
                drawRArm();

                glTranslatef(+0.48, +0.1, 0);///(3)
                glRotatef(angle, 0,0,1);///(1)
                glTranslatef(-0.48, -0.1, 0);///(2)
                drawRhand();
            glPopMatrix();


        glPopMatrix();

        drawHead();
        drawRleg();
        drawRfoot();

        drawLleg();
        drawLfoot();

    glPopMatrix( );
    glutSwapBuffers( );
}

滑鼠操作程式碼
int oldX=0, oldY=0;
void mouse(int button, int state, int x, int y)
{
    oldX=x; oldY=y;
    glutPostRedisplay();
}
void motion(int x, int y)
{
    angle += (x-oldX);
    oldX=x;
    glutPostRedisplay();
}
在int main裡面要加
glutMouseFunc(mouse);

之後在int oldX=0,oldY=0;底下加鍵盤設定
void keyboard(unsigned char key, int x, int y)
{
    if(key=='0')now=0;
    if(key=='1')now=1;
    if(key=='2')now=2;
    if(key=='3')now=3;
    if(key=='4')now=4;
}

角度處理
float angle[20] = {} ;變為函數

之後為了鍵盤設定
在int main底下加
glutKeyboardFunc(keyboard);

2017年5月16日 星期二

Week13!!YORO~~

1.考試示範截圖


2.下載opencv2.1

選項選擇


codeblocks建立貝殼專案

選擇C++

程式碼

搜尋路徑

設定路徑


其他的Linker





執行程式

#include <opencv/highgui.h>
int main()
{
    IplImage * img= cvLoadImage("c:/image.png");圖檔位置
    cvShowImage("my2DImage",img);檔案名稱
    cvWaitKey(0);暫時停止
}

3.轉動的地球
下載myEarth.zip
freeglut





2017年5月2日 星期二

week11太會睡啦~~

1)首先下載3D檔案總管(3d exploration)免費試用版(學校安裝)


2)並在jsyeh.org/3dcg10下載data.zip並解壓縮在桌面

3)使用3d exploration去看data中的3D模型

匯出足球:

1)save as...檔案為.cpp檔

2)並選擇為Sample app type

開啟code blocks
開啟openGL專案

刪掉原本的main.c

在把新的cpp檔放進專案資料夾中

並將他開啟



註解掉那2個讀不了的程式碼

完成


匯出海豚:


依序將海豚匯出成obj檔(要記得選在Visible區域)

FB下載freeglut的檔案
還有老師有給myGLMsample檔案匯入codeblocks
將三隻海豚的檔案放進myGLMsample的data裡
更改檔案讀取部分程式碼就可以秀出模型
要同時做出第二個模型
請複製貼上並更改紅框區域再新增需要的程式碼
2隻完成
再做成3隻
隨意縮放旋轉



#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        angle++;
        glRotatef(angle, 0,1,0);
        if (!pmodel) {
        pmodel = glmReadOBJ("data/dolphins1.obj");
        if (!pmodel) exit(0);
        glmUnitize(pmodel);
        glmFacetNormals(pmodel);
        glmVertexNormals(pmodel, 90.0);
        }

        glmDraw(pmodel,GLM_SMOOTH | GLM_MATERIAL);

        if (!pmodel2) {
        pmodel2 = glmReadOBJ("data/dolphins2.obj");
        if (!pmodel2) exit(0);
        glmUnitize(pmodel2);
        glmFacetNormals(pmodel2);
        glmVertexNormals(pmodel2, 90.0);
        }
        glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);

        if (!pmodel3) {
        pmodel3 = glmReadOBJ("data/dolphins3.obj");
        if (!pmodel3) exit(0);
        glmUnitize(pmodel3);
        glmFacetNormals(pmodel3);
        glmVertexNormals(pmodel3, 90.0);
        }
        glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);

    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 | GLUT_RGB);
    glutCreateWindow("Yes, 3D Model Here");

    glutDisplayFunc(display);
    glutIdleFunc(display);

    glClearColor(1,1,1,1);
    //glEnable(GL_CULL_FACE);
    //glCullFace(GL_BACK);

    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_dif
    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();
}