2022年3月8日 星期二

電腦圖學week03

  Week03

 1. 到小葉的網址 https://jsyeh.org/3dcg10

    下載三個檔案 data. zip , windows zip ,glut32.d11
     
     再將windows.zip、data.zip解壓縮

2.執行 Transformation.exe

       右上角:按右鍵換模型

        下方:glTranslatef (左右,上下,前後)




    





茶壺

1.下載glut檔案,並開啟新Project






2.
執行上星期的茶壺程式碼,並試著改變茶壺位置




















3.製作出四個茶壺





















#include <GL/glut.h>

void myTeapot(float x,float y)

{

    glPushMatrix();///備份矩陣(備份舊的位置)

        glTranslatef(x,y,0);

        glColor3f(1,1,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();

}

茶壺能跟著鼠標移動








#include <GL/glut.h>

float mouseX=0,mouseY=0;

void myTeapot(float x,float y)

{

    glPushMatrix();///備份矩陣(備份舊的位置)

        glTranslatef(x,y,0);

        glColor3f(1,1,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();

}

新增座標


#include <GL/glut.h>
#include <stdio.h> ///printf()印東西用的
float mouseX=0, mouseY=0;///新加的!!!!
void myTeapot( float x , float y )
{
glPushMatrix();///備份矩陣,他會記住舊的位置
glTranslatef(x,y,0);
glColor3f(1,1,0); ///黃色
glutSolidTeapot(0.1); ///變成小茶壺
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 ) ///滑鼠x座標,滑鼠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...