2018年3月12日 星期一

Week03史蒂芬的課堂筆記

一、親手重寫GLUT範例
File-New-Project.GLUT專案,會GLUT設定
#include <GL/glut.h>//使用GLUT外掛
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//2種模式
    glutSolidTeapot(0.3);
    glutSwapBuffers();//交換畫面buffer便會顯示

}
int main(int argc, char**argv)//進階的main()參數(個數,字串)
{
    glutInit(&argc,argv);//進階參數,初始GLUT
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);//開始2種模式
    glutCreateWindow("00");//盡量不要用中文
    glutDisplayFunc(display);//等一下樓上要準備 void display()
    glutMainLoop();//主要迴圈
}











二、上色
#include <GL/glut.h>//使用GLUT外掛
void display()
{
    glClearColor(1,1,0,1);//黃色背景(用來指定clear的顏色)
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//2種模式
    glColor3f(0,1,0);//標是顏色
    glutSolidTeapot(0.3);
    glutSwapBuffers();//交換畫面buffer便會顯示

}
int main(int argc, char**argv)//進階的main()參數(個數,字串)
{
    glutInit(&argc,argv);//進階參數,初始GLUT
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);//開始2種模式
    glutCreateWindow("00");//盡量不要用中文
    glutDisplayFunc(display);//等一下樓上要準備 void display()
    glutMainLoop();//主要迴圈
}

三、加入滑鼠事件
#include <GL/glut.h>//使用GLUT外掛
void display()
{
    glClearColor(1,1,0,1);//黃色背景(用來指定clear的顏色)
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//2種模式
    glColor3f(0,1,0);//標是顏色
    glutSolidTeapot(0.3);
    glutSwapBuffers();//交換畫面buffer便會顯示

}
#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)//進階的main()參數(個數,字串)
{
    glutInit(&argc,argv);//進階參數,初始GLUT
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);//開始2種模式
    glutCreateWindow("00");//盡量不要用中文
    glutDisplayFunc(display);//等一下樓上要準備 void display()
    glutMouseFunc(mouse);//等一下也會準備好 mouse()功能,按鈕
    //glutMotionFunc(mouse);//等一下也會準備好 motion()移動

    glutMainLoop();//主要迴圈
}

四、加上移動
#include <GL/glut.h>//使用GLUT外掛
float teapotX=0,teapotY=0;//teapot的位置,一開始在0.0.
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//2種模式
    glPushMatrix();//Now:備份矩陣
        glTranslatef(teapotX,teapotY,0);//glTranslatef(x,y,z);//Now:移動的參數要準備好!
        glutSolidTeapot(0.3);
    glPopMatrix();///Now:還原矩陣
    glutSwapBuffers();//交換畫面buffer便會顯示
}
#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;//Now把你的mouse的位置,設成teapot的位置
    printf("%d %d\n",x,y);//Now:印出移動的數字
    glutPostRedisplay();
}
int main(int argc, char**argv)//進階的main()參數(個數,字串)
{
    glutInit(&argc,argv);//進階參數,初始GLUT
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);//開始2種模式
    glutCreateWindow("00");//盡量不要用中文
    glutDisplayFunc(display);//等一下樓上要準備 void display()
    glutMouseFunc(mouse);//等一下也會準備好 mouse()功能,按鈕
    glutMotionFunc(motion);//等一下也會準備好 motion()移動
    glutMainLoop();//主要迴圈
}

沒有留言:

張貼留言