##week04_mouse_glScalef
1.安裝freeglut,同時把lib\libfreeglut複製成libglut32.a
2.File-New-Project,GULT專案 week04_mouse_glScalef
3.茶壺放大縮小
程式碼:
#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();
}
##試著交換TransFormation
1.試課本範例Transformation
2.先把translate移到右方,再rotate
3,下方程式,按右鍵,可swap translate/rotate交換
4.再rotate看看,差在哪裡
5.公轉自轉
6.旋轉軸不一樣
7.好像是把一個字轉的人搬到右邊去
8.好像是把桌上的大圓盤把東西放上去轉
##理解技巧
1.把左耳靠著左肩,再看程式碼(從上往下看)
2.把旋轉中 長胖的藍色的車子 移到右邊去
##week04_keyboard_mouse_motion
程式碼:
#include <GL/glut.h>
#include <stdio.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
void keyboard(unsigned char key,int x,int y){
printf("key:%dx:%dy:%d\n",key,x,y);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
##很多個茶壺程式碼:
#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:%dx:%dy:%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_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
}
沒有留言:
張貼留言