檔案網址:http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/
-
壓縮在同一個資料夾(WINDOWS要包含DATA跟GLUT32.DLL和SOURCE)
Download
freeglut 3.0.0 for MinGW壓縮檔案,在LIB裡面複製LIBFREEGLUT,貼上改成LIBGLUT32。
開啟CODEBLOCK。FILE(左上角)->NEW->PROJECT->GLUT PROJECT->(命名檔案)->下一步->路徑改成FREEGLUT資料夾的路徑。
貼圖。注意事項。
- A:貼圖要放在FREEGLUT/BIN。
- B:貼圖需要安裝OPENCV2.1。FREEGLUT。
- C:CODEBLOCK需要在專案的OPTION中進行設定。
- C-1:LINKER SETTING增加 CXCORE210,CV210,HIGHGUI210。(全小寫)
- D-1 SEARCH DIRECTORIES COMPLIER 增加 C:\OPENCV2.1\include
- D-2 SEARCH LINKER C:\OPENCV2.1\lib
- 重開一次CODEBLOCK。
#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("Thunder.jpg"); ///myInit();///我們的貼圖在這裡 ///貼圖需要它
id2 = myTexture("Zero.jpg");
id3 = myTexture("wolf.jpg");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();///之前
}
/
glm注意事項
glm注意事項
- glm.c(改名)->glm.cpp,glm.h。記得需要放在專案裡面。
- 專案記得需要addfiles。
- 模組匯入方面。記得OBJ和MLT放在FREEGLUT/BIN5j/
/
#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("al.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("F-16.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("Zero.jpg"); ///myInit();///我們的貼圖在這裡 ///貼圖需要它
id2 = myTexture("wolf.jpg");
id3 = myTexture("Thunder.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();///之前
}
/
沒有留言:
張貼留言