(1) 作業Q &A
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"小葉說反正就是有ADD的
4. 要放的路徑: C:\OpenCV2.1
2. 載入freeglut
1. 從Transmission Zero 下載 "freeglut 3.0.0 MinGW Package"
2. 解壓縮後放到桌面
3. 在lib複製一個"libfreeglut.a"命名為"libglut32.a"
4. CodeBlocks 開
5. File -> New -> Project -> GLUT Project
6. 把location改成"C:\Users\user\Desktop\freeglut"
3. 設定載入include, bin, lib
1. 在Build Option新增
2. 圖要放在freeglut的bin裏
(2) 複習:貼圖texture
#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();///之前
}
#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();///之前
}
#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();///之前
}
#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();///之前
}
#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();///之前
}
#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();///之前
}
(3) 複習:打光
(4) 複習:模型
#include <GL/glut.h>
#include <opencv/highgui.h> ///OpenCV High GUI讀圖
#include <opencv/cv.h> ///OpenCV cvCvtColor() to convert color
///程式碼是對的, 但是沒有設定好include, lib, bin
#include "glm.h"///呼叫外掛 ///glm需要它
#include <stdio.h>///NOW_FILE (0)
#include <mmsystem.h>///Now (1)
float nowX=0;///NOW2
GLuint id1, id2, id3;///NOW2
float oldAngle[20];///NOW4
float newAngle[20];///NOW4
float angle[20];///NOW2
int angleID=1;///NOW3
FILE * fout = NULL;///NOW_FILE (1)
FILE * fin = NULL;///NOW_FILE_READ (1)
GLMmodel * pmodel=NULL;///準備指標 ///glm需要它
GLMmodel * pmodel2=NULL;///準備指標 ///glm需要它
GLMmodel * pmodel3=NULL;///準備指標 ///glm需要它
GLMmodel * pmodel4=NULL;///準備指標 ///glm需要它
GLMmodel * pmodel5=NULL;///準備指標 ///glm需要它
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[] = { -7,-10, -13, -1 };
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 };
void myReadAllModels()
{
if (!pmodel) { ///now要有3D Model
pmodel = glmReadOBJ("body.obj"); ///now要有3D Model
if (!pmodel) exit(0); ///now要有3D Model
///glmUnitize(pmodel); ///now要有3D Model
glmFacetNormals(pmodel); ///now要有3D Model
glmVertexNormals(pmodel, 90.0); ///now要有3D Model
}
if (!pmodel2) { ///now要有3D Model
pmodel2 = glmReadOBJ("hand.obj"); ///now要有3D Model
if (!pmodel2) exit(0); ///now要有3D Model
///glmUnitize(pmodel2); ///now要有3D Model
glmFacetNormals(pmodel2); ///now要有3D Model
glmVertexNormals(pmodel2, 90.0); ///now要有3D Model
}
if (!pmodel3) { ///now要有3D Model
pmodel3 = glmReadOBJ("hand1.obj"); ///now要有3D Model
if (!pmodel3) exit(0); ///now要有3D Model
///glmUnitize(pmodel); ///now要有3D Model
glmFacetNormals(pmodel3); ///now要有3D Model
glmVertexNormals(pmodel3, 90.0); ///now要有3D Model
}
if (!pmodel4) { ///now要有3D Model
pmodel4 = glmReadOBJ("foot.obj"); ///now要有3D Model
if (!pmodel4) exit(0); ///now要有3D Model
///glmUnitize(pmodel); ///now要有3D Model
glmFacetNormals(pmodel4); ///now要有3D Model
glmVertexNormals(pmodel4, 90.0); ///now要有3D Model
}
if (!pmodel5) { ///now要有3D Model
pmodel5 = glmReadOBJ("foot1.obj"); ///now要有3D Model
if (!pmodel5) exit(0); ///now要有3D Model
///glmUnitize(pmodel); ///now要有3D Model
glmFacetNormals(pmodel5); ///now要有3D Model
glmVertexNormals(pmodel5, 90.0); ///now要有3D Model
}
}
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.5, +1.5, 0.5);
glTexCoord2f(0, 1); glVertex3f(-1.5, -1.5, 0.5);
glTexCoord2f(1, 1); glVertex3f(+1.5, -1.5, 0.5);
glTexCoord2f(1, 0); glVertex3f(+1.5, +1.5, 0.5);
glEnd();
myReadAllModels();
glPushMatrix();
glScalef(0.2,0.2,0.2);
glTranslatef(0, 0, 0);
glRotatef(angle[5],0,1,0);///NOW2 ///(2)旋轉
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPushMatrix();///右邊
glTranslatef(-1.2, 1.2, 0);///NOW2 (3)掛上去
glRotatef(angle[1],0,0,1);///NOW2 ///(2)旋轉
glRotatef(angle[6],1,0,0);///NOW2 ///(2)旋轉
glTranslatef(0, -1.1, 0);///NOW2 (1)旋轉中心
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();///右邊
glTranslatef(1.2, 1.2, 0);///NOW2 (3)掛上去
glRotatef(angle[2],0,0,1);///NOW2 ///(2)旋轉
glRotatef(angle[7],1,0,0);///NOW2 ///(2)旋轉
glTranslatef(0, -1.1, 0);///NOW2 (1)旋轉中心
glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();///右邊
glTranslatef(-0.7, -1, 0);///NOW2 (3)掛上去
glRotatef(angle[3],0,0,1);///NOW2 ///(2)旋轉
glRotatef(angle[8],1,0,0);///NOW2 ///(2)旋轉
glTranslatef(0, -1.1, 0);///NOW2 (1)旋轉中心
glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();///右邊
glTranslatef(0.7, -1, 0);///NOW2 (3)掛上去
glRotatef(angle[4],0,0,1);///NOW2 ///(2)旋轉
glRotatef(angle[9],1,0,0);///NOW2 ///(2)旋轉
glTranslatef(0, -1.1, 0);///NOW2 (1)旋轉中心
glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int oldX=0;///NOW2
void mouse(int button, int state, int x, int y)///NOW2
{///NOW2
oldX = x;///NOW2
}
void motion(int x, int y)///NOW2
{///NOW2
angle[angleID] += x-oldX;///NOW3
oldX = x;///NOW2
///if(fout == NULL) fout = fopen("motion.txt", "w+");///NOW_FILE (2)
for(int i=0;i<20;i++){///NOW3
printf(" %.2f ", angle[i]);///NOW3
///fprintf(fout, " %.2f ", angle[i]);///NOW_FILE
}
printf("\n");///NOW3
///fprintf(fout, "\n");///NOW_FILE
display();///NOW2
}
#include <math.h>///for sin() cos() ///NOW3
float eyeX=2, eyeY=0, eyeZ=0; ///NOW3
void timer(int t){///NOW4
glMatrixMode(GL_PROJECTION); ///NOW4
glLoadIdentity(); ///NOW4
gluPerspective(60, 1.0, 0.001, 1000);///NOW5
float ang = (t/180.0)*3.1415926; ///NOW3
eyeX=0.5*cos(ang); ///NOW3
eyeZ=1*sin(ang); ///NOW3
glutTimerFunc(33, timer, t+1);///NOW4 變快了.. 撥好下個timer的時間..等一下它會醒來
glMatrixMode(GL_MODELVIEW);///NOW3
glLoadIdentity();///NOW3
if(t<100)
gluLookAt( 0, 0, -1.5,///eye ///NOW3
0, 0, 0,///center ///NOW3
0, 1, 0);///up ///NOW3
if(t>=100&&t<150)
gluLookAt( -0.5, 0.5, -1,///eye ///NOW3
0, 0, 0,///center ///NOW3
0, 1, 0);///up ///NOW3
if(t>=150&&t<300)
gluLookAt( 0.5, 0.5, -1,///eye ///NOW3
0, 0, 0,///center ///NOW3
0, 1, 0);///up ///NOW3
if(t>=300&&t<=400)
gluLookAt( -0.5, -0.5, -1,///eye ///NOW3
0, 0, 0,///center ///NOW3
0, 1, 0);///up ///NOW3
if(t>400)
gluLookAt( 0, 0, -1.5,///eye ///NOW3
0, 0, 0,///center ///NOW3
0, 1, 0);///up ///NOW3
if(t%30==0){///關鍵的時間 每過1秒,讀新的 ///NOW4
if(fin==NULL) fin=fopen("motion.txt", "r");///NOW4
for(int i=0; i<20; i++){///NOW4
oldAngle[i] = newAngle[i];///NOW4 原本的新的,就變舊的
fscanf(fin, "%f", &newAngle[i]);///NOW4 再從檔案,讀入新的
}///NOW2
}
float alpha= (t%30)/30.0;///NOW4 算出alpha值 0.0 ... 1.0
for(int i=0; i<20; i++){///NOW4 20個關節角度
angle[i] = alpha * newAngle[i] + (1-alpha) * oldAngle[i];///NOW4 都要做內插
}
glutPostRedisplay();///NOW4 重畫畫面
}
void keyboard(unsigned char key, int x, int y)///NOW3
{///NOW3
if(key=='1') angleID=1; ///NOW3
if(key=='2') angleID=2;///NOW3
if(key=='3') angleID=3;///NOW3
if(key=='4') angleID=4;///NOW3
if(key=='5') angleID=5;///NOW3
if(key=='6') angleID=6;///NOW3
if(key=='7') angleID=7;///NOW3
if(key=='8') angleID=8;///NOW3
if(key=='9') angleID=9;///NOW3
if(key=='0') angleID=0;///NOW3
if(key=='w'){///只有按下'w'才存1組動作 (1行)
if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW
for(int i=0; i<20; i++){///NOW
fprintf(fout, " %.1f ", angle[i]);///NOW
}///NOW
fprintf(fout, "\n");///NOW
}///NOW
if(key=='r'){
if(fin==NULL) fin = fopen("motion.txt", "r");///NOW_FILE_READ (2)
///scanf("%d", &n);
for(int i=0;i<20; i++){///NOW_FILE_READ
fscanf(fin, "%f", &angle[i]);///NOW_FILE_READ
}
}
if(key=='p' || key=='t'){///NOW3 Play!!!!!!! timer 叫起來
glutTimerFunc(500, timer, 0);///NOW3 撥好第1個timer的時間.... 等一下它會醒來
if(fin==NULL) fin=fopen("motion.txt", "r");///NOW3
for(int i=0; i<20; i++){///NOW3
fscanf(fin, "%f", &newAngle[i]);///NOW4
}///NOW2
}
if(key=='p'){
PlaySoundA("Ball.wav", NULL, SND_ASYNC);
}
glutPostRedisplay();///NOW_FILE_READ 和 display()很像
}
///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);
glutInitWindowSize(600,600);///可以開大一點的window
glutCreateWindow("GLUT");///之後
///建好GLUT視窗,可以開始準備 3D 貼圖 (要在create...MainLoop才能建貼圖)
id1 = myTexture("GOOD2.jpg"); ///myInit();///我們的貼圖在這裡
id2 = myTexture("GOOD1.jpg");
id3 = myTexture("GOOD.jpg");
glutKeyboardFunc(keyboard);///NOW3
glutMouseFunc(mouse);///NOW2
glutMotionFunc(motion);///NOW2
glutDisplayFunc(display);
glClearColor(1,1,1,1);
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);
glutMainLoop();///之前
}
沒有留言:
張貼留言