- 安裝OpenCV
- 新增 GLUT專案
- 開啟Codeblocks>>開啟new project>>OpenGL
- 設定好 freeglut
- 刪減範例程式
- 網址:http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
- 下載 [source]
- 將[glm.c]更改為[glm.cpp]
- 將[glm.cpp]和[glm.h]拉到與專案同一層
- 在codeblocks裡加入[glm.cpp]
- 把 Gundam 的 OBJ 和 MTL 檔 和圖檔拉入專案資料夾
- #include <GL/glut.h>
- #include "glm.h" ///使用 glm的外掛
- GLMmodel * model=NULL;///指標變數, 指到 3D model
- static void resize(int width, int height)
- {
- const float ar = (float) width / (float) height;
- glViewport(0, 0, width, height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
- ///這個程式會從0,0,0看出去...我們的模式也會放0,0,0,衝突
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity() ;
- gluLookAt( 0,0,2, 0,0,0, 0,1,0);///我們從0,0,2看中心
- }
- static void display(void)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- ///glutSolidTeapot(1);/// 放Teapot,等下要放 Gundam
- glmDraw(model, GLM_SMOOTH|GLM_TEXTURE);
- glPopMatrix();
- glutSwapBuffers();
- }
- ///打光留下來
- const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
- const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
- const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
- const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
- const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- const GLfloat high_shininess[] = { 100.0f };
- int main(int argc, char *argv[])
- {
- glutInit(&argc, argv);
- glutInitWindowSize(640,480);
- glutInitWindowPosition(10,10);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("GLUT Shapes");
- glutReshapeFunc(resize);
- glutDisplayFunc(display);
- 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);
- ///我們要把 OBJ檔讀進來 (glm.cpp幫忙)
- model = glmReadOBJ("Gundam.obj");///讀檔
- glmUnitize(model);/// 縮成 -1...+1範圍
- glutMainLoop();
- return EXIT_SUCCESS;
- }

- 加上貼圖
- #include <opencv/highgui.h> ///貼圖要用
- #include <opencv/cv.h> ///貼圖要用
- #include <GL/glut.h>
- #include "glm.h" ///TODO: 使用 glm的外掛
- GLMmodel * model=NULL;///TODO: 指標變數, 指到 3D model
- int myTexture(char *filename)
- {
- IplImage * img = cvLoadImage(filename); ///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);
- return id;
- }
- static void resize(int width, int height)
- {
- const float ar = (float) width / (float) height;
- glViewport(0, 0, width, height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
- ///這個程式會從0,0,0看出去...我們的模式也會放0,0,0,衝突
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity() ;
- gluLookAt( 0,0,2, 0,0,0, 0,1,0);///TODO: 我們從0,0,2看中心
- }
- static void display(void)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPushMatrix();///TODO:
- ///glutSolidTeapot(1);///TODO: 放Teapot,等下要放 Gundam
- glmDraw(model, GLM_SMOOTH|GLM_TEXTURE);///TODO:
- glPopMatrix();///TODO:
- glutSwapBuffers();
- }
- ///打光留下來
- const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
- const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
- const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
- const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
- const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- const GLfloat high_shininess[] = { 100.0f };
- int main(int argc, char *argv[])
- {
- glutInit(&argc, argv);
- glutInitWindowSize(640,480);
- glutInitWindowPosition(10,10);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("GLUT Shapes");
- glutReshapeFunc(resize);
- glutDisplayFunc(display);
- 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);
- ///TODO:我們要把 OBJ檔讀進來 (glm.cpp幫忙)
- model = glmReadOBJ("Gundam.obj");///TODO:讀檔
- glmUnitize(model);///TODO: 縮成 -1...+1範圍
- myTexture("Diffuse.jpg");
- glutMainLoop();
- return EXIT_SUCCESS;
- }

- MAYA 如何存各個部位
2020電腦圖學 Computer Graphics 授課教師: 葉正聖 銘傳大學資訊傳播工程系 每週主題: 程式環境、點線面顏色、移動/旋轉/縮放與矩陣(Matrix)、階層性關節轉動(T-R-T)、做出機器人、打光、貼圖、glu/glut函式、鍵盤、滑鼠、計時器(timer)、讀入3D模型、粒子系統、聲音、特效、投影矩陣、攝影機與運鏡、機器人2.0、期末作品
2020年6月17日 星期三
(*^ω^*) 喵~Week17
➽ 複習+期末作品
訂閱:
張貼留言 (Atom)






沒有留言:
張貼留言