(2) 編譯上周 Transformation 程式
(4) 打光 Lighting
GLfloat pos[] = {0.0,0.0,-1.0,0.0};
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, pos);
(5) mouse & keyboard
void keyboard(unsigned char key, int x, int y)
{
printf("now: %c (%d %d)\n", key, x, y);
}
void mouse(int button, int state, int x, int y)
{
}
int main(...)
{.
.
.
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
.
.
.
glutMainLoop();
}
(6) 旋轉 with keyboard
void keyboard(unsigned char key, int x, int y)
{
printf("now: %c (%d %d)\n", key, x, y);
if(key=='1') rotateX ++;
if(key=='2') rotateY ++;
if(key=='3') rotateZ ++;
glutPostRedisplay(); //發出更新畫面要求
}
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();
}
(7) 旋轉 with mouse
int oldX= 0, oldY= 0;
void mouse(int button, int state, int x, int y)
{
//ya
printf("%d %d [%d %d]\n",button, state, x, 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();
}
int main()
{
.
.
.
glutMotionFunc(motion);
.
.
.
}




沒有留言:
張貼留言