期末作品
作品介紹:
人物走著走著,不小心跌倒死掉了。
卻不知道為啥,復活後,獲得超能力。
能夠飛翔,最後飛出螢幕外。
影片連結:https://www.youtube.com/watch?v=ozIi_AN1Sm8
2019電腦圖學 Computer Graphics 授課教師: 葉正聖 銘傳大學資訊傳播工程系 每週主題: 程式環境、點線面顏色、移動/旋轉/縮放與矩陣(Matrix)、階層性關節轉動(T-R-T)、做出機器人、打光、貼圖、glu/glut函式、鍵盤、滑鼠、計時器(timer)、讀入3D模型、粒子系統、聲音、特效、投影矩陣、攝影機與運鏡、機器人2.0、期末作品
2019年6月29日 星期六
2019年5月31日 星期五
Lzhen_hao - 記事
今日主題 : 攝影機x運鏡 & 投影x矩陣
按照慣例,下載 [ data ]、[ glut32.dll ]、[ win32 ],並開啟 Projection.exe。 ( 如下圖 )
( Frustum ↑ )
( Ortho ↑ )
※ Frustum 跟 Ortho 的視野交匯不一樣, Frustum的視野會交匯,Ortho的視野會保持平行。
※ eye 調攝影機的位置、center 調攝影機拍的焦點中心 ( 固定的那個點 )、up 可以旋轉攝影機 ( 可以把攝影機轉 360 度 之類的 )
--------------------------------------------------------------------------------------------------------------------------
程式時間
將 GLUT 專案打開,然後要把 glut.h、glut.cpp、projection.c 放入專案資料夾 ( glut.cpp 要加入CodeBlock專案 )。
將 projection.c 以 notepad++打開,然後將程式碼貼到main.cpp上,執行後會發現缺少 data資料夾,再把 data資料夾放到freeglut資料夾的bin資料夾中。
成功!!!
--------------------------------------------------------------------------------------------------------------------------
從 projection.c 裡面 挑出畫模型的程式,加到自己的程式碼當中。
能夠畫出模型的雛形 ( 白色 | 因為還沒打光 )
加上打光之後
※ GLfloat light_pos[] = { 0.0, 0.0, -1.0, 0.0 }; 可以控制打光的位置
2019年5月24日 星期五
Lzhen_hao - 記事
本日課程 : 計時器、內差公式、動畫
今天的程式碼 ↓
#include <GL/glut.h>
#include <stdio.h>
float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///NOW4
glRotatef(angle, 0, 0, 1);///NOW4
glutSolidTeapot( 0.3 );///NOW4
glPopMatrix();///NOW4
glutSwapBuffers();
}
void timer(int t)///NOW (2)
{
glutTimerFunc(30, timer, t+1);///NOW4 ///每隔3/100秒跑一次t+1
printf("現在的 timer(t)的t是: %d\n", t);///NOW3
angle = t;///NOW4
glutPostRedisplay();///NOW4 要更新畫面
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);///大一點
glutCreateWindow("06160795");
glutDisplayFunc(display);
glutTimerFunc(1000, timer, 0);///NOW (1)///間隔一秒後呼叫timer函式
glutMainLoop();
}
P.S. 在主程式呼叫完Timer後, timer函式裡會開始持續呼叫 timer跑 t+1 讓圖轉起來~~!!
--------------------------------------------------------------------------------------------------------------------------
圖是會動的 ↑
新增這行程式碼 ↓
float alpha = (t%30) / 30.0;///把時間變成距離的單位
nowX = alpha*1.0 + (1-alpha)* -1.0;///利用內差公式計算位移距離
--------------------------------------------------------------------------------------------------------------------------
最後的程式碼
#include <stdio.h>///NOW1 for fprintf()
FILE * fout=NULL;///NOW1 for fopen() + fprintf()
FILE * fin =NULL;///NOW2 for fopen() + fscanf()
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel21 = NULL;
GLMmodel* pmodel3 = NULL;
GLM model* pmodel31 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel41 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel51 = NULL;
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 };
float angle[20];///NOW1
int angleID=1;
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
///身體
glPushMatrix();
glTranslatef(0, 0.3,0 );
glScalef(0.5,0.5,0.5);
if (!pmodel) {
pmodel = glmReadOBJ("data/06160451.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
///右上臂
glPushMatrix();
glTranslatef(0.45,0.3,0 );
glScalef(0.5,0.5,0.5);
glRotatef(angle[1], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPushMatrix();
glTranslatef(0,-1.1,0 );
glScalef(1,1,1);
glRotatef(angle[2], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.45,0.3,0 );
glScalef(0.5,0.5,0.5);
glRotatef(angle[3], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPushMatrix();
glTranslatef(0,-1.1,0 );
glScalef(1,1,1);
glRotatef(angle[4], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0.2,-1.05,0 );
glScalef(0.5,0.5,0.5);
glRotatef(angle[5], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPushMatrix();
glTranslatef(0,-1.2,0 );
glScalef(1,1,1);
glRotatef(angle[6], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2,-1.05,0 );
glScalef(0.5,0.5,0.5);
glRotatef(angle[7], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPushMatrix();
glTranslatef(0,-1.2,0 );
glScalef(1,1,1);
glRotatef(angle[8], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int oldX=0;
void mouse(int button, int state, int x, int y)///NOW2
{
oldX = x;
}
#include <stdio.h>
void motion(int x, int y)
{
angle[angleID] += x-oldX;
oldX = x;
for(int i=0;i<20;i++){
printf(" %.2f ", angle[i]);
}
printf("\n");
display();
}
void timer(int t)///NOW3
{
glutTimerFunc(1000, timer, t+1);///NOW3
if(fin==NULL) fin=fopen("motion.txt", "r");///NOW3
for(int i=0; i<20; i++){///NOW3
fscanf(fin, "%f", &angle[i]);///NOW3
}
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleID=1;
if(key=='2') angleID=2;
if(key=='3') angleID=3;
if(key=='4') angleID=4;
if(key=='5') angleID=5;
if(key=='6') angleID=6;
if(key=='7') angleID=7;
if(key=='8') angleID=8;
if(key=='w'){///NOW1
if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW1
for(int i=0; i<20; i++){///NOW1
fprintf(fout, "%.1f ", angle[i]);///NOW1
}///NOW1
fprintf(fout, "\n");///NOW1
}
if(key=='r'){///NOW2
if(fin==NULL) fin=fopen("motion.txt", "r");///NOW2
for(int i=0; i<20; i++){///NOW2
fscanf(fin, "%f", &angle[i]);///NOW2
}
}
if(key=='t' || key=='p'){///NOW3 ///啟動timer 或是 play
glutTimerFunc(1000, timer, 0);///NOW3
if(fin==NULL) fin=fopen("motion.txt", "r");///NOW3
for(int i=0; i<20; i++){///NOW3
fscanf(fin, "%f", &angle[i]);///NOW3
}
}
glutPostRedisplay();///NOW2 請讀到angle[i]之後, 重畫畫面
}
int main(int argc,char**argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week06");
glutDisplayFunc(display);
glutIdleFunc(display);
glClearColor(1,1,1,1);
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);
glutKeyboardFunc(keyboard);///NOW3
glutMouseFunc(mouse);///NOW2
glutMotionFunc(motion);///NOW2
glutDisplayFunc(display);
glutMainLoop();
}
--------------------------------------------------------------------------------------------------------------------------
最後的程式碼
#include <stdio.h>///NOW1 for fprintf()
FILE * fout=NULL;///NOW1 for fopen() + fprintf()
FILE * fin =NULL;///NOW2 for fopen() + fscanf()
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLMmodel* pmodel2 = NULL;
GLMmodel* pmodel21 = NULL;
GLMmodel* pmodel3 = NULL;
GLM model* pmodel31 = NULL;
GLMmodel* pmodel4 = NULL;
GLMmodel* pmodel41 = NULL;
GLMmodel* pmodel5 = NULL;
GLMmodel* pmodel51 = NULL;
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 };
float angle[20];///NOW1
int angleID=1;
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
///身體
glPushMatrix();
glTranslatef(0, 0.3,0 );
glScalef(0.5,0.5,0.5);
if (!pmodel) {
pmodel = glmReadOBJ("data/06160451.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
///右上臂
glPushMatrix();
glTranslatef(0.45,0.3,0 );
glScalef(0.5,0.5,0.5);
glRotatef(angle[1], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPushMatrix();
glTranslatef(0,-1.1,0 );
glScalef(1,1,1);
glRotatef(angle[2], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.45,0.3,0 );
glScalef(0.5,0.5,0.5);
glRotatef(angle[3], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPushMatrix();
glTranslatef(0,-1.1,0 );
glScalef(1,1,1);
glRotatef(angle[4], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0.2,-1.05,0 );
glScalef(0.5,0.5,0.5);
glRotatef(angle[5], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPushMatrix();
glTranslatef(0,-1.2,0 );
glScalef(1,1,1);
glRotatef(angle[6], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2,-1.05,0 );
glScalef(0.5,0.5,0.5);
glRotatef(angle[7], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPushMatrix();
glTranslatef(0,-1.2,0 );
glScalef(1,1,1);
glRotatef(angle[8], 0,0,1);
glTranslatef(0,-0.7, 0);
if (!pmodel2) {
pmodel2 = glmReadOBJ("data/123.obj");
if (!pmodel2) exit(0);
glmUnitize(pmodel2);
glmFacetNormals(pmodel2);
glmVertexNormals(pmodel2, 90.0);
}
glmDraw(pmodel2, GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int oldX=0;
void mouse(int button, int state, int x, int y)///NOW2
{
oldX = x;
}
#include <stdio.h>
void motion(int x, int y)
{
angle[angleID] += x-oldX;
oldX = x;
for(int i=0;i<20;i++){
printf(" %.2f ", angle[i]);
}
printf("\n");
display();
}
void timer(int t)///NOW3
{
glutTimerFunc(1000, timer, t+1);///NOW3
if(fin==NULL) fin=fopen("motion.txt", "r");///NOW3
for(int i=0; i<20; i++){///NOW3
fscanf(fin, "%f", &angle[i]);///NOW3
}
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') angleID=1;
if(key=='2') angleID=2;
if(key=='3') angleID=3;
if(key=='4') angleID=4;
if(key=='5') angleID=5;
if(key=='6') angleID=6;
if(key=='7') angleID=7;
if(key=='8') angleID=8;
if(key=='w'){///NOW1
if(fout==NULL) fout=fopen("motion.txt", "w+");///NOW1
for(int i=0; i<20; i++){///NOW1
fprintf(fout, "%.1f ", angle[i]);///NOW1
}///NOW1
fprintf(fout, "\n");///NOW1
}
if(key=='r'){///NOW2
if(fin==NULL) fin=fopen("motion.txt", "r");///NOW2
for(int i=0; i<20; i++){///NOW2
fscanf(fin, "%f", &angle[i]);///NOW2
}
}
if(key=='t' || key=='p'){///NOW3 ///啟動timer 或是 play
glutTimerFunc(1000, timer, 0);///NOW3
if(fin==NULL) fin=fopen("motion.txt", "r");///NOW3
for(int i=0; i<20; i++){///NOW3
fscanf(fin, "%f", &angle[i]);///NOW3
}
}
glutPostRedisplay();///NOW2 請讀到angle[i]之後, 重畫畫面
}
int main(int argc,char**argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("Week06");
glutDisplayFunc(display);
glutIdleFunc(display);
glClearColor(1,1,1,1);
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);
glutKeyboardFunc(keyboard);///NOW3
glutMouseFunc(mouse);///NOW2
glutMotionFunc(motion);///NOW2
glutDisplayFunc(display);
glutMainLoop();
}
2019年5月17日 星期五
Lzhen_hao - 記事
用Code開檔案、並在檔案中輸入字
程式碼如下 ↓
FILE * fout = NULL; ///宣告檔案指標、變數
int main()
{
fout = fopen("123.txt","w+");/// 開啟檔案,要寫字
for(int i=0; i<20; i++)
{
fprintf( fout , "Hello World\n"); ///在檔案上要輸入的字
}
}

開始做關節轉動,用上周的程式碼做為基底進行修改。
P.S. glutWindowSize 可以設定視窗大小

加上手肘跟左手

加上前面的在檔案中輸入字,把各個關節的旋轉角度,寫進檔案中。

然後利用以下程式碼
if(fout==NULL) fout=fopen("123.txt", "w+");///NOW4
for(int i=0; i<20; i++){///NOW4
printf( "%.1f ", angle[i]);///NOW4
fprintf( fout, "%.1f ", angle[i]);///NOW4
可以按住R 讀入剛剛檔案內的數據,讓圖像成功照著程式動作。
以下為完整程式碼
#include <stdio.h>
#include <GL/glut.h>
FILE * fout=NULL;///NOW4
FILE * fin =NULL;///NOW5
float angle[20];
int angleID=1;///第幾個要轉動的關節 ///NOW3
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///body
glColor3f(1,1,1);///white
glutSolidTeapot( 0.3 );///body
glPushMatrix();///right Arm
glTranslatef(0.18, 0, 0);///(3)掛到肩上
glRotatef(angle[1], 0,0,1);///(2)轉動 ///NOW
glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心
glColor3f(1,0,0);///red
glutSolidTeapot( 0.2 );///right upper arm
glPushMatrix();///NOW2 長出下手臂
glTranslatef(0.18, 0, 0);///(3)掛到肘上
glRotatef(angle[2], 0,0,1);///(2)轉動 ///NOW
glTranslatef(0.18, 0, 0);///(1)改變旋轉中心,放到中心
glColor3f(1,0,0);///red
glutSolidTeapot( 0.2 );///right lower arm
glPopMatrix();
glPopMatrix();
glPushMatrix();///left Arm
glTranslatef(-0.18, 0, 0);///(3)掛到肩上
glRotatef(angle[3], 0,0,1);///(2)轉動 ///NOW
glTranslatef(-0.18, 0, 0);///(1)改變旋轉中心,放到中心
glColor3f(1,0,0);///red
glutSolidTeapot( 0.2 );///left upper arm
glPushMatrix();///NOW2 長出下手臂
glTranslatef(-0.18, 0, 0);///(3)掛到肘上
glRotatef(angle[4], 0,0,1);///(2)轉動 ///NOW
glTranslatef(-0.18, 0, 0);///(1)改變旋轉中心,放到中心
glColor3f(1,0,0);///red
glutSolidTeapot( 0.2 );///left lower arm
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int oldX;///NOW
void mouse(int button, int state, int x, int y)///NOW
{
oldX = x;///NOW
}
void motion(int x, int y)///NOW
{
angle[angleID] += (x-oldX);///NOW ///NOW3
oldX = x;///NOW
if(fout==NULL) fout=fopen("123.txt", "w+");///NOW4
for(int i=0; i<20; i++){///NOW4
printf( "%.1f ", angle[i]);///NOW4
fprintf( fout, "%.1f ", angle[i]);///NOW4
}
printf("\n");///NOW4
fprintf(fout, "\n");///NOW4
display();///NOW
}
void keyboard(unsigned char key, int x, int y)///NOW3
{
if(key=='1') angleID=1;///NOW3
if(key=='2') angleID=2;///NOW3
if(key=='3') angleID=3;///NOW3
if(key=='4') angleID=4;///NOW3
if(key=='w' || key=='W'){///NOW4 小寫的 w
if(fout==NULL) fout=fopen("123.txt", "w+");///NOW4
for(int i=0; i<20; i++){///NOW4
printf( "%.1f ", angle[i]);///NOW4
fprintf( fout, "%.1f ", angle[i]);///NOW4
}
printf("\n");///NOW4
fprintf(fout, "\n");///NOW4
}
if(key=='r'){///NOW5
if(fin==NULL) fin = fopen("123.txt", "r");///NOW5
for(int i=0; i<20;i++){///NOW5
fscanf(fin, "%f", &angle[i]);///NOW5
}///NOW5
}
glutPostRedisplay();///和 display()很像,但更好 ///NOW5
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
///glutInitWindowSize(600,600);///我們可以開很大的視窗
glutCreateWindow("week 13 motion TRT angle file");
glutKeyboardFunc(keyboard);///NOW3
glutMouseFunc(mouse);///NOW
glutMotionFunc(motion);///NOW
glutDisplayFunc(display);
glutMainLoop();
}
2019年5月10日 星期五
Lzhen_hao - 記事
用程式播音樂!!
首先先開啟Console application
確認開起專案是否正確、無問題
將程式碼更改為下圖
程式碼如下 ↓
#include <mmsystem.h> ← MultiMedia system可以播聲音
int main()
{
PlaySoundA("C:\\Users\\user\\Desktop\\Wate.wav",NULL,SND_SYNC);///檔名用英文ASCII檔名(wav檔)、空值、聲音等待同步
return 0;
}
P.S. 反斜線必須打兩個!!!!!
最後必須到專案 → Build options → Linker settings → Add ( winmm ) 完成後即可執行程式

--------------------------------------------------------------------------------------------------------------------------
下個步驟,用glut專案打開,並加上聲音程式碼 ( SND_ASYNC是讓程式跟聲音同時出來 )

--------------------------------------------------------------------------------------------------------------------------
下個步驟,用出可以用鍵盤敲鋼琴的程式。


--------------------------------------------------------------------------------------------------------------------------
下個步驟,讀入MP3,播放MP3


2019年5月3日 星期五
Lzhen_hao - 記事
今天在教幫茶壺畫手臂,旋轉
畫完右邊的,試著把左邊的也畫出來
把右邊的複製下來,貼成第二段
然後將x座標改成負的
今天的程式碼 ↓
--------------------------------------------------------------------------------------------------------------------------
#include <GL/glut.h>float angle=0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(255,255,255);
glutSolidTeapot( 0.3 );///身體
glPushMatrix();///開始畫右邊手臂
glTranslatef(0.2, 0, 0);///右肩
glRotatef(angle, 0,0,1);
glTranslatef(0.15, 0,0);
glColor3f(1,0,0);///紅色
glutSolidTeapot( 0.15 );///右手臂
glPushMatrix();///畫右邊手軸
glTranslatef(0.2, 0, 0);///右手肘
glRotatef(angle, 0,0,1);
glTranslatef(0.15, 0,0);
glColor3f(1,0,0);///紅色
glutSolidTeapot( 0.15 );
glPopMatrix();
glPopMatrix();///要來畫左邊
glPushMatrix();///開始畫左邊手臂
glTranslatef(-0.2, 0, 0);///左肩
glRotatef(angle, 0,0,1);
glTranslatef(-0.15, 0,0);
glColor3f(1,0,0);///紅色
glutSolidTeapot( 0.15 );///左手臂
glPushMatrix();///畫左邊手軸
glTranslatef(-0.2, 0, 0);///左手肘
glRotatef(angle, 0,0,1);
glTranslatef(-0.15, 0,0);
glColor3f(1,0,0);///紅色
glutSolidTeapot( 0.15 );
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void motion(int x, int y)///讓手臂隨著滑鼠動
{
angle=x;
display();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("06160795");
glutIdleFunc(display);
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
--------------------------------------------------------------------------------------------------------------------------
可是這樣手臂的轉動會很奇怪,所以對手臂轉動的程式碼進行修改
--------------------------------------------------------------------------------------------------------------------------

void motion(int x, int y)
{
angle += (x- oldX);/// 算每次新的跟舊的相減的移動幅度
oldX=x;
display();
}
void mouse (int botton, int state, int x, int y)
{
oldX=x; oldY=y;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("06160795");
glutIdleFunc(display);
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutMainLoop();
}
--------------------------------------------------------------------------------------------------------------------------
可是這樣手臂的轉動會很奇怪,所以對手臂轉動的程式碼進行修改
--------------------------------------------------------------------------------------------------------------------------

void motion(int x, int y)
{
angle += (x- oldX);/// 算每次新的跟舊的相減的移動幅度
oldX=x;
display();
}
void mouse (int botton, int state, int x, int y)
{
oldX=x; oldY=y;
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("06160795");
glutIdleFunc(display);
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutMainLoop();
}
--------------------------------------------------------------------------------------------------------------------------
利用鍵盤讓每個關節可以獨自動
--------------------------------------------------------------------------------------------------------------------------
#include <GL/glut.h>
float angle1=0, angle2=0, angle3=0, angle4=0;/// 配合鍵盤
int jointID=1;///配合鍵盤
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(255,255,255);
glutSolidTeapot( 0.3 );///身體
glPushMatrix();///開始畫右邊手臂
glTranslatef(0.2, 0, 0);///右肩
glRotatef(angle1, 0,0,1);
glTranslatef(0.15, 0,0);
glColor3f(1,0,0);///紅色
glutSolidTeapot( 0.15 );///右手臂
glPushMatrix();///畫右邊手軸
glTranslatef(0.2, 0, 0);///右手肘
glRotatef(angle2, 0,0,1);
glTranslatef(0.15, 0,0);
glColor3f(1,0,0);///紅色
glutSolidTeapot( 0.15 );
glPopMatrix();
glPopMatrix();///要來畫左邊
glPushMatrix();///開始畫左邊手臂
glTranslatef(-0.2, 0, 0);///左肩
glRotatef(-angle3, 0,0,1);
glTranslatef(-0.15, 0,0);
glColor3f(1,0,0);///紅色
glutSolidTeapot( 0.15 );///左手臂
glPushMatrix();///畫左邊手軸
glTranslatef(-0.2, 0, 0);///左手肘
glRotatef(-angle4, 0,0,1);
glTranslatef(-0.15, 0,0);
glColor3f(1,0,0);///紅色
glutSolidTeapot( 0.15 );
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
int oldX,oldY;
void motion(int x, int y)
{
if(jointID==1) angle1 += (x-oldX);///NOW3 用滑鼠mouse來控制angle ///NOW6 配合鍵盤
if(jointID==2) angle2 += (x-oldX);///NOW3 用滑鼠mouse來控制angle ///NOW6 配合鍵盤
if(jointID==3) angle3 += (x-oldX);///NOW3 用滑鼠mouse來控制angle ///NOW6 配合鍵盤
if(jointID==4) angle4 += (x-oldX);///NOW3 用滑鼠mouse來控制angle ///NOW6 配合鍵盤
oldX=x;
display();
}
void mouse (int botton, int state, int x, int y)
{
oldX=x; oldY=y;
}
void keyboard(unsigned char key, int x, int y)///NOW6 配合鍵盤
{
if(key=='1') jointID=1;///NOW6 配合鍵盤
if(key=='2') jointID=2;///NOW6 配合鍵盤
if(key=='3') jointID=3;///NOW6 配合鍵盤
if(key=='4') jointID=4;///NOW6 配合鍵盤
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("06160795");
glutIdleFunc(display);
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);///配合鍵盤
glutMainLoop();
}
2019年4月26日 星期五
Li_zhen-hao 記事
如果把Rotate、Translate順序調換會怎樣??
Ans :
自轉與公轉。
自轉與公轉。
R在前是公轉、T在前是自轉。
廣義是會變成由第一行包覆第二行,一層一層包住最裡面的東西。
2019年4月12日 星期五
Lzhen_hao - 記事
考試的十條程式碼!!!
glPushMatrix(); 備份矩陣 10%
glTranslatef(x,y,z); 移動 10%
glRotatef(angle , x,y,z); 旋轉 10%
glScalef(x,y,z); 放大縮小 10%
glBegin(GL_POLYGON); 開始畫 10%
glColor3f(r,g,b); 色彩 10%
glNormal3f(nx,ny,nz); 打光的法向量 10%
glTexCoord2f(tx,ty); 貼圖 10%
glVertex3f(x,y,z); 頂點 10%
glEnd(); 結束畫
glPopMatrix(); 還原矩陣 10%
貼圖
到 ( http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/ ) 下載 [ data ]、[ win32 ]、[ glu32.dll ]
建立windows資料夾,將win32解壓縮、glu32.dll、data資料夾丟入
執行Texture.exe
前置動作
搜尋OpenCV 2.1 ( https://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.1/ )
下載圖中檔案 ↓

安裝過程中,此步驟選擇 第二選項,且安裝在預設資料夾

可在C槽/OpenCV 2.1 / bin 中 看到OpenCV需要的3個檔案

完成後開啟CodeBlocks 建立貝殼專案

程式碼打完會有問題
一、




二、


三、

四、
最後要把3個dll檔和照片檔丟到專案資料夾

更正錯誤!!! 是C:\OpneCV2.1\lib 不是bin & 是cxcore210不是cvaux210
開一個GLUT的專案,把茶壺程式碼打上去
然後對GLUT專案重複貝殼專案 1~3步驟

改用GLUT的專案的時候,前面的第四步驟要改成丟到freeglut/bin裡面!!!
2019年3月29日 星期五
Lzhen_hao - 記事
#include <GL/glut.h>
#include <stdio.h>
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 };
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main(int argc , char ** argv)
{
glutInit(&argc , argv);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("06160795");
glutDisplayFunc(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();
}
螢光的程式碼是用來打光
#include <stdio.h>
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 };
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main(int argc , char ** argv)
{
glutInit(&argc , argv);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow("06160795");
glutDisplayFunc(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();
}
螢光的程式碼是用來打光
P.S. NOTE
Include → .h 檔
link → lib 檔
執行 bin → dll 檔
開始打"讀入模型"的程式碼
打完程式碼 發現執行缺了 glm.h 把glm.h放到專案資料夾
在專案加入 glm.cpp 檔
Step.1 把 glm.cpp放到專案的資料夾
Step.2 從專案資料夾新增 glm.cpp
接下來把data.zip裡面的data資料夾丟入freeglut資料夾的 bin 裡面
訂閱:
文章 (Atom)


























