2017電腦圖學 Computer Graphics 授課教師: 葉正聖 銘傳大學資訊傳播工程系 每週主題: 程式環境、點線面顏色、移動/旋轉/縮放與矩陣(Matrix)、階層性關節轉動(T-R-T)、做出機器人、打光、貼圖、glu/glut函式、鍵盤、滑鼠、計時器(timer)、讀入3D模型、粒子系統、聲音、特效、投影矩陣、攝影機與運鏡、機器人2.0、期末作品
2017年6月28日 星期三
2017年6月12日 星期一
Week 16 阿威的課堂筆記
1.使用內插公式讓機器人動的順暢:

2.寫檔:
if(key=='s')
{
if(fout==NULL)
{
fout=fopen("file.txt","w+");
}
fprintf(fout,"%.2f %.2f %.2f %.2f %.2f\n",angle[0],angle[1],angle[2],angle[3],angle[4]);
printf("%.2f %.2f %.2f %.2f %.2f\n",angle[0],angle[1],angle[2],angle[3],angle[4]);
}
3.讀檔:
if(key=='r')
{
//PlaySound("C:\\Users\\Home\\Desktop\\04160613-robot2.0\\04160613-robot2.0\\04160613\\data\Linkin Park - New Divide.mp3",NULL,SND_ASYNC);
if(fin==NULL)
{
fin=fopen("file.txt","r");
fscanf(fin,"%f %f %f %f %f\n",&newangle[0],&newangle[1],&newangle[2],&newangle[3],&newangle[4]);
}
if(alpha>=1.0)
{
for(int i=0;i<5;i++)oldangle[i]=newangle[i];
fscanf(fin,"%f %f %f %f %f\n",&newangle[0],&newangle[1],&newangle[2],&newangle[3],&newangle[4]);
alpha=0.0;
}
for(int i=0;i<5;i++)
{
angle[i]=alpha*newangle[i]+(1-alpha)*oldangle[i];
}
alpha+=0.02;
//fscanf(fin,"%f %f %f %f %f\n",&angle[0],&angle[1],&angle[2],&angle[3],&angle[4]);
printf("%.2f %.2f %.2f %.2f %.2f\n",angle[0],angle[1],angle[2],angle[3],angle[4]);
glutPostRedisplay();
}
2.寫檔:
if(key=='s')
{
if(fout==NULL)
{
fout=fopen("file.txt","w+");
}
fprintf(fout,"%.2f %.2f %.2f %.2f %.2f\n",angle[0],angle[1],angle[2],angle[3],angle[4]);
printf("%.2f %.2f %.2f %.2f %.2f\n",angle[0],angle[1],angle[2],angle[3],angle[4]);
}
3.讀檔:
if(key=='r')
{
//PlaySound("C:\\Users\\Home\\Desktop\\04160613-robot2.0\\04160613-robot2.0\\04160613\\data\Linkin Park - New Divide.mp3",NULL,SND_ASYNC);
if(fin==NULL)
{
fin=fopen("file.txt","r");
fscanf(fin,"%f %f %f %f %f\n",&newangle[0],&newangle[1],&newangle[2],&newangle[3],&newangle[4]);
}
if(alpha>=1.0)
{
for(int i=0;i<5;i++)oldangle[i]=newangle[i];
fscanf(fin,"%f %f %f %f %f\n",&newangle[0],&newangle[1],&newangle[2],&newangle[3],&newangle[4]);
alpha=0.0;
}
for(int i=0;i<5;i++)
{
angle[i]=alpha*newangle[i]+(1-alpha)*oldangle[i];
}
alpha+=0.02;
//fscanf(fin,"%f %f %f %f %f\n",&angle[0],&angle[1],&angle[2],&angle[3],&angle[4]);
printf("%.2f %.2f %.2f %.2f %.2f\n",angle[0],angle[1],angle[2],angle[3],angle[4]);
glutPostRedisplay();
}
2017年5月23日 星期二
Week 14 阿威的課堂筆記
(1)上週作業
(2)讓機器人動
(3)Timer計時器

