2016年5月24日 星期二

week14 鄭玉妮

課堂作業一

開新檔案




#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");
}

課堂作業二
移動滑鼠可以讀取座標


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

課堂作業三
按r移動茶壺可以讀到座標x,y

void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef((nowX-150)/150.0,(-nowY-150)/150.0,0);
///圖片原大小約為300*300
///-150在除以150
///就會大約在-1到1之間
///y 軸計算完之後會相反方向差個負號
///所以y軸必須加個負號將此導正
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}


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();
///請電腦重新公告,有空就重新display
}

沒有留言:

張貼留言