更新时间:2023-11-09 22:38:22
gluPerspective()
(或glFrustum()
)设置透视投影矩阵gluLookAt()
将相机"从原点上侦走,这样您就可以看到自己绘制的内容GLUT_SINGLE
在现代的复合窗口管理器上有时很奇怪gluPerspective()
(or glFrustum()
) to set up a perspective projection matrixgluLookAt()
to scoot the "camera" away from the origin so you can see what you drawGLUT_SINGLE
is sometimes weird on modern composited window managers一起:
#include <GL/glut.h>
double rnd( double lo, double hi )
{
return lo + ( hi - lo ) * ( rand() / static_cast<double>( RAND_MAX ) );
}
double angle = 0.0;
void timer( int value )
{
angle += 1.0;
glutTimerFunc( 16, timer, 0 );
glutPostRedisplay();
}
void display()
{
glClearColor( 0, 0, 0, 0 );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
gluPerspective( 60.0, w / h, 0.1, 1000.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt( 100, 100, 100, 0, 0, 0, 0, 0, 1 );
glRotated( angle, 0, 0, 1 );
srand( 0 );
glBegin( GL_LINES );
for( size_t i = 0; i < 100; i++ )
{
glColor3d( rnd( 0.0, 1.0 ), rnd( 0.0, 1.0 ), rnd( 0.0, 1.0 ) );
glVertex3d( rnd( -50, 50 ), rnd( -50, 50 ), rnd( -50, 50 ) );
glVertex3d( rnd( -50, 50 ), rnd( -50, 50 ), rnd( -50, 50 ) );
}
glEnd();
glutSwapBuffers();
}
int main( int argc, char *argv[] )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "Special key" );
glutDisplayFunc( display );
glutTimerFunc( 0, timer, 0 );
glutMainLoop();
}