2017電腦圖學 Computer Graphics 授課教師: 葉正聖 銘傳大學資訊傳播工程系 每週主題: 程式環境、點線面顏色、移動/旋轉/縮放與矩陣(Matrix)、階層性關節轉動(T-R-T)、做出機器人、打光、貼圖、glu/glut函式、鍵盤、滑鼠、計時器(timer)、讀入3D模型、粒子系統、聲音、特效、投影矩陣、攝影機與運鏡、機器人2.0、期末作品
2017年6月29日 星期四
Week16,圓圈圈 圈圈圓
hw1:貼圖背景
要放背景圖,我們在display函式中修改成如下:
void display()
{ glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glBegin(GL_POLYGON);///多邊形 開始畫
glTexCoord2f(0,0); glVertex3f(-1,+1,0);
glTexCoord2f(1,0); glVertex3f(+1,+1,0);
glTexCoord2f(1,1); glVertex3f(+1,-1,0);
glTexCoord2f(0,1); glVertex3f(-1,-1,0);
glEnd();///結束畫
glPopMatrix();
glFlush();
}
然後按下執行,貼圖就變成2D的了
要放背景圖,我們在display函式中修改成如下:
void display()
{ glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glBegin(GL_POLYGON);///多邊形 開始畫
glTexCoord2f(0,0); glVertex3f(-1,+1,0);
glTexCoord2f(1,0); glVertex3f(+1,+1,0);
glTexCoord2f(1,1); glVertex3f(+1,-1,0);
glTexCoord2f(0,1); glVertex3f(-1,-1,0);
glEnd();///結束畫
glPopMatrix();
glFlush();
}
其中,
glTexCoord2f(0,0); glVertex3f(-1,+1,0); glTexCoord2f(1,0); glVertex3f(+1,+1,0);
glTexCoord2f(1,1); glVertex3f(+1,-1,0);
glTexCoord2f(0,1); glVertex3f(-1,-1,0);
glTexCoord2f(1,1); glVertex3f(+1,-1,0);
glTexCoord2f(0,1); glVertex3f(-1,-1,0);
前面代表貼圖座標,後面代表3D頂點
Week14,圓圈圈 圈圈圓
hw1:讓上週我們做的機器人動起來
加入glRotatef(angle,0,1,0); 讓角色旋轉
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel1 = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel6 = NULL;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;
GLMmodel* pmodel9 = NULL;
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
angle++;
glRotatef(180, 0,1,0);
if (!pmodel) {
pmodel = glmReadOBJ("data/head.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
if (!pmodel1) {
pmodel1 = glmReadOBJ("data/body.obj");
if (!pmodel1) exit(0);
glmUnitize(pmodel1);
glmFacetNormals(pmodel1);
glmVertexNormals(pmodel1, 90.0);
}
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/larm1.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
if (!pmodel3) {
pmodel3 = glmReadOBJ("data/larm2.obj");
if (!pmodel3) exit(0);
glmUnitize(pmodel3);
glmFacetNormals(pmodel3);
glmVertexNormals(pmodel3, 90.0);
}
if (!pmodel4) {
pmodel4 = glmReadOBJ("data/lhand.obj");
if (!pmodel4) exit(0);
glmUnitize(pmodel4);
glmFacetNormals(pmodel4);
glmVertexNormals(pmodel4, 90.0);
}
if (!pmodel5) {
pmodel5 = glmReadOBJ("data/rarm1.obj");
if (!pmodel5) exit(0);
glmUnitize(pmodel5);
glmFacetNormals(pmodel5);
glmVertexNormals(pmodel5, 90.0);
}
if (!pmodel6) {
pmodel6 = glmReadOBJ("data/rarm2.obj");
if (!pmodel6) exit(0);
glmUnitize(pmodel6);
glmFacetNormals(pmodel6);
glmVertexNormals(pmodel6, 90.0);
}
if (!pmodel7) {
pmodel7 = glmReadOBJ("data/rhand.obj");
if (!pmodel7) exit(0);
glmUnitize(pmodel7);
glmFacetNormals(pmodel7);
glmVertexNormals(pmodel7, 90.0);
}
if (!pmodel8) {
pmodel8 = glmReadOBJ("data/rfoot.obj");
if (!pmodel8) exit(0);
glmUnitize(pmodel8);
glmFacetNormals(pmodel8);
glmVertexNormals(pmodel8, 90.0);
}
if (!pmodel9) {
pmodel9 = glmReadOBJ("data/lfoot.obj");
if (!pmodel9) exit(0);
glmUnitize(pmodel9);
glmFacetNormals(pmodel9);
glmVertexNormals(pmodel9, 90.0);
}
glPushMatrix();
glRotatef(angle, 0,1,0);
glPushMatrix();
glScalef(0.3,0.3,0.3);
glTranslatef(0,2,0);
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();
glScalef(0.3,0.3,0.3);
glTranslatef(0,0.5,0);
glmDraw(pmodel1, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();
glScalef(0.3,0.3,0.3);
glTranslatef(-0.4,0.8,0);
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();
glScalef(0.3,0.3,0.3);
glTranslatef(-0.55,0.55,0);
glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();
glScalef(0.3,0.3,0.3);
glTranslatef(-0.85,0.2,0);
glmDraw(pmodel4, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();
glScalef(0.3,0.3,0.3);
glTranslatef(0.2,0.8,0);
glmDraw(pmodel5, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();
glScalef(0.3,0.3,0.3);
glTranslatef(0.38,0.5,0);
glmDraw(pmodel6, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();
glScalef(0.3,0.3,0.3);
glTranslatef(0.8,0.1,0);
glmDraw(pmodel7, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();
glScalef(0.3,0.3,0.3);
glTranslatef(0.2,0,0);
glmDraw(pmodel8, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix();
glScalef(0.3,0.3,0.3);
glTranslatef(-0.2,0,0);
glmDraw(pmodel9, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
const GLfloat light_ambient[] = { 10.0f, 10.0f, 10.0f, 10.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, 4.0f, -10.0f, 10.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 };
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
glutCreateWindow("Yes, 3D Model Here");
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();
}
hw2:
3.計時器,在main()
const GLfloat light_ambient[] = { 10.0f, 10.0f, 10.0f, 10.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, 4.0f, -10.0f, 10.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 <stdio.h>
void timer(int t)///計時器叫的時候, 要做什麼呢?
{
glutTimerFunc(100, timer, t+1);///先撥下一個鬧鐘
angle++;///做了要做的事
printf("現在時間是 %d, 我起床了, 等一下鬧鐘會再叫一下哦 ", t);
glutPostRedisplay();///提醒電腦,有空要畫一下畫面哦
printf("... 然後我就又睡了....\n");
}///再度沈睡
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
glutCreateWindow("Yes, 3D Model Here");
glutDisplayFunc(display);
glutIdleFunc(display);
glutTimerFunc(0, timer, 0);///(1) 第一次呼叫timer計時器 glutTimerFunc(等多久,叫誰,參數);
2017年5月15日 星期一
2017年5月1日 星期一
Week11,圓圈圈 圈圈圓
hw1:輸出soccerball為open GL檔
step1:搜尋3D exploration,點選第一個
step2:下載檔案
step3:解壓縮後開啟
step4:至jsyeh.org/3dcg10下載data,並解壓縮
step5:至3D exploration匯入data
step6:另存為.cpp檔
hw2:讓
step1:新增專案
step2:建為soccerball
step3:移除原本的main
step4:將剛才輸出的soccerball放置近新的專案
step5:新增剛剛輸出的soccerball.cpp
step6:取消這兩行
step7:可放大縮小
hw3:放入新模型
step1:建立新專案
step2:些改程式碼
step3:至jsyeh.org/3dcg10下載source
step4:至3D exploration輸出三隻海豚
第一隻海豚
三個都要打勾
第二隻海豚
第三隻海豚
step1:搜尋3D exploration,點選第一個
step2:下載檔案
step3:解壓縮後開啟
step4:至jsyeh.org/3dcg10下載data,並解壓縮
step5:至3D exploration匯入data
step6:另存為.cpp檔
hw2:讓
step1:新增專案
step2:建為soccerball
step3:移除原本的main
step4:將剛才輸出的soccerball放置近新的專案
step5:新增剛剛輸出的soccerball.cpp
step6:取消這兩行
step7:可放大縮小
hw3:放入新模型
step1:建立新專案
step2:些改程式碼
step3:至jsyeh.org/3dcg10下載source
step4:至3D exploration輸出三隻海豚
第一隻海豚
三個都要打勾
第二隻海豚
第三隻海豚
2017年4月24日 星期一
Week10,圓圈圈 圈圈圓
hw1:操作遊戲
step1:搜尋nehe
step2:開啟右側Lesson 32
step3:移至最下方下載
DOWNLOAD Visual C++ Code For This Lesson.
step4:解壓縮檔案
step5:跑lesson32.ee的程式
hw2:使遊戲有聲音
step1:開啟codeblocks
step2:新增一個GLUT專案
step3:新增聲音程式碼
#include <mmsystem.h>///Multimedia System
void mouse(int button, int state, int x, int y)///Add a mouse Function
{
PlaySound("C:\\Users\\user\\Desktop\\lesson32\\Lesson32\\data/shot.wav", NULL, SND_ASYNC);///Copy this line
///要把檔名裡面的目錄裡面的斜線,從\變成(A)\\ or (B)/
}
glutMouseFunc(mouse);///Add a mouse Function
step1:搜尋nehe
step2:開啟右側Lesson 32
step3:移至最下方下載
DOWNLOAD Visual C++ Code For This Lesson.
step4:解壓縮檔案
step5:跑lesson32.ee的程式
hw2:使遊戲有聲音
step1:開啟codeblocks
step2:新增一個GLUT專案
step3:新增聲音程式碼
#include <mmsystem.h>///Multimedia System
void mouse(int button, int state, int x, int y)///Add a mouse Function
{
PlaySound("C:\\Users\\user\\Desktop\\lesson32\\Lesson32\\data/shot.wav", NULL, SND_ASYNC);///Copy this line
///要把檔名裡面的目錄裡面的斜線,從\變成(A)\\ or (B)/
}
glutMouseFunc(mouse);///Add a mouse Function
hw3:新增do re mi fa so聲音
step1:
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') PlaySound("C:\\Users\\user\\Desktop\\freeglut\\bin/do.wav", NULL, SND_ASYNC);
if(key=='2') PlaySound("C:\\Users\\user\\Desktop\\freeglut\\bin/re.wav", NULL, SND_ASYNC);
if(key=='3') PlaySound("C:\\Users\\user\\Desktop\\freeglut\\bin/mi.wav", NULL, SND_ASYNC);
if(key=='4') PlaySound("C:\\Users\\user\\Desktop\\freeglut\\bin/fa.wav", NULL, SND_ASYNC);
if(key=='5') PlaySound("C:\\Users\\user\\Desktop\\freeglut\\bin/so.wav", NULL, SND_ASYNC);
}
下面glutKeyboardFunc(key);
改為glutKeyboardFunc(keyboard);
hw4:播放自己的音樂
step1:
加這兩行#include "CMP3_MCI.h"///(1)放在同一個程式碼專案的目錄中
CMP3_MCI myMP3;///(2)宣告一個變數,叫myMP3
myMP3.Load("C:\\Users\\user\\Downloads/PLAYINGWITHFIRE.mp3");
myMP3.Play();
step1:
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') PlaySound("C:\\Users\\user\\Desktop\\freeglut\\bin/do.wav", NULL, SND_ASYNC);
if(key=='2') PlaySound("C:\\Users\\user\\Desktop\\freeglut\\bin/re.wav", NULL, SND_ASYNC);
if(key=='3') PlaySound("C:\\Users\\user\\Desktop\\freeglut\\bin/mi.wav", NULL, SND_ASYNC);
if(key=='4') PlaySound("C:\\Users\\user\\Desktop\\freeglut\\bin/fa.wav", NULL, SND_ASYNC);
if(key=='5') PlaySound("C:\\Users\\user\\Desktop\\freeglut\\bin/so.wav", NULL, SND_ASYNC);
}
下面glutKeyboardFunc(key);
改為glutKeyboardFunc(keyboard);
hw4:播放自己的音樂
step1:
加這兩行#include "CMP3_MCI.h"///(1)放在同一個程式碼專案的目錄中
CMP3_MCI myMP3;///(2)宣告一個變數,叫myMP3
myMP3.Load("C:\\Users\\user\\Downloads/PLAYINGWITHFIRE.mp3");
myMP3.Play();
2017年4月17日 星期一
2017年4月10日 星期一
Week08,圓圈圈 圈圈圓
hw1:複習期中考程式碼
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);///打光法向量
glTextCoord2f(tx, ty);///貼圖座標
glVertex3f(x, y, z);///頂點
glEnd();///結束
glPopMatrix();///還原矩陣
hw2:幫茶壺打光
如要更改光線→const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
hw4:輸出球形
glutWireSphere(0.4, 15, 5);
hw5:輸出各種形狀
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);///打光法向量
glTextCoord2f(tx, ty);///貼圖座標
glVertex3f(x, y, z);///頂點
glEnd();///結束
glPopMatrix();///還原矩陣
hw2:幫茶壺打光
如要更改光線→const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
改紅色字兩個數值即可
hw3:做出兩個茶壺一個可旋轉一個不動
glutWireSphere(0.4, 15, 5);
hw5:輸出各種形狀
2017年3月20日 星期一
Week05,圓圈圈 圈圈圓
hw1:
1.至jsyeh.org/3dcg10下載data win32 glut32.dll
2.解壓縮後將data的檔案以及glut32.dll放入windows的資料夾裡
3.放入之後即可執行transformation檔
hw2:
1.
2.使用上星期的小精靈來製作可移動的茶壺
3.
hw3:
1.
1.至jsyeh.org/3dcg10下載data win32 glut32.dll
2.解壓縮後將data的檔案以及glut32.dll放入windows的資料夾裡
3.放入之後即可執行transformation檔
hw2:
1.
2.使用上星期的小精靈來製作可移動的茶壺
3.
hw3:
1.
2017年3月13日 星期一
Week04,圓圈圈 圈圈圓
hw1:複習前兩週的內容
1.如果使用蘋果電腦需打→#inlcude <GLUT/glut.h>
2.今日上課學習glutMouseFunc(mouse);
glutMotionFunc(motion);
兩種程式一共5行
hw2:加入點按滑鼠及可出現座標
1.加入#include <math.h>
#include <stdio.h> 可使用printf
2.第3到7行為滑鼠點按之後顯示的座標
hw3:點按滑鼠之後即可改變茶壺顏色
1.glColor3f(1, x/300.0, y/300.0); 用1是因為RGB需要三個數值,但只有x y所以再加個1,沒有固定可以調換順序,例:glColor3f(x/300.0, 1, y/300.0);也可
hw4:拖曳滑鼠及可改變顏色
1.增加void motion(int x, int y)
{
printf("%d %d\n", x, y);
glColor3f(1, x/500.0, y/500.0);
glutPostRedisplay();
}
2.RGB裡除以的數字為視窗大小,基本設定為300,如需放大或縮小可在int main裡加上glutInitWindowSize(); 括號裡寫所需的視窗大小數值就好
hw5:畫圓形,使用三角函數
hw6:製作小精靈可用滑鼠開口閉口
1.
2.今日上課學習glutMouseFunc(mouse);
glutMotionFunc(motion);
兩種程式一共5行
hw2:加入點按滑鼠及可出現座標
1.加入#include <math.h>
#include <stdio.h> 可使用printf
2.第3到7行為滑鼠點按之後顯示的座標
hw3:點按滑鼠之後即可改變茶壺顏色
1.glColor3f(1, x/300.0, y/300.0); 用1是因為RGB需要三個數值,但只有x y所以再加個1,沒有固定可以調換順序,例:glColor3f(x/300.0, 1, y/300.0);也可
hw4:拖曳滑鼠及可改變顏色
1.增加void motion(int x, int y)
{
printf("%d %d\n", x, y);
glColor3f(1, x/500.0, y/500.0);
glutPostRedisplay();
}
2.RGB裡除以的數字為視窗大小,基本設定為300,如需放大或縮小可在int main裡加上glutInitWindowSize(); 括號裡寫所需的視窗大小數值就好
hw5:畫圓形,使用三角函數
hw6:製作小精靈可用滑鼠開口閉口
1.
訂閱:
文章 (Atom)




















































