2020年3月17日 星期二

week03



複習上周的茶壺程式,這次加入了可以顯示座標的程式碼⏫⏬


#include <GL/glut.h>
#include <stdio.h> //為了執行printf的標頭檔

void display()
{
    glutSolidTeapot(0.3);
    glColor3f(1.0,0.0,0.0);
    glutSwapBuffers();
}

void mouse(int button,int state,int x,int y){
    printf("%d %d %d %d\n",button,state,x,y); 
}

int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week03");
    glutDisplayFunc(display);
    glutMouseFunc(mouse); 
    glutMainLoop();
}






茶壺隨著滑鼠點擊移動到指定位置⏫⏬



#include <GL/glut.h>
#include <stdio.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(); //備份矩陣
    glColor3f(1.0,0.0,0.0);
    glutSwapBuffers();
}

void mouse(int button,int state,int x,int y){

    //printf("%d %d %d %d\n",button,state,x,y);

    printf("%f %f\n",(x-150)/150.0,(y-150)/150.0);
    teapotX = (x-150)/150.0;  // x 0-300  ➜ -1=+1
    teapotY = -(y-150)/150.0;  // y 300-0  ➜ -1-+1
}


int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week03");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMainLoop();
}






點擊位置取得座標,畫出相對應圖形⏫⏬


#include <GL/glut.h>
#include <stdio.h>
float teapotX=0,teapotY=0;

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBegin(GL_POLYGON); // 畫圖
// 程式碼產生的頂點
glVertex2f((83-150)/150.0,-(57-150)/150.0); 
glVertex2f((136-150)/150.0,-(126-150)/150.0);
glVertex2f((192-150)/150.0,-(170-150)/150.0);
glVertex2f((115-150)/150.0,-(246-150)/150.0);
glVertex2f((122-150)/150.0,-(174-150)/150.0);
glVertex2f((66-150)/150.0,-(131-150)/150.0);
glVertex2f((42-150)/150.0,-(261-150)/150.0);
glVertex2f((31-150)/150.0,-(91-150)/150.0);
glVertex2f((19-150)/150.0,-(52-150)/150.0);

    glEnd(); // 程式結束
    glutSwapBuffers();
}

void mouse(int button,int state,int x,int y){
    if(state==GLUT_DOWN)// 按下去時產生座標,減一半,除一半.0,y 要加負號
        printf("glVertex2f((%d-150)/150.0,-(%d-150)/150.0);\n",x,y); // 顯示
    }
}


int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week03");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMainLoop();
}






oof .jpg





利用小畫家,點擊位置取得座標,畫出相對應彩色圖形⏫⏬


#include <GL/glut.h>
#include <stdio.h>
float teapotX=0,teapotY=0;

void display()
{
    glClearColor(0/255.0, 0/255.0, 0/255.0, 1);     //底層顏色
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBegin(GL_POLYGON);     //開始

    glColor3f(255/255.0, 255/255.0, 26/255.0);     //圖層顏色
    glVertex2f((40-150)/150.0,-(40-150)/150.0);
    glVertex2f((40-150)/150.0,-(245-150)/150.0);
    glVertex2f((245-150)/150.0,-(245-150)/150.0);
    glVertex2f((245-150)/150.0,-(40-150)/150.0);

    glEnd();      //結束,從開始到結束算一塊圖形

    glutSwapBuffers();
}

void mouse(int button,int state,int x,int y){
    if(state==GLUT_DOWN){
        printf("glVertex2f((%d-150)/150.0,-(%d-150)/150.0);\n",x,y);
    }
}

int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week03");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMainLoop();
}


畫圓形⏬



 glBegin(GL_POLYGON);     //開始

    glColor3f(255/255.0, 255/255.0, 26/255.0);   //圖層顏色

    for(float angle=0;angle<=2*3.1415;angle+=0.01){     //可以調整是否為半圓形
             glVertex2f(0.3*cos(angle),0.7*sin(angle));     //x和y軸的縮放
    }

    glEnd();      //結束,從開始到結束算一塊圖形






沒有留言:

張貼留言