2018年3月12日 星期一

Week03_馬明

1.色彩、點線面複習
#include <GL/glut.h>
void display()
{
    glClearColor(1,1,0, 1);///背景
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///兩種模式
    glutSolidTeapot(0.3);

    glutSwapBuffers();///教畫畫面BUFFER便會顯示
}

int main(int argc,char ** argv)///進階的main()參數(個數,字串)
{
    glutInit(&argc,argv);///近接參數初始GLUT
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);///開啟兩種模式
    glutCreateWindow("week03 Mouse操作");///盡量不要用中文
    glutDisplayFunc(display);///等一下樓上要準備void display()
    ///glutMouseFunc(mouse);///等一下會準備好MOUSE()功能,按鈕
    ///glutMotionFunc(motion);///等一下會準備好MOTION(),移動
    glutMainLoop();///主要迴圈


-----------------------------------------------------------
2.加入mouse功能
加入printf("%d %d %d %d\n",button,state,x,y);
















3.加MOTION

放大象
#include <GL/glut.h>
float teapotX=0,teapotY=0;
void display()
{
    glClearColor(1,1,0, 1);///背景
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(teapotX,teapotY,0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
#include<stdio.h>
void mouse (int button,int state,int x,int y)
{
    if(state==GLUT_DOWN) printf("glVertex2f(%f,%f);\n",(x-150)/150.0,-(y-150)/150.0);
}///開冰箱門、把大象放到冰箱、關冰箱門

void motion(int x,int y)
{
    teapotX=(x-150)/150.0;
    teapotY=-(y-150)/150.0;
    printf("%d %d\n",x,y);
    glutPostRedisplay();
}
int main(int argc,char ** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week03 Mouse操作");

    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}


沒有留言:

張貼留言