2020年3月18日 星期三

欸你過來一下03

照慣例 > 先用Zuvio複習上周
點、線、面複習

滑鼠左中右鍵、x,y座標值
#include <GL/glut.h>///蘋果電腦要用 GLUT/glut.h
#include <stdio.h>///為了printf
void display()
{
    glutSolidTeapot( 0.3 );
    glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)///J個是滑鼠的函式
{/// 鍵:0左、1中、2右鍵, 0下、1上, (座標)x:0~299、y:0~299
    printf("%d %d %d %d\n", button, state, x, y);
}///printf()印值
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("標題");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);

    glutMainLoop();
}


讓茶壺可以移動

float teapotX=0, teapotY=0;///用變數1~-1表示座標
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///備份矩陣
    ///glTranslatef(x, y, z);///會移動 x, y, z
        glTranslatef(teapotX, teapotY, 0);///移動
        glutSolidTeapot( 0.3 );
    glPopMatrix();///備份矩陣

    glutSwapBuffers();
}
void mouse(int button, int state, int x, int y)
{///       x:0...150...300
///減150   -150   0    150 先把mouse的座標-150
///除150.0  -1    0    +1  再把結果除以150.0
///改成     -1    0    +1  就會得到我們要的-1~+1的範圍
    teapotX = (x-150)/150.0;///換算一下座標
    teapotY = -(y-150)/150.0;///換算一下座標
}///小心y反過來加負號


哇嗚~可以自己製作圖案了欸

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBegin(GL_POLYGON);
        glVertex2f( (227-150)/150.0, -(132-150)/150.0);
        glVertex2f( (151-150)/150.0, -(115-150)/150.0);
        glVertex2f( (85-150)/150.0, -(141-150)/150.0);
        glVertex2f( (180-150)/150.0, -(186-150)/150.0);
    glEnd();
    glutSwapBuffers();
}

自己做了個小箭頭
**POLYGON是凸多邊形,有凹進去的地方要記得分開做喔

沒有留言:

張貼留言