OpenGL学习六:3D图形

glBegin(GL_QUADS);
  glVertex3f( 1.0f, 1.0f,-1.0f);   // 四边形的右上顶点 (顶面)
  glVertex3f(-1.0f, 1.0f,-1.0f);   // 四边形的左上顶点 (顶面)
  glVertex3f(-1.0f, 1.0f, 1.0f);   // 四边形的左下顶点 (顶面)
  glVertex3f( 1.0f, 1.0f, 1.0f);   // 四边形的右下顶点 (顶面)

  glVertex3f( 1.0f,-1.0f, 1.0f);   // 四边形的右上顶点(底面)
  glVertex3f(-1.0f,-1.0f, 1.0f);   // 四边形的左上顶点(底面)
  glVertex3f(-1.0f,-1.0f,-1.0f);   // 四边形的左下顶点(底面)
  glVertex3f( 1.0f,-1.0f,-1.0f);   // 四边形的右下顶点(底面)

  glVertex3f( 1.0f, 1.0f, 1.0f);   // 四边形的右上顶点(前面)
  glVertex3f(-1.0f, 1.0f, 1.0f);   // 四边形的左上顶点(前面)
  glVertex3f(-1.0f,-1.0f, 1.0f);   // 四边形的左下顶点(前面)
  glVertex3f( 1.0f,-1.0f, 1.0f);   // 四边形的右下顶点(前面)

  glVertex3f(-1.0f, 1.0f, 1.0f);   // 四边形的右上顶点(左面)
  glVertex3f(-1.0f, 1.0f,-1.0f);   // 四边形的左上顶点(左面)
  glVertex3f(-1.0f,-1.0f,-1.0f);   // 四边形的左下顶点(左面)
  glVertex3f(-1.0f,-1.0f, 1.0f);   // 四边形的右下顶点(左面)

  glVertex3f( 1.0f, 1.0f,-1.0f);   // 四边形的右上顶点(右面)
  glVertex3f( 1.0f, 1.0f, 1.0f);   // 四边形的左上顶点(右面)
  glVertex3f( 1.0f,-1.0f, 1.0f);   // 四边形的左下顶点(右面)
  glVertex3f( 1.0f,-1.0f,-1.0f);   // 四边形的右下顶点(右面)

  glVertex3f( 1.0f,-1.0f,-1.0f);   // 四边形的右上顶点(后面)
  glVertex3f(-1.0f,-1.0f,-1.0f);   // 四边形的左上顶点(后面)
  glVertex3f(-1.0f, 1.0f,-1.0f);   // 四边形的左下顶点(后面)
  glVertex3f( 1.0f, 1.0f,-1.0f);   // 四边形的右下顶点(后面
glEnd();

#include "header.h"

GLfloat rtri;
GLfloat rquad;      

GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
{
    if (height==0)
    {
        height=1;
    }

    glViewport(0,0,width,height);                   

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();                               

    gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

int InitGL(void)
{
    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    return TRUE;
}

void DrawGLScene(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(-1.5f,0.0f,-6.0f);
    glRotatef(rtri,0.0f,1.0f,0.0f);
    glBegin(GL_TRIANGLES);
        glColor3f(1.0f,0.0f,0.0f);
        glVertex3f( 0.0f, 1.0f, 0.0f);
        glColor3f(0.0f,1.0f,0.0f);
        glVertex3f(-1.0f,-1.0f, 1.0f);
        glColor3f(0.0f,0.0f,1.0f);
        glVertex3f( 1.0f,-1.0f, 1.0f);
        glColor3f(1.0f,0.0f,0.0f);
        glVertex3f( 0.0f, 1.0f, 0.0f);
        glColor3f(0.0f,0.0f,1.0f);
        glVertex3f( 1.0f,-1.0f, 1.0f);
        glColor3f(0.0f,1.0f,0.0f);
        glVertex3f( 1.0f,-1.0f, -1.0f);
        glColor3f(1.0f,0.0f,0.0f);
        glVertex3f( 0.0f, 1.0f, 0.0f);
        glColor3f(0.0f,1.0f,0.0f);
        glVertex3f( 1.0f,-1.0f, -1.0f);
        glColor3f(0.0f,0.0f,1.0f);
        glVertex3f(-1.0f,-1.0f, -1.0f);
        glColor3f(1.0f,0.0f,0.0f);
        glVertex3f( 0.0f, 1.0f, 0.0f);
        glColor3f(0.0f,0.0f,1.0f);
        glVertex3f(-1.0f,-1.0f,-1.0f);
        glColor3f(0.0f,1.0f,0.0f);
        glVertex3f(-1.0f,-1.0f, 1.0f);
    glEnd();                                            

    glLoadIdentity();
    glTranslatef(1.5f,0.0f,-7.0f);
    glRotatef(rquad,1.0f,1.0f,1.0f);
    glBegin(GL_QUADS);
        glColor3f(0.0f,1.0f,0.0f);
        glVertex3f( 1.0f, 1.0f,-1.0f);
        glVertex3f(-1.0f, 1.0f,-1.0f);
        glVertex3f(-1.0f, 1.0f, 1.0f);
        glVertex3f( 1.0f, 1.0f, 1.0f);
        glColor3f(1.0f,0.5f,0.0f);
        glVertex3f( 1.0f,-1.0f, 1.0f);
        glVertex3f(-1.0f,-1.0f, 1.0f);
        glVertex3f(-1.0f,-1.0f,-1.0f);
        glVertex3f( 1.0f,-1.0f,-1.0f);
        glColor3f(1.0f,0.0f,0.0f);
        glVertex3f( 1.0f, 1.0f, 1.0f);
        glVertex3f(-1.0f, 1.0f, 1.0f);
        glVertex3f(-1.0f,-1.0f, 1.0f);
        glVertex3f( 1.0f,-1.0f, 1.0f);
        glColor3f(1.0f,1.0f,0.0f);
        glVertex3f( 1.0f,-1.0f,-1.0f);
        glVertex3f(-1.0f,-1.0f,-1.0f);
        glVertex3f(-1.0f, 1.0f,-1.0f);
        glVertex3f( 1.0f, 1.0f,-1.0f);
        glColor3f(0.0f,0.0f,1.0f);
        glVertex3f(-1.0f, 1.0f, 1.0f);
        glVertex3f(-1.0f, 1.0f,-1.0f);
        glVertex3f(-1.0f,-1.0f,-1.0f);
        glVertex3f(-1.0f,-1.0f, 1.0f);
        glColor3f(1.0f,0.0f,1.0f);
        glVertex3f( 1.0f, 1.0f,-1.0f);
        glVertex3f( 1.0f, 1.0f, 1.0f);
        glVertex3f( 1.0f,-1.0f, 1.0f);
        glVertex3f( 1.0f,-1.0f,-1.0f);
    glEnd();                                            

    rtri+=0.2f;
    rquad-=0.15f;
    glFlush();
}

void rotate()
{
    rtri+=0.2f;
    rquad-=0.15f;
    glutPostRedisplay();
}
void mouse(int button,int state,int x,int y)
{
    switch(button)
    {
    case GLUT_LEFT_BUTTON:
        if(GLUT_DOWN==state)
        {
            glutIdleFunc(rotate);
        }
        break;
    case GLUT_RIGHT_BUTTON:
        if(GLUT_DOWN==state)
        {
            //glutIdleFunc(0);
        }
        break;
    }
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(640,480);
    glutCreateWindow("3D图形");
    glutReshapeFunc(ReSizeGLScene);
    glutDisplayFunc(DrawGLScene);
    glutMouseFunc(mouse);
    InitGL();
    glutMainLoop();
    return 0;
}

发表评论