2014 電腦圖學 Computer Graphics 授課教師: 葉正聖 銘傳大學資訊傳播工程系 每週主題: 程式環境、點線面顏色、移動/旋轉/縮放與矩陣(Matrix)、階層性關節轉動(T-R-T)、做出機器人、打光、貼圖、glu/glut函式、鍵盤、滑鼠、計時器(timer)、讀入3D模型、粒子系統、聲音、特效、投影矩陣、攝影機與運鏡、機器人2.0、期末作品
2014年6月13日 星期五
2014年6月6日 星期五
.
#include <GL/glut.h>
#include <math.h>
float angleX =0,angle2=0,angle3=0,angle4=0,angle5=0,angle6=0,angle7=0;
float eyeX=0.3,eyeY=0.2,eyeZ=0.4,objX=0,objY=0,objZ=0,upX=0,upY=1,upZ=0;
void timerRotateShow(int t);
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=5;
if(key=='3')
{
if(angle2<=100)angle2+=5;
}
if(key=='4')
{
if(angle2>=-110)angle2-=5;
}
if(key=='5')
{
if(angle3<=100)angle3+=5;
}
if(key=='6')
{
if(angle3>=-110)angle3-=5;
}
if(key=='7')
{
if(angle4<=90)angle4+=5;
}
if(key=='8')
{
if(angle4>=-50)angle4-=5;
}
if(key=='q')
{
if(angle5<=90)angle5+=5;
}
if(key=='w')
{
if(angle5>=-50)angle5-=5;
}
if(key=='e')
{
if(angle6<=0)angle6+=5;
}
if(key=='r')
{
if(angle6>=-50)angle6-=5;
}
if(key=='t')
{
if(angle7<=0)angle7+=5;
}
if(key=='y')
{
if(angle7>=-50)angle7-=5;
}
if(key==' ')
{
eyeX-=0.02;
eyeY-=0.02;
}
else if(key=='o')
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2,2, -2,2, -10,10);
glMatrixMode(GL_MODELVIEW);//垂直正交投影
}
else if(key=='k')
{
glutTimerFunc(0,timerRotateShow,0);
}
glutPostRedisplay();//請電腦重新畫一次畫面
}
void timerRotateShow(int t)
{
glutTimerFunc(30,timerRotateShow,t+1);
eyeX=cos(t/100.0*3.1415);
eyeZ=sin(t/100.0*3.1415);
eyeY-=0.005;
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();
gluLookAt( eyeX, eyeY, eyeZ, objX, objY, objZ, upX, upY, upZ);
glRotatef(angleX, 0,1,0);
glutSolidTeapot(0.3);
glPushMatrix();//右手臂
glTranslatef(0.5,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();
glRotatef(angle2,0,0,1);
glTranslatef(0.2,0,0);
glScalef(0.4,0.2,0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//左手臂
glTranslatef(-0.5,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();
glRotatef(angle3,0,0,1);
glTranslatef(-0.2,0,0);
glScalef(0.4,0.2,0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//右肩
glTranslatef(0.5,0,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix();//左肩
glTranslatef(-0.5,0,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix();//左大腿
glRotatef(angle4, 1,0,0);
glTranslatef(0.15,-0.4,0);
glScalef(0.2,0.5,0.2);
glutSolidCube(1);
glPushMatrix();//左小腿
glTranslatef(0,-0.1,0);
glRotatef(angle6, 1,0,0);
glTranslatef(0,-0.8,0);
glScalef(0.6,1,0.5);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//右大腿
glRotatef(angle5, 1,0,0);
glTranslatef(-0.15,-0.4,0);
glScalef(0.2,0.5,0.2);
glutSolidCube(1);
glPushMatrix();//右小腿
glTranslatef(0,-0.1,0);
glRotatef(angle7, 1,0,0);
glTranslatef(0,-0.8,0);
glScalef(0.6,1,0.5);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//頭
glTranslatef(0,0.5,0);
glutSolidSphere(0.3,30,30);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("01160315_robot");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}
#include <math.h>
float angleX =0,angle2=0,angle3=0,angle4=0,angle5=0,angle6=0,angle7=0;
float eyeX=0.3,eyeY=0.2,eyeZ=0.4,objX=0,objY=0,objZ=0,upX=0,upY=1,upZ=0;
void timerRotateShow(int t);
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=5;
if(key=='3')
{
if(angle2<=100)angle2+=5;
}
if(key=='4')
{
if(angle2>=-110)angle2-=5;
}
if(key=='5')
{
if(angle3<=100)angle3+=5;
}
if(key=='6')
{
if(angle3>=-110)angle3-=5;
}
if(key=='7')
{
if(angle4<=90)angle4+=5;
}
if(key=='8')
{
if(angle4>=-50)angle4-=5;
}
if(key=='q')
{
if(angle5<=90)angle5+=5;
}
if(key=='w')
{
if(angle5>=-50)angle5-=5;
}
if(key=='e')
{
if(angle6<=0)angle6+=5;
}
if(key=='r')
{
if(angle6>=-50)angle6-=5;
}
if(key=='t')
{
if(angle7<=0)angle7+=5;
}
if(key=='y')
{
if(angle7>=-50)angle7-=5;
}
if(key==' ')
{
eyeX-=0.02;
eyeY-=0.02;
}
else if(key=='o')
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2,2, -2,2, -10,10);
glMatrixMode(GL_MODELVIEW);//垂直正交投影
}
else if(key=='k')
{
glutTimerFunc(0,timerRotateShow,0);
}
glutPostRedisplay();//請電腦重新畫一次畫面
}
void timerRotateShow(int t)
{
glutTimerFunc(30,timerRotateShow,t+1);
eyeX=cos(t/100.0*3.1415);
eyeZ=sin(t/100.0*3.1415);
eyeY-=0.005;
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();
gluLookAt( eyeX, eyeY, eyeZ, objX, objY, objZ, upX, upY, upZ);
glRotatef(angleX, 0,1,0);
glutSolidTeapot(0.3);
glPushMatrix();//右手臂
glTranslatef(0.5,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();
glRotatef(angle2,0,0,1);
glTranslatef(0.2,0,0);
glScalef(0.4,0.2,0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//左手臂
glTranslatef(-0.5,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();
glRotatef(angle3,0,0,1);
glTranslatef(-0.2,0,0);
glScalef(0.4,0.2,0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//右肩
glTranslatef(0.5,0,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix();//左肩
glTranslatef(-0.5,0,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix();//左大腿
glRotatef(angle4, 1,0,0);
glTranslatef(0.15,-0.4,0);
glScalef(0.2,0.5,0.2);
glutSolidCube(1);
glPushMatrix();//左小腿
glTranslatef(0,-0.1,0);
glRotatef(angle6, 1,0,0);
glTranslatef(0,-0.8,0);
glScalef(0.6,1,0.5);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//右大腿
glRotatef(angle5, 1,0,0);
glTranslatef(-0.15,-0.4,0);
glScalef(0.2,0.5,0.2);
glutSolidCube(1);
glPushMatrix();//右小腿
glTranslatef(0,-0.1,0);
glRotatef(angle7, 1,0,0);
glTranslatef(0,-0.8,0);
glScalef(0.6,1,0.5);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();//頭
glTranslatef(0,0.5,0);
glutSolidSphere(0.3,30,30);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("01160315_robot");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}
2014年5月30日 星期五
2014年5月16日 星期五
.
#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]={"1.png", "2.png", "3.png", "4.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("week13");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myTexture();
glutMainLoop();
}
2014年5月9日 星期五
.
#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);
glCoord
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myTexture();
myLight();
glutMainLoop();
}
2014年5月2日 星期五
.
#include <GL/glut.h>
float angleX =0,angle2=0;
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleX+=5;
if(key=='2') angleX-=5;
if(key=='3') angle2+=5;
if(key=='4') angle2-=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);
glPushMatrix();//右肩
glTranslatef(0.4,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();//右手臂
glRotatef(angle2,0,0,1);
glTranslatef(0,-0.5,0);
glScalef(0.2,1.5,0.2);
glutSolidCube(0.5);
glPopMatrix();
glPopMatrix();
glPushMatrix();//左肩
glTranslatef(-0.4,0,0);
glutSolidSphere(0.2,30,30);
glPushMatrix();//左手臂
glRotatef(angle2,0,0,1);
glTranslatef(0,-0.5,0);
glScalef(0.2,1.5,0.2);
glutSolidCube(0.5);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0,0.4,0);
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week11");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}
2014年4月25日 星期五
.
#include<stdio.h>
float angleX=0;
void keyboard (unsigned char key,int x,int y)
{
printf("%c %d %d\n",key,x,y);
if(key=='a') angleX+=3;
if(key=='b') angleX-=3;
glutPostRedisplay();
}
2014年4月11日 星期五
2014年3月28日 星期五
.
cvCvtColor(img,img,CV_BGR2RGB);
GLuint id;
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);
GLUquadric * quad=NULL;
float angle=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle,0,1,0);
glRotatef(90,1,0,0);
gluQuadricTexture(quad,true);
gluSphere(quad,1,30,30);
glPopMatrix();
glutSwapBuffers();
}
void idle()
{
angle+=1;
glutPostRedisplay();
}
2014年3月21日 星期五
2014年3月14日 星期五
2014年3月7日 星期五
2014年2月21日 星期五
訂閱:
文章 (Atom)













