2014 電腦圖學 Computer Graphics 授課教師: 葉正聖 銘傳大學資訊傳播工程系 每週主題: 程式環境、點線面顏色、移動/旋轉/縮放與矩陣(Matrix)、階層性關節轉動(T-R-T)、做出機器人、打光、貼圖、glu/glut函式、鍵盤、滑鼠、計時器(timer)、讀入3D模型、粒子系統、聲音、特效、投影矩陣、攝影機與運鏡、機器人2.0、期末作品
2014年6月11日 星期三
2014年6月5日 星期四
2014年5月29日 星期四
2014年5月15日 星期四
week13
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <stdio.h>
//IplImage *img1, *img2, *img3, *img4;
//GLuint id1, id2, id3, id4;
IplImage * img[40];//技巧1: 可以把重覆的東西, 用陣列來簡化
GLuint id[40];//技巧1: 可以把重覆的東西, 用陣列來簡化
float angle1=0, angle2=0, angle3=0, angle4=0, angle5=0, angle6=0, angle7=0, angle8=0;
void myPrepareTexture(int i)//用參數來配, 找對應正確的變數
{//技巧3: 可以把重覆的東西, 用函式來簡化
cvCvtColor(img[i], img[i], CV_BGR2RGB);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &id[i]);
glBindTexture(GL_TEXTURE_2D, id[i]);
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 myTexture()
{
img[0]=cvLoadImage("imgbody.png");
img[1]=cvLoadImage("imgHand1.png");
img[2]=cvLoadImage("imgLeg1.png");
for(int i=0;i<3;i++){//技巧2: 可以把重覆的東西, 用迴圈來簡化
myPrepareTexture(i);
}
}
void myPolygon(int num){//技巧3: 可以把重覆的東西, 用函式來簡化
glPushMatrix();
glScalef(0.3, 0.8, 0.8);
glBindTexture(GL_TEXTURE_2D, id[num]);
glBegin(GL_POLYGON);
glTexCoord2f(0, 0); glVertex3f( -0.5, 0.5, 0);
glTexCoord2f(1, 0); glVertex3f( 0.5, 0.5, 0);
glTexCoord2f(1, 1); glVertex3f( 0.5, -0.5, 0);
glTexCoord2f(0, 1); glVertex3f( -0.5, -0.5, 0);
glEnd();
glPopMatrix();
}
void myBody(){
myPolygon(0);
}
void myLeg(){
glPushMatrix();
glScalef(0.5, 0.5, 0.5);
myPolygon(1);
glPopMatrix();
}
void myHand(){
glPushMatrix();// 今天的技巧2: 可以利用 scale把後面的東西都變小
//T R S: Translate, Rotate, Scale, 要放在 glPushMatrix()/glPopMatrix()
glScalef(0.5, 0.5, 0.5);
myPolygon(2);
glPopMatrix();
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glScalef(0.5, 0.5, 0.5);//今天的技巧1: 可以利用 scale把後面的東西都變小
myBody();
glPushMatrix();//今天的技巧3: 大的在外面
glTranslatef(0.2,0,0);//右手
glRotatef(angle1, 0,0,1);
glTranslatef(0, -0.2, 0);
myHand();
glPushMatrix();//今天的技巧3: 小的在裡面
glTranslatef(0,-0.2,0);//小右手
glRotatef(angle2, 0,0,1);
glTranslatef(0, -0.2, 0);
myHand();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2,0,0);//左手
glRotatef(-angle3, 0,0,1);
glTranslatef(0, -0.2, 0);
myHand();
glPushMatrix();//今天的技巧3: 小的在裡面
glTranslatef(0,-0.2,0);//小右手
glRotatef(-angle4, 0,0,1);
glTranslatef(0, -0.2, 0);
myHand();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0.1,-0.3,0);//右腿
glRotatef(angle5, 0,0,1);
glTranslatef(0, -0.2, 0);
myLeg();
//請把小腿及腳掌做出來
glPushMatrix();
glTranslatef(0,-0.2,0);//右腿
glRotatef(angle6, 0,0,1);
glTranslatef(0, -0.2, 0);
myLeg();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.1,-0.3,0);//左腿
glRotatef(-angle7, 0,0,1);
glTranslatef(0, -0.2, 0);
myLeg();
//請把小腿及腳掌做出來
glPushMatrix();
glTranslatef(0,-0.2,0);//右腿
glRotatef(-angle8, 0,0,1);
glTranslatef(0, -0.2, 0);
myLeg();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angle1+=3;
if(key=='2') angle1-=3;
if(key=='3') angle2+=3;
if(key=='4') angle2-=3;
if(key=='5') angle3+=3;
if(key=='6') angle3-=3;
if(key=='7') angle4+=3;
if(key=='8') angle4-=3;
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
glutCreateWindow("week12");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myTexture();//不要忘了加哦!
glutMainLoop();
}
2014年5月8日 星期四
week12
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <stdio.h>
//IplImage *img1, *img2, *img3, *img4;
//GLuint id1, id2, id3, id4;
IplImage * img[40];//技巧1: 可以把重覆的東西, 用陣列來簡化
GLuint id[40];//技巧1: 可以把重覆的東西, 用陣列來簡化
float angleX=0;
void myPrepareTexture(int i)//用參數來配, 找對應正確的變數
{//技巧3: 可以把重覆的東西, 用函式來簡化
cvCvtColor(img[i], img[i], CV_BGR2RGB);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &id[i]);
glBindTexture(GL_TEXTURE_2D, id[i]);
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 myTexture()
{
img[0]=cvLoadImage("imgbody.png");
img[1]=cvLoadImage("imgHand1.png");
img[2]=cvLoadImage("imgLeg1.png");
for(int i=0;i<3;i++){//技巧2: 可以把重覆的東西, 用迴圈來簡化
myPrepareTexture(i);
}
}
void myPolygon(int num){//技巧3: 可以把重覆的東西, 用函式來簡化
glPushMatrix();
glScalef(0.3, 0.8, 0.8);
glBindTexture(GL_TEXTURE_2D, id[num]);
glBegin(GL_POLYGON);
glTexCoord2f(0, 0); glVertex3f( -0.5, 0.5, 0);
glTexCoord2f(1, 0); glVertex3f( 0.5, 0.5, 0);
glTexCoord2f(1, 1); glVertex3f( 0.5, -0.5, 0);
glTexCoord2f(0, 1); glVertex3f( -0.5, -0.5, 0);
glEnd();
glPopMatrix();
}
void myBody(){
myPolygon(0);
}
void myLeg(){
myPolygon(1);
}
void myHand(){
myPolygon(2);
}
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
myBody();
glPushMatrix();
glTranslatef(0.2,0,0);//右手
glRotatef(angleX, 0,0,1);
glTranslatef(0, -0.4, 0);
myHand();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2,0,0);//左手
glRotatef(-angleX, 0,0,1);
glTranslatef(0, -0.4, 0);
myHand();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2,-0.3,0);//左腿
glRotatef(-angleX, 0,0,1);
glTranslatef(0, -0.4, 0);
myLeg();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='a') angleX+=3;
if(key=='b') angleX-=3;
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
glutCreateWindow("week12");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myTexture();//不要忘了加哦!
glutMainLoop();
}
2014年5月1日 星期四
week11
#include <GL/glut.h>
float angleX=0,angle1=0,angle2;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX, 0,1,0);
// glutSolidTeapot(0.3);
glutWireTeapot(0.3);//網狀
//包起來 <右手>
glPushMatrix();
glTranslatef(0.4,0,0);//球移出
glutSolidSphere(0.2,30,30);
glPushMatrix();//手臂<右>
glRotatef(angle1,0,0,1);
glTranslatef(0.3,0,0);//移出
glScalef(2,0.8,0.8);//方塊方向
glutSolidCube(0.2);
glPopMatrix();
glPopMatrix();
//包起來 <左>
glPushMatrix();
glTranslatef(-0.4,0,0);//球移出
glutSolidSphere(0.2,30,30);
glPushMatrix();//手臂<左>
glRotatef(angle1,0,0,1);
glTranslatef(-0.3,0,0);//移出
glScalef(2,0.8,0.8);//方塊方向
glutSolidCube(0.2);
glPopMatrix();
glPopMatrix();
//頭
glPushMatrix();//包起來 <上>
glTranslatef(0,0.4,0);//球移出
glutSolidSphere(0.2,30,30);
glPopMatrix();
//腳左
glPushMatrix();//大腿
glTranslatef(-0.2,-0.2,0);//球移出
glRotatef(angle1,0,0,1);
glTranslatef(0,-0.36/2,0);//移出
glPushMatrix();
glScalef(0.2,0.36,0.2);//方塊方向
glutSolidCube(1);
glPopMatrix();
glPushMatrix();//小腿
glTranslatef(0,-0.18,0);//球移出
glRotatef(angle1,0,0,1);
glTranslatef(0,-0.36/2,0);//移出
glScalef(0.2,0.36,0.2);//方塊方向
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
//腳右
glPushMatrix();//大腿
glTranslatef(0.2,-0.2,0);//球移出
glRotatef(angle1,0,0,1);
glTranslatef(0,-0.36/2,0);//移出
glPushMatrix();
glScalef(0.2,0.36,0.2);//方塊方向
glutSolidCube(1);
glPopMatrix();
glPushMatrix();//小腿
glTranslatef(0,-0.18,0);//球移出
glRotatef(angle1,0,0,1);
glTranslatef(0,-0.36/2,0);//移出
glScalef(0.2,0.36,0.2);//方塊方向
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='a') angleX+=3;
if(key=='b') angleX-=3;
if(key=='1') angle1+=3;
if(key=='2') angle2+=3;
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);
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}
2014年4月24日 星期四
2014年4月10日 星期四
2014年3月20日 星期四
2014年3月6日 星期四
2014年2月27日 星期四
2014年2月20日 星期四
訂閱:
文章 (Atom)













