Week03
1. 到小葉的網址 https://jsyeh.org/3dcg10
右上角:按右鍵換模型
下方:glTranslatef (左右,上下,前後)
2.執行上星期的茶壺程式碼,並試著改變茶壺位置
#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(); | |
| } |
![]() |






沒有留言:
張貼留言