2022年3月8日 星期二

新手上碌_week03

 1.早鳥福利_利用sin(),cos()函數畫出圓形

#include <math.h>        //sin(),cos()

#include <GL/glut.h>

void mycircle(float r){

    glBegin(GL_POLYGON);    //多邊型

    for(float a=0 ; a<3.1415926*2 ; a+=0.01){        //範圍(弧度,圓或是半圓)

        glVertex2f(    r*cos(a) ,    r*sin(a)    );    //r:半徑(圓的大小)

    }

    glEnd();        //    結束畫

}

void display(){

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glCcolr3f(1,1,0);            //顏色

    mycircle(2);


    glCcolr3f(0,1,1);           

    mycircle(0.6);


    glutSwapBuffers();

}

int main(int argc,char**argv){

}

2.移動圖形    glTranslate(x,y,z)

#include <GL/glut.h>

void myteatop(float x,float y){         ///把它變成函式就不用一直打

    glPushMatrix();///備份矩陣

    ///移動會累積,因為它會修改矩陣

        glTranslatef(x, y , 0);//x,y,z方向

        glColor3f(1,1,0);

        glutSolidTeapot(0.3);

    glPopMatrix();///還原矩陣(還原舊的位置)

}

void display(){

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    /*glPushMatrix();///備份矩陣

    ///移動會累積,因為它會修改矩陣

        glTranslatef(0.5, 0.5 , 0);//x,y,z方向

        glColor3f(1,1,0);

        glutSolidTeapot(0.3);

    glPopMatrix();///還原矩陣(還原舊的位置)

    */

    myteatop(0.5,0.5);

    myteatop(-0.5,0.5);

    myteatop(0.5,-0.5);

    myteatop(-0.5,-0.5);

    glutSwapBuffers();

}

int main(int argc,char**argv){

    glutInit(&argc,argv);

    

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week03_translate");


    glutDisplayFunc(display);

    glutMainLoop();

}



3.用滑鼠控制位置      glutMouseFunc()
#include <GL/glut.h>
float mouseX=0,mouseY=0;
void myteatop(float x,float y){
    glPushMatrix();///備份矩陣
    ///移動會累積,因為它會修改矩陣
        glTranslatef(x, y , 0);//x,y,z方向
        glColor3f(1,1,0);
        glutSolidTeapot(0.3);
    glPopMatrix();///還原矩陣(還原舊的位置)
}
void display(){
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    myteatop(    (mouseX-150)/150.0,   -(mouseY-150)/150.0    );
    glutSwapBuffers();
}
void mouse(int button,int state,int x,int y){
    mouseX=x;
    mouseY=y;
}
int main(int argc,char**argv){
    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week03_mouse");

    glutDisplayFunc(display);
    glutMouseFunc(mouse);

    glutMainLoop();
}
<原本的位置>

<滑鼠點擊後>

3.Windows,mouse坐標軸與圖學的座標軸_換算
windows座標=mouse座標    (0~300,0~300)
座標換算成3D世界    (-1~1,-1~1)    之口訣:減一半,除一半,Y顛倒(*-1)




4.如何使用上述方法畫圖:
    小畫家大小設為300*300 >>>可用滑鼠找座標
    小畫家滴管  >>> 找顏色
滑鼠事件下用printf(  "  (%d-150)/150.0,    -(%d-150)/150.0",x,y   \n );    輸出得知x,y座標
>>>在小黑視窗中連續點擊你要的點
>>>複製輸出的程式碼
>>>在圓程式碼上貼上即顯示剛剛點擊的範圍













沒有留言:

張貼留言

VERY BEAUTIFUL, VERY POWERFUL

一.     一樣先安裝且設定好freeglut,OpecCV, 開啟CodeBlocks建立新專案 week11_gundam,                 把 MyGundam.zip下載解壓縮後的data資料夾放到freeglut/bin裡面 把week09_openc...