#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel6 = NULL;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;
float angle = 0 ;
void display()
{
glClearColor(1,248/255.0,203/255.0,24/255.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix( );
angle++;
glRotatef(angle, 0,1,0);
if (!pmodel)
{
pmodel = glmReadOBJ("obj/dolphins1.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
if (!pmodel2)
{
pmodel2 = glmReadOBJ("obj/dolphins2.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
if (!pmodel3)
{
pmodel3 = glmReadOBJ("obj/porsche.obj");
if (!pmodel3) exit(0);
glmUnitize(pmodel3);
glmFacetNormals(pmodel3);
glmVertexNormals(pmodel3, 90.0);
}
if (!pmodel4)
{
pmodel4 = glmReadOBJ("obj/soccerball.obj");
if (!pmodel4) exit(0);
glmUnitize(pmodel4);
glmFacetNormals(pmodel4);
glmVertexNormals(pmodel4, 90.0);
}
if (!pmodel5)
{
pmodel5 = glmReadOBJ("obj/Upleg_R.obj");
if (!pmodel5) exit(0);
glmUnitize(pmodel5);
glmFacetNormals(pmodel5);
glmVertexNormals(pmodel5, 90.0);
}
if (!pmodel6)
{
pmodel6 = glmReadOBJ("obj/Upleg_L.obj");
if (!pmodel6) exit(0);
glmUnitize(pmodel6);
glmFacetNormals(pmodel6);
glmVertexNormals(pmodel6, 90.0);
}
if (!pmodel7)
{
pmodel7 = glmReadOBJ("obj/Lowleg_R.obj");
if (!pmodel7) exit(0);
glmUnitize(pmodel7);
glmFacetNormals(pmodel7);
glmVertexNormals(pmodel7, 90.0);
}
if (!pmodel8)
{
pmodel8 = glmReadOBJ("obj/Lowleg_L.obj");
if (!pmodel8) exit(0);
glmUnitize(pmodel8);
glmFacetNormals(pmodel8);
glmVertexNormals(pmodel8, 90.0);
}
glPushMatrix( ); ///body
glTranslatef(0,0.1,0);///T
glRotatef(90,0,0,1);///R
glRotatef(90,0,1,0);
glRotatef(90,0,0,1);
glRotatef(180,1,0,0);
glTranslatef(0,0.5,0);///T
glScalef(0.3,0.3,0.3);
glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix( ); ///left hand arm
glTranslatef(-0.25,0.1,0);///T
glRotatef(45,0,0,1);///R
glTranslatef(0,0.2,0);///T
glScalef(0.25,0.25,0.25);
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix( ); ///left hand
glTranslatef(-0.25,0,0);///T
glRotatef(90,0,0,1);///R
glTranslatef(-0.1,0.25,0);///T
glScalef(0.25,0.25,0.25);
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix( ); ///Right hand arm
glTranslatef(0.25,0.1,0);///T
glRotatef(180,0,0,1);///R
glRotatef(180,1,0,0);
glRotatef(45,0,0,1);
glTranslatef(0,0.2,0);///T
glScalef(0.25,0.25,0.25);
glmDraw( pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Right hand
glTranslatef(0.5,0,0);///T
glRotatef(180,0,0,1);///R
glRotatef(180,1,0,0);
glRotatef(90,0,0,1);
glTranslatef(-0.1,0,0);///T
glScalef(0.25,0.25,0.25);
glmDraw( pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///head
glTranslatef(0,1,0);///T
glTranslatef(0,-0.3,0);///T
glScalef(0.3,0.3,0.3);
glmDraw( pmodel4, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Right Leg up
glTranslatef(-0.15,-0.3,0);///T
//glTranslatef(0,-0.2,0);///T
glScalef(0.3,0.5,0.3);
glmDraw( pmodel5, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Left Leg up
glTranslatef(0.15,-0.3,0);///T
//glTranslatef(0,-0.2,0);///T
glScalef(0.3,0.5,0.3);
glmDraw( pmodel6, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Right Leg low
glTranslatef(-0.2,-0.65,0);///T
glRotatef(180,0,1,0);
//glTranslatef(0,-0.2,0);///T
glScalef(0.5,0.75,0.5);
glmDraw( pmodel7, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Left Leg low
glTranslatef(0.2,-0.65,0);///T
glRotatef(180,0,1,0);
//glTranslatef(0,-0.2,0);///T
glScalef(0.5,0.75,0.5);
glmDraw( pmodel8, 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 <stdio.h>
void timer(int t)
{
printf("現在我起床了, t是%d...", t);
glutTimerFunc(100, timer, t+1);
printf("我撥了一個新的timer....");
angle+=3;
glutPostRedisplay();
printf("記得重畫畫面哦! 我睡了, bye\n");
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
glutCreateWindow("04160683's Robbot.");
glutDisplayFunc(display);
glutTimerFunc(2000, timer, 0);///睡前,先設定一個鬧鐘,2秒後會叫
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();
}
(4)鍵盤滑鼠控制
4-1 把含數拉出去,程式便清爽
4-2 經常把程式暫註解,讓畫面乾淨,好判斷T-R-T的數字
4-3 先把上手臂搞定,再搞定下手臂
(5)機器人2.0
(2)讓機器人動
(3)Timer計時器

#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel3 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel6 = NULL;
GLMmodel* pmodel7 = NULL;
GLMmodel* pmodel8 = NULL;
float angle = 0 ;
void display()
{
glClearColor(1,248/255.0,203/255.0,24/255.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix( );
angle++;
glRotatef(angle, 0,1,0);
if (!pmodel)
{
pmodel = glmReadOBJ("obj/dolphins1.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
if (!pmodel2)
{
pmodel2 = glmReadOBJ("obj/dolphins2.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
if (!pmodel3)
{
pmodel3 = glmReadOBJ("obj/porsche.obj");
if (!pmodel3) exit(0);
glmUnitize(pmodel3);
glmFacetNormals(pmodel3);
glmVertexNormals(pmodel3, 90.0);
}
if (!pmodel4)
{
pmodel4 = glmReadOBJ("obj/soccerball.obj");
if (!pmodel4) exit(0);
glmUnitize(pmodel4);
glmFacetNormals(pmodel4);
glmVertexNormals(pmodel4, 90.0);
}
if (!pmodel5)
{
pmodel5 = glmReadOBJ("obj/Upleg_R.obj");
if (!pmodel5) exit(0);
glmUnitize(pmodel5);
glmFacetNormals(pmodel5);
glmVertexNormals(pmodel5, 90.0);
}
if (!pmodel6)
{
pmodel6 = glmReadOBJ("obj/Upleg_L.obj");
if (!pmodel6) exit(0);
glmUnitize(pmodel6);
glmFacetNormals(pmodel6);
glmVertexNormals(pmodel6, 90.0);
}
if (!pmodel7)
{
pmodel7 = glmReadOBJ("obj/Lowleg_R.obj");
if (!pmodel7) exit(0);
glmUnitize(pmodel7);
glmFacetNormals(pmodel7);
glmVertexNormals(pmodel7, 90.0);
}
if (!pmodel8)
{
pmodel8 = glmReadOBJ("obj/Lowleg_L.obj");
if (!pmodel8) exit(0);
glmUnitize(pmodel8);
glmFacetNormals(pmodel8);
glmVertexNormals(pmodel8, 90.0);
}
glPushMatrix( ); ///body
glTranslatef(0,0.1,0);///T
glRotatef(90,0,0,1);///R
glRotatef(90,0,1,0);
glRotatef(90,0,0,1);
glRotatef(180,1,0,0);
glTranslatef(0,0.5,0);///T
glScalef(0.3,0.3,0.3);
glmDraw(pmodel3, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix( ); ///left hand arm
glTranslatef(-0.25,0.1,0);///T
glRotatef(45,0,0,1);///R
glTranslatef(0,0.2,0);///T
glScalef(0.25,0.25,0.25);
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix( ); ///left hand
glTranslatef(-0.25,0,0);///T
glRotatef(90,0,0,1);///R
glTranslatef(-0.1,0.25,0);///T
glScalef(0.25,0.25,0.25);
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPushMatrix( ); ///Right hand arm
glTranslatef(0.25,0.1,0);///T
glRotatef(180,0,0,1);///R
glRotatef(180,1,0,0);
glRotatef(45,0,0,1);
glTranslatef(0,0.2,0);///T
glScalef(0.25,0.25,0.25);
glmDraw( pmodel, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Right hand
glTranslatef(0.5,0,0);///T
glRotatef(180,0,0,1);///R
glRotatef(180,1,0,0);
glRotatef(90,0,0,1);
glTranslatef(-0.1,0,0);///T
glScalef(0.25,0.25,0.25);
glmDraw( pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///head
glTranslatef(0,1,0);///T
glTranslatef(0,-0.3,0);///T
glScalef(0.3,0.3,0.3);
glmDraw( pmodel4, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Right Leg up
glTranslatef(-0.15,-0.3,0);///T
//glTranslatef(0,-0.2,0);///T
glScalef(0.3,0.5,0.3);
glmDraw( pmodel5, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Left Leg up
glTranslatef(0.15,-0.3,0);///T
//glTranslatef(0,-0.2,0);///T
glScalef(0.3,0.5,0.3);
glmDraw( pmodel6, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Right Leg low
glTranslatef(-0.2,-0.65,0);///T
glRotatef(180,0,1,0);
//glTranslatef(0,-0.2,0);///T
glScalef(0.5,0.75,0.5);
glmDraw( pmodel7, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix( );
glPushMatrix( ); ///Left Leg low
glTranslatef(0.2,-0.65,0);///T
glRotatef(180,0,1,0);
//glTranslatef(0,-0.2,0);///T
glScalef(0.5,0.75,0.5);
glmDraw( pmodel8, 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 <stdio.h>
void timer(int t)
{
printf("現在我起床了, t是%d...", t);
glutTimerFunc(100, timer, t+1);
printf("我撥了一個新的timer....");
angle+=3;
glutPostRedisplay();
printf("記得重畫畫面哦! 我睡了, bye\n");
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
glutCreateWindow("04160683's Robbot.");
glutDisplayFunc(display);
glutTimerFunc(2000, timer, 0);///睡前,先設定一個鬧鐘,2秒後會叫
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();
}
4-1 把含數拉出去,程式便清爽
4-2 經常把程式暫註解,讓畫面乾淨,好判斷T-R-T的數字
4-3 先把上手臂搞定,再搞定下手臂
(5)機器人2.0
2017年5月16日 星期二
Week 13 阿威的課堂筆記
2017年5月9日 星期二
Week 12 阿威的課堂筆記
(1)MP3播放
(2)3D TRT 轉動
(3)矩陣,投影矩陣
(4)攝影機運鏡
(5)會動的機器人
TODO:播MP3 最簡單 CMP3_MCI.h
#include "CMP3_MCI.h"//(1)檔案放同目錄
CMP3_MCI myMP3;//(2)宣告變數
//在main()裡
(2)3D TRT 轉動
(3)矩陣,投影矩陣
(4)攝影機運鏡
(5)會動的機器人
TODO:播MP3 最簡單 CMP3_MCI.h
#include "CMP3_MCI.h"//(1)檔案放同目錄
CMP3_MCI myMP3;//(2)宣告變數
//在main()裡
int main()//(3)Load檔案
{
myMP3.Load("音樂.mp3");
myMP3.Play();(4)Play播放
//小心,要卡住
}
}
TODO:運鏡
TODO:jsyeh.org/3dcg10 下載
data.zip-> 桌面\windows\data\模型
windows.zip->桌面\windows\Projection.exe
glut32.dll->桌面\windows\glut32.dll
TODO:gluLookAt(eyeX,eyeY,eyeZ, centerX,centerY,centerZ,upX,upY,upZ);
2017年5月2日 星期二
Week 11 阿威的課堂筆記
TODO:Google:3D Exploration
1. 1.81免費使用版
2.jsyeh.org/3dcg10 有data.zip (有3D模形),解壓縮在桌面
3.用1.3D Exploration 去看 2模形 Ex soccerball
4.匯出CPP(OPENGL)程式 soocerball.cpp
Export Dialog裡SampleAPP (才會有main()函式)
5.CodeBlocks,File-new-Project. OpenGL專案
偷天換日 Remove main.c
Add soccerball.cpp
F9 Build&Run 有2行錯誤刪掉
完成了一個會轉動的足球!!!!
6.Save As Wavefront OBJ 檔
可調Visible 區域
data後面的檔案更改後,可以跑出兩隻海豚
也可以跑出一隻海豚的程式
也可以藉由更改程式,可以將兩隻海豚重疊
2017年4月25日 星期二
Week 10 阿威的課堂筆記
1.Maya匯出OBJ模型
2.glm讀模型、畫
3.wav音效,MP3音樂
4.小遊戲(回家作業)
void mouse(int button,int state,int x,int y)
{
PlaySound("C:\\Users\\user\\Downloads\\lesson32\\Lesson32\\Data/shot.wav",NULL,SND_ASYNC);
}
glutMouseFunc(mouse);
2.glm讀模型、畫
3.wav音效,MP3音樂
4.小遊戲(回家作業)
利用老師給我們的程式碼,成功的讓我們執行出一台會轉動的車子!!!
經過一點修改之後,可以變成一顆會轉動的足球!!!!
經過一點修改之後,可以變成一個會轉動的花瓶!!!!
經過一點修改之後,可以變成三隻會轉動的海豚!!!!
nehe很棒的圖學學習網站,裡面的小遊戲
新增了音效,讓點擊畫面就會有槍聲
#include <mmsystem.h>void mouse(int button,int state,int x,int y)
{
PlaySound("C:\\Users\\user\\Downloads\\lesson32\\Lesson32\\Data/shot.wav",NULL,SND_ASYNC);
}
glutMouseFunc(mouse);
2017年4月11日 星期二
Week 08 阿威的課堂筆記
期中模擬考:
glPushMatrix();//備份矩陣
glRotatef(angle,x,y,z);//旋轉
glTranslatef(x,y,z);//移動
glScalef(x,y,z);//縮放
glBegin(GL_POLYGON);//開始畫
glNormal3f(nx,ny,nz);//打光的法向量
glTexCoord2f(tx,ty);//貼圖座標
glColor3f(r,g,b);//顏色
glVertex3f(x,y,z);//頂點
glEnd();
glPopMatrix();//還原矩陣
(二)打光:
#include <GL/glut.h>
static void display(void)
{
const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
const double a = t*90.0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(1,0,0);
glPushMatrix();
glutSolidTeapot(0.3);
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 };
/* Program entry point */
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
// glutReshapeFunc(resize);
glutDisplayFunc(display);
// glutKeyboardFunc(key);
// glutIdleFunc(idle);
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();
return EXIT_SUCCESS;
}
口訣:
(1)找light 留8行
(2)找light 留8行
(3)找light 留8行
(4)留你的display()
(5)留你的main()
(三)讓茶壺可以轉動
#include <GL/glut.h>
float angle=0;
static void display(void)
{
angle++;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(1,0,0);
glPushMatrix();
///glTranslatef(x,y,z);
glRotatef(angle,0,1,0);
glutSolidTeapot(0.3);
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 };
/* Program entry point */
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
// glutReshapeFunc(resize);
glutDisplayFunc(display);
// glutKeyboardFunc(key);
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();
return EXIT_SUCCESS;
}
#include <GL/glut.h>
float angle=0;
static void display(void)
{
angle++;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3d(1,0,0);
glPushMatrix();
///glTranslatef(x,y,z);
glRotatef(angle,0,1,0);
// glutSolidTeapot(0.3);
// glutSolidCone(0.8,1,30,30);
//glutSolidDodecahedron();
glScalef(0.5,0.5,0.5);
glutSolidDodecahedron();
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 };
/* Program entry point */
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
// glutReshapeFunc(resize);
glutDisplayFunc(display);
// glutKeyboardFunc(key);
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();
return EXIT_SUCCESS;
}
2017年3月28日 星期二
Week 06 阿威的課堂筆記
(1)在Blog 練習期中考題:
glPushMatrix(); //備份矩陣
glTranslatef(x,y,z); //移動
glRotatef(角度x,y,z); //旋轉
glScalef(x,y,z); //縮放
glBegin(GL_POLYGON); //開始畫
glNormal3f(nx,ny,nz); //打光的法向量
glTexCoord2f(tx,ty); //貼圖座標
glColor3f(r,g,b); //顏色
glVertex3f(x,y,z); //頂點
glEnd();
glPopMatrix();
float mouseX=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(mouseX,0,0,1);
glColor3f(1,0,0);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.5, 0.14,0);
glRotatef(mouseX,0,0,1);
glTranslatef(0.41,-0.05,0);
glColor3f(1,1,1);
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y)
{
mouseX = x;
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("TRT robot");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
glPushMatrix(); //備份矩陣
glTranslatef(x,y,z); //移動
glRotatef(角度x,y,z); //旋轉
glScalef(x,y,z); //縮放
glBegin(GL_POLYGON); //開始畫
glNormal3f(nx,ny,nz); //打光的法向量
glTexCoord2f(tx,ty); //貼圖座標
glColor3f(r,g,b); //顏色
glVertex3f(x,y,z); //頂點
glEnd();
glPopMatrix();
茶壺轉:
#include <GL/glut.h>float mouseX=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(mouseX,0,0,1);
glColor3f(1,0,0);
glutSolidTeapot(0.3);
glPushMatrix();
glTranslatef(0.5, 0.14,0);
glRotatef(mouseX,0,0,1);
glTranslatef(0.41,-0.05,0);
glColor3f(1,1,1);
glutSolidTeapot(0.3);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y)
{
mouseX = x;
glutPostRedisplay();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("TRT robot");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
2017年3月21日 星期二
Week 05 阿威的課堂筆記
1.先了解今天課堂主題:
(1)移動
(2)旋轉
(3)縮放
float mouseX,mouseY;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(mouseX,mouseY,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
mouseX=(x-150)/150.0;
mouseY=-(y-150)/150.0;
glutPostRedisplay();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("04160613 translate rotate scale");
glutDisplayFunc(display);
// glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
3.Scale 縮放
float mouseX,mouseY;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glScalef(mouseX,mouseY,0);//可以跟著滑鼠放大縮小
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
mouseX=(x-150)/150.0;
mouseY=-(y-150)/150.0;
glutPostRedisplay();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("04160613 translate rotate scale");
glutDisplayFunc(display);
// glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
(1)移動
(2)旋轉
(3)縮放
2.加入glTranslatef() 可以更改茶壺的座標位置
3.配合glutMotion()和glTranslatef(),可以讓茶壺跟著我們移動
程式碼:
#include <GL/glut.h>float mouseX,mouseY;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(mouseX,mouseY,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
mouseX=(x-150)/150.0;
mouseY=-(y-150)/150.0;
glutPostRedisplay();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("04160613 translate rotate scale");
glutDisplayFunc(display);
// glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
3.Scale 縮放
程式碼:
#include <GL/glut.h>float mouseX,mouseY;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glScalef(mouseX,mouseY,0);//可以跟著滑鼠放大縮小
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
mouseX=(x-150)/150.0;
mouseY=-(y-150)/150.0;
glutPostRedisplay();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("04160613 translate rotate scale");
glutDisplayFunc(display);
// glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
3.rotate
程式碼:
#include <GL/glut.h>
float mouseX,mouseY,rotX=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(rotX,0,0,1);//rotX代表旋轉角度
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void motion(int x,int y)
{
mouseX=(x-150)/150.0;
mouseY=-(y-150)/150.0;
rotX=x;
glutPostRedisplay();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("04160613 translate rotate scale");
glutDisplayFunc(display);
// glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
}
2017年3月14日 星期二
Week 04 阿威的課堂筆記
Hw1 作業互評 選出三個在作業二的優秀作品:
Hw2 畫出一個圓形:
利用Cos和Sin畫出一個正圓:
圓形的程式碼:
#include <GL/glut.h>#include <math.h>
void display()
{
glBegin(GL_POLYGON);
for(float angle=0;angle<=3.1415926*2;angle+=0.01)
{
glVertex2f(cos(angle),sin(angle));
}
glEnd();
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMainLoop();
return EXIT_SUCCESS;
}
Hw3 利用滑鼠點擊來變色:
程式碼:
#include <GL/glut.h>#include <math.h>
#include <stdio.h>
void display()
{
glBegin(GL_POLYGON);
for(float angle=0;angle<=3.1415926*2;angle+=0.01)
{
glVertex2f(cos(angle),sin(angle));
}
glEnd();
glutSwapBuffers();
}
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);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
return EXIT_SUCCESS;
}
Hw4 滑鼠移動改變顏色
程式碼:
#include <GL/glut.h>#include <math.h>
#include <stdio.h>
void display()
{
glBegin(GL_POLYGON);
for(float angle=0;angle<=3.1415926*2;angle+=0.01)
{
glVertex2f(cos(angle),sin(angle));
}
glEnd();
glutSwapBuffers();
}
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);
}
void motion(int x,int y)
{
glColor3f(1,x/300.0,y/300.0);
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotiFunc(motion);
glutMainLoop();
return EXIT_SUCCESS;
}
Hw5 畫小精靈嘴巴開合
程式碼:
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
float mouth=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(0,0);
for(float angle=0+mouth;angle<=3.1415926*2-mouth;angle+=3.1415926/100)
{
glVertex2f(cos(angle),sin(angle));
}
glEnd();
glutSwapBuffers();
}
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);
}
void motion(int x,int y)
{
mouth=x/300.0;
glColor3f(1,x/300.0,y/300.0);
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutMainLoop();
return EXIT_SUCCESS;
}
2017年3月7日 星期二
Week 03 阿威的課堂筆記
Hw1 更改茶壺的顏色
先用glColor3f(r,g,b);
來更改茶壺的顏色
Hw2畫一個三角形
先用glVertex3f(x,y,z);
來話三個頂點的位置
程式碼:
#include <GL/glut.h>
static void display(void)
{
glBegin(GL_POLYGON);
glColor3f(1,0,0);//利用此函式設定為紅色
glVertex3f(0,0,0);//利用此函式將頂點設定為(0,0,0)
glColor3f(0,1,0);//利用此函式設定為綠色
glVertex3f(1,1,0);//利用此函式將頂點設定為(1,1,0)
glColor3f(0,0,1);//利用此函式設定為藍色
glVertex3f(-1,1,0);//利用此函式將頂點設定為(-1,1,0)
glEnd();
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMainLoop();
return EXIT_SUCCESS;
}
2017年2月21日 星期二
Week 01 阿威的課堂筆記
Week 01
WEBGL 課堂截圖
第一個OPENGL截圖
第一個GLUT截圖
經過刪減後的GLUT程式,跑出了實心的茶壺
上面那個茶壺的程式碼:
#include <GL/glut.h>
static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMainLoop();
return EXIT_SUCCESS;
}
訂閱:
文章 (Atom)
















