顯示具有 04160480_陳冠霖 標籤的文章。 顯示所有文章
顯示具有 04160480_陳冠霖 標籤的文章。 顯示所有文章

2017年6月12日 星期一

Week14陳冠霖的上課筆記

本周課程:
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include <stdio.h>
GLUquadric * quad;
GLuint id;
float angle=0;
void display()
{   glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
    glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///自動轉很帥
        glBegin(GL_POLYGON);
            glTexCoord2f(0,0);glVertex3f(-1,+1,0);
            glTexCoord2f(1,0);glVertex3f(+1,+1,0);
            glTexCoord2f(1,1);glVertex3f(+1,-1,0);
            glTexCoord2f(0,1);glVertex3f(-1,-1,0);
        glEnd();
        ///glRotatef(90, 1,0,0);
        ///glRotatef(angle, 0,0,1);///自動轉很帥
        ///gluQuadricTexture(quad, 1);
        ///gluSphere(quad, 1, 30, 30);///glutSolidTeapot(0.3);
    glPopMatrix();///自動轉很帥
    glFlush();
}
void timer(int t)
{   glutTimerFunc(20, timer, 0);/// 1000 msec   50fps:20msec
    angle+=1;///自動轉很帥
    glutPostRedisplay();
}
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 myInit()
{   quad = gluNewQuadric();
    id = myTexture("image.jpg");
}
int main(int argc, char**argv)
{   glutInit(&argc, argv);
    glutCreateWindow("3D");
    glutDisplayFunc(display); ///顯示
    glutTimerFunc(0, timer, 0);
    myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
    glutMainLoop();
}

2017年6月5日 星期一

Week13陳冠霖的上課筆記

本周進度:
內插講解
內差公式:d*新+(1-d)*舊
在之前的機器人裡加入float alpha=0.0;float oldAngle[10]={0,0,0,0,0,0,0,0,0,0};float newAngle[10]={10,20,30,40,50,60,70,80,90,100};
void interpolation(){for(int i=0;i<9;i++){angle[i]=alpha*newAngle[i]+(1-alpha)*oldAngle[i];}
    alpha+=0.1;},修改倒數器裡加入interpolation();
