1-1.先去下載windows.zip跟data.zip然後把data放進windows 並開啟transformation 嘗試調整比較下方的 swap translates跟swap rotatef(自轉VS公轉)
2-1複製上週程式碼並增加顯示位置程式碼:
#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("Week06 keyboard");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
2-2 新增mouse跟motion函式
2-3把mouse函式根motion裡面的東西新增 並設定mouseX跟mouseY oldX跟oldY變數來調整滑鼠拖曳茶壺
程式碼:
#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);
glColor3f(1,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void keyboard ( unsigned char key, int mouseX,int mouseY)
{
printf("現在按下:%c 座標在:%d %d\n",key ,x ,y);
}
void mouse(int buuton, 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();
}
2-4註解掉滑鼠移動 並新增放大縮小
程式碼:
#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);
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)
{
printf("現在按下:%c 座標在:%d %d\n",key ,x ,y);
}
void mouse(int buuton, 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;
//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();
}
2-5
程式碼:
#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();
glScalef(scale,scale,scale);
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)
{
//printf("現在按下:%c 座標在:%d %d\n",key ,x ,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 buuton, 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(mouseY>oldY)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();
}
更改背景為灰色,並把程式碼調整成可以調整大小跟位置以及旋轉
程式碼:
#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);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glScalef(scale,scale,scale);
glTranslatef((x-250)/250.0,-(y-250)/250.0,z);
glRotatef(angle,0,0,1);
glColor3f(1,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void keyboard ( unsigned char key, int mouseX,int mouseY)
{
//printf("現在按下:%c 座標在:%d %d\n",key ,x ,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 buuton, 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(mouseY>oldY)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();
}
沒有留言:
張貼留言