顯示具有 000葉正聖老師 標籤的文章。 顯示所有文章
顯示具有 000葉正聖老師 標籤的文章。 顯示所有文章

2014年5月30日 星期五

Week 15 葉正聖老師示範

每週上課都要有目標
1. 距離期末作品繳交, 只剩下一次的上課囉
Week 15 今天
Week 16 最後一次上課
Week 17 展示期末作品

2. 看去年學長姐們的作品








 =====
 =====
內插(interpolation)

動作A ------> 動作B
a=0 .................a=1

你的動作(angle[...])
angle[0] = angleOld[0]*(1-a) + angleNew[0]*a;   //內插公式
口訣: 交叉相乘

ex.
a=0, angle[0] 是 angleOld[0]
a=0.1, angleOld[0]*0.9 + angleNew[0]*0.1
a=0.5,  angleOld[0]*0.5 + angleNew[0]*0.5
a=0.9,
a=1, anlge[0] 是 angleNew[0]

第一節課重點: 有內插效果
第二節課重點: 使用陣列來做內插

第二節課重點: 使用 timer 來自動播放

第三節課重點: 集大成, 把今天兩節課教的, 與最近四週教的結合在一起
1. 可以內插
2. 使用陣列
3. 寫檔/讀檔 
4. 自動播放 (使用計時器)
5. 用mouse/motion 配合 keyboard 來改變陣列裡面的值


期末作品佔40%哦! 大家好好地做吧!


2014年5月23日 星期五

Week 14 葉正聖老師示範

今天的學習目標: 
(1) 自動讓機器人的身體動 (讀檔, 自動播放, timer)
(2) 音效、音樂


=====
課堂練習: 先練習使用 PlaySound 播音效 (使用 SND_SYNC 模式)

課堂練習: 開新的 glut 專案, 再試著將 PlaySound() 放在一個新的 glut 專案中, 在main() 的前面, 想要在執行 3D程式的同時也播音樂。
然後老師在課堂上現場示範 6個問題哦! 6位同學都回答得很好!


Q1: 請問下圖是什麼問題?





=====
接下來試 CMP3_MCI.h 的特異功能囉