以下是程式碼:
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel1 = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel6 = NULL;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;
int now = 0;
float angle[10]={};
float alpha=0.0;
float oldAngle[10]={0,0,0,0,0,0,0,0,0,0};
float newAngle[10]={10,20,30,40,50,60,70,80,90,100};
float anglee=0;
void interpolation()
{
    for(int i=0;i<9;i++)
    {
        angle[i]=alpha*newAngle[i]+(1-alpha)*oldAngle[i];
    }
    alpha+=0.1;
}
void display()
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();


        if (!pmodel1) {//head

        pmodel1 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/head.obj");
        if (!pmodel1) exit(0);

        glmUnitize(pmodel1);
        glmFacetNormals(pmodel1);
        glmVertexNormals(pmodel1, 90.0);
        }
           if (!pmodel2) {//body

        pmodel2 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/body.obj");
        if (!pmodel2) exit(0);

        glmUnitize(pmodel2);
        glmFacetNormals(pmodel2);
        glmVertexNormals(pmodel2, 90.0);
        }
             if (!pmodel3) {

        pmodel3 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/up1.obj");
        if (!pmodel3) exit(0);

        glmUnitize(pmodel3);
        glmFacetNormals(pmodel3);
        glmVertexNormals(pmodel3, 90.0);
        }
             if (!pmodel4) {

        pmodel4 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/up2.obj");
        if (!pmodel4) exit(0);

        glmUnitize(pmodel4);
        glmFacetNormals(pmodel4);
        glmVertexNormals(pmodel4, 90.0);
        }
            if (!pmodel5) {

        pmodel5 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/hand.obj");
        if (!pmodel5) exit(0);

        glmUnitize(pmodel5);
        glmFacetNormals(pmodel5);
        glmVertexNormals(pmodel5, 90.0);
        }
            if (!pmodel6) {

        pmodel6 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down1.obj");
        if (!pmodel6) exit(0);

        glmUnitize(pmodel6);
        glmFacetNormals(pmodel6);
        glmVertexNormals(pmodel6, 90.0);
        }
            if (!pmodel7) {

        pmodel7 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down2.obj");
        if (!pmodel7) exit(0);

        glmUnitize(pmodel7);
        glmFacetNormals(pmodel7);
        glmVertexNormals(pmodel7, 90.0);
        }
            if (!pmodel8) {

        pmodel8 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down3.obj");
        if (!pmodel8) exit(0);

        glmUnitize(pmodel8);
        glmFacetNormals(pmodel8);
        glmVertexNormals(pmodel8, 90.0);
        }

         glPushMatrix();
            //glRotatef(anglee,0,1,0);//旋轉
            glPushMatrix();//頭
                glTranslatef(0,0.65,0);//移動
                glRotatef(90,0,1,0);//旋轉
                glScalef(0.6,0.6,0.6);//縮放
                glmDraw(pmodel1, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
            glPushMatrix();  //身體
                glTranslatef(0,0.1,0);//移動
                glRotatef(90,0,1,0);//旋轉
                glScalef(0.4,0.4,0.4);//縮放
                glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
            glPushMatrix();//右臂
                    glTranslatef(0.43,0.3,0);
                    glRotatef(angle[0],1,0,0);
                    glTranslatef(-0.45,-0.3,0);
                glPushMatrix();
                    glTranslatef(0.45,0.3,0);
                    glRotatef(90,0,1,0);
                    glScalef(0.5,0.5,0.5);
                    glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
            glPushMatrix();//右籌
                    glTranslatef(0.43,0.3,0);
                    glRotatef(angle[2],0,1,0);
                    glTranslatef(-0.45,-0.3,0);
                glPushMatrix();
                    glTranslatef(0.55,0,0);
                    glRotatef(90,0,1,0);
                    glScalef(0.5,0.5,0.5);
                    glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
                glPushMatrix();//右長
                    glTranslatef(0.8,0,0);
                    glRotatef(270,0,1,0);
                    glScalef(0.5,0.5,0.5);
                    glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
            glPopMatrix();
          glPopMatrix();
            glPushMatrix();//左臂
                    glTranslatef(0.46,0.3,0);
                    glRotatef(angle[1],1,0,0);
                    glTranslatef(-0.45,-0.3,0);
                glPushMatrix();
                        glTranslatef(-0.45,0.3,0);
                        glRotatef(90,0,1,0);
                        glScalef(0.5,0.5,0.5);
                        glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
             glPushMatrix();//左籌
                    glTranslatef(-0.46,-0.3,0);
                    glRotatef(angle[3],0,1,0);
                    glTranslatef(0.45,0.3,0);
                glPushMatrix();
                    glTranslatef(-0.55,0,0);
                        glRotatef(90,0,1,0);
                        glScalef(0.5,0.5,0.5);
                        glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
                glPushMatrix();//左掌
                        glTranslatef(-0.8,0,0);
                        glRotatef(90,0,1,0);
                        glScalef(0.5,0.5,0.5);
                        glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
            glPopMatrix();
        glPopMatrix();
        glPushMatrix();//左大腿
            glTranslatef(-0.2,-0.35,0);
            glRotatef(angle[4],0,1,0);
            glTranslatef(0.2,0.3,0);
         glPushMatrix();
            glTranslatef(-0.2,-0.35,0);
            glRotatef(90,0,1,0);
            glScalef(0.2,0.25,0.25);
            glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
         glPushMatrix();//左小腿
            glTranslatef(-0.25,-0.25,0);
            glRotatef(angle[5],0,1,0);
            glTranslatef(0.25,0.3,0);
         glPushMatrix();
            glTranslatef(-0.25,-0.7,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.4,0.5);
            glmDraw(pmodel7, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
         glPushMatrix();//左掌
            glTranslatef(-0.3,-0.9,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.5,0.5);
            glmDraw(pmodel8, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
         glPopMatrix();
         glPopMatrix();
         glPushMatrix();//右大腿
            glTranslatef(0.2,0.25,0);
            glRotatef(angle[6],0,1,0);
            glTranslatef(-0.2,-0.3,0);
         glPushMatrix();
            glTranslatef(0.2,-0.35,0);
            glRotatef(270,0,1,0);
            glScalef(0.2,0.25,0.25);
            glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
         glPushMatrix();//右小腿
            glTranslatef(0.25,0.35,0);
            glRotatef(angle[7],0,1,0);
            glTranslatef(-0.25,-0.3,0);
        glPushMatrix();
            glTranslatef(0.25,-0.7,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.4,0.5);
            glmDraw(pmodel7, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
          glPushMatrix();//右腳掌
            glTranslatef(0.3,-0.9,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.5,0.5);
            glmDraw(pmodel8, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
         glPopMatrix();
         glPopMatrix();
    glPopMatrix();

    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[now]+=(x-oldx);
    oldx = x;
    glutPostRedisplay();
}

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;
    if(key=='5') now=5;
    if(key=='6') now=6;
    if(key=='7') now=7;
    if(key=='8') now=8;
}
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 };
#include <stdio.h>
void timer(int t)
{
    glutTimerFunc(10,timer,t+1);
    interpolation();
    anglee++;
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
    glutCreateWindow("04160480");

    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutTimerFunc(0,timer,0);
    glClearColor(1,1,1,1);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

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

老師講解圖:
讀檔跟存檔
新增以下程式碼:
#include <stdio.h>
FILE * fout=NULL;
FILE * fin=NULL;
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;
    if(key=='5') now=5;
    if(key=='6') now=6;
    if(key=='7') now=7;
    if(key=='8') now=8;
    if(key=='s' || key=='S')
    {
    if(fout==NULL){
        fout=fopen("file.txt","w+");
        printf("產生FILE了\n");
    }
    fprintf(fout,"%.3f %.3f %.3f %.3f %.3f\n",angle[0],angle[1],angle[2],angle[3],angle[4]);
    printf("文件有資料了喔%.3f %.3f %.3f %.3f %.3f\n",angle[0],angle[1],angle[2],angle[3],angle[4]);
    }
    if(key=='r' || key=='R')
    {
    if(fin==NULL){
        fin=fopen("file.txt","r");
        printf("123");
    }
    fscanf(fin,"%f %f %f %f %f",&angle[0],&angle[1],angle[2],&angle[3],&angle[4]);
    glutPostRedisplay();
    }
}
老師講解圖:

2017年5月22日 星期一

Week12陳冠霖的筆記

本周課程:
機器人的頭跟手跟著身體旋轉
將程式碼中的display變數中在新增一組glPushMatrix();、glPopMatrix();將要旋轉的物體丟進去,接著在裡面打上旋轉的程式碼glRotatef(headAngle,0,1,0);,這樣放在glPushMatrix();、glPopMatrix();之間的物體都能跟身體旋轉,簡單說就是在glPushMatrix();、glPopMatrix();之間的物體,一起旋轉。
程式碼如下:
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel1 = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel6 = NULL;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;
float angle=0;
float headAngle=0;
void display()
{
    headAngle++;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();

        glRotatef(angle, 0,1,0);
        if (!pmodel1) {//head

        pmodel1 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/head.obj");
        if (!pmodel1) exit(0);

        glmUnitize(pmodel1);
        glmFacetNormals(pmodel1);
        glmVertexNormals(pmodel1, 90.0);
        }
           if (!pmodel2) {//body

        pmodel2 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/body.obj");
        if (!pmodel2) exit(0);

        glmUnitize(pmodel2);
        glmFacetNormals(pmodel2);
        glmVertexNormals(pmodel2, 90.0);
        }
             if (!pmodel3) {

        pmodel3 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/up1.obj");
        if (!pmodel3) exit(0);

        glmUnitize(pmodel3);
        glmFacetNormals(pmodel3);
        glmVertexNormals(pmodel3, 90.0);
        }
             if (!pmodel4) {

        pmodel4 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/up2.obj");
        if (!pmodel4) exit(0);

        glmUnitize(pmodel4);
        glmFacetNormals(pmodel4);
        glmVertexNormals(pmodel4, 90.0);
        }
            if (!pmodel5) {

        pmodel5 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/hand.obj");
        if (!pmodel5) exit(0);

        glmUnitize(pmodel5);
        glmFacetNormals(pmodel5);
        glmVertexNormals(pmodel5, 90.0);
        }
            if (!pmodel6) {

        pmodel6 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down1.obj");
        if (!pmodel6) exit(0);

        glmUnitize(pmodel6);
        glmFacetNormals(pmodel6);
        glmVertexNormals(pmodel6, 90.0);
        }
            if (!pmodel7) {

        pmodel7 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down2.obj");
        if (!pmodel7) exit(0);

        glmUnitize(pmodel7);
        glmFacetNormals(pmodel7);
        glmVertexNormals(pmodel7, 90.0);
        }
            if (!pmodel8) {

        pmodel8 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down3.obj");
        if (!pmodel8) exit(0);

        glmUnitize(pmodel8);
        glmFacetNormals(pmodel8);
        glmVertexNormals(pmodel8, 90.0);
        }

         glPushMatrix();  //頭
            glRotatef(headAngle,0,1,0);//旋轉
            glPushMatrix();  //身體
                glTranslatef(0,0.1,0);//移動
                glRotatef(90,0,1,0);//旋轉
                glScalef(0.4,0.4,0.4);//縮放
                glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
            glPushMatrix();
                glTranslatef(0,0.65,0);//移動
                glRotatef(90,0,1,0);//旋轉
                glScalef(0.6,0.6,0.6);//縮放
                glmDraw(pmodel1, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
            glPushMatrix();//右臂
                glTranslatef(0.45,0.3,0);
                glRotatef(90,0,1,0);
                glScalef(0.5,0.5,0.5);
                glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
            glPushMatrix();//右籌
                glTranslatef(0.55,0,0);
                glRotatef(90,0,1,0);
                glScalef(0.5,0.5,0.5);
                glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
            glPushMatrix();//右長
                glTranslatef(0.8,0,0);
                glRotatef(270,0,1,0);
                glScalef(0.5,0.5,0.5);
                glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
            glPushMatrix();//左臂
                glTranslatef(-0.45,0.3,0);
                glRotatef(90,0,1,0);
                glScalef(0.5,0.5,0.5);
                glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
                glPushMatrix();//左籌
                glTranslatef(-0.55,0,0);
                glRotatef(90,0,1,0);
                glScalef(0.5,0.5,0.5);
                glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
                glPushMatrix();//左掌
                glTranslatef(-0.8,0,0);
                glRotatef(90,0,1,0);
                glScalef(0.5,0.5,0.5);
                glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
         glPopMatrix();




         glPushMatrix();//左大腿
            glTranslatef(-0.2,-0.35,0);
            glRotatef(90,0,1,0);
            glScalef(0.2,0.25,0.25);
            glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
        glPushMatrix();//左小腿
            glTranslatef(-0.25,-0.7,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.4,0.5);
            glmDraw(pmodel7, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
         glPushMatrix();//左掌
            glTranslatef(-0.3,-0.9,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.5,0.5);
            glmDraw(pmodel8, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();

         glPushMatrix();//右大腿
            glTranslatef(0.2,-0.35,0);
            glRotatef(270,0,1,0);
            glScalef(0.2,0.25,0.25);
            glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
        glPushMatrix();//右小腿
            glTranslatef(0.25,-0.7,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.4,0.5);
            glmDraw(pmodel7, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
         glPushMatrix();//右腳掌
            glTranslatef(0.3,-0.9,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.5,0.5);
            glmDraw(pmodel8, GLM_SMOOTH | GLM_MATERIAL);
         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 | GLUT_RGB);
    glutCreateWindow("04160496hw");

    glutDisplayFunc(display);
    glutIdleFunc(display);

    glClearColor(1,1,1,1);


    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();
}
利用計時氣旋轉
在原本的程式碼裡,加入 glutTimerFunc(0,timer,0);在主程式中,然後設定void timer的函數,在裡面打上 glutTimerFunc(10,timer,t+1);、angle++;,讓物體能跟著計時氣旋轉。
程式碼如下:
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel1 = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel6 = NULL;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;
float angle=0;
void display()
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();


        if (!pmodel1) {//head

        pmodel1 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/head.obj");
        if (!pmodel1) exit(0);

        glmUnitize(pmodel1);
        glmFacetNormals(pmodel1);
        glmVertexNormals(pmodel1, 90.0);
        }
           if (!pmodel2) {//body

        pmodel2 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/body.obj");
        if (!pmodel2) exit(0);

        glmUnitize(pmodel2);
        glmFacetNormals(pmodel2);
        glmVertexNormals(pmodel2, 90.0);
        }
             if (!pmodel3) {

        pmodel3 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/up1.obj");
        if (!pmodel3) exit(0);

        glmUnitize(pmodel3);
        glmFacetNormals(pmodel3);
        glmVertexNormals(pmodel3, 90.0);
        }
             if (!pmodel4) {

        pmodel4 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/up2.obj");
        if (!pmodel4) exit(0);

        glmUnitize(pmodel4);
        glmFacetNormals(pmodel4);
        glmVertexNormals(pmodel4, 90.0);
        }
            if (!pmodel5) {

        pmodel5 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/hand.obj");
        if (!pmodel5) exit(0);

        glmUnitize(pmodel5);
        glmFacetNormals(pmodel5);
        glmVertexNormals(pmodel5, 90.0);
        }
            if (!pmodel6) {

        pmodel6 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down1.obj");
        if (!pmodel6) exit(0);

        glmUnitize(pmodel6);
        glmFacetNormals(pmodel6);
        glmVertexNormals(pmodel6, 90.0);
        }
            if (!pmodel7) {

        pmodel7 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down2.obj");
        if (!pmodel7) exit(0);

        glmUnitize(pmodel7);
        glmFacetNormals(pmodel7);
        glmVertexNormals(pmodel7, 90.0);
        }
            if (!pmodel8) {

        pmodel8 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down3.obj");
        if (!pmodel8) exit(0);

        glmUnitize(pmodel8);
        glmFacetNormals(pmodel8);
        glmVertexNormals(pmodel8, 90.0);
        }

         glPushMatrix();
            //glRotatef(angle,0,1,0);//旋轉
            glPushMatrix();//頭
                glTranslatef(0,0.65,0);//移動
                glRotatef(90,0,1,0);//旋轉
                glScalef(0.6,0.6,0.6);//縮放
                glmDraw(pmodel1, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
            glPushMatrix();  //身體
                glTranslatef(0,0.1,0);//移動
                glRotatef(90,0,1,0);//旋轉
                glScalef(0.4,0.4,0.4);//縮放
                glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
            glPushMatrix();//右臂
                    glTranslatef(0.43,0.3,0);
                    glRotatef(angle,1,0,0);
                    glTranslatef(-0.45,-0.3,0);
                glPushMatrix();
                    glTranslatef(0.45,0.3,0);
                    glRotatef(90,0,1,0);
                    glScalef(0.5,0.5,0.5);
                    glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
                glPushMatrix();//右籌
                    glTranslatef(0.55,0,0);
                    glRotatef(90,0,1,0);
                    glScalef(0.5,0.5,0.5);
                    glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
                glPushMatrix();//右長
                    glTranslatef(0.8,0,0);
                    glRotatef(270,0,1,0);
                    glScalef(0.5,0.5,0.5);
                    glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
            glPopMatrix();
            glPushMatrix();
                    glTranslatef(0.46,0.3,0);
                    glRotatef(angle,1,0,0);
                    glTranslatef(-0.45,-0.3,0);
                glPushMatrix();//左臂
                        glTranslatef(-0.45,0.3,0);
                        glRotatef(90,0,1,0);
                        glScalef(0.5,0.5,0.5);
                        glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
                glPushMatrix();//左籌
                    glTranslatef(-0.55,0,0);
                        glRotatef(90,0,1,0);
                        glScalef(0.5,0.5,0.5);
                        glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
                glPushMatrix();//左掌
                        glTranslatef(-0.8,0,0);
                        glRotatef(90,0,1,0);
                        glScalef(0.5,0.5,0.5);
                        glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();




         glPushMatrix();//左大腿
            glTranslatef(-0.2,-0.35,0);
            glRotatef(90,0,1,0);
            glScalef(0.2,0.25,0.25);
            glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
        glPushMatrix();//左小腿
            glTranslatef(-0.25,-0.7,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.4,0.5);
            glmDraw(pmodel7, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
         glPushMatrix();//左掌
            glTranslatef(-0.3,-0.9,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.5,0.5);
            glmDraw(pmodel8, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();

         glPushMatrix();//右大腿
            glTranslatef(0.2,-0.35,0);
            glRotatef(270,0,1,0);
            glScalef(0.2,0.25,0.25);
            glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
        glPushMatrix();//右小腿
            glTranslatef(0.25,-0.7,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.4,0.5);
            glmDraw(pmodel7, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
         glPushMatrix();//右腳掌
            glTranslatef(0.3,-0.9,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.5,0.5);
            glmDraw(pmodel8, GLM_SMOOTH | GLM_MATERIAL);
         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 };
#include <stdio.h>
void timer(int t)
{
    glutTimerFunc(10,timer,t+1);
    angle++;
    printf("%d\n",t);
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
    glutCreateWindow("04160496hw");

    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutTimerFunc(0,timer,0);
    glClearColor(1,1,1,1);


    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();
}
利用TRT讓手臂旋轉
將display函數裡手臂的程式碼再多加一組glPushMatrix();、glPopMatrix();,然後在裡面加入TRT,glTranslatef(0.46,0.3,0);、glRotatef(angle,1,0,0);、glTranslatef(-0.45,-0.3,0);裡面的數職自行調整到合適。
程式碼如下:
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel1 = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel6 = NULL;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;
float angle=0;
void display()
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();


        if (!pmodel1) {//head

        pmodel1 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/head.obj");
        if (!pmodel1) exit(0);

        glmUnitize(pmodel1);
        glmFacetNormals(pmodel1);
        glmVertexNormals(pmodel1, 90.0);
        }
           if (!pmodel2) {//body

        pmodel2 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/body.obj");
        if (!pmodel2) exit(0);

        glmUnitize(pmodel2);
        glmFacetNormals(pmodel2);
        glmVertexNormals(pmodel2, 90.0);
        }
             if (!pmodel3) {

        pmodel3 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/up1.obj");
        if (!pmodel3) exit(0);

        glmUnitize(pmodel3);
        glmFacetNormals(pmodel3);
        glmVertexNormals(pmodel3, 90.0);
        }
             if (!pmodel4) {

        pmodel4 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/up2.obj");
        if (!pmodel4) exit(0);

        glmUnitize(pmodel4);
        glmFacetNormals(pmodel4);
        glmVertexNormals(pmodel4, 90.0);
        }
            if (!pmodel5) {

        pmodel5 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/hand.obj");
        if (!pmodel5) exit(0);

        glmUnitize(pmodel5);
        glmFacetNormals(pmodel5);
        glmVertexNormals(pmodel5, 90.0);
        }
            if (!pmodel6) {

        pmodel6 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down1.obj");
        if (!pmodel6) exit(0);

        glmUnitize(pmodel6);
        glmFacetNormals(pmodel6);
        glmVertexNormals(pmodel6, 90.0);
        }
            if (!pmodel7) {

        pmodel7 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down2.obj");
        if (!pmodel7) exit(0);

        glmUnitize(pmodel7);
        glmFacetNormals(pmodel7);
        glmVertexNormals(pmodel7, 90.0);
        }
            if (!pmodel8) {

        pmodel8 = glmReadOBJ("C:\\Users\\user\\Desktop\\hw122/down3.obj");
        if (!pmodel8) exit(0);

        glmUnitize(pmodel8);
        glmFacetNormals(pmodel8);
        glmVertexNormals(pmodel8, 90.0);
        }

         glPushMatrix();
            //glRotatef(angle,0,1,0);//旋轉
            glPushMatrix();//頭
                glTranslatef(0,0.65,0);//移動
                glRotatef(90,0,1,0);//旋轉
                glScalef(0.6,0.6,0.6);//縮放
                glmDraw(pmodel1, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
            glPushMatrix();  //身體
                glTranslatef(0,0.1,0);//移動
                glRotatef(90,0,1,0);//旋轉
                glScalef(0.4,0.4,0.4);//縮放
                glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
            glPopMatrix();
            glPushMatrix();//右臂
                    glTranslatef(0.43,0.3,0);
                    glRotatef(angle,1,0,0);
                    glTranslatef(-0.45,-0.3,0);
                glPushMatrix();
                    glTranslatef(0.45,0.3,0);
                    glRotatef(90,0,1,0);
                    glScalef(0.5,0.5,0.5);
                    glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
                glPushMatrix();//右籌
                    glTranslatef(0.55,0,0);
                    glRotatef(90,0,1,0);
                    glScalef(0.5,0.5,0.5);
                    glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
                glPushMatrix();//右長
                    glTranslatef(0.8,0,0);
                    glRotatef(270,0,1,0);
                    glScalef(0.5,0.5,0.5);
                    glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
            glPopMatrix();
            glPushMatrix();
                    glTranslatef(0.46,0.3,0);
                    glRotatef(angle,1,0,0);
                    glTranslatef(-0.45,-0.3,0);
                glPushMatrix();//左臂
                        glTranslatef(-0.45,0.3,0);
                        glRotatef(90,0,1,0);
                        glScalef(0.5,0.5,0.5);
                        glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
                glPushMatrix();//左籌
                    glTranslatef(-0.55,0,0);
                        glRotatef(90,0,1,0);
                        glScalef(0.5,0.5,0.5);
                        glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
                glPushMatrix();//左掌
                        glTranslatef(-0.8,0,0);
                        glRotatef(90,0,1,0);
                        glScalef(0.5,0.5,0.5);
                        glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);
                glPopMatrix();
            glPopMatrix();
        glPopMatrix();
    glPopMatrix();




         glPushMatrix();//左大腿
            glTranslatef(-0.2,-0.35,0);
            glRotatef(90,0,1,0);
            glScalef(0.2,0.25,0.25);
            glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
        glPushMatrix();//左小腿
            glTranslatef(-0.25,-0.7,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.4,0.5);
            glmDraw(pmodel7, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
         glPushMatrix();//左掌
            glTranslatef(-0.3,-0.9,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.5,0.5);
            glmDraw(pmodel8, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();

         glPushMatrix();//右大腿
            glTranslatef(0.2,-0.35,0);
            glRotatef(270,0,1,0);
            glScalef(0.2,0.25,0.25);
            glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
        glPushMatrix();//右小腿
            glTranslatef(0.25,-0.7,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.4,0.5);
            glmDraw(pmodel7, GLM_SMOOTH | GLM_MATERIAL);
         glPopMatrix();
         glPushMatrix();//右腳掌
            glTranslatef(0.3,-0.9,0);
            glRotatef(90,0,1,0);
            glScalef(0.5,0.5,0.5);
            glmDraw(pmodel8, GLM_SMOOTH | GLM_MATERIAL);
         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 };
#include <stdio.h>
void timer(int t)
{
    glutTimerFunc(10,timer,t+1);
    angle++;
    printf("%d\n",t);
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
    glutCreateWindow("04160496hw");

    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutTimerFunc(0,timer,0);
    glClearColor(1,1,1,1);


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

2017年5月15日 星期一

Week11陳冠霖的上課筆記

本周課程:
TRT的應用考試
glPushMatrix();
             glTranslatef(0.4,0.5,0);~~~將東西掛上
             glRotatef(angle,0,0,1);~~~旋轉~~~小心正負號
             glTranslatef(0.5,0.3,0);~~~定住旋轉中心~~~小心反方向
             drawArm();
glPopMatrix(); 
貼圖
下載opevcv2.1,然後開始安裝,持續按下一步道了這個畫面後,選擇第三個Add OpenCV to the system PATH for current user,再持續按下一步。
完成安裝後,C槽會有三個重要的資料夾include、lib、bin。
打開Codeblock,建立新的project,選擇Console application。
建立Project。
project建立完成。
將程式碼進行修改。
程式碼如下:
#include <opencv/highgui.h>
int main()
{
    IplImage * img = cvLoadImage("C:/img.png");
    cvShowImage("a",img);
    cvWaitKey(0);
}
電腦系統找不到第一行進行設定。
選擇檔案名稱按下左鍵,選擇Bulid option。
到了這個介面後,選擇Search directories,到選下層Compiler,會跳出Add directory,在底下Directory:打上C:\opencv2.1\include,按下確定。
在下個介面,選擇Search directories,到選下層Linker,會跳出Add directory,在底下Directory:打上C:\opencv2.1\lib,按下確定。
在這個介面,選擇Linker settings,按下Add,會出現Add library,再File:上打cv210、cxcore210、highgui210,按下OK。
設定完成。
執行完成。
旋轉地球
將freeglut、myEarth解壓縮到桌面。
用Codeblock打開myEarth專案裡的myEarth.cbp。
執行完成。
修改程式碼,變成旋轉茶壺。
程式碼如下:
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include <stdio.h>
GLUquadric * quad;
GLuint id;
float angle=0;
void display()
{   glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
    glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///自動轉很帥
        glRotatef(90, 1,0,0);
        glRotatef(angle, 0,0,1);///自動轉很帥
        gluQuadricTexture(quad, 1);
        ///gluSphere(quad, 1, 30, 30);///glutSolidTeapot(0.3);
        glutSolidTeapot(0.3);
    glPopMatrix();///自動轉很帥
    glFlush();
}
void timer(int t)
{   glutTimerFunc(20, timer, 0);/// 1000 msec   50fps:20msec
    angle+=1;///自動轉很帥
    glutPostRedisplay();
}
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 myInit()
{   quad = gluNewQuadric();
    id = myTexture("image.jpg");
}
int main(int argc, char**argv)
{   glutInit(&argc, argv);
    glutCreateWindow("3D");
    glutDisplayFunc(display); ///顯示
    glutTimerFunc(0, timer, 0);
    myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
    glutMainLoop();
}