📄 ing.c
字号:
#include<gl/glut.h>
#include <math.h>
float u=0;
#pragma comment( linker, "/subsystem:windows /entry:mainCRTStartup" )
float m[16]={10,0,0,0,
-10,0,-10,0,
0,0,10,0,
0,0,0,10};
void init(void)
{
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glClearColor(0.2,0.2,0.7,1.0);
}
void render()
{
glPushMatrix();
glTranslatef(-1.5,3.0,-3.0);
glutSolidSphere(1.0,16,20);
glPopMatrix();
glPushMatrix();
glTranslatef(1.5,3.0,-2.0);
glRotatef(55,1,3,4);
glutSolidCube(2.0);
glPopMatrix();
}
void display(void)
{
float pos[3];
m[4]=14.12*cos(u);
m[6]=14.12*sin(u)-15;
m[7]=0;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0,-3,-5);
pos[0]=-m[4];
pos[1]=10;
pos[2]=-m[6];
glPushMatrix();
glLightfv(GL_LIGHT0,GL_POSITION,pos);
render();
glMultMatrixf(m);
glColor3f(0.7,0.11,0.11);
glDisable(GL_LIGHTING);
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(-0.1,-0.1);
render();
glDisable(GL_POLYGON_OFFSET_FILL);
glEnable(GL_LIGHTING);
glPopMatrix();
glDisable(GL_LIGHTING);
glColor3f(0.9,0.1,0.1);
glPushMatrix();
// glTranslatef(0,-0.01,0);
glBegin(GL_QUADS);
glVertex3f(-20,0,0);
glVertex3f(20,0,0);
glVertex3f(0,0,-20);
glVertex3f(-20,0,-20);
glEnd();
glPopMatrix();
glEnable(GL_LIGHTING);
glutSwapBuffers();
}
void special(int key, int x, int y)
{
switch(key)
{
case GLUT_KEY_LEFT:
u+=0.1;
glutPostRedisplay();
break;
case GLUT_KEY_RIGHT:
u-=0.1;
glutPostRedisplay();
break;
}
}
void reshape(int w,int h)
{
glViewport(0,0,(GLint)w,(GLint)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(80,(GLfloat)w/(GLfloat)h,0.1,200.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
// glutInitDisplayMode(GLUT_RGB);
glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH|GLUT_DOUBLE);
glutInitWindowSize(500,500);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutSpecialFunc(special);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -