#include <GL/glut.h>
#include <math.h>
float angle=0,potX=0,potY=0,potVX=0,potVY=0;
int potState=0;
void timer(int t)
{
glutTimerFunc(10,timer,t+1);
angle+=1;
if(potState==0)potY=0.5*sin(angle/180.0*3.14);
if(potState==2){potX+=potVX; potY+=potVY;}
glutPostRedisplay();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.01);
glPushMatrix();
glTranslatef(potX,potY,0);
glRotatef(angle,0,1,0);
glutSolidTeapot(0.01);
glPopMatrix();
glFlush();
}
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN)
{
potState=1;
}
else if(state==GLUT_UP)
{
potState=2;
potVX=-(x-350)/3500.0;
potVY=(y-350)/3500.0;
}
}
void motion(int x,int y)
{
if(potState==1)
{
potX=(x-350)/350.0;
potY=-(y-350)/350.0;
}
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutCreateWindow("3D");
glutReshapeWindow(700,700);
glutDisplayFunc(display);
glutTimerFunc(10,timer,0);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
2015 電腦圖學 Computer Graphics 授課教師: 葉正聖 銘傳大學資訊傳播工程系 每週主題: 程式環境、點線面顏色、移動/旋轉/縮放與矩陣(Matrix)、階層性關節轉動(T-R-T)、做出機器人、打光、貼圖、glu/glut函式、鍵盤、滑鼠、計時器(timer)、讀入3D模型、粒子系統、聲音、特效、投影矩陣、攝影機與運鏡、機器人2.0、期末作品
2015年6月17日 星期三
2015年5月6日 星期三
Week11 02160580課堂作業
地球固定速度轉動
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include <stdio.h>
float angle=0; ///自動轉很帥
GLUquadric * quad;
void display()
{ glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///自動轉很帥
glRotatef(90, 1,0,0);
glRotatef(angle, 0,0,1);///自動轉很帥
gluQuadricTexture(quad, 1);
gluSphere(quad, 0.5, 30, 30); ///glutSolidTeapot(0.3);
glPopMatrix();///自動轉很帥
glFlush();
angle+=1;///自動轉很帥
}
void myInit()
{
quad = gluNewQuadric();
IplImage * img = cvLoadImage("image.jpg"); ///OpenCV讀圖
cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
glGenTextures(1, &id); /// 產生Generate 貼圖ID
glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
} ///最後一行最難/最重要, 所貼圖影像的資料都設定好
void timer(int t)
{/// 1000 msec 50fps:20msec
glutTimerFunc(20, timer, 0);
//printf("timer now\n");
glutPostRedisplay();
}
int main(int argc, char**argv)
{ glutInit(&argc, argv);
glutCreateWindow("3D");
glutDisplayFunc(display); ///顯示
//glutIdleFunc(display);///自動轉很帥 (閒閒沒事幹, 就重畫)
glutTimerFunc(0, timer, 0);
myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
glutMainLoop();
}
茶壺原地旋轉
#include <GL/glut.h>
float angle=0;
void timer(int t)
{
glutTimerFunc(10,timer,t+1);
angle+=1;
glutPostRedisplay();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,0,1,0);
glutSolidTeapot(0.5);
glPopMatrix();
glFlush();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutCreateWindow("3D");
glutDisplayFunc(display);
glutTimerFunc(10,timer,0);
glutMainLoop();
}
茶壺旋轉移動
#include <GL/glut.h>
#include <math.h>
float angle=0,potX=0,potY=0;
void timer(int t)
{
glutTimerFunc(10,timer,t+1);
angle+=1;
potX=cos(angle/180.0*3.14);
potY=sin(angle/180.0*3.14);
glutPostRedisplay();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(potX,potY,0);
glRotatef(angle,0,1,0);
glutSolidTeapot(0.5);
glPopMatrix();
glFlush();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutCreateWindow("3D");
glutDisplayFunc(display);
glutTimerFunc(10,timer,0);
glutMainLoop();
}
茶壺上下跳動
#include <GL/glut.h>
#include <math.h>
float angle=0,potX=0,potY=0;
void timer(int t)
{
glutTimerFunc(10,timer,t+1);
angle+=1;
//potX=cos(angle/180.0*3.14);
potY=0.5*sin(angle/180.0*3.14);
glutPostRedisplay();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(potX,potY,0);
glRotatef(angle,0,1,0);
glutSolidTeapot(0.5);
glPopMatrix();
glFlush();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutCreateWindow("3D");
glutDisplayFunc(display);
glutTimerFunc(10,timer,0);
glutMainLoop();
}
茶壺憤怒鳥
#include <GL/glut.h>
#include <math.h>
float angle=0,potX=0,potY=0,potVX=0,potVY=0;
int potState=0;
void timer(int t)
{
glutTimerFunc(10,timer,t+1);
angle+=1;
//potX=cos(angle/180.0*3.14);
if(potState==0)
potY=0.5*sin(angle/180.0*3.14);
if(potState==2)
{
potX+=potVX;
potY+=potVY;
}
glutPostRedisplay();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.01);
glPushMatrix();
glTranslatef(potX,potY,0);
glRotatef(angle,0,1,0);
glutSolidTeapot(0.01);
glPopMatrix();
glFlush();
}
void mouse(int button, int state, int x,int y)
{
if(state==GLUT_DOWN)
{potState=1;}
else if(state==GLUT_UP)
{
potState=2;
potVX=-(x-350)/3500.0;
potVY=(y-350)/3500.0;
}
}
void motion(int x,int y)
{
if(potState==1)
{
potX=(x-350)/350.0;
potY=-(y-350)/350.0;
}
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutCreateWindow("3D");
glutReshapeWindow(700,700);
glutDisplayFunc(display);
glutTimerFunc(10,timer,0);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
02163051_邱冠程 HW11
把茶壺換成球體,
丟上老師的程式
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include <stdio.h>
float angle=0; ///自動轉很帥
GLUquadric * quad;
void display()
{ glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///自動轉很帥
glRotatef(90, 1,0,0);
glRotatef(angle, 0,0,1);///自動轉很帥
gluQuadricTexture(quad, 1);
gluSphere(quad, 0.5, 30, 30); ///glutSolidTeapot(0.3);
glPopMatrix();///自動轉很帥
glFlush();
angle+=1;///自動轉很帥
}
void myInit()
{
quad = gluNewQuadric();
IplImage * img = cvLoadImage("image.jpg"); ///OpenCV讀圖
cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
glGenTextures(1, &id); /// 產生Generate 貼圖ID
glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
} ///最後一行最難/最重要, 所貼圖影像的資料都設定好
void timer(int t)
{/// 1000 msec 50fps:20msec
glutTimerFunc(20, timer, 0);
//printf("timer now\n");
glutPostRedisplay();
}
int main(int argc, char**argv)
{ glutInit(&argc, argv);
glutCreateWindow("3D");
glutDisplayFunc(display); ///顯示
//glutIdleFunc(display);///自動轉很帥 (閒閒沒事幹, 就重畫)
glutTimerFunc(0, timer, 0);
myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
glutMainLoop();
}
改變轉速跟角度
利用圖片的
void timer(int t)
{/// 1000 msec 50fps:20msec
glutTimerFunc(20, timer, 0);
//printf("timer now\n");
glutPostRedisplay();
}
憤怒茶壺
可以拉可以彈
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include <stdio.h>
#include <math.h>
float angle=0,potX=0,potY=0,potVX=0,potVY=0; ///自動轉很帥
int potState=0;
GLUquadric * quad;
void timer(int t)
{
glutTimerFunc(10, timer, t+1);
angle+=1;
if(potState==0)potY=0.5*sin(angle/180.0*3.14);
if(potState==2)
{
potX+=potVX;
potY+=potVY;
}
glutPostRedisplay();
}
void display()
{ glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.1);
glPushMatrix();///自動轉很帥
// glRotatef(90, 1,0,0);
glTranslatef(potX,potY,0);
glRotatef(angle, 0,1,0);///自動轉很帥
glutSolidTeapot(0.1);
//gluQuadricTexture(quad, 1);
// gluSphere(quad, 0.5, 30, 30); ///glutSolidTeapot(0.3);
glPopMatrix();///自動轉很帥
glFlush();
}
void mouse(int button,int state,int x,int y)
{
if(state==GLUT_DOWN){potState=1;}
else if(state==GLUT_UP){potState=2;potVX=-(x-350)/3500.0;potVY=(y-350)/3500.0;}
}
void motion(int x,int y)
{
if(potState==1){potX=(x-350)/350.0;potY=-(y-350)/350.0;}
}
int main(int argc, char**argv)
{ glutInit(&argc, argv);
glutCreateWindow("3D");
glutReshapeWindow(700,700);
glutDisplayFunc(display); ///顯示
glutTimerFunc(10, timer, 0);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
Week11 課堂作業, 許志遙
地球等速旋轉(加入計時器)
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include <stdio.h>
float angle=0; ///自動轉很帥
GLUquadric * quad;
void display()
{ glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///自動轉很帥
glRotatef(90, 1,0,0);
glRotatef(angle, 0,0,1);///自動轉很帥
gluQuadricTexture(quad, 1);
gluSphere(quad, 0.5, 30, 30); ///glutSolidTeapot(0.3);
glPopMatrix();///自動轉很帥
glFlush();
angle+=1;///自動轉很帥
}
void myInit()
{
quad = gluNewQuadric();
IplImage * img = cvLoadImage("earth.jpg"); ///OpenCV讀圖
cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
glGenTextures(1, &id); /// 產生Generate 貼圖ID
glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
} ///最後一行最難/最重要, 所貼圖影像的資料都設定好
void timer(int t)
{/// 1000 msec 50fps:20msec
glutTimerFunc(20, timer, 0);
//printf("timer now\n");
glutPostRedisplay();
}
int main(int argc, char**argv)
{ glutInit(&argc, argv);
glutCreateWindow("earth");
glutDisplayFunc(display); ///顯示
//glutIdleFunc(display);///自動轉很帥 (閒閒沒事幹, 就重畫)
glutTimerFunc(0, timer, 0);
myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
glutMainLoop();
}
茶壺上下繞圈旋轉
#include <GL/glut.h>
#include <math.h>
float angle=0, potX=0, potY=0;
void timer(int t){
glutTimerFunc(10, timer, t+1);
angle+=1; //每次timer轉1度
potX=cos(angle/180.0*3.14);
potY=0.5*sin(angle/180.0*3.14);
glutPostRedisplay();
}
void display(){
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(potX, potY, 0);
glRotatef(angle, 0,1,0); //angle轉動
glutSolidTeapot(0.5);
glPopMatrix();
glFlush();
}
int main(int argc, char**argv){
glutInit(&argc, argv);
glutCreateWindow("3D");
glutDisplayFunc(display);
glutTimerFunc(10,timer,0);
glutMainLoop();
}
茶壺上下旋轉 ※註解掉紅色的
#include <GL/glut.h>
#include <math.h>
float angle=0, potX=0, potY=0;
void timer(int t){
glutTimerFunc(10, timer, t+1);
angle+=1; //每次timer轉1度
//potX=cos(angle/180.0*3.14);
potY=0.5*sin(angle/180.0*3.14);
glutPostRedisplay();
}
void display(){
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(potX, potY, 0);
glRotatef(angle, 0,1,0); //angle轉動
glutSolidTeapot(0.5);
glPopMatrix();
glFlush();
}
int main(int argc, char**argv){
glutInit(&argc, argv);
glutCreateWindow("3D");
glutDisplayFunc(display);
glutTimerFunc(10,timer,0);
glutMainLoop();
}
#include <GL/glut.h>
#include <math.h>
float angle=0, potX=0, potY=0, potVX=0, potVY=0;
int potState=0;
void timer(int t){
glutTimerFunc(10, timer, t+1);
angle+=1; //每次timer轉1度
if(potState==0) potY=0.5*sin(angle/180.0*3.14);
if(potState==2){potX+=potVX; potY+=potVY;}
glutPostRedisplay();
}
void display(){
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.01);
glPushMatrix();
glTranslatef(potX, potY, 0);
glRotatef(angle, 0,1,0); //angle轉動
glutSolidTeapot(0.01);
glPopMatrix();
glFlush();
}
void motion(int x, int y){
if(potState==1){potX=(x-350)/350.0; potY=-(y-350)/350.0;}
}
void mouse(int button, int state, int x, int y){
if(state==GLUT_DOWN) potState=1;
else if(state==GLUT_UP){
potState=2;
potVX=-(x-350)/3500.0;potVY=(y-350)/3500.0;
}
}
int main(int argc, char**argv){
glutInit(&argc, argv);
glutCreateWindow("angry Teapot");
glutReshapeWindow(700,700);
glutDisplayFunc(display);
glutTimerFunc(10,timer,0);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
02162411_劉永健_week11
等速轉動的地球
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include <stdio.h>
float angle=0; ///自動轉很帥
GLUquadric * quad;
void display()
{ glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///自動轉很帥
glRotatef(90, 1,0,0);
glRotatef(angle, 0,0,1);///自動轉很帥
gluQuadricTexture(quad, 1);
gluSphere(quad, 0.5, 30, 30); ///glutSolidTeapot(0.3);
glPopMatrix();///自動轉很帥
glFlush();
angle+=1;///自動轉很帥
}
void myInit()
{
quad = gluNewQuadric();
IplImage * img = cvLoadImage("image.jpg"); ///OpenCV讀圖
cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
glGenTextures(1, &id); /// 產生Generate 貼圖ID
glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
} ///最後一行最難/最重要, 所貼圖影像的資料都設定好
void timer(int t)
{/// 1000 msec 50fps:20msec
glutTimerFunc(20, timer, 0);
//printf("timer now\n");
glutPostRedisplay();
}
int main(int argc, char**argv)
{ glutInit(&argc, argv);
glutCreateWindow("3D");
glutDisplayFunc(display); ///顯示
//glutIdleFunc(display);///自動轉很帥 (閒閒沒事幹, 就重畫)
glutTimerFunc(0, timer, 0);
myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
glutMainLoop();
}
Freeglut timer 利用timer旋轉的茶壺
#include <GL/glut.h>
float angle=0;
void timer(int t)
{
glutTimerFunc(10,timer,t+1);
angle+=1;
glutPostRedisplay();
}
void display()
{
glEnable(Gl_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,0,1,0);
glutSolidTeapot(0.5);
glPopMatrix();
glFlush();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutCreateWindow("3d");
glutDisplayFunc(display);
glutTimerFunc(10,timer,0);
glutMainLoop();
}
逆時針轉加自身旋轉的茶壺
#include <GL/glut.h>
#include <math.h>
float angle=0,potX=0,potY=0;
void timer(int t)
{
glutTimerFunc(10,timer,t+1);
angle+=1;
potX=cos(angle/180.0*3.14);
potY=0.5*sin(angle/180.0*3.14);
glutPostRedisplay();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(potX,potY,0);
glRotatef(angle,0,1,0);
glutSolidTeapot(0.5);
glPopMatrix();
glFlush();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutCreateWindow("3d");
glutDisplayFunc(display);
glutTimerFunc(10,timer,0);
glutMainLoop();
}
上下彈跳加自身旋轉的茶壺
#include <GL/glut.h>
#include <math.h>
float angle=0,potX=0,potY=0;
void timer(int t)
{
glutTimerFunc(10,timer,t+1);
angle+=1;
//potX=cos(angle/180.0*3.14);
potY=0.5*sin(angle/180.0*3.14);
glutPostRedisplay();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(potX,potY,0);
glRotatef(angle,0,1,0);
glutSolidTeapot(0.5);
glPopMatrix();
glFlush();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutCreateWindow("3d");
glutDisplayFunc(display);
glutTimerFunc(10,timer,0);
glutMainLoop();
}
憤怒的小茶壺
#include <GL/glut.h>
#include <math.h>
float angle=0, potX=0, potY=0, potVX=0, potVY=0;
int potState=0;
void timer(int t)
{
glutTimerFunc(10,timer, t+1);
angle+=1;
if(potState==0) potY=0.5*sin(angle/180.0*3.14);
if(potState==2){
potX+=potVX;
potY+=potVY;
}
glutPostRedisplay();
}
void display()
{ glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.02);
glPushMatrix();///自動轉很帥
glTranslatef(potX,potY,0);
glRotatef(angle, 0,1,0);///自動轉很帥
glutSolidTeapot(0.03);
glPopMatrix();///自動轉很帥
glFlush();
}
void mouse(int button, int state,int x, int y )
{
if(state==GLUT_DOWN) potState=1;
else if(state==GLUT_UP){
potState=2;
potVX=-(x-350)/3500.0;
potVY=(y-350)/3500.0;
}
}
void motion(int x,int y)
{
if(potState==1){
potX=(x-350)/350.0;
potY=-(y-350)/350.0;
}
}
int main(int argc, char**argv)
{ glutInit(&argc, argv);
glutCreateWindow("angry");
glutReshapeWindow(700,700);
glutDisplayFunc(display);
glutTimerFunc(10,timer,0);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
訂閱:
文章 (Atom)










