WEEK04
1.縮放茶杯程式碼
#include <GL/glut.h>
float s=1;
void display()
{
glClearColor(1,1,0.9,1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(0,1,0);
glPushMatrix();
glScalef(s,s,s);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
s=1+(x-150)/150.0;
display();
}
int main(int argc,char*argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week04 mouse glScalef");
glutDisplayFunc(display);
glutMotionFunc(motion);glutMainLoop();
}
2.試者交換Translate Rolate 兩行程式
Step01 Translate move to right rolate-公轉
Step02 右鍵,swap Translate/rolate -自轉
差別在於更改旋轉軸 第一個在物體外 另一個則在物體內
2-1.茶壺自轉程式碼
#include <GL/glut.h>
float teapotX=0.5,teapotY=0;
float angle=0;
float s=0.3;
void display()
{
glClearColor(1,1,0.9,1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(0,1,0);
glPushMatrix();
glTranslatef(teapotX,teapotY,0);
glRotatef(angle++,0,0,1);
glScalef(s,s,s);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
s=1+(x-150)/150.0;
display();
}
int main(int argc,char*argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week04 mouse glScalef");
glutIdleFunc(display);
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
*公轉程式碼 將第13,14行交換即有改變旋轉軸的效果\
3.鍵盤滑鼠(按一下就會出現一個茶壺)
#include <GL/glut.h>
#include <stdio.h>
int N=0;
float teapotX[1000],teapotY[1000];
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
for(int i=0;i<N;i++){
glPushMatrix();
glTranslatef( teapotX[i],teapotY[i],0);
glutSolidTeapot(0.1);
glPopMatrix();
}
glutSwapBuffers();
}
void keyboard(unsigned char key,int x,int y){
printf("key:%c x:%d\n",key,x,y);
}
void mouse(int button,int state,int x,int y){
teapotX[N]=(x-150)/150.0;
teapotY[N]=(y-150)/150.0;
N++;
}
int main(int argc,char*argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week04 keyboard mouse motion");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);///鍵盤函式
glutMouseFunc(mouse);///滑鼠函式
glutMainLoop();
}
沒有留言:
張貼留言