請在 Facebook 的社團, 下載它, 然後
#include "CMP3_MCI.h"
CMP3_MCI myMP3;
int main()
{
myMP3.Load("BGMusic.mp3");
myMP3.Play();
...



=====






2014年5月16日 星期五

Week 13 葉正聖老師示範

今天的目標: 做出完整的機器人, 寫檔, 讀檔, 自動播放



=====以下的程式碼不可以用哦! 因為它是錯的!=====
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
float angle0=0, angle1=0, angle2=0, angle3=0, angle4=0, angle5=0, angle6=0, angle7=0;
GLuint id[4];//技巧2: 使用陣列, 讓變數 變 簡單 simple
IplImage * img[4];
char filename[4][100]={"imgBody.png", "imgHand0.png", "imgLeg0.png", "imgLeg1.png"};
void myTexture()
{
glEnable(GL_TEXTURE_2D);
for(int i=0; i<4; i++){//技巧3: 迴圈, 可以讓程式碼, 做重覆的事可以很爽 清爽
img[i] = cvLoadImage( filename[i] );
cvCvtColor(img[i], img[i], CV_BGR2RGB);
glGenTextures(1, &id[i]);
glBindTexture(GL_TEXTURE_2D, id[i]);//head
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img[i]->width, img[i]->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img[i]->imageData);
}
}
void myRect()//技巧1: 重覆的程式碼, 用函式拉出來, 就不用一直copy啦
{//函式就是 黑盒子 幫你做事情!
glPushMatrix();
glScalef(0.2, 0.5, 0.5);
glBegin(GL_POLYGON);
glTexCoord2f(0,0);  glVertex3f(-0.5, 0.5,0);
glTexCoord2f(0,1);  glVertex3f(-0.5,-0.5,0);
glTexCoord2f(1,1);  glVertex3f( 0.5,-0.5,0);
glTexCoord2f(1,0);  glVertex3f( 0.5, 0.5,0);
glEnd();
glPopMatrix();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, id[0]);
myRect();//body//技巧1: 用函式, 哥就是帥!
glPushMatrix();//left hand 今天的技巧1: 大的手
glTranslatef(-0.1, 0.15, 0);
glRotatef(angle0, 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();//技巧1: 用函式, 哥就是帥!
glPushMatrix();// 今天的技巧2: 小的手
glTranslatef(0, -0.25, 0);//(3) T: 掛在該掛的位子
glRotatef(angle1, 0,0,1);//(2) R:
glTranslatef(0, -0.25, 0);//(1) T: 改變旋轉軸的位置
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right hand
glTranslatef(0.1, 0.15, 0);
glRotatef(-angle2, 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();//技巧1: 用函式, 哥就是帥!
glPushMatrix();// 今天的技巧2: 小的手
glTranslatef(0, -0.25, 0);//(3) T: 掛在該掛的位子
glRotatef(-angle3, 0,0,1);//(2) R:
glTranslatef(0, -0.25, 0);//(1) T: 改變旋轉軸的位置
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//left leg
glTranslatef( -0.05, -0.25, 0);
glRotatef(180-angle4, 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix();//left foot
glTranslatef( 0, 0.25, 0);
glRotatef(angle5, 0,0,1);
glTranslatef(0, 0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[3]);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right leg
glTranslatef( 0.05, -0.25, 0);
glRotatef(-(180-angle6), 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix();//left foot
glTranslatef( 0, 0.25, 0);
glRotatef(-angle7, 0,0,1);
glTranslatef(0, 0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[3]);
myRect();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='0') angle0+=5;
if(key=='1') angle0-=5;
if(key=='2') angle1+=5;
if(key=='3') angle1-=5;
if(key=='4') angle2+=5;
if(key=='5') angle2-=5;
if(key=='6') angle3+=5;
if(key=='7') angle3-=5;
if(key=='8') angle4+=5;
if(key=='9') angle4-=5;
if(key=='a') angle5+=5;
if(key=='b') angle5-=5;
if(key=='c') angle6+=5;
if(key=='d') angle6-=5;
if(key=='e') angle7+=5;
if(key=='f') angle7-=5;
glutPostRedisplay();//請電腦重新畫一次畫面
}
int oldX=0, oldY=0;
void mouse(int button, int state, int x, int y)
{
oldX=x; oldY=y;
}
void motion(int x, int y)
{
angle1 += x-oldX;  oldX=x;
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myTexture();

glutMainLoop();
}
=====下面才是真的程式碼=====





2014年5月9日 星期五

Week 12 葉正聖老師示範



#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
float angleX =0;

void myTexture()
{
GLuint id;
IplImage * img = cvLoadImage("imgBody.png");
cvCvtColor(img, img, CV_BGR2RGB);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=5;
glutPostRedisplay();//請電腦重新畫一次畫面
}

void myLight()
{
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}

void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX, 0,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}

int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");

glutDisplayFunc(display);
glutKeyboardFunc(keyboard);

myTexture();
myLight();
glutMainLoop();
}

以下的程式, 很爛, 不要用!
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
float angleX =0;

