#include <GL/glut.h>
#include<stdio.h>
#include "glm.h"
GLMmodel* pmodel = NULL;
int rotateX=0,rotateY=0,rotateZ=0;///旋轉軸
int oldX=0,oldY=0;///舊的座標
void keyboard(unsigned char key,int x,int y){
printf("now : %c , (%d ,%d)\n",key,x,y);
if(key=='1')rotateX++;///控制旋轉的方向
else if(key=='2')rotateY++;
else if(key=='3')rotateZ++;
glutPostRedisplay();///立即更新畫面
}
void motion(int x,int y){
rotateY+=-(x-oldX);///Y軸方向旋轉
rotateX+=-(y-oldY);///X軸方向旋轉
oldX=x;oldY=y;///更新座標
glutPostRedisplay();
}
void mouse(int button,int state,int x,int y){
printf("button: %d state: %d (%d %d)\n",button,state,x,y);
if(state==GLUT_DOWN){///點下滑鼠
oldX=x,oldY=y;
}
}
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();
}
GLfloat pos[] = { 0.0, 0.0, -1.0, 0.0 };
int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week06");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);///註冊mouse
glutMotionFunc(motion);///註冊motion
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}