2017電腦圖學 Computer Graphics 授課教師: 葉正聖 銘傳大學資訊傳播工程系 每週主題: 程式環境、點線面顏色、移動/旋轉/縮放與矩陣(Matrix)、階層性關節轉動(T-R-T)、做出機器人、打光、貼圖、glu/glut函式、鍵盤、滑鼠、計時器(timer)、讀入3D模型、粒子系統、聲音、特效、投影矩陣、攝影機與運鏡、機器人2.0、期末作品
2017年6月14日 星期三
2017年5月23日 星期二
Week14 陳冠宇 讓作業轉
用老師示範阿邱的檔案
用notepad++去更改自己作業cbp檔案的freeglut路徑
以及debug的dir與release的dir
使用timer計時器讓模型轉動
void timer(int t)
{
angle+=5;
glutTimerFunc(100,timer,t+1);///重複呼叫
glutPostRedisplay();
}
int main(){
GlutDisplayfun(display);
GlutTimerfun(2000,timer,0);///時間,函式名稱,參數
}
重點
把畫圖函式整理成draw....();
加入TRT讓轉動軸心與位置成功後
加入mouse keyboard motion函式
讓滑鼠鍵盤可以控制動作
int oldX=0,oldY=0;
void mouse(int button,int state, int x, int y)
{
oldX=x;
oldY=y;
glutPostRedisplay(display);
}
void motion(int x,int y){
angle+=(x-oldX);////x座標控制角度 其他還沒使用
oldX=x;
glutPostRedisplay(display);
}
利用keyboard函式分開控制關節
宣告float angle[20]={};
int now;
void keyboard(unsigned char key,int x, int y)
{
if(key=='0')now=0
...
...
...
}
void motion函式內改為angle[now]+=(x-oldX);
並將關節角度改成指定的angle[now]即可
2017年5月16日 星期二
Week13 陳冠宇
TRT小考練習
老師moodle舉例
glPushMatrix();//
glTranslatef(x,y,z);//移動會旋轉的圖
glRotatef(angle,x,y,z);//旋轉角度與方式
glTranslatef(x,y,z);//移動旋轉的中心
drawHand();//畫手手
glPopMatrix();//
老師的截圖
加入圖檔 建置
(1)安裝opencv2.1 設定PATH
(2)開啟CodeBlocks 貝殼專案
(3)BuildOptions設定
1.Compiler include : c:\opencv2.1\include
2.Linker lib : c:\opencv2.1\lib
MYEARTH
換成茶壺
2017年5月9日 星期二
Week11 陳冠宇
(上禮拜請假)
下載3D Ecploration
打開soccerball和soccer
使用海豚模型
下載3D Ecploration
打開soccerball和soccer
使用海豚模型
將glm.h放入目錄
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
angle++;
glRotatef(angle, 0,1,0);
if (!pmodel) {
pmodel = glmReadOBJ("data/dolphins1.obj"); //第一隻
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/dolphins2.obj"); //第二隻
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
if (!pmodel3) {
pmodel3 = glmReadOBJ("data/dolphins3.obj"); //第三隻
if (!pmodel3) exit(0);
glmUnitize(pmodel3);
glmFacetNormals(pmodel3);
glmVertexNormals(pmodel3, 90.0);
}
glPushMatrix(); //第一隻
glTranslatef(0,0,0);//移動
glRotatef(90,0,1,0);//旋轉
glScalef(0.8,0.8,0.8);//縮放
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix(); //第二隻
glTranslatef(0.5,0,0);//移動
glScalef(0.4,0.4,0.4);//縮放
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix(); //第三隻
glTranslatef(-0.5,0,0);//移動
glRotatef(180,0,1,0);//旋轉
glScalef(0.4,0.4,0.4);//縮放
glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
#include <mmsystem.h> //(1)Multimedia system
void keyboard(unsigned char key,int x,int y)
{ //(5)Add keyboard for Do/Re/Mi/Fa/So
if(key=='1') PlaySound("Do.wav",NULL,SND_ASYNC);
if(key=='2') PlaySound("Re.wav",NULL,SND_ASYNC);
if(key=='3') PlaySound("Mi.wav",NULL,SND_ASYNC);
if(key=='4') PlaySound("Fa.wav",NULL,SND_ASYNC);
if(key=='5') PlaySound("So.wav",NULL,SND_ASYNC);
}
void mouse(int button,int state,int x,int y)
{
PlaySound("C:\\Users\\user\\Desktop\\myGLMsample\\data\\Shot.wav",NULL,SND_ASYNC);
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
glutCreateWindow("Yes, 3D Model Here");
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutDisplayFunc(display);
glutIdleFunc(display);
glClearColor(1,1,1,1);
//glEnable(GL_CULL_FACE);
//glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
glutMainLoop();
}
Week12 陳冠宇
WEEK12
PLAY MUSIC
將CMP3_MCI.h 還有音樂檔案yukai.mp3放進目錄
才能incldue及load&play檔案
gluLookAt(eyeX,eyeY,eyeZ);
加入攝影機gluLookat後
加入四行不知道是什麼東西的後
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1, 0.001, 10000);
glMatrixMode(GL_MODELVIEW);
(還有加入angle讓他轉動 並在main加入重寫display的glutIdleFunc(display);)
glMatrixMode(GL_PROJECTION); ///透視投影法
glLoadIdentity();
gluPerspective(60, 1, 0.001, 10000);
///fov: field of view 視角方向 (y)
///aspect: 圖xy的比例
///zNear,zFar
glMatrixMode(GL_MODELVIEW); ///切回正常投影
gluLookAt(eyeX,eyeY,eyeZ, ///攝影機位置
centerX,centerY,centerZ, ///center位置
upX,upY,upZ); ///相機角度
使用T-R-T與angle&dir角度用出骨架示範 茶壺棒棒
PLAY MUSIC
將CMP3_MCI.h 還有音樂檔案yukai.mp3放進目錄
才能incldue及load&play檔案
Michael Bay rotation camera
茶壺進化史
加入攝影機gluLookat後
加入四行不知道是什麼東西的後
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1, 0.001, 10000);
glMatrixMode(GL_MODELVIEW);
(還有加入angle讓他轉動 並在main加入重寫display的glutIdleFunc(display);)
glMatrixMode(GL_PROJECTION); ///透視投影法
glLoadIdentity();
gluPerspective(60, 1, 0.001, 10000);
///fov: field of view 視角方向 (y)
///aspect: 圖xy的比例
///zNear,zFar
glMatrixMode(GL_MODELVIEW); ///切回正常投影
gluLookAt(eyeX,eyeY,eyeZ, ///攝影機位置
centerX,centerY,centerZ, ///center位置
upX,upY,upZ); ///相機角度
使用T-R-T與angle&dir角度用出骨架示範 茶壺棒棒
2017年4月25日 星期二
Week10 陳冠宇
用足球檔案了解模型的程式碼,obj and mlt檔案
使用codeblocks匯入模型
是車車
play game
把裡面的槍聲拿來用
在模型的程式碼加上滑鼠函式
void mouse(int button, int state, int x , int y)
{
PlaySound("C:\\Users\\user\\Desktop\\myGLMsample\\Shot.wav",NULL,SND_ASYNC);
}
並在main函式中加上glutMouseFunc(mouse);
加入do re mi fa so鋼琴音 使用keyboard函式
使用codeblocks匯入模型
是車車
換成花 檔案在data資料夾 更改匯入來源名稱
把裡面的槍聲拿來用
在模型的程式碼加上滑鼠函式
void mouse(int button, int state, int x , int y)
{
PlaySound("C:\\Users\\user\\Desktop\\myGLMsample\\Shot.wav",NULL,SND_ASYNC);
}
並在main函式中加上glutMouseFunc(mouse);
加入do re mi fa so鋼琴音 使用keyboard函式
2017年4月11日 星期二
Week08 陳冠宇
複習期中考題
glPushMatrix();///備份矩陣
glTranslatef(x,y,z);///移動
glRotatef(angle,x,y,z);///旋轉
glScalef(x,y,z);///縮放
glBegin(GL_POLYGON);///開始畫
glColor3f(r,g,b);///顏色
glNormal3f(nx,ny,nz);///打光的法向量
glTexCoord2f(tx,ty);///貼圖
glVertex3f(x,y,z);///座標
glEnd();///結束
glPopMatrix();///還原矩陣
製作打光茶壺
畫完茶壺留下打光的八行與main函式中間的打光八行後
2017年3月28日 星期二
Week03 陳冠宇
1.
讓茶壺變色
glColor3f(r,g,b);
2.
glBegin(GL_POLYGON);
glColor3f(1,0,0);//顏色(R,G,B)
glVertex3f(0,0,0);//頂點(x,y,z)
glColor3f(0,1,0);
glVertex3f(1,1,0);
glColor3f(0,0,1);
glVertex3f(1,-1,0);
glEnd();
week06 陳冠宇
期中考考題筆記
glPushMatrix();//備份矩陣
glTraslatef(x,y,z);//移動
glRotatef(angle,x,y,z);//旋轉
glScalef(x,y,z);//縮放
glBegin(GL_POLYGON);//開始畫
glColor3f(r,g,b);//顏色
glVertex3f(x,y,z)頂點
glNormal3f(nx,ny,nz);//打光的法向量
glTexCoord2f(tx,ty);//貼圖座標
glEnd();//結束畫
glPopMatrix;//還原矩陣
這樣的程式碼由開始、結束往上跑
再縮小放大
旋轉
移動
如果把旋轉跟移動換順序
glPushMatrix();//備份矩陣
glRotatef(angle,x,y,z);//旋轉
glTraslatef(x,y,z);//移動
glBegin(GL_POLYGON);//開始畫
glEnd();//結束畫
glPopMatrix;//還原矩陣
機械手臂的基礎教學(利用兩個茶壺嘗試)
用glPushMatrix();和glPopMatrix();當大括號小括號
glPushMatrix();
glutSolidTeapot(0.3);
glPushMatrix();
glRotatef(mouseX, 0,0,1);
glTranslatef(0.5, 0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
目前可以照著圓心旋轉,然後嘗試讓他以第一個茶壺尖端旋轉
glPushMatrix();
glRotatef(rotX,0,0,1);///旋轉
glColor3f(1,0,0);//////第三步 畫紅色茶壺
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.5,0.13,0);///第四步 讓白色茶壺把手移動到紅色茶壺嘴
glRotatef(rotX,0,0,1);///旋轉
glTranslatef(0.42,-0.09,0);///第二步 讓把手剛好移動到旋轉中心
glColor3f(1,1,1);///第一步 畫一個白色茶壺
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
2017年3月14日 星期二
Week04 陳冠宇
今天我遲到第二節課才到教室
但積極向上的我跟上進度
畫出一個圓
void display()
{
glBegin(GL_POLYGON);
for(float angle=0; angle <= 3.1415926*2; angle += 0.01)
{
glVertex2f(cos(angle),sin(angle));
}
glEnd();
glutSwapBuffers();
}
並在main中加上
glutDisplayFunc(display);
然後使用Mouse函式
在main中加入
glutMouseFunc(mouse);
打上
void mouse(int button,int state, int x, int y)
{
printf("%d %d %d %d\n",button,state,x,y);
glColor3f(1,x/300.0,y/300.0);
}
mouse函式使用四個int整數
第一個整數button代表滑鼠左(0)中(1)右(2)鍵
第二個整數state代表滑鼠動作按下(0)放開(1)
第三個第四個整數x,y則代表座標
在這邊特別使用printf把四個整數秀出來讓大家更清楚了解函式內容
記得加上include <stido.h>
glColor3f(1,x/300.0,y/300.0);
後方x,y除300.0意義在
此圓視窗為300*300
整數除以浮點數讓後方兩個值會在0~1間
如何不用一直點滑鼠讓他改變顏色?
使用motion函式
在main中加入glutMotionfunc(motion);
函式使用兩個整數int,x座標與y座標
void motion(int x,int y)
{
glColor3f(1,x/300.0,y/300.0);
glutPostRedisplay();
}
按下滑鼠後移動座標就可以持續改變顏色
glutPostRedisplay();作用在於
讓程式重覆執行display函式
畫出小精靈式的嘴巴開口
先在外面宣告一個mouth浮點數為0
void motion(int x,int y)
{
mouth = x/300.0;
glColor3f(1,x/300.0,y/300.0);
glutPostRedisplay();
}.
在motion函式中加入mouth=x/300.0;
讓開口跟著滑鼠座標改變大小
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
///這行每次都必打 清畫面
glBegin(GL_TRIANGLE_FAN);
///使用TRAINGLE FAN 扇形
glVertex2f(0,0);
///宣告扇形起始座標為0.0
for(float angle=0+mouth; angle <= 3.1415926*2-mouth; angle += 0.01)
///畫圓起始點+mouth,結束點-mouth
{
glVertex2f(cos(angle),sin(angle));
}
glEnd();
glutSwapBuffers();
}
可愛的小精靈出現ㄌ
2017年2月21日 星期二
Week01 陳冠宇
一、WebGL Water
測試電腦是否可以跑WebGL Water
二、CodeBlocks OPENGL
使用CodeBlocks opengl專案
1.開啟codeblocks
2.左上file→new→project
3.選擇OpenGL project
4.輸入標題、記得選擇存取檔案位置
5.按F9執行他給的原始程式碼
三、Opengl GLUT
1.使用codeblocks開起一個新的GLUT project
2.下載程式所需要的資料 小葉老師有提供在社團
3.選擇資料夾位置
4.原始程式碼F9執行
5.簡化程式碼後執行
6.加入glutSolidTeapot(); ///實心茶壺
成功完成第一個程式 666
訂閱:
文章 (Atom)




















































