顯示具有 02161095_陳薇亘 標籤的文章。 顯示所有文章
顯示具有 02161095_陳薇亘 標籤的文章。 顯示所有文章

2015年6月10日 星期三

02161095 陳薇亘 week16


#include <GL/glut.h>
#include <stdio.h>///存檔用
FILE * fout=NULL;///存檔用file output
FILE * fin=NULL;
float oldX=0, oldY=0;///angle=0,angle2=0,angle3=0,angle4=0
float angle[10]={0};
int angleID=1;
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
        glColor3f(0,0,1);
        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
            glColor3f(0,1,0);
            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
        glColor3f(0.5,0.5,1);
        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
            glColor3f(0.5,1,0.5);
            drawArm(); ///Left Lower Arm
        glPopMatrix();
    glPopMatrix();
    drawBody();
    glFlush();
}
void motion(int x, int y)
{
    angle[angleID] -= y - oldY;
    oldY=y; oldX=x;
    if(fout==NULL)fout=fopen("a.txt","w+");///存檔用
    for(int i=0;i<10;i++)
    {
        printf(    "%f ",angle[i]);
        fprintf(fout,"%.1f ",angle[i]);
    }
    printf("\n");
     fprintf(fout,"\n");
}
void timerPlay(int t)
{
    glutTimerFunc(500,timerPlay,t+1);
    if(fin==NULL) fin=fopen("a.txt","r");
    for(int i=0;i<10;i++){
        fscanf(fin,"%f ",&angle[i]);
        printf("%f ",angle[i]);
    }
    glutPostRedisplay();
}
void mouse(int button, int state, int x, int y){
    if(state==GLUT_DOWN) { oldX=x; oldY=y; }
}
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(10,timerPlay,0);

    glutPostRedisplay();

}
void timer(int t)
{
    glutTimerFunc(20, timer, t+1);
    //angle++;
    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();
}
機器人自動化
#include <GL/glut.h>
#include <stdio.h>///存檔用
FILE * fout=NULL;///存檔用file output
FILE * fin=NULL;
float oldX=0, oldY=0;///angle=0,angle2=0,angle3=0,angle4=0
float angle[10]={0};
float angleOld[10]={0};
float angleNew[10]={0};
int angleID=1;
float a=0;
void drawBody(){
    glPushMatrix();
    glScalef(0.8, 0.9, 0.3);
    glutSolidSphere(0.5,100,100);
    glPopMatrix();
}
void drawArm(){
    glPushMatrix();
    glScalef(1.2, 0.8, 0.3);
    glutSolidSphere(0.3,50,50);
    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
        glColor3f(0,0,1);
        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
            glColor3f(0,1,0);
            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
        glColor3f(0.5,0.5,1);
        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
            glColor3f(0.5,1,0.5);
            drawArm(); ///Left Lower Arm
        glPopMatrix();
    glPopMatrix();
    drawBody();
    glFlush();
}
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<10;i++)
        {
        angleOld[i]=angleNew[i];
        fscanf(fin,"%f ",&angleNew[i]);
        printf("%f ",angleNew[i]);
        }
    printf("\n");
}
void timerPlay(int t)
{
    glutTimerFunc(20,timerPlay,t+1);
    if(t%20==0) readNew();
    for(int i=0;i<10;i++){
        angle[i]=(1-a)*angleOld[i]+(a)*angleNew[i];
    }
    glutPostRedisplay();
}
void mouse(int button, int state, int x, int y){
    if(state==GLUT_DOWN) { oldX=x; oldY=y; }
}
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(10,timerPlay,0);
    if(key=='s') {

                if(fout=NULL)fout=fopen("a.txt","w+");
                for(int i=0;i<10;i++)
                    {
                    printf("%.1f \t",&angle[i]);
                    fprintf(fout,"%.1f ",angle[i]);
                    }
                fprintf(fout,"\n");
                }
    glutPostRedisplay();

}
void timer(int t)
{
    glutTimerFunc(20, timer, t+1);
    //angle++;
    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日 星期三

02161095_陳薇亘, week15

畫兩個茶壺 可運用變數改變位置或角度
#include <GL/glut.h>
float x1=-0.4,y1=0,x2=0.4,y2=0;
void display()
{
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(x1,y1,0);
        glColor3f(1,1,0);
        glutSolidTeapot(0.2);
    glPopMatrix();
    glPushMatrix();
        glTranslatef(x2,y2,0);
        glColor3f(1,0,0);
        glutSolidTeapot(0.2);
    glPopMatrix();
    glFlush();
}
int main(int argc,char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("Robot3D");
    glutDisplayFunc(display);
    glutMainLoop();
}

#include <GL/glut.h>
#include <stdio.h>
float x1=-0.4,y1=0,x2=0,y2=0,angle=0,angle2=0;
void display()
{
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///blue
        glTranslatef(x2,y2,0);
        glRotatef(angle,0,0,1);
        glColor3f(0,0,1);
        glutSolidTeapot(0.2);
    glPopMatrix();
    glPushMatrix();///green
        glTranslatef(x1,y1,0);
        glRotatef(angle2,0,0,1);
        glTranslatef(-0.25,-0.11,0);///先把茶壺移置畫面中心
        glColor3f(0,1,0);
        glutSolidTeapot(0.2);
    glPopMatrix();
    glFlush();
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='1')x1+=0.1;
    if(key=='2')x1-=0.1;
    if(key=='3')y1+=0.1;
    if(key=='4')y1-=0.1;
    if(key=='5')angle+=10;
    if(key=='6')angle-=10;
    if(key=='7')angle2+=10;
    if(key=='8')angle2-=10;
    printf("%f %f\n",x1,y1);
    glutPostRedisplay();
}
int main(int argc,char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("Robot3D");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}

