2018年3月16日 星期五

Week03 林郁珊

1.下載data.zip, windows.zip, glut32.dll












2.將其檔案解壓縮,並將glut32.dll複製到windows.zip













3.按下Shapes.exe執行



















主題:滑鼠
1.新建一個專案(glut)
2.刪除原有程式碼,重新撰寫
#include <stdio.h>///printf()會用到
#include <GL/glut.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.3);
    glutSwapBuffers();///搭配GLUT_DOUBLE兩倍顯示
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);///完整的main()參數
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);///2個顯示的參數
    glutCreateWindow("Week03 Mouse");

    glutDisplayFunc(display);///display()
   // glutMouseFunc(mouse);///滑鼠事件
    //glutMotionFunc(motion);///滑鼠motion事件

    glutMainLoop();///主要GLUT迴圈
}

3.增加3行程式碼
void mouse(int button, int state, int x, int y)
{///button:左、中、右鍵, state: 按下去/彈起來 x,y:mouse位置
    printf("%d %d %d %d\n",button, state, x, y);
}











4.描茶壺的邊
if(state==GLUT_DOWN) printf("   glVertex2f(%f, %f);\n",(x-150)/150.0,(150-y)/150.0);













5.讓茶壺移動
#include <GL/glut.h>
#include <stdio.h>
float teapotX=0,teapotY=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(teapotX, teapotY, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}

void motion(int x, int y)
{
    teapotX=(x-150)/150.0;
    teapotY=(150-y)/150.0;
    glutPostRedisplay();
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week03 Mouse");

    glutDisplayFunc(display);
    glutMotionFunc(motion);

    glutMainLoop();
}

沒有留言:

張貼留言