今日課程內容
嗚嗚嗚嗚嗚
1.使用老師提供的網站(http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/)
1.先下載win32, data, glut32.dll,source 2.將win32, data解壓縮後將所有資料放入windows資料夾2.開起每周慣例的茶壺
1.按照老方法開啟glut
2.使用上周打光的茶壺
#include <GL/glut.h>
float x=0,y=0;
float angle=0;//開始角度
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);//滑鼠移動
glPushMatrix();
glRotatef(angle,0,1,0);//以角度決定旋轉
glColor3f(0.8,0.6,0);
glutSolidTeapot(0.3);
glPopMatrix();
angle++;//角度++,可調整角度以增加速度
glutSwapBuffers();
}
void motion(int nowx,int nowy)
{
x=nowx; y=nowy;//滑鼠移動
display();
}
int main(int argc,char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week05 translate 06160095");
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);
glutMotionFunc(motion);//執行
glutMainLoop();
}
2.更改程式碼
將lightmaterial.c的檔案用Notepad++開啟並搜尋"glm"來更改程式碼
3.程式碼:
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = 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 (!pmodel) {
pmodel = glmReadOBJ("data/soccerball.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
glmDraw(pmodel, GLM_SMOOTH|GLM_MATERIAL);
glutSwapBuffers();
}
int main(int argc,char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("隱睪勝");
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();
}
4.開啟時會遇到三個問題
1.找不到glm檔
解決辦法:
將"source"壓縮檔裡的"glm.h"檔案丟到跟mian.cpp同位置的資料夾
2.找不到模組
解決辦法:將"source"壓縮檔裡的"glm.c"檔案丟到跟mian.cpp同位置的資料夾並更改名稱為"glm.cpp"
之後點選右方"week06"然後"add files"點選"glm.cpp"
3.視窗閃退
解決辦法:將"data"壓縮檔裡的"data"檔加到"freeglut"的"bin"裡面
終於可以執行了~~~~~
5.如何讓程式碼變彩色
1.將所有程式碼全選貼到notepad++2.存成.cpp檔
3.安裝外掛 ⇒ NppexPort ⇒ Export to HTML ⇒ 用google開啟
4.再將程式碼複製貼上
沒有留言:
張貼留言