#include <stdio.h>
float angle=0;
void keyboard(unsigned char key , int x , int y)
{
printf("%c %d %d\n",key,x,y);
if(key=='a') angle+=3;
if(key=='b') angle-=3;
glutPostRedisplay();
//glutSwapBuffers();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();
glRotatef(angle,0,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week10");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
打光
#include <GL/glut.h>
#include <stdio.h>
float angle=0;
void keyboard(unsigned char key , int x , int y)
{
printf("%c %d %d\n",key,x,y);
if(key=='a') angle+=3;
if(key=='b') angle-=3;
glutPostRedisplay();
//glutSwapBuffers();
}
void myLight()
{
const GLfloat light_position[] = {2.0f, 5.0f,0.0f,0.0f};
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();
glRotatef(angle,0,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week10");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
myLight();
glutMainLoop();
}
用滑鼠轉動(x軸)
#include <GL/glut.h>
#include <stdio.h>
float angleX=0, startX=0;
void mouse(int button,int state , int x , int y)
{
printf("%c %d %d\n",button,state,x,y);
startX=x;
glutPostRedisplay();
//glutSwapBuffers();
}
void motion(int x,int y)
{
printf("%d %d\n,x,y");
angleX=x - startX;
glutPostRedisplay();
}
void myLight()
{
const GLfloat light_position[] = {2.0f, 5.0f,0.0f,0.0f};
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();
glRotatef(angleX,0,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week10");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myLight();
glutMainLoop();
}
#include <GL/glut.h>
#include <stdio.h>
float angleX=0, startX=0;
void mouse(int button,int state , int x , int y)
{
printf("%c %d %d\n",button,state,x,y);
startX=x;
glutPostRedisplay();
//glutSwapBuffers();
}
void motion(int x,int y)
{
printf("%d %d\n,x,y");
angleX=x - startX;
glutPostRedisplay();
}
void myLight()
{
const GLfloat light_position[] = {2.0f, 5.0f,0.0f,0.0f};
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glPushMatrix();
glRotatef(angleX,0,1,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week10");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myLight();
glutMainLoop();
}
沒有留言:
張貼留言