期末作品網址連結
https://www.youtube.com/watch?v=tl_QVlidL2E&feature=youtu.be
2014 電腦圖學 Computer Graphics 授課教師: 葉正聖 銘傳大學資訊傳播工程系 每週主題: 程式環境、點線面顏色、移動/旋轉/縮放與矩陣(Matrix)、階層性關節轉動(T-R-T)、做出機器人、打光、貼圖、glu/glut函式、鍵盤、滑鼠、計時器(timer)、讀入3D模型、粒子系統、聲音、特效、投影矩陣、攝影機與運鏡、機器人2.0、期末作品
2014年6月12日 星期四
2014年5月29日 星期四
Week15 課堂作業_01161044劉毓潔
#include <stdio.h>
#include <GL/glut.h>
int angleID=0;
float a=0;
float angle[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleold[20]={00,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float anglenew[20]={90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle[0], 0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
FILE *fout=NULL, *fin=NULL;
void time(int t)
{
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='0'){
angleID=1;
}
else if(key=='1'){
angleID=1;
}
else if(key=='2'){
angleID=2;
}
else if(key=='s'){
if(fout==NULL)
fout=fopen("this_is_my_saved_angles.txt","w+");
//fprintf(fout, "%f %f %f %f %f %f)
for(int i=0;i<20;i++)
fprintf(fout,"%f",angle[i]);
fprintf(fout,"\n");
}
else if(key=='r'){
if(fin==NULL) fin=fopen("this is my saved angles.txt","r");
a+=0.1;
angle[0]=angleold[0]*(1-a) + anglenew[0]*a;
if(a>=1){
a=0;
for(int i=0;i<20;i++){
angleold[i]=anglenew[i];
fscanf(fin,"%f", &anglenew[i]);
}
}
glutPostRedisplay();
}
else if(key=='p'){
}
}
float oldX=0, oldY=0;
void mouse(int button, int state, int x, int y){
oldX=x; oldY=y;
}
void motion(int x, int y){
angle[angleID]+= (x-oldX);
oldX=x;
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}
#include <stdio.h>
#include <GL/glut.h>
int angleID=0;
float a=0;
float angle[20]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float angleold[20]={00,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
float anglenew[20]={90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle[0], 0,0,1);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
FILE *fout=NULL, *fin=NULL;
void readNext(){
if(fin==NULL) fin=fopen("this_is_my_saved_angles.txt","r");
a+=0.1;
for(int i=0;i<20;i++)
angle[i]=angleold[i]*(1-a) + anglenew[i]*a;
if(a>=1){
a=0;
for(int i=0;i<20;i++){
angleold[i]=anglenew[i];
fscanf(fin,"%f",&anglenew[i]);
}
}
}
void timer(int t)
{
glutTimerFunc(100,timer,0);
readNext();
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='0'){
angleID=1;
}
else if(key=='1'){
angleID=1;
}
else if(key=='2'){
angleID=2;
}
else if(key=='s'){
if(fout==NULL)
fout=fopen("this_is_my_saved_angles.txt","w+");
//fprintf(fout, "%f %f %f %f %f %f)
for(int i=0;i<20;i++)
fprintf(fout,"%f",angle[i]);
fprintf(fout,"\n");
}
else if(key=='r'){
readNext();
glutPostRedisplay();
}
else if(key=='p'){
glutTimerFunc(0, timer, 0);
}
}
float oldX=0, oldY=0;
void mouse(int button, int state, int x, int y){
oldX=x; oldY=y;
}
void motion(int x, int y){
angle[angleID]+= (x-oldX);
oldX=x;
glutPostRedisplay();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week15");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
}
2014年5月22日 星期四
2014年5月15日 星期四
Week13 課堂作業_01161044劉毓潔
程式碼:
#include <GL/glut.h>
#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("body.png");
img[1]=cvLoadImage("Hand1.png");
img[2]=cvLoadImage("Leg1.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();
}
手和腳都出來了(大手.小手)
程式碼:
#include <GL/glut.h>
#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("body.png");
img[1]=cvLoadImage("Hand1.png");
img[2]=cvLoadImage("Leg1.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();
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);
myBody();
glPushMatrix();
glTranslatef(0.2,0,0);//右手
glRotatef(angleX, 0,0,1);
glTranslatef(0, -0.2, 0);
myHand();
glPushMatrix();
glTranslatef(0,-0.2,0);//小右手
glRotatef(-angleX, 0,0,1);
glTranslatef(0, -0.2, 0);
myHand();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2,0,0);//左手
glRotatef(-angleX, 0,0,1);
glTranslatef(0, -0.2, 0);
myHand();
glPushMatrix();
glTranslatef(0,-0.2,0);//小右手
glRotatef(-angleX, 0,0,1);
glTranslatef(0, -0.2, 0);
myHand();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2,-0.3,0);//左腿
glRotatef(-angleX, 0,0,1);
glTranslatef(0, -0.2, 0);
myLeg();
glPopMatrix();
glPushMatrix();
glTranslatef(0.2,-0.3,0);//右腿
glRotatef(-angleX, 0,0,1);
glTranslatef(0, -0.2, 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();
}
利用數字1~8分別控制關節點
加入程式碼:
float angle1=0, angle2=0, angle3=0, angle4=0, angle5=0, angle6=0, angle7=0, angle8=0;
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();
}
2014年5月1日 星期四
Week11 課堂作業_01161044劉毓潔
#include <GL/glut.h>
float angleX=0;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();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='a') angleX+=3;
if(key=='b') angleX-=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();
}
(兩顆球)
(4顆球)
(較好的4顆球寫法)
利用glPushMatrix(); glPopMatrix();包起來保護他
#include <GL/glut.h>
float angleX=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX, 0,1,0);
glutWireTeapot(0.3);
glPushMatrix();
glTranslatef(0.4,0,0); //右邊球
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.4,0,0); //左邊球
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix();
glTranslatef(0,0.4,0); //上面球
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPushMatrix();
glTranslatef(0,-0.4,0); //下面球
glutSolidSphere(0.2,30,30);
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='a'||key=='A') angleX+=3;
if(key=='b'||key=='B') angleX-=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);
glutIdleFunc(display);
myLight();
glutMainLoop();
}
任務: 左右各畫個長方塊,像手臂一樣
旋轉長方塊
長方塊的腳,有大腿跟小腿
#include <GL/glut.h>
float angleX=0,angleY=0;
void display()
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX, 0,1,0);
glutWireTeapot(0.3); //茶壺
glPushMatrix(); //右腳
glTranslatef(0.2,-0.2,0);
glRotatef(angleY, 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(angleY, 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(angleY, 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(angleY, 0,0,1);
glTranslatef(0,-0.36/2,0);
glScalef(0.2,0.36,0.2);
glutSolidCube(1);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0.4,0,0); //右邊球
glutSolidSphere(0.2,30,30);
glRotatef(angleY, 0,0,1);
glTranslatef(0.3,0,0);
glScalef(1.5,1,1);
glutSolidCube(0.3);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.4,0,0); //左邊球
glutSolidSphere(0.2,30,30);
glRotatef(angleY, 0,0,-1);
glTranslatef(-0.3,0,0);
glScalef(1.5,1,1);
glutSolidCube(0.3);
glPopMatrix();
glPushMatrix();
glTranslatef(0,0.4,0); //上面球
glutSolidSphere(0.2,30,30);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='a'||key=='A') angleX+=3;
if(key=='b'||key=='B') angleX-=3;
if(key=='1') angleY+=3;
if(key=='2') angleY-=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);
glutIdleFunc(display);
myLight();
glutMainLoop();
}
2014年4月24日 星期四
2014年4月10日 星期四
2014年3月27日 星期四
Week06 課堂作業_01161044劉毓潔
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月20日 星期四
Week 05,課堂作業_01161044劉毓潔
做出圓形球體並且增加glutKeyboardFunc(keyboard);
按空白鍵,即可旋轉
用移動,增加另一個圓形球體,讓他在(0,0.5,0)座標上
http://jsyeh.org/3dcg10
下載 source, data, win32執行檔
下載 source, data, win32執行檔
2014年3月13日 星期四
Week 04 課堂作業,01161044劉毓潔
TODO: 加上 glPushMatrix() 及 glPopMatrix() 保護矩陣
加上glTranslatef(0, 0.5, 0);
重點:glPushMatrix(); 後面要記得加 glPopMatrix();
稍作修改,將茶壺增加為四個
2014年3月6日 星期四
2014年2月27日 星期四
2014年2月20日 星期四
訂閱:
文章 (Atom)







































