2022年3月8日 星期二

END之書 week03

1. 範例 http://jsyeh.org/3dcg10/


1.1 download   "data"、"win32"

      windows.zip => 下載 => windows => Transformation.exe

      data.zip => 下載 => windows => data => 模型.obj  


1.2 執行 Transformation.exe   => 右上右鍵(換模型)





2-1.  codeblock => file => project => .......freeglut

     確認可執行 => delete all => copy上週的茶壺




2-2. 修改



#include <GL/glut.h>


void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix(); ///備份舊矩陣位置,因為會修改矩陣,所以移動會累積

    glTranslatef(0.5,0.5,0);///往右上角跑

    glColor3d(1,0,1);///色彩


    glutSolidTeapot(0.6);

    glPopMatrix(); ///還原舊矩陣

    glutSwapBuffers();

}


int main(int argc, char *argv[])

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);


    glutCreateWindow("week03 移動茶壺");


    glutDisplayFunc(display);


    glutMainLoop();

}


2-3. 四個茶壺



#include <GL/glut.h>

void myTeapot(float x,float y)

{

    glPushMatrix(); ///備份舊矩陣位置,因為會修改矩陣,所以移動會累積

    glTranslatef(x,y,0);

    glColor3d(1,0,0);///色彩

    glutSolidTeapot(0.3);

    glPopMatrix(); ///還原舊矩陣

}

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    myTeapot(0.5,0.5);

    myTeapot(-0.5,-0.5);

    myTeapot(-0.5,0.5);

    myTeapot(0.5,-0.5);

    glutSwapBuffers();

}


int main(int argc, char *argv[])

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);


    glutCreateWindow("week03 移動茶壺");


    glutDisplayFunc(display);


    glutMainLoop();

}

2-4  滑鼠移動(只限點,押著不行)




#include <GL/glut.h>

float mouseX=0,mouseY=0;///新加
void myTeapot(float x,float y)
{
    glPushMatrix();
    glTranslatef(x,y,0);
    glColor3d(1,0,0);
    glutSolidTeapot(0.3);
    glPopMatrix();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    myTeapot(  (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 移動茶壺");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);///新加,滑鼠
    glutMainLoop();
}


2-5 印出滑鼠座標(按一放一)




#include <GL/glut.h>
#include <stdio.h>///printf用
float mouseX=0,mouseY=0;
void myTeapot(float x,float y)
{
    glPushMatrix();
    glTranslatef(x,y,0);
    glColor3d(1,0,0);
    glutSolidTeapot(0.3);
    glPopMatrix();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    myTeapot(  (mouseX-150)/150.0,-(mouseY-150)/150.0  );///視窗預設300*300
    glutSwapBuffers();
}
void mouse(int button,int state,int x,int y)
{
    printf("%d %d %d %d\n",button,state,x,y);///印出滑鼠座標,按一個,放一個
    mouseX=x,mouseY=y;
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week03 移動茶壺");

    glutDisplayFunc(display);
    glutMouseFunc(mouse);///滑鼠

    glutMainLoop();
}


沒有留言:

張貼留言

VERY BEAUTIFUL, VERY POWERFUL

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