2018年3月12日 星期一

week03 阮致峰

(複習茶壺)
#include <GL/glut.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week03");

    glutDisplayFunc(display);
    glutMainLoop();
}


(用滑鼠印程式碼)
#include <GL/glut.h>
void display()
{
    glClearColor(1,1,0,1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(0,1,0);
    glutSolidTeapot(0.3);

    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);
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("Week03 Mouse操作");

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


(可拖曳顯示座標)
#include <GL/glut.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();
}
#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();
}


沒有留言:

張貼留言