顯示具有 02160145_方得榮 標籤的文章。 顯示所有文章
顯示具有 02160145_方得榮 標籤的文章。 顯示所有文章

2015年6月10日 星期三

Week16 02160145 方得榮




#include<GL/glut.h>
float angle1=0;
void display()
{
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
        glColor3f(0,0,1);
        glutSolidTeapot(0.3);

        glPushMatrix();
            glTranslatef(0.5,0.15,0);
            glRotatef(angle1,0,0,1);
            glTranslatef(0.3,0,0);

            glScalef(1,0.3,0.3);
            glColor3f(1,0,0);
            glutSolidSphere(0.3,30,30);
        glPopMatrix();
     glPopMatrix();

     glFlush();

}
void motion (int x,int y)
{
    angle1 = x;
    glutPostRedisplay();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("robot");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}







#include <GL/glut.h>
float angle[10]={}, oldX=0, oldY=0;
void drawBody(){
    glPushMatrix();
    glScalef(1, 0.5, 0.5);
    glutSolidCube(1);
    glPopMatrix();
}
void drawArm(){
    glPushMatrix();
    glScalef(0.6, 0.3, 0.3);
    glutSolidCube(1);
    glPopMatrix();
}
void display()
{
    { ///Lighting
    glColor3ub(166,132,100);
    GLfloat pos[]={0, 0, -1, 0};
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glEnable(GL_LIGHT0);
    //glEnable(GL_LIGHTING);
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }
    glPushMatrix();
        glTranslatef(0.5, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
        glRotatef(angle[1], 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
        glTranslatef(0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
        drawArm();///這是個Arm ver 1  Right Upper Arm
        glPushMatrix();
            glTranslatef(0.25, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
            glRotatef(angle[2], 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
            glTranslatef(0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
            drawArm(); ///Right Lower Arm
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.5, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
        glRotatef(-angle[3], 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
        glTranslatef(-0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
        drawArm();///這是個Arm ver 1  Left Upper Arm
        glPushMatrix();
            glTranslatef(-0.25, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
            glRotatef(-angle[4], 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
            glTranslatef(-0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
            drawArm(); ///Left Lower Arm
        glPopMatrix();
    glPopMatrix();
    drawBody();
    glFlush();
}
int angleID=1;
void motion(int x, int y)
{   angle[angleID] -= y - oldY;
    oldY=y; oldX=x;
}
void mouse(int button, int state, int x, int y){
    if(state==GLUT_DOWN) { oldX=x; oldY=y; }
}
void timer(int t)
{
    glutTimerFunc(20, timer, t+1);
    //angle++;
    glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='1')angleID=1;
    if(key=='2')angleID=2;
    if(key=='3')angleID=3;
    if(key=='4')angleID=4;
}

int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("T-R-T test");
    glutDisplayFunc(display);
    glutTimerFunc(20, timer, 0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}




#include <GL/glut.h>
#include<stdio.h>
FILE * fout=NULL;
FILE * fin=NULL;
int angleID=1;
float  oldX=0, oldY=0;
float angle[8]={0,0,0,0,0,0,0,0};
float angleOld[8]={0,0,0,0,0,0,0,0};
float angleNew[8]={0,0,0,0,0,0,0,0};
int now=0;
float a=0;
void motion(int x,int y)
{
    angle[angleID] -= y - oldY;
    oldY=y; oldX=x;
}
void readNew()
{
    if(fin==NULL)fin = fopen("a.txt","r");
    for(int i=0;i<8;i++)
    {
            angleOld[i]=angleNew[i];
        fscanf(fin,"%f ",&angleNew[i]);
        printf("%.1f ",angleNew[i]);
    }
}
void timerPlay(int t)
{
    glutTimerFunc(20,timerPlay,t+1);

    if(t%100==0) readNew();
    a=(t%100)/100;
    for(int i=0;i<8;i++)
    {
        angle[i]=(1-a)*angleOld[i] + (a)*angleNew[i];
    }
    glutPostRedisplay();
}
void drawBody(){
    glPushMatrix();
    glScalef(1, 0.5, 0.5);
    glutSolidCube(1);
    glPopMatrix();
}
void drawArm(){
    glPushMatrix();
    glScalef(0.6, 0.3, 0.3);
    glutSolidCube(1);
    glPopMatrix();
}
void display()
{
    { ///Lighting
    glColor3ub(166,132,100);
    GLfloat pos[]={0, 0, -1, 0};
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glEnable(GL_LIGHT0);
    //glEnable(GL_LIGHTING);
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }
    glPushMatrix();
        glTranslatef(0.5, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
        glRotatef(angle[1], 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
        glTranslatef(0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
        drawArm();///這是個Arm ver 1  Right Upper Arm
        glPushMatrix();
            glTranslatef(0.25, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
            glRotatef(angle[2], 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
            glTranslatef(0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
            drawArm(); ///Right Lower Arm
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.5, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
        glRotatef(-angle[3], 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
        glTranslatef(-0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
        drawArm();///這是個Arm ver 1  Left Upper Arm
        glPushMatrix();
            glTranslatef(-0.25, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
            glRotatef(-angle[4], 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
            glTranslatef(-0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
            drawArm(); ///Left Lower Arm
        glPopMatrix();
    glPopMatrix();
    drawBody();
    glFlush();
}


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

    glutTimerFunc(20, timer, t+1);
    //angle++;
    glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='1')angleID=1;
    if(key=='2')angleID=2;
    if(key=='3')angleID=3;
    if(key=='4')angleID=4;
    if(key=='p'){glutTimerFunc(20,timerPlay,0);}
    if(key=='s')
    {
        if(fout==NULL)fout = fopen("a.txt","w+");
        for(int i=0;i<8;i++)
        {
            printf("%.1f\t",angle[i]);
            fprintf(fout,"%.1f\t",angle[i]);
        }
        fprintf(fout,"\n");
        }
    glutPostRedisplay();
}

int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("T-R-T test");
    glutDisplayFunc(display);
    glutTimerFunc(20, timer, 0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}


2015年6月3日 星期三

Week15 02160145 方得榮

固定旋轉軸,
讓數字鍵可以移動茶壺
.
固定關節,
讓特定關節移動。

#include <GL/glut.h>
float oldX=0, oldY;
float angle[10]={0,0,0,0,0,0,0,0,0,0};
int now=0;

void keyboard(unsigned char key, int x, int y)
{
    if(key=='1') now=1;
    if(key=='2') now=2;
    if(key=='3') now=3;
    if(key=='4') now=4;
    glutPostOverlayRedisplay();
}

void drawBody()
{
    glPushMatrix();
    glScalef(1, 0.5, 0.5);
    glutSolidCube(1);
    glPopMatrix();
}
void drawArm()
{
    glPushMatrix();
    glScalef(0.6, 0.3, 0.3); ///手臂角度
    glutSolidCube(1);
    glPopMatrix();
}
void display()
{
    GLfloat pos[]={0, 0, -1, 0};
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);

    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
        glTranslatef(0.5, 0, 0); ///旋轉軸
        glRotatef(angle[1], 0,0,1); ///角度
        glTranslatef(0.25, 0,0);///
        drawArm();
        //glutSolidTeapot(0.3);
    glPushMatrix();
        glTranslatef(0.25, 0, 0); ///旋轉軸
        glRotatef(angle[2], 0,0,1); ///角度
        glTranslatef(0.25, 0,0);///
        drawArm();
    glPopMatrix();
glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.5, 0, 0); ///旋轉軸
        glRotatef(-angle[3], 0,0,1); ///角度
        glTranslatef(-0.25, 0,0);///
        drawArm();
    glPushMatrix();
        glTranslatef(-0.25, 0, 0); ///旋轉軸
        glRotatef(-angle[4], 0,0,1); ///角度
        glTranslatef(-0.25, 0,0);///
        drawArm();
    glPopMatrix();
    glPopMatrix();
    drawBody();

    glFlush();
}
void motion(int x, int y)
{
    angle[now] -= y - oldY;
    oldY=y; oldX=x;
}

void timer(int t)
{
    glutTimerFunc(20, timer, t+1);
    ///angle++; 要自己動,要刪掉
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("TRT test");
    glutDisplayFunc(display);
    glutTimerFunc(20, timer, 0);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}

2015年5月27日 星期三

02160145方得榮 Week14 關節運動

#include<GL/glut.h>
float angle=0,oldX=0,oldY=0;
void drawBody()
{
    glPushMatrix();
    glScaled(1,0.3,0.3);
    glutSolidCube(1);
    glPopMatrix();

}
void drawArm()
{
    glPushMatrix();
    glScaled(0.8,0.3,0.3);
    glutSolidCube(1);
    glPopMatrix();

}
void display()
{
    GLfloat pos[]={0,0,-1,0};
    glLightfv(GL_LIGHT0,GL_POSITION,pos);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);

    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
        glTranslatef(0.5,0,0);
        glRotatef(angle,0,0,1);
        glTranslatef(0.25,0,0);
        drawArm();
        glPushMatrix();
        glTranslatef(0.25,0,0);
        glRotatef(angle,0,0,1);
        glTranslatef(0.25,0,0);
        drawArm();
    glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.5,0,0);
        glRotatef(-angle,0,0,1);
        glTranslatef(-0.25,0,0);
        drawArm();
        glPushMatrix();
        glTranslatef(-0.25,0,0);
        glRotatef(-angle,0,0,1);
        glTranslatef(-0.25,0,0);
        drawArm();
    glPopMatrix();
    glPopMatrix();
    drawBody();


    glFlush();
}
void motion(int x,int y)
{
    angle-=y-oldY;
    oldY=y; oldX=x;
}
void mouse(int button,int state ,int x,int y)
{
    if(state==GLUT_DOWN){oldX=x; oldY=y;}
}
void timer (int t)
{
    glutTimerFunc(20,timer,t+1);

    glutPostRedisplay();

}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("T-R-T test");
    glutDisplayFunc(display);
    glutTimerFunc(20,timer,0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}



2015年5月13日 星期三

02160145 方得榮 week12

#include "glm.h"
GLMmodel* pmodel = NULL;

void display()
{
    GLfloat pos[] ={0.0,0.0,-1.0,0.0};
    glLightfv(GL_LIGHT0,GL_POSITION,pos);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
    glFlush();
}
int main(int argc, char** argv)
{

    pmodel = glmReadOBJ("porsche.obj");
    glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);

glutInit(&argc,argv);
glutCreateWindow("3D");
glutDisplayFunc(display);
glutMainLoop();
}



#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include <stdio.h>
float angle=0, earthX=0, earthY=0, earthVY=0, earthVX=0; ///自動轉很帥
GLUquadric * quad;
int pikaState=0;
void timer(int t)
{   glutTimerFunc(20, timer, 0);/// 1000 msec   50fps:20msec
    if(pikaState==0){
        angle+=1;///自動轉很帥
        earthX=0.8*cos(angle*3.14/180);
        earthY=0.8*sin(angle*3.14/180);
    }else if(pikaState==2){
        earthX+=earthVX;
        earthY+=earthVY;
        earthVY-=0.0098;
    }
    glutPostRedisplay();
}
void display()
{   glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
    glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///自動轉很帥
        glTranslatef(earthX, earthY, 0);
        glRotatef(90, 1,0,0);
        glRotatef(angle, 0,0,1);///自動轉很帥
        gluQuadricTexture(quad, 1);
        gluSphere(quad, 0.1, 30, 30);///glutSolidTeapot(0.3);
    glPopMatrix();///自動轉很帥
    glFlush();
}
GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
GLuint id2; ///準備一個 unsigned int 整數, 叫 貼圖ID
void mouse(int button, int state, int x, int y)
{   if(state==GLUT_UP){
        glBindTexture(GL_TEXTURE_2D, id2);
        pikaState=2;
        //earthVX=??
        //earthVY= 0.1;
        earthVY= (y-150.0)/500;
        earthVX= (150.0-x)/500;
    }else if(state==GLUT_DOWN){
        pikaState=1;
        glBindTexture(GL_TEXTURE_2D, id);
    }
}
void motion(int x, int y){
    earthX=(x-150.0)/150.0;
    earthY=(150.0-y)/150.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 myInit()
{   quad = gluNewQuadric();
    id = myTexture("image.jpg");
    id2 = myTexture("image2.jpg");
}   ///最後一行最難/最重要, 所貼圖影像的資料都設定好
int main(int argc, char**argv)
{   glutInit(&argc, argv);
    glutCreateWindow("3D");
    glutDisplayFunc(display); ///顯示
    glutTimerFunc(0, timer, 0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
    glutMainLoop();
}

2015年5月6日 星期三

02160145 方得榮 Week11 "Timer"

#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include <stdio.h>
float angle=0; ///自動轉很帥
GLUquadric * quad;
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, 0.5, 30, 30); ///glutSolidTeapot(0.3);
    glPopMatrix();///自動轉很帥
    glFlush();
    angle+=1;///自動轉很帥
}
void myInit()
{
    quad = gluNewQuadric();
    IplImage * img = cvLoadImage("image.jpg"); ///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);
}   ///最後一行最難/最重要, 所貼圖影像的資料都設定好
void timer(int t)
{/// 1000 msec   50fps:20msec
    glutTimerFunc(20, timer, 0);
    //printf("timer now\n");
    glutPostRedisplay();
}
int main(int argc, char**argv)
{   glutInit(&argc, argv);
    glutCreateWindow("3D");
    glutDisplayFunc(display); ///顯示
    //glutIdleFunc(display);///自動轉很帥 (閒閒沒事幹, 就重畫)
    glutTimerFunc(0, timer, 0);

    myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
    glutMainLoop();
}


#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include <stdio.h>
float angle=0; ///自動轉很帥
void timer(int t)
{
    glutTimerFunc(10,timer,t+1);
    angle+=1;//每次timer轉1度
    glutPostRedisplay();//要更新
}
void display()
{   glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
    glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///自動轉很帥

        glRotatef(angle, 0,1,0);///自動轉很帥
        glutSolidTeapot(0.5);

    glPopMatrix();///自動轉很帥
    glFlush();

}


int main(int argc, char**argv)
{   glutInit(&argc, argv);
    glutCreateWindow("3D");
    glutDisplayFunc(display); ///顯示
    //glutIdleFunc(display);///自動轉很帥 (閒閒沒事幹, 就重畫)
    glutTimerFunc(10, timer, 0);


    glutMainLoop();
}



#include <GL/glut.h>
#include <math.h>
float angle=0,potX=0,potY=0,potVX=0,potVY=0;
int potState=0;
void timer(int t)
{
    glutTimerFunc(10,timer,t+1);
    angle+=1;
    if(potState==0)potY=0.5*sin(angle/180.0*3.14);
    if(potState==2){potX+=potVX;  potY+=potVY;}
    glutPostRedisplay();
}
void display()
{
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.01);
    glPushMatrix();
        glTranslatef(potX,potY,0);
        glRotatef(angle,0,1,0);
        glutSolidTeapot(0.01);
    glPopMatrix();
    glFlush();
}
void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN)
    {
        potState=1;
    }
    else if(state==GLUT_UP)
    {
        potState=2;
        potVX=-(x-350)/3500.0;
        potVY=(y-350)/3500.0;
    }
}
void motion(int x,int y)
{
    if(potState==1)
    {
        potX=(x-350)/350.0;
        potY=-(y-350)/350.0;
    }
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("3D");
    glutReshapeWindow(700,700);
    glutDisplayFunc(display);
    glutTimerFunc(10,timer,0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}




#include<GL/glut.h>
#include<math.h>
float angle=0,potX=0,potY=0;
void timer(int t)
{
    glutTimerFunc(10,timer,t+1);
    angle+=1;///每次timer轉1度
    potX=cos(angle/180.0*3.14);
    potY=0.5*sin(angle/180.0*3.14);
    glutPostRedisplay();///要更新
}
void display()
{
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(potX,potY,0);
        glRotatef(angle,0,1,0);///把改過的angle轉動
        glutSolidTeapot(0.5);
    glPopMatrix();
    glFlush();
}

int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("3D");
    glutDisplayFunc(display);
    glutTimerFunc(10,timer,0);
    glutMainLoop();
}

2015年4月29日 星期三

02160145方得榮 Week 10 "texture"

1.下載並安裝opencv2.1  2008
2.下載freeglut
3.設定opencv
            (1)新專案選貝殼console app
            (2)project/build option 增加東西


4.將部分dll檔案複製到自己的專案資料夾


5.找texture並命名"image"放到專案資料夾
6.打程式

#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
float angle=0; ///自動轉很帥
void display()
{   glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
    glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///自動轉很帥
        glRotatef(angle, 0,1,0);///自動轉很帥
        glutSolidTeapot(0.3);
    glPopMatrix();///自動轉很帥
    glFlush();
    angle+=0.1;///自動轉很帥
}
void myInit()
{   IplImage * img = cvLoadImage("image.jpg"); ///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);
}   ///最後一行最難/最重要, 所有貼圖影像的資料都設定好
int main(int argc, char**argv)
{   glutInit(&argc, argv);
    glutCreateWindow("3D");
    glutDisplayFunc(display); ///顯示
    glutIdleFunc(display);///自動轉很帥 (閒閒沒事幹, 就重畫)
    myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
    glutMainLoop();
}

2015年4月15日 星期三

02160145方得榮 Week 08

做出一個移動的茶壺
做出點擊滑鼠可以發出聲音

#include<GL/glut.h>
#include<stdio.h>
#include<mmsystem.h> //認識Playsound
#include<opencv/highgui.h>
float potX=-1 ,potY=0;
void display ()
{
    glClearColor(0,0,1,0);
    glClear(GL_COLOR_BUFFER_BIT);
     glPushMatrix();
    glTranslatef(potX,potY, 0);
    glutSolidTeapot(0.1);
    glPopMatrix();

    glFlush();
    potX+=0.001;
    if(potX>1.1)potX=-1.1;
}
void keyboard(unsigned char key,int x,int y)
{
    exit(0);
}
void mouse(int button,int state,int x,int y)
{
    if(state==GLUT_DOWN) //如果滑鼠按下
    {
        PlaySound("Data/Shot.wav",NULL,SND_ASYNC);
        printf("SHOT!");
        float mouseX= 2*x/1280.0 -1,mouseY = -(2*y/1024.0 -1);
        if(abs(mouseX-potX)<0.01 && abs(mouseY-potY)<0.01)
        {
            printf("YA! got it!\n");
            PlaySound("ahhh.wav",NULL,SND_ASYNC);
        }
    }
}
int main(int argc,char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("hello");
    glutFullScreen();
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    IplImage * img=cvLoadImage("C:/Users/Public/Pictures/Sample Pictures/tulips.jpg");
    cvShowImage("a", img);
    cvWaitKey(10);
    glutMainLoop();
   
}

2015年4月8日 星期三

02160145方得榮 Week07

#include<GL/glut.h>
#include<mmsystem.h>
#include<stdio.h>
void display()
{

        glutSolidTeapot(0.3);
        glFlush();                                   //弄出一個茶壺

}
void keyboard(unsigned char key,int x,int y)
{

    if(key=='a')
    {
        printf("AAA\n");                       //如果打A會出現aaa
    }
    else if(key=='b')
    {
        printf("BBB\n");
    }
}
void keyboard(unsigned char key, int x,int y)
{
    if(key=='1')PlaySound("Do.WAV",NULL,SND_ASYNC);   //(檔名,NULL,播放模式)
    if(key=='2')PlaySound("Re.WAV",NULL,SND_ASYNC);
    if(key=='3')PlaySound("Mi.WAV",NULL,SND_ASYNC);
    if(key=='4')PlaySound("Fa.WAV",NULL,SND_ASYNC);
    if(key=='5')PlaySound("So.WAV",NULL,SND_ASYNC);
    if(key=='6')PlaySound("La.WAV",NULL,SND_ASYNC);
    if(key=='7')PlaySound("Si.WAV",NULL,SND_ASYNC);         //如果打1234567會出現聲音
}
void mouse(int button,int state,int x,int y)
{
    printf("Playing sound\n");
    PlaySound("atchoum.wav",NULL,SND_ASYNC);   //滑鼠點擊會執行
}
int main(int argc,char **argv)
{
    glutInit(&argc, argv);

    glutCreateWindow("hello");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);

    glutMainLoop();
}

2015年3月25日 星期三

02160145_方得榮 Week5

hw01:劃出一個3D圖形
#include <GL/glut.h>//宣告外掛程式
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POLYGON);
        glColor3f(1,0,0);//調整三原色
        glVertex3f(0.1,0.1,0.8);
        glVertex3f(-0.1,0.1,0.8);
        glVertex3f(-0.1,-0.1,0.8);
        glVertex3f(0.1,-0.1,0.8);//各點座標
    glEnd();
    glFlush();
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("02160446");//標題名稱
    glutDisplayFunc(display);
    glutMainLoop();
}


hw02:讓方塊轉動
#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,1,0);
        glBegin(GL_POLYGON);
            glColor3f(1,0,0);//調整三原色
            glVertex3f(0.1,0.1,0.8);
            glVertex3f(-0.1,0.1,0.8);
            glVertex3f(-0.1,-0.1,0.8);
            glVertex3f(0.1,-0.1,0.8);//各點座標
        glEnd();
    glPopMatrix();
    angle +=0.01;
    glFlush();
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("02160446");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop();
}
hw03:在劃出一個對等的方塊,再將兩個方塊用一個長方形連接起來
#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,1,0.5);
        glBegin(GL_POLYGON);
            glColor3f(1,0,0);
            glVertex3f(0.1,0.1,0.8);
            glVertex3f(-0.1,0.1,0.8);
            glVertex3f(-0.1,-0.1,0.8);
            glVertex3f(0.1,-0.1,0.8);
        glEnd();

        glBegin(GL_POLYGON);
            glColor3f(1,0,0);
            glVertex3f(0.1,0.1,-0.8);
            glVertex3f(-0.1,0.1,-0.8);
            glVertex3f(-0.1,-0.1,-0.8);
            glVertex3f(0.1,-0.1,-0.8);
         glEnd();

         glBegin(GL_QUAD_STRIP);
            glVertex3f(0.1,0.1,0.8);
            glVertex3f(0.1,0.1,-0.8);
            glVertex3f(-0.1,0.1,0.8);
            glVertex3f(-.1,0.1,-0.8);
            glVertex3f(-0.1,-0.1,0.8);
            glVertex3f(-0.1,-0.1,-0.8);
            glVertex3f(0.1,-0.1,0.8);
            glVertex3f(0.1,-0.1,-0.8);
        glEnd();
    glPopMatrix();
    angle +=0.01;
    glFlush();
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("02160446");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop();
}
hw04:讓滑鼠可以轉動物件
#include <GL/glut.h>

float angle=0;
int oldx=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    glRotatef(angle,0,1,0.5);
    glBegin(GL_POLYGON);
        glColor3f(0,1,1);
        glVertex3f(0.1,0.1,0.8);
        glVertex3f(-0.1,0.1,0.8);
        glVertex3f(-0.1,-0.1,0.8);
        glVertex3f(0.1,-0.1,0.8);
    glEnd();

     glBegin(GL_POLYGON);
        glColor3f(0,1,1);
        glVertex3f(0.1,0.1,-0.8);
        glVertex3f(-0.1,0.1,-0.8);
        glVertex3f(-0.1,-0.1,-0.8);
        glVertex3f(0.1,-0.1,-0.8);
    glEnd();

    glBegin(GL_QUAD_STRIP);
        glColor3f(1,0,0);
        glVertex3f(0.1,0.1,0.8);
        glVertex3f(0.1,0.1,-0.8);

        glVertex3f(-0.1,0.1,0.8);
        glVertex3f(-0.1,0.1,-0.8);

        glVertex3f(-0.1,-0.1,0.8);
        glVertex3f(-0.1,-0.1,-0.8);

        glVertex3f(0.1,-0.1,0.8);
        glVertex3f(0.1,-0.1,-0.8);
    glEnd();

    glPopMatrix();
    angle+=0.01;
    glFlush();
}

void motion(int x,int y)
{
    angle+=(x-oldx);
    oldx=x;
}

int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("02160145");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMotionFunc(motion);//讓滑鼠可以轉動物件
    glutMainLoop();
}