1.作業Q&A
2.複習:貼圖
3.複習:打光
4.複習:模型
下載檔案
將檔案移至桌面並開啟
將檔案移至桌面並開啟
將lib打開
將此檔案複製並貼上
將貼上的檔案重新命名為libglut32.a
File→New→Project
選取 GLUT project
輸入名稱,將檔案儲存在桌面
將老師提供的程式碼貼上去,自行上網找圖片,將圖片貼上,執行後,就有圓球體
#include <opencv/highgui.h> ///for cvCvtColor()
#include <opencv/cv.h>
#include <GL/glut.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 50fps:20msec
glutTimerFunc(20, timer, 0);
//printf("timer now\n");
glutPostRedisplay();
}
int main(int argc, char**argv)
{ glutInit(&argc, argv);
glutCreateWindow("3D");
glutDisplayFunc(display); ///顯示
//glutIdleFunc(display);///自動轉很帥 (閒閒沒事幹, 就重畫)
glutTimerFunc(0, timer, 0);
myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
glutMainLoop();
}
改程式碼變成茶壺
#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("aaa.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("aaa.jpg"); ///myInit();///我們的貼圖在這裡
id2 = myTexture("bbb.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("aaa.jpg"); ///myInit();///我們的貼圖在這裡
id2 = myTexture("bbb.jpg");
id3 = myTexture("ccc.jpg");
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();///之前
}
沒有留言:
張貼留言