2. 複習: 貼圖
1. 下載OpenCV (裝好才能開CodeBlocks)
1. 去 https://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.1/
2. 下載 OpenCV-2.1.0-win32-vs2008.exe
3. 要選 "Add OpenCV to the system PATH for all users"
4. 要放的路徑: C:\OpenCV2.1
2. 先載入freeglut (跟第一週一樣)
1. Google search "freeglut windows"2. 從Transmission Zero 下載 "freeglut 3.0.0 MinGW Package"
3. 解壓縮後放到桌面
4. 在lib複製一個"libfreeglut.a"命名為"libglut32.a"
5. CodeBlocks 開
6. File -> New -> Project -> GLUT Project
7. 把location改成"C:\Users\user\Desktop\freeglut"
3. 先載入include, bin, lib
1. 在Build Option新增 2. 圖要放在freeglut的bin裏
** 記得名字要改好 不然會壞掉
4. 把程式碼貼上
#include opencvhighgui.h for cvCvtColor()#include opencvcv.h
#include GLglut.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(marvel.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 50fps20msec
glutTimerFunc(20, timer, 0);
printf(timer nown);
glutPostRedisplay();
}
int main(int argc, charargv)
{ glutInit(&argc, argv);
glutCreateWindow(3D);
glutDisplayFunc(display); 顯示
glutIdleFunc(display);自動轉很帥 (閒閒沒事幹, 就重畫)
glutTimerFunc(0, timer, 0);
myInit(); 我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
glutMainLoop();
}
5. 把程式碼貼上
#include <GL/glut.h>#include <opencv/highgui.h> ///OpenCV High GUI讀圖
///程式碼是對的, 但是沒有設定好include, lib, bin
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot( 0.3 );
glutSwapBuffers();
}
GLuint id;
void myInit()
{
glEnable(GL_TEXTURE_2D);
IplImage * img = cvLoadImage("marvel.jpg");
glGenTextures(1, &id );
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
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);
}
int main(int argc, char **argv)
{
///IplImage * img = cvLoadImage("marvel.jpg");
///cvShowImage("opencv", img);
///cvWaitKey(0);///等你按按鍵
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
glutCreateWindow("GLUT");///之後
///建好GLUT視窗,可以開始準備 3D 貼圖 (要在create...MainLoop才能建貼圖)
myInit();///我們的貼圖在這裡
glutDisplayFunc(display);
glutMainLoop();///之前
}
6. 把程式碼貼上
#include <GL/glut.h>#include <opencv/highgui.h> ///OpenCV High GUI讀圖
#include <opencv/cv.h> ///OpenCV cvCvtColor() to convert color
///程式碼是對的, 但是沒有設定好include, lib, bin
GLuint id1, id2;///NOW2
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, id1);///id1 for background
glBegin(GL_POLYGON);
glTexCoord2f(0, 0); glVertex3f(-1, +1, 0);
glTexCoord2f(0, 1); glVertex3f(-1, -1, 0);
glTexCoord2f(1, 1); glVertex3f(+1, -1, 0);
glTexCoord2f(1, 0); glVertex3f(+1, +1, 0);
glEnd();
glBindTexture(GL_TEXTURE_2D, id2);///id2 for teapot
glutSolidTeapot( 0.3 );
glutSwapBuffers();
}
///void myInit()
GLuint myTexture(char * filename)
{
IplImage * img = cvLoadImage(filename); ///NOW OpenCV
cvCvtColor(img, img, CV_BGR2RGB);///convert color ///NOW OpenCV
GLuint id;
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &id );
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
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;
}
int main(int argc, char **argv)
{
///IplImage * img = cvLoadImage("marvel.jpg");
///cvShowImage("opencv", img);
///cvWaitKey(0);///等你按按鍵
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
glutCreateWindow("GLUT");///之後
///建好GLUT視窗,可以開始準備 3D 貼圖 (要在create...MainLoop才能建貼圖)
id1 = myTexture("marvel.jpg"); ///myInit();///我們的貼圖在這裡
id2 = myTexture("teapot.jpg");
glutDisplayFunc(display);
glutMainLoop();///之前
}
7. 把程式碼貼上
#include <GL/glut.h>#include <opencv/highgui.h> ///OpenCV High GUI讀圖
#include <opencv/cv.h> ///OpenCV cvCvtColor() to convert color
///程式碼是對的, 但是沒有設定好include, lib, bin
GLuint id1, id2, id3;///NOW2
float a=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, id1);///id1 for background
glBegin(GL_POLYGON);
glTexCoord2f(0, 0); glVertex3f(-1, +1, 0);
glTexCoord2f(0, 1); glVertex3f(-1, -1, 0);
glTexCoord2f(1, 1); glVertex3f(+1, -1, 0);
glTexCoord2f(1, 0); glVertex3f(+1, +1, 0);
glEnd();
glBindTexture(GL_TEXTURE_2D, id3);///id3 for rotating square
glPushMatrix();
glTranslatef(0.5, 0.5, 0);
glRotatef(a++, 0, 0, 1);
glScalef(0.3, 0.3, 0.3);
glBegin(GL_POLYGON);
glTexCoord2f(0, 0); glVertex3f(-1, +1, 0);
glTexCoord2f(0, 1); glVertex3f(-1, -1, 0);
glTexCoord2f(1, 1); glVertex3f(+1, -1, 0);
glTexCoord2f(1, 0); glVertex3f(+1, +1, 0);
glEnd();
glPopMatrix();
glBindTexture(GL_TEXTURE_2D, id2);///id2 for teapot
glutSolidTeapot( 0.3 );
glutSwapBuffers();
}
///void myInit()
GLuint myTexture(char * filename)
{
IplImage * img = cvLoadImage(filename); ///NOW OpenCV
cvCvtColor(img, img, CV_BGR2RGB);///convert color ///NOW OpenCV
GLuint id;
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &id );
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
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;
}
int main(int argc, char **argv)
{
///IplImage * img = cvLoadImage("marvel.jpg");
///cvShowImage("opencv", img);
///cvWaitKey(0);///等你按按鍵
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
glutCreateWindow("GLUT");///之後
///建好GLUT視窗,可以開始準備 3D 貼圖 (要在create...MainLoop才能建貼圖)
id1 = myTexture("marvel.jpg"); ///myInit();///我們的貼圖在這裡
id2 = myTexture("teapot.jpg");
id3 = myTexture("square.jpg");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();///之前
}
3. 複習: 打光 、 模型
(沒模型)
1. 先載自己的模型 (放到freeglut的bin裏)
2. 點進去小葉的網頁: http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
3. 下載 Example裏的 source
4. 把souces .zip解壓縮
5. 把source裡的glm.h放到自己專案的資料夾裡
6. 把source裡的glm.c改成glm.cpp
4. 把souces .zip解壓縮
5. 把source裡的glm.h放到自己專案的資料夾裡
6. 把source裡的glm.c改成glm.cpp
7. 在CodeBlocks裡把glm.cpp加到自己專案
#include <GL/glut.h>
#include <opencv/highgui.h> ///OpenCV High GUI讀圖 ///貼圖需要它
#include <opencv/cv.h> ///OpenCV cvCvtColor() to convert color///貼圖需要它
///程式碼是對的, 但是沒有設定好include, lib, bin
GLuint id1, id2, id3;///NOW2 ///貼圖需要它
float a=0;
#include "glm.h"///呼叫外掛 ///glm需要它
GLMmodel * pmodel=NULL;///準備指標 ///glm需要它
GLMmodel * pmodel2=NULL;///準備指標 ///glm需要它
GLMmodel * pmodel3=NULL;///準備指標 ///glm需要它
GLMmodel * pmodel4=NULL;///準備指標 ///glm需要它
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, id1);///id1 for background ///貼圖需要它
glBegin(GL_POLYGON); ///貼圖需要它
glTexCoord2f(0, 0); glVertex3f(-1, +1, 0); ///貼圖需要它
glTexCoord2f(0, 1); glVertex3f(-1, -1, 0); ///貼圖需要它
glTexCoord2f(1, 1); glVertex3f(+1, -1, 0); ///貼圖需要它
glTexCoord2f(1, 0); glVertex3f(+1, +1, 0); ///貼圖需要它
glEnd();///貼圖需要它
if (!pmodel) { ///glm需要它
pmodel = glmReadOBJ("body.obj"); ///glm需要它
if (!pmodel) exit(0); ///glm需要它
glmUnitize(pmodel); ///glm需要它
glmFacetNormals(pmodel); ///glm需要它
glmVertexNormals(pmodel, 90.0); ///glm需要它
}
glDisable(GL_TEXTURE_2D);
glPushMatrix();
glRotatef(a++, 0,1,0);
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glutSwapBuffers();
}
///void myInit()
GLuint myTexture(char * filename) ///貼圖需要它
{
IplImage * img = cvLoadImage(filename); ///NOW OpenCV ///貼圖需要它
cvCvtColor(img, img, CV_BGR2RGB);///convert color ///NOW OpenCV ///貼圖需要它
GLuint id; ///貼圖需要它
glEnable(GL_TEXTURE_2D); ///貼圖需要它
glGenTextures(1, &id ); ///貼圖需要它
glBindTexture(GL_TEXTURE_2D, id); ///貼圖需要它
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); ///貼圖需要它
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); ///貼圖需要它
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;///貼圖需要它
}
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f }; ///glm需要它
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f }; ///glm需要它
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; ///glm需要它
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f }; ///glm需要它
int main(int argc, char **argv)
{
///IplImage * img = cvLoadImage("marvel.jpg");
///cvShowImage("opencv", img);
///cvWaitKey(0);///等你按按鍵
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
glutCreateWindow("GLUT");///之後
///建好GLUT視窗,可以開始準備 3D 貼圖 (要在create...MainLoop才能建貼圖)
id1 = myTexture("marvel.jpg"); ///myInit();///我們的貼圖在這裡 ///貼圖需要它
id2 = myTexture("teapot.jpg");
id3 = myTexture("square.jpg");
glutDisplayFunc(display);
glutIdleFunc(display);
glEnable(GL_DEPTH_TEST); ///glm需要它
glEnable(GL_LIGHT0); ///glm需要它
glEnable(GL_NORMALIZE); ///glm需要它
glEnable(GL_COLOR_MATERIAL); ///glm需要它
glEnable(GL_LIGHTING); ///glm需要它
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); ///glm需要它
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); ///glm需要它
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); ///glm需要它
glLightfv(GL_LIGHT0, GL_POSITION, light_position); ///glm需要它
glutMainLoop();///之前
}
__________________________________________________________________________________________
#include <GL/glut.h>
#include <opencv/highgui.h> ///OpenCV High GUI讀圖 ///貼圖需要它
#include <opencv/cv.h> ///OpenCV cvCvtColor() to convert color///貼圖需要它
///程式碼是對的, 但是沒有設定好include, lib, bin
GLuint id1, id2, id3;///NOW2 ///貼圖需要它
float a=0;
#include "glm.h"///呼叫外掛 ///glm需要它
GLMmodel * pmodel=NULL;///準備指標 ///glm需要它
GLMmodel * pmodel2=NULL;///準備指標 ///glm需要它
GLMmodel * pmodel3=NULL;///準備指標 ///glm需要它
GLMmodel * pmodel4=NULL;///準備指標 ///glm需要它
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, id1);///id1 for background ///貼圖需要它
glBegin(GL_POLYGON); ///貼圖需要它
glTexCoord2f(0, 0); glVertex3f(-1, +1, -1); ///貼圖需要它
glTexCoord2f(0, 1); glVertex3f(-1, -1, -1); ///貼圖需要它
glTexCoord2f(1, 1); glVertex3f(+1, -1, -1); ///貼圖需要它
glTexCoord2f(1, 0); glVertex3f(+1, +1, -1); ///貼圖需要它
glEnd();///貼圖需要它
if (!pmodel) { ///glm需要它
pmodel = glmReadOBJ("body.obj"); ///glm需要它
if (!pmodel) exit(0); ///glm需要它
///glmUnitize(pmodel); ///glm需要它
glmFacetNormals(pmodel); ///glm需要它
glmVertexNormals(pmodel, 90.0); ///glm需要它
}
glDisable(GL_TEXTURE_2D);
glPushMatrix();
glRotatef(a++, 0,1,0);
glScalef(0.2, 0.2, 0.2);
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glutSwapBuffers();
}
///void myInit()
GLuint myTexture(char * filename) ///貼圖需要它
{
IplImage * img = cvLoadImage(filename); ///NOW OpenCV ///貼圖需要它
cvCvtColor(img, img, CV_BGR2RGB);///convert color ///NOW OpenCV ///貼圖需要它
GLuint id; ///貼圖需要它
glEnable(GL_TEXTURE_2D); ///貼圖需要它
glGenTextures(1, &id ); ///貼圖需要它
glBindTexture(GL_TEXTURE_2D, id); ///貼圖需要它
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); ///貼圖需要它
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); ///貼圖需要它
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;///貼圖需要它
}
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f }; ///glm需要它
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f }; ///glm需要它
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; ///glm需要它
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f }; ///glm需要它
int main(int argc, char **argv)
{
///IplImage * img = cvLoadImage("marvel.jpg");
///cvShowImage("opencv", img);
///cvWaitKey(0);///等你按按鍵
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
glutCreateWindow("GLUT");///之後
///建好GLUT視窗,可以開始準備 3D 貼圖 (要在create...MainLoop才能建貼圖)
id1 = myTexture("marvel.jpg"); ///myInit();///我們的貼圖在這裡 ///貼圖需要它
id2 = myTexture("teapot.jpg");
id3 = myTexture("square.jpg");
glutDisplayFunc(display);
glutIdleFunc(display);
glEnable(GL_DEPTH_TEST); ///glm需要它
glEnable(GL_LIGHT0); ///glm需要它
glEnable(GL_NORMALIZE); ///glm需要它
glEnable(GL_COLOR_MATERIAL); ///glm需要它
glEnable(GL_LIGHTING); ///glm需要它
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); ///glm需要它
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); ///glm需要它
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); ///glm需要它
glLightfv(GL_LIGHT0, GL_POSITION, light_position); ///glm需要它
glMatrixMode(GL_PROJECTION); ///投影矩陣相關 戴好我們的正確度數的眼睛
glLoadIdentity(); ///投影矩陣相關 戴好我們的正確度數的眼睛
glOrtho(-1,+1, -1,+1, -100,+100); ///投影矩陣相關 戴好我們的正確度數的眼睛
glMatrixMode(GL_MODELVIEW); ///投影矩陣相關 切換回我們正確的矩陣
glutMainLoop();///之前
}
_________________________________________________________________________________
#include <GL/glut.h>
#include <opencv/highgui.h> ///OpenCV High GUI讀圖 ///貼圖需要它
#include <opencv/cv.h> ///OpenCV cvCvtColor() to convert color///貼圖需要它
///程式碼是對的, 但是沒有設定好include, lib, bin
GLuint id1, id2, id3;///NOW2 ///貼圖需要它
float a=0;
#include "glm.h"///呼叫外掛 ///glm需要它
GLMmodel * pmodel=NULL;///準備指標 ///glm需要它
GLMmodel * pmodel2=NULL;///準備指標 ///glm需要它
GLMmodel * pmodel3=NULL;///準備指標 ///glm需要它
GLMmodel * pmodel4=NULL;///準備指標 ///glm需要它
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, id1);///id1 for background ///貼圖需要它
glBegin(GL_POLYGON); ///貼圖需要它
glTexCoord2f(0, 0); glVertex3f(-1, +1, -1); ///貼圖需要它
glTexCoord2f(0, 1); glVertex3f(-1, -1, -1); ///貼圖需要它
glTexCoord2f(1, 1); glVertex3f(+1, -1, -1); ///貼圖需要它
glTexCoord2f(1, 0); glVertex3f(+1, +1, -1); ///貼圖需要它
glEnd();///貼圖需要它
if (!pmodel) { ///glm需要它
pmodel = glmReadOBJ("body.obj"); ///glm需要它
if (!pmodel) exit(0); ///glm需要它
///glmUnitize(pmodel); ///glm需要它
glmFacetNormals(pmodel); ///glm需要它
glmVertexNormals(pmodel, 90.0); ///glm需要它
}
glDisable(GL_TEXTURE_2D);
glPushMatrix();
glRotatef(a++, 0,1,0);
glScalef(0.2, 0.2, 0.2);
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
if (!pmodel2) { ///glm需要它
pmodel2 = glmReadOBJ("head.obj"); ///glm需要它
if (!pmodel2) exit(0); ///glm需要它
///glmUnitize(pmodel2); ///glm需要它
glmFacetNormals(pmodel2); ///glm需要它
glmVertexNormals(pmodel2, 90.0); ///glm需要它
}
glDisable(GL_TEXTURE_2D);
glPushMatrix();
glRotatef(a, 0,1,0);
glScalef(0.2, 0.2, 0.2);
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glutSwapBuffers();
}
///void myInit()
GLuint myTexture(char * filename) ///貼圖需要它
{
IplImage * img = cvLoadImage(filename); ///NOW OpenCV ///貼圖需要它
cvCvtColor(img, img, CV_BGR2RGB);///convert color ///NOW OpenCV ///貼圖需要它
GLuint id; ///貼圖需要它
glEnable(GL_TEXTURE_2D); ///貼圖需要它
glGenTextures(1, &id ); ///貼圖需要它
glBindTexture(GL_TEXTURE_2D, id); ///貼圖需要它
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); ///貼圖需要它
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); ///貼圖需要它
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;///貼圖需要它
}
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f }; ///glm需要它
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f }; ///glm需要它
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; ///glm需要它
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f }; ///glm需要它
int main(int argc, char **argv)
{
///IplImage * img = cvLoadImage("marvel.jpg");
///cvShowImage("opencv", img);
///cvWaitKey(0);///等你按按鍵
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
glutCreateWindow("GLUT");///之後
///建好GLUT視窗,可以開始準備 3D 貼圖 (要在create...MainLoop才能建貼圖)
id1 = myTexture("marvel.jpg"); ///myInit();///我們的貼圖在這裡 ///貼圖需要它
id2 = myTexture("teapot.jpg");
id3 = myTexture("square.jpg");
glutDisplayFunc(display);
glutIdleFunc(display);
glEnable(GL_DEPTH_TEST); ///glm需要它
glEnable(GL_LIGHT0); ///glm需要它
glEnable(GL_NORMALIZE); ///glm需要它
glEnable(GL_COLOR_MATERIAL); ///glm需要它
glEnable(GL_LIGHTING); ///glm需要它
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); ///glm需要它
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); ///glm需要它
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); ///glm需要它
glLightfv(GL_LIGHT0, GL_POSITION, light_position); ///glm需要它
glMatrixMode(GL_PROJECTION); ///投影矩陣相關 戴好我們的正確度數的眼睛
glLoadIdentity(); ///投影矩陣相關 戴好我們的正確度數的眼睛
glOrtho(-1,+1, -1,+1, -100,+100); ///投影矩陣相關 戴好我們的正確度數的眼睛
glMatrixMode(GL_MODELVIEW); ///投影矩陣相關 切換回我們正確的矩陣
glutMainLoop();///之前
}
沒有留言:
張貼留言