- 電腦圖學之父 - Ivan Sutherland (伊凡·蘇澤蘭)
- 課堂範例: https://jsyeh.org/3dcg10/
1. 自轉
2. 公轉
- Keyboard函式 (鍵盤)
#include <GL/glut.h>
#include <stdio.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1, 0, 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("week06-keyboard");
glutDisplayFunc(display);
glutKeyboardFunc(Keyboard);
glutMainLoop();
}
- Keyboard+Mouse+Motion 函式
#include <GL/glut.h>
#include <stdio.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1, 0, 0);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
void Keyboard(unsigned char key, int x, int y)
{
printf("現在按下:%c 座標在:%d %d\n",key, x, y);
}
void mouse(int button, int state, int x, int y)
{
}
void motion(int x, int y)
{
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week06-keyboard");
glutDisplayFunc(display);
glutKeyboardFunc(Keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
2. 移動位置
#include <GL/glut.h>
#include <stdio.h>
float x=0, y=0, z=0, oldX, oldY;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef( (x-150)/150.0, -(y-150)/150.0, z); ///Maya => w
glColor3f(1, 0, 0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void Keyboard(unsigned char key, int x, int y)
{
printf("現在按下:%c 座標在:%d %d\n",key, x, y);
}
void mouse(int button, int state, int mouseX, int mouseY)
{
oldX= mouseX;
oldY= 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("week06-keyboard");
glutDisplayFunc(display);
glutKeyboardFunc(Keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
3. 調整縮放
#include <GL/glut.h>
#include <stdio.h>
float x=0, y=0, z=0, scale=1.0, oldX, oldY;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glScalef(scale, scale, scale); ///Maya => r
glColor3f(1, 0, 0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void Keyboard(unsigned char key, int x, int y)
{
printf("現在按下:%c 座標在:%d %d\n",key, x, y);
}
void mouse(int button, int state, int mouseX, int mouseY)
{
oldX= mouseX;
oldY= mouseY;
}
void motion(int mouseX, int mouseY)
{
if(mouseX>oldX) scale= scale * 1.01;
if(mouseX<oldX) scale= scale * 0.99;
oldX= mouseX; oldY= mouseY;
display();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week06-keyboard");
glutDisplayFunc(display);
glutKeyboardFunc(Keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
4. 鍵盤 (w/W) 移動、(e/E) 旋轉、(r/R) 縮放 切換想要的行為
#include <GL/glut.h>
#include <stdio.h>
float x=0, y=0, z=0, scale=1.0, angle=0.0, oldX, oldY;
int now=1;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef( (x-150)/150.0, -(y-150)/150.0, z); ///Maya => w
glRotatef(angle, 0, 0, 1); ///Maya => e
glScalef(scale, scale, scale); ///Maya => r
glColor3f(1, 0, 0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void Keyboard(unsigned char key, int x, int y)
{
if(key == 'w' || key == 'W') now=1; ///移動
if(key == 'e' || key == 'E') now=2; ///旋轉
if(key == 'r' || key == 'R') now=3; ///縮放
}
void mouse(int button, int state, int mouseX, int mouseY)
{
oldX= mouseX;
oldY= mouseY;
}
void motion(int mouseX, int mouseY)
{
if(now == 1){ ///移動
x += (mouseX-oldX);
y += (mouseY-oldY);
}
else if(now == 3){ ///縮放
if(mouseX>oldX) scale= scale * 1.01;
if(mouseX<oldX) scale= scale * 0.99;
}
oldX= mouseX; oldY= mouseY;
display();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week06-keyboard");
glutDisplayFunc(display);
glutKeyboardFunc(Keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
5. 旋轉 + 調整視窗大小,並將茶壺原是位置移至正中心
#include <GL/glut.h>
#include <stdio.h>
float x=250, y=250, z=0, scale=1.0, angle=0.0, oldX, oldY;
int now=1;
void display()
{
glClearColor(0.5, 0.5, 0.5, 1); ///用來clear的色彩 R G B A 其中Alpha(半透明)暫沒用到
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef( (x-250)/250.0, -(y-250)/250.0, z); ///Maya => w
glRotatef(angle, 0, 0, 1); ///Maya => e
glScalef(scale, scale, scale); ///Maya => r
glColor3f(1, 1, 0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void Keyboard(unsigned char key, int x, int y)
{
if(key == 'w' || key == 'W') now=1; ///移動
if(key == 'e' || key == 'E') now=2; ///旋轉
if(key == 'r' || key == 'R') now=3; ///縮放
}
void mouse(int button, int state, int mouseX, int mouseY)
{
oldX= mouseX;
oldY= mouseY;
}
void motion(int mouseX, int mouseY)
{
if(now == 1){ ///移動
x += (mouseX-oldX);
y += (mouseY-oldY);
}
else if(now == 2){
angle += (mouseX-oldX);
}
else if(now == 3){ ///縮放
if(mouseX>oldX) scale= scale * 1.01;
if(mouseX<oldX) scale= scale * 0.99;
}
oldX= mouseX; oldY= mouseY;
display();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("week06-keyboard");
glutDisplayFunc(display);
glutKeyboardFunc(Keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
















沒有留言:
張貼留言