void myTexture()
{
GLuint id;
IplImage * img = cvLoadImage("imgBody.png");
cvCvtColor(img, img, CV_BGR2RGB);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glPushMatrix();//body
glScalef(0.2, 0.5, 0.5);
glBegin(GL_POLYGON);
glTexCoord2f(0,0);  glVertex3f(-0.5, 0.5,0);
glTexCoord2f(0,1);  glVertex3f(-0.5,-0.5,0);
glTexCoord2f(1,1);  glVertex3f( 0.5,-0.5,0);
glTexCoord2f(1,0);  glVertex3f( 0.5, 0.5,0);
glEnd();
glPopMatrix();
glPushMatrix();//left hand
glTranslatef(-0.1, 0, 0);
glRotatef(angleX, 0,0,1);
glTranslatef(0, 0.25, 0);
glScalef(0.2, 0.5, 0.5);
glBegin(GL_POLYGON);
glTexCoord2f(0,0);  glVertex3f(-0.5, 0.5,0);
glTexCoord2f(0,1);  glVertex3f(-0.5,-0.5,0);
glTexCoord2f(1,1);  glVertex3f( 0.5,-0.5,0);
glTexCoord2f(1,0);  glVertex3f( 0.5, 0.5,0);
glEnd();
glPopMatrix();
glPushMatrix();//right hand
glTranslatef( 0.1, 0, 0);
glRotatef(-angleX, 0,0,1);
glTranslatef(0, 0.25, 0);
glScalef(0.2, 0.5, 0.5);
glBegin(GL_POLYGON);
glTexCoord2f(0,0);  glVertex3f(-0.5, 0.5,0);
glTexCoord2f(0,1);  glVertex3f(-0.5,-0.5,0);
glTexCoord2f(1,1);  glVertex3f( 0.5,-0.5,0);
glTexCoord2f(1,0);  glVertex3f( 0.5, 0.5,0);
glEnd();
glPopMatrix();
glPushMatrix();//left leg
glTranslatef( -0.05, -0.25, 0);
glRotatef(180-angleX, 0,0,1);
glTranslatef(0, 0.25, 0);
glScalef(0.2, 0.5, 0.5);
glBegin(GL_POLYGON);
glTexCoord2f(0,0);  glVertex3f(-0.5, 0.5,0);
glTexCoord2f(0,1);  glVertex3f(-0.5,-0.5,0);
glTexCoord2f(1,1);  glVertex3f( 0.5,-0.5,0);
glTexCoord2f(1,0);  glVertex3f( 0.5, 0.5,0);
glEnd();
glPopMatrix();
glPushMatrix();//right leg
glTranslatef( 0.05, -0.25, 0);
glRotatef(-(180-angleX), 0,0,1);
glTranslatef(0, 0.25, 0);
glScalef(0.2, 0.5, 0.5);
glBegin(GL_POLYGON);
glTexCoord2f(0,0);  glVertex3f(-0.5, 0.5,0);
glTexCoord2f(0,1);  glVertex3f(-0.5,-0.5,0);
glTexCoord2f(1,1);  glVertex3f( 0.5,-0.5,0);
glTexCoord2f(1,0);  glVertex3f( 0.5, 0.5,0);
glEnd();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=5;
glutPostRedisplay();//請電腦重新畫一次畫面
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myTexture();

glutMainLoop();
}

======
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
float angleX =0;

void myTexture()
{
GLuint id;
IplImage * img = cvLoadImage("imgBody.png");
cvCvtColor(img, img, CV_BGR2RGB);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
}
void myRect()//技巧1: 重覆的程式碼, 用函式拉出來, 就不用一直copy啦
{//函式就是 黑盒子 幫你做事情!
glPushMatrix();
glScalef(0.2, 0.5, 0.5);
glBegin(GL_POLYGON);
glTexCoord2f(0,0);  glVertex3f(-0.5, 0.5,0);
glTexCoord2f(0,1);  glVertex3f(-0.5,-0.5,0);
glTexCoord2f(1,1);  glVertex3f( 0.5,-0.5,0);
glTexCoord2f(1,0);  glVertex3f( 0.5, 0.5,0);
glEnd();
glPopMatrix();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
myRect();//body//技巧1: 用函式, 哥就是帥!
glPushMatrix();//left hand
glTranslatef(-0.1, 0, 0);
glRotatef(angleX, 0,0,1);
glTranslatef(0, 0.25, 0);
myRect();//技巧1: 用函式, 哥就是帥!
glPopMatrix();
glPushMatrix();//right hand
glTranslatef( 0.1, 0, 0);
glRotatef(-angleX, 0,0,1);
glTranslatef(0, 0.25, 0);
myRect();//技巧1: 用函式, 哥就是帥!
glPopMatrix();
glPushMatrix();//left leg
glTranslatef( -0.05, -0.25, 0);
glRotatef(180-angleX, 0,0,1);
glTranslatef(0, 0.25, 0);
myRect();
glPushMatrix();//left foot
glTranslatef( 0, 0.25, 0);
glRotatef(30, 0,0,1);
glTranslatef(0, 0.15, 0);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right leg
glTranslatef( 0.05, -0.25, 0);
glRotatef(-(180-angleX), 0,0,1);
glTranslatef(0, 0.25, 0);
myRect();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=5;
glutPostRedisplay();//請電腦重新畫一次畫面
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myTexture();

glutMainLoop();
}

