step 0 :下載模型
0.0 jsyeh.org/3dcg10 下載 data.zip , windows.zip
0.1 data.zip =>下載/windows/data
windows.zip => 下載/windows/Transformation.exe
step 1 : 鍵盤點出位置
1.1開新專案 命名為week05_keyboard
1.2使用keyboardFunc函式 , 按鍵盤可以在小黑視窗顯示滑鼠所在位置及按鍵盤的字母
1.3貼上程式碼
#include <GL/glut.h>
#include <stdio.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,0);
glutSolidTeapot( 0.3 );
glutSwapBuffers();
}
void keyboard( unsigned char key , int x ,int y)
{
printf("你按下了 %c 在 %d %d座標\n",key , x, y);
}
int main(int argc , char**argv)
{
glutInit(&argc ,argv);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("week05 keyboard");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
step 2 : keyboard + mouse + motion
2.1 接續week05_keyboard , 開新專案 week05_keyboard_mouse_motion
2.2接續上個程式碼 , 新增mouse , motion 函式
2.3貼上程式碼
#include <GL/glut.h>
#include <stdio.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,0);
glutSolidTeapot( 0.3 );
glutSwapBuffers();
}
void keyboard( unsigned char key , int mouseX ,int mouseY )
{
}
void mouse (int button , int state , int mouseX , int mouseY)
{
}
void motion (int mouseX ,int mouseY)
{
}
int main(int argc , char**argv)
{
glutInit(&argc ,argv);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("week05 keyboard");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
2.4 新增motion函式內容
#include <GL/glut.h>
#include <stdio.h>
float x=0 , y=0 , z=0 ;///設定初始值
float oldX=0 , oldY=0;///設定初始值
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef( (x-150)/150.0, -(y-150)/150.0, z);
glColor3f(1,1,0);
glutSolidTeapot( 0.3 );
glPopMatrix();
glutSwapBuffers();
}
void keyboard( unsigned char key , int mouseX ,int mouseY)
{
}
void mouse(int button , int state , int mouseX , int mouseY)
{
}
void motion (int mouseX ,int mouseY)
{
x+=(mouseX-oldX); y+=(mouseY-oldY);
oldX=mouseX; oldY=mouseY;
display();
}
int main(int argc , char**argv)
{
glutInit(&argc ,argv);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("week05 keyboard");
glutDisplayFunc(display);
glutMotionFunc(motion); ///motion用
glutMouseFunc(mouse); ///mouse用
glutKeyboardFunc(keyboard); keyboard用
glutMainLoop();
}
沒有留言:
張貼留言