範例 http://jsyeh.org/3dcg10
1. 下載網頁中的data, win32
2. 將windows解壓縮, data.zip/data丟進解壓縮後的windows資料夾使用上週寫出茶壺的程式碼
1.在座標上畫出圖形
```
1.在座標上畫出圖形
```
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glPushMatrix();///備份矩陣
///移動會累積, 因為它會修改矩陣
glTranslatef(0.2, 0.6, 0.8);
glColor3f(1, 1, 0);
glutSolidTeapot(0.3);
glPopMatrix();///還原矩陣
/*備份與還原間要縮排*/
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit( &argc, argv);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("week_02");
glutDisplayFunc(display);
glutMainLoop();
}
結果:
```
#include <GL/glut.h>
void MyTeapot(float x, float y)
{
glPushMatrix();///備份矩陣
///移動會累積, 因為它會修改矩陣
/// glTranslatef(0.5,0.5,0.5)
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("week_02");
glutDisplayFunc(display);
glutMainLoop();
}
結果:
1.新增滑鼠事件,茶壺會移動到指定位置
```
#include <GL/glut.h>
#include <GL/glut.h>
float mouseX=0, mouseY=0;
void MyTeapot(float x, float y)
{
glPushMatrix();///備份矩陣
///移動會累積, 因為它會修改矩陣
/// glTranslatef(0.5,0.5,0.5)
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.0)/150.0, -(mouseY-150)/150.0 );
glutSwapBuffers();
}
void MouseClick(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("week_03");
glutMouseFunc(MouseClick);///滑鼠事件
glutDisplayFunc(display);
glutMainLoop();
```
#include <GL/glut.h>
#include <stdio.h>
float mouseX=0, mouseY=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
/// MyTeapot( (mouseX-150.0)/150.0, -(mouseY-150)/150.0 );
glutSwapBuffers();
}
void MouseClick(int button, int state, int x, int y)
{
mouseX=x; mouseY=y;
printf(" glVertex3f((%d-150.0)/150.0, -(%d-150)/150.0);\n", x, y);
}
int main(int argc, char** argv)
{
glutInit( &argc, argv);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("week_03_mouse");
glutMouseFunc(MouseClick);///滑鼠事件
glutDisplayFunc(display);
glutMainLoop();
}
沒有留言:
張貼留言