[NEHE Couse] 02.My first polygon

     这节比较简单但却很重要,因为几乎所有复杂的物体都是有基本的图形面片组成的(这点会随着你对图形学的深入而变的明显起来),我在教程原来的基础上,画了一个三角面片,长方体面片和一条直线,都是OpenGL里简单却基础的知识,注意下glPushMatrix()和glPopMatrix()的使用。
代码如下:

  1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /*
  2Introduction:Written by krisdy,finished at 2009.1.9
  3Apply to:NEHE Couse 02:My First Polygon
  4*/

  5 #include  < gl / glut.h >
  6 #include  < stdlib.h >
  7
  8 #define  WinWidth 500         // the width of the window
  9 #define  WinHeight 500         // the height of the window
 10
 11 static  GLint t;
 12
 13 void  init( void )
 14 ExpandedBlockStart.gifContractedBlock.gif {
 15    glShadeModel(GL_SMOOTH);
 16    glClearColor(0.0f,0.0f,0.0f,0.0f);
 17    glClearDepth(1.0f);
 18    glEnable(GL_DEPTH_TEST);
 19    glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
 20}

 21 void  display( void )
 22 ExpandedBlockStart.gifContractedBlock.gif {
 23    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
 24    glColor3f(1.0f,0.0f,0.0f);
 25    glMatrixMode(GL_MODELVIEW);
 26    glLoadIdentity();
 27    glPushMatrix();
 28    glTranslatef(1.0f,1.0f,0.0f);
 29    //draw a triangle
 30    glBegin(GL_TRIANGLES);
 31        glVertex3f(0.0f,0.0f,0.0f);
 32        glVertex3f(3.0f,0.0f,0.0f);
 33        glVertex3f(1.5f,4.0f,0.0f);
 34    glEnd();
 35    //draw a line
 36    glLineWidth(3.0);
 37    glBegin(GL_LINES);
 38        glVertex3f(1.5f,7.0f,0.0f);
 39        glVertex3f(8.0f,7.0f,0.0f);
 40    glEnd();
 41    glPopMatrix();
 42    glPushMatrix();
 43    glTranslatef(6.0f,1.0f,0.0f);
 44    //draw a rectangle,without the help of glBegin&&glEnd
 45    glRectf(0.0f,0.0f,3.0f,3.0f);
 46    glPopMatrix();
 47    glFlush();
 48}

 49 void  reshape( int  w, int  h)
 50 ExpandedBlockStart.gifContractedBlock.gif {
 51    GLfloat range = 10.0f;
 52    if(h == 0)
 53        h = 1;
 54    glViewport(0,0,w,h);
 55    glMatrixMode(GL_PROJECTION);
 56    glLoadIdentity();
 57    if(w < h)
 58        glOrtho(0.0f,range,0.0f,GLfloat(range*h/w),-1.0f,1.0f);
 59    else
 60        glOrtho(0.0f,GLfloat(range*w/h),0.0f,range,-1.0f,1.0f);
 61    glMatrixMode(GL_MODELVIEW);
 62    glLoadIdentity();
 63}

 64 void  keyboard(unsigned  char  key, int  x, int  y)
 65 ExpandedBlockStart.gifContractedBlock.gif {
 66    switch(key)
 67ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 68        //use the space key to decide whether the window will be full-screen displayed.
 69    case 32:                                            
 70        if(t%2 == 0)
 71                        //requests that the current window be made full screen
 72            glutFullScreen();                            
 73        else
 74                        //requests a change to the size of the current window
 75            glutReshapeWindow(WinWidth,WinHeight);        
 76        t++;
 77        break;
 78        //use the Esc key to quit the application
 79    case 27:                                            
 80        exit(0);
 81        break;
 82    default:
 83        break;
 84    }

 85}

 86 int  main( int  argc, char   * argv[])
 87 ExpandedBlockStart.gifContractedBlock.gif {
 88    glutInit(&argc,argv);
 89    glutInitDisplayMode(GLUT_DEPTH|GLUT_RGB|GLUT_SINGLE);
 90    glutInitWindowSize(WinWidth,WinHeight);
 91    glutInitWindowPosition(100,100);
 92    glutCreateWindow("Lesson 02");
 93
 94    init();
 95    glutDisplayFunc(display);
 96    glutReshapeFunc(reshape);
 97    glutKeyboardFunc(keyboard);
 98    glutMainLoop();
 99    return 0;
100}

 

程序效果如下:

转载于:https://www.cnblogs.com/krisdy/archive/2009/01/09/1372389.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值