- 專案檔裡的[bin][obj]資料夾是什麼?
- [obj]資料夾裡有[main.o],[bin]資料夾裡有[week13.exe]
關係如下: - [main.cpp]先變成⭆[main.o]再變⭆[week13.exe]
- 結論:可以刪除!!再次執行build時,會自動生成這兩個資料夾
- 為什麼檔案(圖檔、音檔)都必須放到[freeglut\bin]?很奇怪!!
- 將模型檔(.obj和.mtl)拉入專案資料夾裡,跟[freeglut.dll]同層
- 改寫程式碼
- #include <GL/glut.h>
- #include "glm.h"
- GLMmodel * model=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 };
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- if(model==NULL){
- model=glmReadOBJ("al.obj");
- glmUnitize(model);///單位向量(+1...-1)
- glmFacetNormals(model);//重算(面)法向量
- glmVertexNormals(model, 90);//重算(頂點vn)法向量
- }
- glPushMatrix();
- glRotatef(180,0,1,0);//轉正
- glmDraw(model,GLM_SMOOTH|GLM_MATERIAL);
- glPopMatrix();
- glutSwapBuffers();
- }
- int main(int argc, char *argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("week13");
- glutDisplayFunc(display);
- ///打光函式
- glEnable(GL_DEPTH_TEST);
- 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();
- }
![]() |
| 成功 |
- 重點1:利用函式,簡化程式碼
- 重點2:匯入新模型(車車),使其隨滑鼠轉動
- 重點3:放到正確的位置
- #include <GL/glut.h>
- #include "glm.h"
- GLMmodel * model=NULL;
- GLMmodel * model2=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 };
- void drawBody()
- {
- if(model==NULL){
- model = glmReadOBJ("Al.obj");
- glmUnitize(model);
- glmFacetNormals(model);
- glmVertexNormals(model, 90);
- glPushMatrix();
- glScalef(0.8, 0.8, 0.8);
- glRotatef(180, 0,1,0);
- glmDraw(model, GLM_SMOOTH | GLM_MATERIAL);
- glPopMatrix();
- }
- void drawArm()
- {
- if(model2==NULL){
- model2 = glmReadOBJ("porsche.obj");
- glmUnitize(model2);
- }
- glPushMatrix();
- glScalef(0.2, 0.2, 0.2);
- glRotatef(90, 0,1,0);
- glmDraw(model2, GLM_SMOOTH | GLM_MATERIAL);
- glPopMatrix();
- }
- float angle=0;
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- drawBody();
- glPushMatrix();
- glTranslatef(0.3, 0.3, 0);///(3)等一下要掛在哪裡
- glRotatef(angle, 0,0,1);/// (1) 有個轉動
- glTranslatef(0.2, 0, 0);/// (2) 移動轉動的中心
- drawArm();
- glPopMatrix();
- glutSwapBuffers();
- }
- void motion(int x, int y)///隨滑鼠移動
- {
- angle = x;
- glutPostRedisplay();
- }
- int main(int argc, char *argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("Week13");
- glutDisplayFunc(display);
- glutMotionFunc(motion);
- 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();
- }

- 更多更多的車車
- 在display()裡加入程式碼
- glPushMatrix();/// 右 手臂
- glTranslatef(0.3, 0.3, 0);
- glRotatef(angle, 0,0,1);
- glTranslatef(0.2, 0, 0);
- drawArm();///右上 手臂
- glPushMatrix();
- glTranslatef(0.2,0,0);
- glRotatef(angle, 0,0,1);
- glTranslatef(0.2, 0, 0);
- drawArm();///右下手臂
- glPopMatrix();
- glPopMatrix();
- glPushMatrix();/// 左 手臂
- glTranslatef(-0.3, 0.3, 0);
- glRotatef(angle, 0,0,1);
- glTranslatef(-0.2, 0, 0);
- drawArm();///左上 手臂
- glPushMatrix();
- glTranslatef(-0.2,0,0);
- glRotatef(angle, 0,0,1);
- glTranslatef(-0.2, 0, 0);
- drawArm();/// 左下手臂
- glPopMatrix();
- glPopMatrix();

- 可以指定關節轉動!!
- #include <GL/glut.h>
- #include "glm.h"
- GLMmodel * model=NULL;
- GLMmodel * model2=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 };
- void drawBody()
- {
- if(model==NULL){
- model = glmReadOBJ("Al.obj");
- glmUnitize(model);.
- glmFacetNormals(model);
- glmVertexNormals(model, 90);
- }
- glPushMatrix();
- glScalef(0.8, 0.8, 0.8);
- glRotatef(180, 0,1,0);
- glmDraw(model, GLM_SMOOTH | GLM_MATERIAL);
- glPopMatrix();
- }
- void drawArm()
- {
- if(model2==NULL){
- model2 = glmReadOBJ("porsche.obj");
- glmUnitize(model2);
- }
- glPushMatrix();
- glScalef(0.2, 0.2, 0.2);
- glRotatef(90, 0,1,0);
- glmDraw(model2, GLM_SMOOTH | GLM_MATERIAL);
- glPopMatrix();
- }
- float angle[4]={0,0,0,0};///變成很多個陣列!!!
- int angleID=0;///現在在動的,是哪一個關節呢?
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- drawBody();
- glPushMatrix();/// 右 手臂
- glTranslatef(0.3, 0.3, 0);
- glRotatef(angle[0], 0,0,1);
- glTranslatef(0.2, 0, 0);
- drawArm();///右上 手臂
- glPushMatrix();
- glTranslatef(0.2,0,0);
- glRotatef(angle[1], 0,0,1);
- glTranslatef(0.2, 0, 0);
- drawArm();///右下手臂
- glPopMatrix();
- glPopMatrix();
- glPushMatrix();/// 左 手臂
- glTranslatef(-0.3, 0.3, 0);
- glRotatef(angle[2], 0,0,1);
- glTranslatef(-0.2, 0, 0);
- drawArm();///左上 手臂
- glPushMatrix();
- glTranslatef(-0.2,0,0);
- glRotatef(angle[3], 0,0,1);
- glTranslatef(-0.2, 0, 0);
- drawArm();/// 左下手臂
- glPopMatrix();
- glPopMatrix();
- glutSwapBuffers();
- }
- int oldX, oldY;
- 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();
- }
- void keyboard(unsigned char key, int x, int y)///按鍵盤數字控制關節
- {
- if(key=='0') angleID=0;
- if(key=='1') angleID=1;
- if(key=='2') angleID=2;
- if(key=='3') angleID=3;
- }
- int main(int argc, char *argv[])
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("Week13");
- glutDisplayFunc(display);
- glutMotionFunc(motion);
- glutMouseFunc(mouse);
- glutKeyboardFunc(keyboard);
- 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();
- }
![]() |
| [0]右上手臂[1]右下手臂[2]左上手臂[3]左下手臂 |








沒有留言:
張貼留言