2016年5月24日 星期二

Week 14 張烝譯

WEEK 14 

作業 1 :



1.開啟新的專案


2.選擇C++


 3.命名


4.程式碼


程式碼:
#include <stdio.h>
FILE *fout=NULL, *fin=NULL;
int main(int argc,char **argv)
{
    if(fout==NULL)
fout=fopen("output.txt","w+")
fprintf(fout,"Hello world\n");
return 0;
}





作業 2 :


沿用作業1的程式
並且做一些設定
  "Build Option"
設定 1


設定 2


設定 3 


出現錯誤!
因為在資料夾中沒有freeglut.dll檔


將freglut.dll檔拉進去即可


完成!!!
按下滑鼠就會出現座標


程式碼 :

#include <GL/glut.h>
#include <stdio.h>

FILE  *fout=NULL,*fin=NULL;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
void motion(int x,int y )
{
    if(fout==NULL)
        fout=fopen("motion.txt","w+");
    fprintf(fout, "%d %d\n",x,y);
    printf("%d %d\n",x,y);
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("3D");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}



作業 3 :


按著滑鼠可以移動茶壺
並且會顯示作標





程式碼 :

#include <GL/glut.h>
#include <stdio.h>
FILE *fout=NULL,*fin=NULL;
int nowX=0,nowY=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef((nowX-150)/150.0,-(nowY-150)/150.0,0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void motion(int x,int y )
{
    if(fout==NULL)fout=fopen("motion.txt","w+");
    fprintf(fout, "%d %d\n",x,y);
    printf("%d %d\n",x,y);
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='r'){
        if(fin==NULL) fin=fopen("motion.txt","r");
        fscanf(fin,"%d %d\n",&nowX,&nowY);
        printf("%d %d\n",nowX,nowY);
    }
    glutPostRedisplay();
}

int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("3D");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}







沒有留言:

張貼留言