#include <GL/glut.h>
float angle=0,angle2=0,angle3=0,angle4=0 ,oldX=0, oldY=0;
///float angle[10]={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, 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
        glTranslatef(0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
        glColor3f(0,0,1);
        drawArm();///這是個Arm ver 1  Right Upper Arm
        glPushMatrix();
            glTranslatef(0.25, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
            glRotatef(angle2, 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
            glTranslatef(0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
            glColor3f(0,1,0);
            drawArm(); ///Right Lower Arm
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.5, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
        glRotatef(-angle3, 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
        glTranslatef(-0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
        glColor3f(0.5,0.5,1);
        drawArm();///這是個Arm ver 1  Left Upper Arm
        glPushMatrix();
            glTranslatef(-0.25, 0, 0);///最後一步, 把樓下的 "會對Arm Joint做旋轉的東西" 再移到右上 掛上去 ver 4
            glRotatef(-angle4, 0,0,1);///把下面整團, 都對中間做旋轉 ver 3
            glTranslatef(-0.25, 0,0);///這是把它往右移, Arm Joint在中間 ver 2
            glColor3f(0.5,1,0.5);
            drawArm(); ///Left Lower Arm
        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 keyboard(unsigned char key,int x,int y)
{
    if(key=='1')angle++;
    if(key=='2')angle2++;
    if(key=='3')angle3++;
    if(key=='4')angle4++;
    if(key=='5')angle--;
    if(key=='6')angle2--;
    if(key=='7')angle3--;
    if(key=='8')angle4--;
    glutPostRedisplay();

}
void timer(int t)
{
    glutTimerFunc(20, timer, t+1);
    //angle++;
    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();
}
    按1則動關節1
按2則動關節2
利用滑鼠配合鍵盤控制機器人手臂
#include <GL/glut.h>
float oldX=0, oldY=0;///angle=0,angle2=0,angle3=0,angle4=0
float angle[10]={0};
int now=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
        glColor3f(0,0,1);
        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
            glColor3f(0,1,0);
            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
        glColor3f(0.5,0.5,1);
        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
            glColor3f(0.5,1,0.5);
            drawArm(); ///Left Lower Arm
        glPopMatrix();
    glPopMatrix();
    drawBody();
    glFlush();
}
void motion(int x, int y)
{
    angle[now] -= 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 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;

    glutPostRedisplay();

}
void timer(int t)
{
    glutTimerFunc(20, timer, t+1);
    //angle++;
    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();
}

做存檔用的txt.
#include <GL/glut.h>
#include <stdio.h>///存檔用
FILE *fout=NULL;///存檔用file output

float oldX=0, oldY=0;///angle=0,angle2=0,angle3=0,angle4=0
float angle[10]={0};
int now=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
        glColor3f(0,0,1);
        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
            glColor3f(0,1,0);
            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
        glColor3f(0.5,0.5,1);
        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
            glColor3f(0.5,1,0.5);
            drawArm(); ///Left Lower Arm
        glPopMatrix();
    glPopMatrix();
    drawBody();
    glFlush();
}
void motion(int x, int y)
{
    angle[now] -= y - oldY;
    oldY=y; oldX=x;
    if(fout==NULL)fout=fopen("a.txt","w+");///存檔用
    for(int i=0;i<10;i++)
    {
        fprintf(fout,"%.1f",angle[i]);
    }
     fprintf(fout,"\n");
}
void mouse(int button, int state, int x, int y){
    if(state==GLUT_DOWN) { oldX=x; oldY=y; }
}
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;

    glutPostRedisplay();

}
void timer(int t)
{
    glutTimerFunc(20, timer, t+1);
    //angle++;
    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年5月27日 星期三

week14,02161095,陳薇亘

改變點座標(0,1,0)改(0,0,1)-->對著z軸轉動(本來對著瘠髓--->對鈕扣)
交換Rotatef和Translatef結果轉動點是對著手旋轉
打光
#include <GL/glut.h> ///GL最好用大寫比較不會出bug
void display()
{
    GLfloat pos[]={0,0,-200,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); ///打畫面清乾淨

    glutSolidTeapot(0.5);
    glFlush();
}
int main(int argc, char **argv)
{
    glutInit(&argc,argv);

    glutCreateWindow("TRT");
    glutDisplayFunc(display);

    glutMainLoop();
}
#include <GL/glut.h>
float angle=0;///預設角度
void display()
{
    GLfloat pos[]={0,0,-200,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.6,0.2,0);///4.將會轉動的茶壺往右上,掛上去
    glRotatef(angle,0,0,1);///3.固定然後以點為基準轉動
    glTranslatef(0.3,0,0);///2.把會茶向右移
    glutSolidTeapot(0.3);///1.畫出茶壺
    glPopMatrix();
    glutSolidTeapot(0.5);
    glFlush();

}
void timer(int t)///以時間為基準對角度做迴圈
{
    glutTimerFunc(20,timer,t+1);
    angle++;
    glutPostRedisplay();
}
int main(int argc, char **argv)
{
    glutInit(&argc,argv);

    glutCreateWindow("TRT");
    glutDisplayFunc(display);
    glutTimerFunc(20,timer,0);
    glutMainLoop();
}














#include <GL/glut.h>
float angle=0;
void drawBody()///身體方塊大小設定
{
    glPushMatrix();
    glScalef(1,0.5,0.5);
    glutSolidCube(1);
    glPopMatrix();
}
void drawArm()///手臂方塊大小設定
{
    glPushMatrix();
    glScalef(0.8,0.3,0.3);
    glutSolidCube(1);
    glPopMatrix();
}
void display()
{
    GLfloat pos[]={0,0,-200,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,0);
    glRotatef(angle,0,0,1);
    glTranslatef(0.3,0,0);
    drawArm();
    glPopMatrix();
    drawBody();
    glFlush();

}
void timer(int t)
{
    glutTimerFunc(20,timer,t+1);
    angle++;
    glutPostRedisplay();
}
int main(int argc, char **argv)
{
    glutInit(&argc,argv);

    glutCreateWindow("TRT");
    glutDisplayFunc(display);
    glutTimerFunc(20,timer,0);
    glutMainLoop();
}
上面多設定
float oldX=0;oldY=y;
 void motion(int x,int y)
{
angel -=Y-oldY;
oldY=y;oldX=x;
}
void mouse(int botton,int state,int x,int y)
{
if(state==GLUT_DOWN){oldX=x;oldY=y;}
下面多設定
glutMouseFunc(mouse);
glutMotionFunc(motion);

做右邊的上手臂和下手臂
glPushMatrix();///下
    glTranslatef(0.5,0,0);
    glRotatef(angle,0,0,1);
    glTranslatef(0.25,0,0);
    drawArm();
 glPopMatrix();
glPushMatrix();///上
    glTranslatef(0.25,0.2,0);
    glRotatef(angle,0,0,1);
    glTranslatef(0.25,0,0);
    drawArm();
 glPopMatrix();





















坐左邊的上手臂和下手臂
glPushMatrix();///上
    glTranslatef(-0.5,0,0);
    glRotatef(angle,0,0,1);
    glTranslatef(-0.25,0,0);
    drawArm();
 glPopMatrix();
glPushMatrix();///下
    glTranslatef(-0.25,0.2,0);
    glRotatef(angle,0,0,1);
    glTranslatef(-0.25,0,0);
    drawArm();
 glPopMatrix();

2015年5月20日 星期三

02161095 陳薇亘 week13

電腦圖學 PDF講義

window-->projection.exe 隨意改變人物的方向,位子,大小
做出大的茶壺但茶壺會因為視窗放大縮小而壓縮
茶壺不會因視窗縮放而改變大小
#include <GL/glut.h>

void display()
{
    glPushMatrix();
        glutSolidTeapot(100);
    glPopMatrix();
    glFlush();
}
void reshape(int w,int h)
{

    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-w/2,w/2,-h/2,h/2,-1000,1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
int main(int argc, char **argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(700,500);
    glutCreateWindow("3d");
    glutDisplayFunc(display);

    glutReshapeFunc(reshape);

    glutMainLoop();
}
#include <GL/glut.h>
float eyeX=-100,eyeY=80,eyeZ=-200;
void display()
{
    GLfloat pos[]={0,0,200,0};
    glLightfv(GL_LIGHT0,GL_POSITION,pos);
    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_LIGHTING);

    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        gluLookAt(eyeX,eyeY,eyeZ,0,0,0,0,1,0);
     glPushMatrix();
        glutSolidTeapot(100);
     glPopMatrix();
    glPopMatrix();
    glFlush();
}
void motion(int x,int y)
{
    eyeX=x-400;eyeY=300-y;
    glutPostRedisplay();
}
void reshape(int w,int h)
{

    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90,w/h,0.001,10000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
void timer(int t)
{
    glutTimerFunc(20,timer,t+1);
    eyeX+=2;
    if(eyeX>300) eyeY+=4;
    glutPostRedisplay();
}
int main(int argc, char **argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(700,500);
    glutCreateWindow("3d");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMotionFunc(motion);
    glutTimerFunc(0,timer,0);
    glutMainLoop();
}
匯入音樂mp3 
#include <GL/glut.h>
#include "CMP3_MCI.h"
CMP3_MCI   MyMP3;

float eyeX=-100,eyeY=80,eyeZ=-200;
void display()
{
    GLfloat pos[]={0,0,200,0};
    glLightfv(GL_LIGHT0,GL_POSITION,pos);
    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_LIGHTING);

    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        gluLookAt(eyeX,eyeY,eyeZ,0,0,0,0,1,0);
     glPushMatrix();
        glutSolidTeapot(100);
     glPopMatrix();
    glPopMatrix();
    glFlush();
}
void motion(int x,int y)
{
    eyeX=x-400;eyeY=300-y;
    glutPostRedisplay();
}
void reshape(int w,int h)
{

    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(90,w/h,0.001,10000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
void timer(int t)
{
    glutTimerFunc(20,timer,t+1);
    eyeX+=2;
    if(eyeX>300) eyeY+=4;
    glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y)
{
    MyMP3.Play();
}
int main(int argc, char **argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(700,500);
    glutCreateWindow("3d");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMotionFunc(motion);
    glutTimerFunc(0,timer,0);
    glutKeyboardFunc(keyboard);
    MyMP3.Load("music.mp3");
    glutMainLoop();

2015年5月13日 星期三

week12,陳薇亘

建置工作
 將glm.c和glm.h拉入建立我們所資料夾內
 修改glm.c-->glm.cpp
 到資料夾內開啟附檔名標示1
 到資料夾內開啟附檔名標示2
 重新將glm.cpp加入codeblocks內然後按下
將window-->data內的posche.mtl和posche.obj複製到freeglut內
開始畫3d
6行
#include "glm.h"
GLMmodel* pmodel = NULL;

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

 GLfloat pos[]={0.0,0.0,-1.0,0.0};
  glLightfv(GL_LIGHT0, GL_POSITION, pos);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);///打光

2015年5月6日 星期三

02161095 week11 陳薇亘


#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);
}   ///最後一行最難/最重要, 所有貼圖影像的資料都設定好
    void timer(int t)
    {
        //1000msec 50fps=20msec
       glutTimerFunc(20,timer,0);
       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; ///自動轉很帥
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();
}



從0開始:
#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();
}
sin cos控制旋轉方向
旋轉繞圈
上下震盪
 滑鼠控制射出
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include <stdio.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;//每次timer轉1度
    if(potState==0)potY=0.5*sin(angle/180.0*3.14);//狀態0:上下跳動旋轉
    if(potState==2){potX+=potVX; potY+=potVY; potY+=potVY;}//狀態2:切線等速飛出
    glutPostRedisplay();//要更新
}
void display()
{   glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
    glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///自動轉很帥
        glTranslatef(potX,potY,0);
        glRotatef(angle, 0,1,0);///自動轉很帥
        glutSolidTeapot(0.08);

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

}

void motion(int x,int y)
{
    if(potState==1){potX=(x-350)/350.0 ; potY=-(y-350)/350.0;}//狀態1

}
void mouse(int botton,int state,int x,int y)
{
    if(state==GLUT_DOWN){potState=1;}
    else if(state==GLUT_UP){potState=2;potVX=-(x-350)/3500.0;}
}
int main(int argc, char**argv)
{   glutInit(&argc, argv);
    glutCreateWindow("3D");
    glutReshapeWindow(700,700);
    glutDisplayFunc(display); ///顯示
    //glutIdleFunc(display);///自動轉很帥 (閒閒沒事幹, 就重畫)
    glutTimerFunc(10, timer, 0);
    glutMouseFunc(mouse);//up down
    glutMotionFunc(motion);//Drag


    glutMainLoop();
}

2015年4月15日 星期三

week08 陳薇亘

#include <GL/glut.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glFlush();
}
void keyboard(unsigned char key,int x,int y)//按下exit跳出畫面
{
    exit(0);
}
int main(int argc,char**argv)
{

    glutInit(&argc,argv);
    glutCreateWindow("02161095");
    glutFullScreen();//全屏黑幕

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}
#include <GL/glut.h>
#include <stdio.h>
#include <mmsystem.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glFlush();
}
void keyboard(unsigned char key,int x,int y)
{
    exit(0);
}
void mouse(int button,int state, int x,int y)//滑鼠按下會跑出聲音和印出shoot
{
    if(state==GLUT_DOWN)
        PlaySound("Data/Shot.wav",NULL,SND_ASYNC);
        printf("shoot\n");
}
int main(int argc,char**argv)
{

    glutInit(&argc,argv);
    glutCreateWindow("02161095");
    glutFullScreen();//全屏

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);//鍵盤控制
    glutMouseFunc(mouse);//滑鼠控制
    glutMainLoop();
}


 #include <GL/glut.h>
#include <stdio.h>
#include <mmsystem.h>
float potX=-1,potY=0;
void display()//讓茶壺移動
{
    glClearColor(0.1,0.1,0.5);
    glClearColor(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(potX,potY,0);
        glSolidTeapot(0.01);
     flPopMatrix();
    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("shoot\n");
        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("got it\n");
            PlaySound("ahh.wav",NULL.SND_ASYNC);//尖叫聲
        }
}
int main(int argc,char**argv)
{

    glutInit(&argc,argv);
    glutCreateWindow("02161095");
    glutFullScreen();//全屏

    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMainLoop();
}
//利用openCV寫入鬱金香圖片

2015年4月8日 星期三

week07 02161095 陳薇亘

#include <GL/glut.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");
    }
    if(key=='b'){
        printf("bbb\n");
    }


}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("hello");
    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);//呼叫鍵盤
    glutMainLoop();

}

#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");
    }
    if(key=='b'){
        printf("bbb\n");
    }


}
void mouse(int botton,int state,int x,int y)//按滑鼠加入音樂
{
    printf("播放\n");
    PlaySound("fart.wav",NULL,SND_ASYNC);
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("hello");
    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);//增加滑鼠
    glutMainLoop();

}
#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=='1')PlaySound("Do.wav",NULL,SND_ASYNC);
    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("Sol.wav",NULL,SND_ASYNC);
    if(key=='6')PlaySound("La.wav",NULL,SND_ASYNC);
    if(key=='7')PlaySound("Si.wav",NULL,SND_ASYNC);
}
void mouse(int botton,int state,int x,int y)
{
    printf("播放\n");
    PlaySound("fart.wav",NULL,SND_ASYNC);
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("hello");
    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMainLoop();

}