2018年3月12日 星期一

week3_一刀修羅

複習(點.線面.顏色)
#include <GL/glut.h>
void display()
{
    glClearColor(1,1,0,1);///黃色背景(用來指定Clear的顏色)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///兩種模式(顏色.模式)
    glBegin(GL_POLYGON); ///多邊形
        glColor3f(1,5,0); glVertex2f(1,1); ///顏色與位置
        glColor3f(0,1,0); glVertex2f(0,1);
        glColor3f(0,0,1); glVertex2f(0,0);
    glEnd();
    glutSwapBuffers();
}
int main(int argc,char **argv) 
{
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_DOUBLE);
    glutCreateWindow("05160372");///程式執行名字
    glutDisplayFunc(display);
    glutMainLoop();///主要迴圈
}


本周新教

滑鼠

#include <GL/glut.h>
void display()
{
    glClearColor(5,0,0,1); ///背景顏色
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ///兩種模式
    glColor3f(0,1,0); ///茶壺顏色
    glutSolidTeapot(0.3); ///茶壺
    glutSwapBuffers(); ///交換畫面buffer便會顯示
}
#include <stdio.h>
void mouse(int butten,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("05160372");
    glutDisplayFunc(display); ///
    glutMouseFunc(mouse);
    glutMainLoop(); ///主要迴圈
}


移動茶壺

#include <GL/glut.h> ///使用GLUT外掛
float teapotX=0, teapotY=0;///teapot的位置,一開始在0
void display()
{
    glClearColor(1,1,0,1);///背景顏色
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);///兩種模式
    glPushMatrix();
        glTranslatef(teapotX,teapotY,0);///glTranslate(x,y,z);
        glColor3f(0,1,0);///茶壺顏色
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();///交換畫面buffer便會顯示
}
#include <stdio.h>
void mouse(int butten,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;///把你的mouse的位置,設成teapot的位置
    printf("%d %d\n",x,y);///印出移動的數字
    glutPostRedisplay();
}
int main(int argc, char**argv)///進階的main()參數(個數,字串)
{
    glutInit(&argc,argv);///進階參數,初始GLUT
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);///開啟兩種模式
    glutCreateWindow("05160372");
    glutDisplayFunc(display);///等一下樓上要準備 void display()
    glutMouseFunc(mouse);///等一下也會準備好mouse()功能,按鈕
    glutMotionFunc(motion);///等一下也會準備好motion(),移動
    glutMainLoop();///主要迴圈
}

沒有留言:

張貼留言