- 貼圖的範例程式碼跟輸出
- 安裝openCV2.1步驟
安裝在預設路徑是為了讓codeblock設定好用哦
因為openCV2.1在較舊或較新的版本皆適用,
所以小葉老師建議大家以後要安裝都安裝2.1~
不然其他都會有一點點地方不適用會很麻煩呢
- 設定codeblock路徑
- 上課範例程式碼
#include <opencv/highgui.h> //貼圖
const GLfloat light_ambient[] = { 0.5f, 0.5f, 0.0f, 0.0f };
const GLfloat light_diffuse[] = { 0.8f, 0.42f, 0.31f, 0.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 0.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 1.00f, 1.00f, 1.00f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.0f, 0.5f, 1.0f };
const GLfloat mat_specular[] = { 0.61f, 0.44f, 0.54f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
int x=150, y=150;
float ang=0;
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef((x-150)/150.0, -(y-150)/150.0, 0);
glRotatef(ang, 0, 1, 0);
IplImage * img=cvLoadImage("earth.jpg"); //貼圖
cvShowImage("opencv windows", img);//貼圖
cvWaitKey(0);//圖片停留,不然會閃退
glutSolidTeapot(0.3); //圖要放裡面
glPopMatrix();
ang+=5;
glutSwapBuffers();
}
void motion(int nx, int ny)
{
x=nx;
y=ny;
display();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("06160982_WK5");
glutDisplayFunc(display);
glutIdleFunc(display); //如果畫面停滯,做display
glutMotionFunc(motion);
glClearColor(1,0.5,0.5,1); //light 的 int main()開頭
//背景顏色
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess); //light 的 int main() 結尾
glutMainLoop();
}
沒有留言:
張貼留言