基于图形之动起来实例

1.旋转的球

#include <GL/gl.h>
#include <GL/glut.h>

GLfloat step = 0.0f;

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glColor3f( 1, 0, 0 );
glPushMatrix();
    glTranslated(0,0,-3);
    glRotated(60,30,-15,0);//倾斜
    glRotated(step,0,1,1);//旋转轴
    glutWireSphere(1,16,16);
glPopMatrix();

glutSwapBuffers();
}

void resize(int width, int height)
{
    float ar = (float) width / (float) height;

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity() ;
    glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
    glMatrixMode(GL_MODELVIEW);
}

void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
    step -= 5.0f;
if(key == GLUT_KEY_DOWN)
    step += 5.0f;

step = (GLfloat)((int)step % 360);
glutPostRedisplay();
}

int main( int argc, char ** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( 400, 400 );
glutCreateWindow( "OpenGL Examples!" );
glutSpecialFunc(SpecialKeys);
glutDisplayFunc( display );
glutReshapeFunc(resize);
glClearColor(1,1,1,1);
glutMainLoop();
return 0;
}

2.旋转的茶壶

#include <GL/gl.h>
#include <GL/glut.h>

static int iDirection = 0;

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

switch (iDirection)
{
case 0:
    gluLookAt(0.0,0.0,5.0,  0,0,0, 0.0,1.0,0.0);
    break;
case 1:
    gluLookAt(0.0,0.0,5.0,  0,0,0, 1.0,0.0,0.0);
    break;
case 2:
    gluLookAt(5.0,0.0,0.0,  0,0,0, 0.0,1.0,0.0);
    break;
case 3:
    gluLookAt(5.0,0.0,0.0,  0,0,0, 0.0,-1.0,0.0);
    break;
default:
    gluLookAt(0.0,0.0,5.0,  0,0,0, 0.0,1.0,0.0);
    break;
}

glColor3f( 0, 1, 0 );
glutWireTeapot(1);

glutSwapBuffers();
}

void resize(int width, int height)
{
float ar = (float) width / (float) height;

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity() ;
gluPerspective(45.0, ar, 1, 100.0);
glMatrixMode(GL_MODELVIEW);
}

void NormalKeys(unsigned char key, int x, int y)
{
switch (key)
{
    case '1':
        iDirection = 0;
        break;
    case '2':
        iDirection = 1;
        break;
    case '3':
        iDirection = 2;
        break;
    case '4':
        iDirection = 3;
        break;
    default:
        break;
}
glutPostRedisplay();
}

int main( int argc, char ** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( 400, 400 );
glutCreateWindow( "OpenGL Examples!" );
glutKeyboardFunc(NormalKeys);
glutDisplayFunc( display );
glutReshapeFunc(resize);
glClearColor(0,0,0,1);
glutMainLoop();
return 0;
}

3.日地系统

#include <GL/gl.h>
#include <GL/glut.h>
GLfloat step = 0.0f;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glColor3f( 1, 0, 0 );//中心球
glTranslated(0,0,-80);
glutSolidSphere(5,32,32);

glColor3f(0,0,1);
glPushMatrix();
glRotated(10,0,0,1);
glRotated(step,0,1,0);
glTranslated(0,0,20);
glutSolidSphere(3,32,32);
glPopMatrix();
glutSwapBuffers();
}

void resize(int width, int height)
{
    float ar = (float) width / (float) height;

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity() ;
    glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
    //gluPerspective(45.0, ar, 2.0, 100.0);
    //glOrtho (-30.0f, 30.0f, -30.0f/ar, 30.0f/ar,2.0f, 100.0f);
    glMatrixMode(GL_MODELVIEW);
}

void NormalKeys(unsigned char key, int x, int y)
{
switch(key)
{
    case '1':
      step += 5.0f;
      break;
    case '2':
      step -= 5.0f;
      break;
    case 'z':
      glEnable(GL_DEPTH_TEST);
      break;
    case 'x':
      glDisable(GL_DEPTH_TEST);
      break;
    default:
      break;
}

step = (GLfloat)((int)step % 360);
glutPostRedisplay();
}
int main( int argc, char ** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( 400, 400 );
glutCreateWindow( "OpenGL Examples!" );
glutKeyboardFunc(NormalKeys);
glutDisplayFunc( display );
glutReshapeFunc(resize);
glClearColor(0,0,0,1);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}

例2

#include <GL/gl.h>
#include <GL/glut.h>
GLfloat step = 0.0f;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glColor3f( 1, 1, 0 );
glTranslated(0,0,-80);
glutSolidSphere(5,32,32);
glColor3f(0,0,1);
glPushMatrix();
glRotated(10,0,0,1);
glRotated(step,0,1,0);
glTranslated(0,0,20);
glutSolidSphere(3,32,32);
glPopMatrix();
glutSwapBuffers();
}

void resize(int width, int height)
{
    float ar = (float) width / (float) height;

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity() ;
    glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
    //gluPerspective(45.0, ar, 2.0, 100.0);
    //glOrtho (-30.0f, 30.0f, -30.0f/ar, 30.0f/ar,2.0f, 100.0f);
    glMatrixMode(GL_MODELVIEW);
}

void animation(void)
{
    step += 1.0f;//翻转速度
    step = (GLfloat)((int)step % 360);//翻转角度
    glutPostRedisplay();
}

/*
void NormalKeys(unsigned char key, int x, int y)
{
switch(key)
{
    case '1':
      step += 5.0f;
      break;
    case '2':
      step -= 5.0f;
      break;
    case 'z':
      glEnable(GL_DEPTH_TEST);
      break;
    case 'x':
      glDisable(GL_DEPTH_TEST);
      break;
    default:
      break;
}

step = (GLfloat)((int)step % 360);
glutPostRedisplay();
}*/
int main( int argc, char ** argv )
{/*
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( 400, 400 );
glutCreateWindow( "OpenGL Examples!" );
//glutKeyboardFunc(NormalKeys);
glutDisplayFunc( display );
glutReshapeFunc(resize);
glClearColor(0,0,0,1);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;

*/
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( 1000, 1000 );//画布大小
glutCreateWindow( "OpenGL Examples!" );
glutDisplayFunc( display );
glutIdleFunc(animation);
glutReshapeFunc(resize);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);//平滑着色

//glShadeModel(GL_SMOOTH);
glutMainLoop();
return 0;

}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值