===以下很爛, 小朋友不要學====
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
float angleX =0;
GLuint id0;
GLuint id1;
GLuint id2;
GLuint id3;
void myTexture()
{
IplImage * img0 = cvLoadImage("imgBody.png");
IplImage * img1 = cvLoadImage("imgHand0.png");
IplImage * img2 = cvLoadImage("imgLeg0.png");
IplImage * img3 = cvLoadImage("imgLeg1.png");
cvCvtColor(img0, img0, CV_BGR2RGB);
cvCvtColor(img1, img1, CV_BGR2RGB);
cvCvtColor(img2, img2, CV_BGR2RGB);
cvCvtColor(img3, img3, CV_BGR2RGB);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &id0);
glGenTextures(1, &id1);
glGenTextures(1, &id2);
glGenTextures(1, &id3);
glBindTexture(GL_TEXTURE_2D, id0);//head
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img0->width, img0->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img0->imageData);

glBindTexture(GL_TEXTURE_2D, id1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img1->width, img1->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img1->imageData);

glBindTexture(GL_TEXTURE_2D, id2);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img2->width, img2->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img2->imageData);

glBindTexture(GL_TEXTURE_2D, id3);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img3->width, img3->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img3->imageData);

}
void myRect()//技巧1: 重覆的程式碼, 用函式拉出來, 就不用一直copy啦
{//函式就是 黑盒子 幫你做事情!
glPushMatrix();
glScalef(0.2, 0.5, 0.5);
glBegin(GL_POLYGON);
glTexCoord2f(0,0);  glVertex3f(-0.5, 0.5,0);
glTexCoord2f(0,1);  glVertex3f(-0.5,-0.5,0);
glTexCoord2f(1,1);  glVertex3f( 0.5,-0.5,0);
glTexCoord2f(1,0);  glVertex3f( 0.5, 0.5,0);
glEnd();
glPopMatrix();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, id0);
myRect();//body//技巧1: 用函式, 哥就是帥!
glPushMatrix();//left hand
glTranslatef(-0.1, 0.15, 0);
glRotatef(angleX, 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id1);
myRect();//技巧1: 用函式, 哥就是帥!
glPopMatrix();
glPushMatrix();//right hand
glTranslatef(0.1, 0.15, 0);
glRotatef(-angleX, 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id1);
myRect();//技巧1: 用函式, 哥就是帥!
glPopMatrix();
glPushMatrix();//left leg
glTranslatef( -0.05, -0.25, 0);
glRotatef(180-angleX, 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id2);
myRect();
glPushMatrix();//left foot
glTranslatef( 0, 0.25, 0);
glRotatef(30, 0,0,1);
glTranslatef(0, 0.15, 0);
glBindTexture(GL_TEXTURE_2D, id3);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right leg
glTranslatef( 0.05, -0.25, 0);
glRotatef(-(180-angleX), 0,0,1);
glTranslatef(0, 0.25, 0);
myRect();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=5;
glutPostRedisplay();//請電腦重新畫一次畫面
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myTexture();

