記得把compiler設定一下(C++11取消勾選)
去 jsyeh.org/3dcg10/ 下載 data win32 glut32.dll 並且解壓縮(跟上週一樣)
↑記得要這樣丟檔案才能正常執行歐歐歐
順便把source也載下來 才有code!!
*本週重點*
鍵盤&滑鼠旋轉
#include <GL/glut.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
GLfloat pos[] = { 0.0, 0.0, -1.0, 0.0 }; ///打光用的位置
int rotateX=0, rotateY=0, rotateZ=0;
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') rotateX+=5;
if(key=='2') rotateY+=5;
if(key=='3') rotateZ+=5; ///按鍵盤上面123可以旋轉
glutPostRedisplay(); ///貼標籤, 請電腦有空時把畫面重新Re-display
}
float oldX=0,oldY=0,oldZ=0;
void mouse(int button, int state, int x, int y)
{
if(state==GLUT_DOWN)
{
oldX=x;
oldY=y;
}
}
void motion(int x, int y)
{
rotateY += -(x-oldX);
rotateX += -(y-oldY);
oldX=x;
oldY=y;
glutPostRedisplay();
}
void
drawmodel(void)
{
if (!pmodel) {
pmodel = glmReadOBJ("data/al.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(rotateX,1,0,0);
glRotatef(rotateY,0,1,0);
glRotatef(rotateZ,0,0,1);
drawmodel();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH); ///3D深度測試 清depth buffer才可以正確繪圖
glutCreateWindow("week05");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMotionFunc(motion); ///讓他動
glEnable(GL_DEPTH_TEST); ///把3D深度測試打開
glEnable(GL_LIGHT0); ///打開偷來打光的code
glEnable(GL_LIGHTING); ///也是打開偷來打光的code
glLightfv(GL_LIGHT0, GL_POSITION, pos); ///打光的位置
glutMainLoop();
}
沒有留言:
張貼留言