OpenGL繪製的幾類三維實體

OpenGL繪製的幾類三維實體函數

 

 

EXAM304_Hoop

//防止重複嵌入spa

#if !defined _EXAM304_H_原型

#define _EXAM304_H_回調函數

//指定嵌入文件it

#include<gl/glut.h>io

#include<math.h>變量

//float

#define RADIUS 50.0fgc

#define SIZE 100.0f

 

#define CONE 1

#define TETRAHEDRON 2

#define CUBE 3

#define DODECAHEDRON 4

#define ICOSAHEDRON 5

#define OCTAHEDRON 6

#define SPHERE 7

#define TORUS 8

#define TEAPOT 9

#define WIRED 10

//全局變量

GLboolean bWire=false;

GLint nSelected=CONE;

//函數原型

void OnDisplay(void);

void OnReshape(int,int);

void OnMenu(int value);

void CreateMenu();

void SetupLights();

#endif

 

EXAM304.cpp

#include "EXAM304.H"

void main(int argc,char *argv[])

{

       glutInit(&argc,argv);

       glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);

       glutInitWindowSize(640,480);

       glutInitWindowPosition(10,10);

       glutCreateWindow("EXAM304");

       glutCreateMenu(OnMenu);

       glutDisplayFunc(OnDisplay);

       glutReshapeFunc(OnReshape);

    CreateMenu();

       SetupLights();

       glutMainLoop();

}

void OnDisplay(void)

{

       glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

       glEnable(GL_DEPTH_TEST);

       glPushMatrix();

       glColor3f(0.0f,1.0f,1.0f);

       glEnable(GL_NORMALIZE);

       switch(nSelected)

       {

       case(CONE):

              if(bWire)

                     glutWireCone(RADIUS,2*RADIUS,30,30);

              else

            glutSolidCone(RADIUS,2*RADIUS,30,30);

              break;

 

       case(TETRAHEDRON):

              glPushMatrix();

              glScalef(RADIUS,RADIUS,RADIUS);

        if(bWire)

                     glutWireTetrahedron();

              else

            glutSolidTetrahedron();

       glPopMatrix();

          break;

 

       case(CUBE):

           if(bWire)

                     glutWireCube(1.5*RADIUS);

              else

            glutSolidCube(1.5*RADIUS);

        break;

 

    case(DODECAHEDRON):

           glPushMatrix();

              glScalef(RADIUS/2,RADIUS/2,RADIUS/2);

        if(bWire)

                     glutWireDodecahedron();

              else

            glutSolidDodecahedron();

       glPopMatrix();

          break;

 

    case(ICOSAHEDRON):

        glPushMatrix();

              glScalef(RADIUS,RADIUS,RADIUS);

        if(bWire)

                     glutWireIcosahedron();

              else

            glutSolidIcosahedron();

       glPopMatrix();

          break;

 

       case(OCTAHEDRON):

        glPushMatrix();

              glScalef(RADIUS,RADIUS,RADIUS);

        if(bWire)

                     glutWireOctahedron();

              else

            glutSolidOctahedron();

       glPopMatrix();

          break;

 

       case(SPHERE):

        if(bWire)

                     glutWireSphere(RADIUS,30,30);

              else

            glutSolidSphere(RADIUS,30,30);

              break;

 

       case(TORUS):

        if(bWire)

                     glutWireTorus(RADIUS/2,RADIUS,30,30);

              else

            glutSolidTorus(RADIUS/2,RADIUS,30,30);

              break;

 

       case(TEAPOT):

        if(bWire)

                     glutWireTeapot(RADIUS);

              else

            glutSolidTeapot(RADIUS);

       }

     glPopMatrix();

        glutSwapBuffers();

}

//窗口尺寸變化後的回調函數

void OnReshape(int w,int h)

{

       glViewport(0,0,w,h);

       glMatrixMode(GL_PROJECTION);

       glLoadIdentity();

    float aspect=(float)h/(float)w;

    if(w<=h)

          glOrtho(-SIZE,SIZE,-SIZE*aspect,SIZE*aspect,-SIZE,SIZE);

       else

          glOrtho(-SIZE/aspect,SIZE/aspect,-SIZE,SIZE,-SIZE,SIZE);

       gluLookAt(10.0f,20.0f,25.0f,0.0f,0.0f,0.0f,0.0f,1.0f,0.0f);

}

void OnMenu(int value)

{

       if(value==WIRED)

              bWire=!bWire;

       else

              nSelected=value;

       glutPostRedisplay();

}

void CreateMenu()

{

       glutAddMenuEntry("圓錐體",CONE);

       glutAddMenuEntry("四面體",TETRAHEDRON);

       glutAddMenuEntry("正方體",CUBE);

       glutAddMenuEntry("正十二面體",DODECAHEDRON);

       glutAddMenuEntry("正二十面體",ICOSAHEDRON);

       glutAddMenuEntry("正八面體",OCTAHEDRON);

       glutAddMenuEntry("球體",SPHERE);

       glutAddMenuEntry("圓環體",TORUS);

       glutAddMenuEntry("茶壺",TEAPOT);

       glutAddMenuEntry("線框或實體",WIRED);

       glutAttachMenu(GLUT_RIGHT_BUTTON);

}

void SetupLights()

{

    GLfloat ambientLight[]={0.2f,0.2f,0.2f,1.0f};

       GLfloat diffuseLight[]={0.9f,0.9f,0.9f,1.0f};

       GLfloat specularLight[]={1.0f,1.0f,1.0f,1.0f};

       GLfloat lightPos[]={50.0f,100.0f,80.0f,1.0f};

 

       glEnable(GL_LIGHTING);

       glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);

       glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);

       glLightfv(GL_LIGHT0,GL_SPECULAR,specularLight);

       glLightfv(GL_LIGHT0,GL_POSITION,lightPos);

       glEnable(GL_LIGHT0);

 

       glEnable(GL_COLOR_MATERIAL);

       glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);

       glMaterialfv(GL_FRONT,GL_SPECULAR,specularLight);

    glMateriali(GL_FRONT,GL_SHININESS,100);

}

相關文章
相關標籤/搜索