glutMainLoop();
}
=====下面才是真的帥!======
#include <GL/glut.h>
#include <opencv/highgui.h>
#include <opencv/cv.h>
float angleX =0, angle0=0, angle1=0, angle2=0, angle3=0;
GLuint id[4];//技巧2: 使用陣列, 讓變數 變 簡單 simple
IplImage * img[4];
char filename[4][100]={"imgBody.png", "imgHand0.png", "imgLeg0.png", "imgLeg1.png"};
void myTexture()
{
glEnable(GL_TEXTURE_2D);
for(int i=0; i<4; i++){//技巧3: 迴圈, 可以讓程式碼, 做重覆的事可以很爽 清爽
img[i] = cvLoadImage( filename[i] );
cvCvtColor(img[i], img[i], CV_BGR2RGB);
glGenTextures(1, &id[i]);
glBindTexture(GL_TEXTURE_2D, id[i]);//head
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img[i]->width, img[i]->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img[i]->imageData);
}
}
void myRect()//技巧1: 重覆的程式碼, 用函式拉出來, 就不用一直copy啦
{//函式就是 黑盒子 幫你做事情!
glPushMatrix();
glScalef(0.2, 0.5, 0.5);
glBegin(GL_POLYGON);
glTexCoord2f(0,0);  glVertex3f(-0.5, 0.5,0);
glTexCoord2f(0,1);  glVertex3f(-0.5,-0.5,0);
glTexCoord2f(1,1);  glVertex3f( 0.5,-0.5,0);
glTexCoord2f(1,0);  glVertex3f( 0.5, 0.5,0);
glEnd();
glPopMatrix();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, id[0]);
myRect();//body//技巧1: 用函式, 哥就是帥!
glPushMatrix();//left hand
glTranslatef(-0.1, 0.15, 0);
glRotatef(angleX, 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();//技巧1: 用函式, 哥就是帥!
glPopMatrix();
glPushMatrix();//right hand
glTranslatef(0.1, 0.15, 0);
glRotatef(-angleX, 0,0,1);
glTranslatef(0, -0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[1]);
myRect();//技巧1: 用函式, 哥就是帥!
glPopMatrix();
glPushMatrix();//left leg
glTranslatef( -0.05, -0.25, 0);
glRotatef(180-angleX, 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPushMatrix();//left foot
glTranslatef( 0, 0.25, 0);
glRotatef(30, 0,0,1);
glTranslatef(0, 0.15, 0);
glBindTexture(GL_TEXTURE_2D, id[3]);
myRect();
glPopMatrix();
glPopMatrix();
glPushMatrix();//right leg
glTranslatef( 0.05, -0.25, 0);
glRotatef(-(180-angleX), 0,0,1);
glTranslatef(0, 0.25, 0);
glBindTexture(GL_TEXTURE_2D, id[2]);
myRect();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=5;
glutPostRedisplay();//請電腦重新畫一次畫面
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myTexture();

glutMainLoop();
}

2014年5月2日 星期五

Week11 葉正聖老師示範

#include <GL/glut.h>

float angleX =0;
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=5;
glutPostRedisplay();//請電腦重新畫一次畫面
}

void myLight()
{
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}

void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX, 0,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}

int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");

glutDisplayFunc(display);
glutKeyboardFunc(keyboard);

myLight();
glutMainLoop();
}






2014年3月28日 星期五

Week 06 葉正聖老師示範

接下來我們要做 貼圖 (趕時間可先看去年的 blog:
http://2013graphics.blogspot.tw/2013/04/week08.html

1. Step 1: 下載 glut 及 opencv 的 DevPak紙箱, 並安裝 (你們電腦桌面裡也有)
https://drive.google.com/file/d/0ByYbu0zjxrp1THkybHVJbGFaTGs/edit?usp=sharing
2. Step 2: Dev C++ 新增 GLUT 專案, 放在桌面上的一個新的目錄
3. Step 3: 將 C:\Dev-CPP\bin\opencv\ 目錄中的 .dll 檔案, 都 Copy 到你的專案目錄中
4. Step 4. 設定 Tool-Compiler 設定, 加入咒語 -lcv -lcxcore -lhighgui
5. Step 5. 寫好你的五行程式碼用 OpenCV 1.0 秀圖




2014年3月21日 星期五

Week05 葉正聖老師示範

電腦圖學課 今天目標:
(1) Maya -> 3D OBJ模型 -> OpenGL 畫
(2) 作業的問題, 試著重新解釋移動、轉動, 配上互動的按鍵(keyboard)來搞清楚



http://jsyeh.org/3dcg10