[NEHE Couse] 03.My first polygon with various color

     这一节跟第二节没什么大的区别,就是在原来简单面片上加了颜色,注意glColor3f()的使用以及glShadeModel(GL_SMOOTH)和glShadeModel(GL_FLAT)对颜色的控制,后面出现的glColor3f函数会覆盖前面glColor3f函数的功能。

程序代码:

  1/*
  2Introduction:Written by krisdy,finished at 2009.1.9
  3Apply to:NEHE Couse 03:My First Polygon with various color
  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
 11static GLint t;
 12
 13void init(void)
 14{
 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}

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

 53void reshape(int w,int h)
 54{
 55    GLfloat range = 10.0f;
 56    if(h == 0)
 57        h = 1;
 58    glViewport(0,0,w,h);
 59    glMatrixMode(GL_PROJECTION);
 60    glLoadIdentity();
 61    if(w < h)
 62        glOrtho(0.0f,range,0.0f,GLfloat(range*h/w),-1.0f,1.0f);
 63    else
 64        glOrtho(0.0f,GLfloat(range*w/h),0.0f,range,-1.0f,1.0f);
 65    glMatrixMode(GL_MODELVIEW);
 66    glLoadIdentity();
 67}

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

 89}

 90int main(int argc,char *argv[])
 91{
 92    glutInit(&argc,argv);
 93    glutInitDisplayMode(GLUT_DEPTH|GLUT_RGB|GLUT_SINGLE);
 94    glutInitWindowSize(WinWidth,WinHeight);
 95    glutInitWindowPosition(100,100);
 96    glutCreateWindow("Lesson 03");
 97
 98    init();
 99    glutDisplayFunc(display);
100    glutReshapeFunc(reshape);
101    glutKeyboardFunc(keyboard);
102    glutMainLoop();
103    return 0;
104}

 

程序效果:

posted on 2009-01-09 12:56  笔记  阅读(289)  评论(0编辑  收藏  举报

导航