#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
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);
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)
{
///1000msec 50fps=20msec
glutTimerFunc(20,timer,0);
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutCreateWindow("3D");
glutDisplayFunc(display); ///顯示
glutTimerFunc(0,timer,0);
myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
glutMainLoop();
}
茶壺旋轉(速度固定)
#include <GL/glut.h>
#include <math.h>
float angle=0;
void timer(int t)
{
glutTimerFunc(10,timer,0);
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.1);
glPopMatrix();
glFlush();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutCreateWindow("02160943");
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,0);
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.1);
glPopMatrix();
glFlush();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutCreateWindow("02160943");
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,0);
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.05);
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("02160943");
glutReshapeWindow(700,700);
glutDisplayFunc(display);
glutTimerFunc(10,timer,0);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
沒有留言:
張貼留言