読者です 読者をやめる 読者になる 読者になる

個人的自由ノート

ふと自分が気になった事を書いてます

OpenGLの学習

サンプルプログラムはここまでです。次は練習問題。3D空間のウォークスルーです。次回に成果を載せてみたいと思います。

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

GLdouble vertex[][3] = {
        { 0.0, 0.0, 0.0 },
        { 1.0, 0.0, 0.0 },
        { 1.0, 1.0, 0.0 },
        { 0.0, 1.0, 0.0 },
        { 0.0, 0.0, 1.0 },
        { 1.0, 0.0, 1.0 },
        { 1.0, 1.0, 1.0 },
        { 0.0, 1.0, 1.0 }
};

int face[][4] = {
        { 0, 1, 2, 3 },
        { 1, 5, 6, 2 },
        { 5, 4, 7, 6 },
        { 4, 0, 3, 7 },
        { 4, 5, 1, 0 },
        { 3, 2, 6, 7 }
};

GLdouble normal[][3] = {
        { 0.0, 0.0,-1.0 },
        { 1.0, 0.0, 0.0 },
        { 0.0, 0.0, 1.0 },
        {-1.0, 0.0, 0.0 },
        { 0.0,-1.0, 0.0 },
        { 0.0, 1.0, 0.0 }
};

GLfloat light0pos[] = { 0.0, 3.0, 5.0, 1.0 };
GLfloat light1pos[] = { 5.0, 3.0, 0.0, 1.0 };

GLfloat green[] = { 0.0, 1.0, 0.0, 1.0 };

void idle(void){
        glutPostRedisplay();
}

void display(void){
        int i;
        int j;
        static int r = 0;

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glLoadIdentity();

        gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

        glLightfv(GL_LIGHT0, GL_POSITION, light0pos);
        glLightfv(GL_LIGHT1, GL_POSITION, light1pos);

        glRotated((double)r, 0.0, 1.0, 0.0);

        glBegin(GL_QUADS);
        for(j = 0; j < 6; ++j) {
                glNormal3dv(normal[j]);
                for(i = 0; i < 4; ++i) {                         glVertex3dv(vertex[face[j][i]]);                 }         }         glEnd();         glutSwapBuffers();         if(++r >= 360) r = 0;
}

void resize(int w, int h){
        glViewport(0, 0, w, h);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);
        glMatrixMode(GL_MODELVIEW);
}

void mouse(int button, int state, int x, int y){
        switch(button) {
        case GLUT_LEFT_BUTTON:
                if(state == GLUT_DOWN) {
                        glutIdleFunc(idle);
                } else {
                        glutIdleFunc(0);
                }
                break;
        case GLUT_RIGHT_BUTTON:
                if(state == GLUT_DOWN) {
                        glutPostRedisplay();
                }
                break;
        default:
                break;
        }
}

void keyboard(unsigned char key, int x, int y){
        switch(key) {
        case 'q':
        case 'Q':
        case '\033':
                exit(0);
        default:
                break;
        }
}

void init(void){
        glClearColor(1.0, 1.0, 1.0, 1.0);
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_CULL_FACE);
        glCullFace(GL_FRONT);

        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        glEnable(GL_LIGHT1);
        glLightfv(GL_LIGHT1, GL_DIFFUSE, green);
        glLightfv(GL_LIGHT1, GL_SPECULAR, green);
}

int main(int argc, char** argv){
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
        glutCreateWindow(argv[0]);
        glutDisplayFunc(display);
        glutReshapeFunc(resize);
        glutMouseFunc(mouse);
        glutKeyboardFunc(keyboard);
        init();
        glutMainLoop();
        return 0;
}

%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88-2016-11-15-15-51-39

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

GLdouble vertex[][3] = {
        { 0.0, 0.0, 0.0 },
        { 1.0, 0.0, 0.0 },
        { 1.0, 1.0, 0.0 },
        { 0.0, 1.0, 0.0 },
        { 0.0, 0.0, 1.0 },
        { 1.0, 0.0, 1.0 },
        { 1.0, 1.0, 1.0 },
        { 0.0, 1.0, 1.0 }
};

int face[][4] = {
        { 0, 1, 2, 3 },
        { 1, 5, 6, 2 },
        { 5, 4, 7, 6 },
        { 4, 0, 3, 7 },
        { 4, 5, 1, 0 },
        { 3, 2, 6, 7 }
};

GLdouble normal[][3] = {
        { 0.0, 0.0,-1.0 },
        { 1.0, 0.0, 0.0 },
        { 0.0, 0.0, 1.0 },
        {-1.0, 0.0, 0.0 },
        { 0.0,-1.0, 0.0 },
        { 0.0, 1.0, 0.0 }
};

GLfloat light0pos[] = { 0.0, 3.0, 5.0, 1.0 };
GLfloat light1pos[] = { 5.0, 3.0, 0.0, 1.0 };

GLfloat green[] = { 0.0, 1.0, 0.0, 1.0 };
GLfloat red[] = { 0.8, 0.2, 0.2, 1.0 };

void idle(void){
        glutPostRedisplay();
}

void display(void){
        int i;
        int j;
        static int r = 0;

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glLoadIdentity();

        gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

        glLightfv(GL_LIGHT0, GL_POSITION, light0pos);
        glLightfv(GL_LIGHT1, GL_POSITION, light1pos);

        glRotated((double)r, 0.0, 1.0, 0.0);

        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);
        
        glBegin(GL_QUADS);
        for(j = 0; j < 6; ++j) {
                glNormal3dv(normal[j]);
                for(i = 0; i < 4; ++i) {
                        glVertex3dv(vertex[face[j][i]]);
                }
        }
        glEnd();

        glutSwapBuffers();

        if(++r >= 360) r = 0;
}

void resize(int w, int h){
        glViewport(0, 0, w, h);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);
        glMatrixMode(GL_MODELVIEW);
}

void mouse(int button, int state, int x, int y){
        switch(button) {
        case GLUT_LEFT_BUTTON:
                if(state == GLUT_DOWN) {
                        glutIdleFunc(idle);
                } else {
                        glutIdleFunc(0);
                }
                break;
        case GLUT_RIGHT_BUTTON:
                if(state == GLUT_DOWN) {
                        glutPostRedisplay();
                }
                break;
        default:
                break;
        }
}

void keyboard(unsigned char key, int x, int y){
        switch(key) {
        case 'q':
        case 'Q':
        case '\033':
                exit(0);
        default:
                break;
        }
}

void init(void){
        glClearColor(1.0, 1.0, 1.0, 1.0);
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_CULL_FACE);
        glCullFace(GL_FRONT);

        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        glEnable(GL_LIGHT1);
        glLightfv(GL_LIGHT1, GL_DIFFUSE, green);
        glLightfv(GL_LIGHT1, GL_SPECULAR, green);
}

int main(int argc, char** argv){
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
        glutCreateWindow(argv[0]);
        glutDisplayFunc(display);
        glutReshapeFunc(resize);
        glutMouseFunc(mouse);
        glutKeyboardFunc(keyboard);
        init();
        glutMainLoop();
        return 0;
}

%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88-2016-11-15-15-59-59

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

GLdouble vertex[][3] = {
        { 0.0, 0.0, 0.0 },
        { 1.0, 0.0, 0.0 },
        { 1.0, 1.0, 0.0 },
        { 0.0, 1.0, 0.0 },
        { 0.0, 0.0, 1.0 },
        { 1.0, 0.0, 1.0 },
        { 1.0, 1.0, 1.0 },
        { 0.0, 1.0, 1.0 }
};

int face[][4] = {
        { 0, 1, 2, 3 },
        { 1, 5, 6, 2 },
        { 5, 4, 7, 6 },
        { 4, 0, 3, 7 },
        { 4, 5, 1, 0 },
        { 3, 2, 6, 7 }
};

GLdouble normal[][3] = {
        { 0.0, 0.0,-1.0 },
        { 1.0, 0.0, 0.0 },
        { 0.0, 0.0, 1.0 },
        {-1.0, 0.0, 0.0 },
        { 0.0,-1.0, 0.0 },
        { 0.0, 1.0, 0.0 }
};

GLfloat light0pos[] = { 0.0, 3.0, 5.0, 1.0 };
GLfloat light1pos[] = { 5.0, 3.0, 0.0, 1.0 };

GLfloat green[] = { 0.0, 1.0, 0.0, 1.0 };
GLfloat red[] = { 0.8, 0.2, 0.2, 1.0 };

void cube(void){
    int i;
    int j;

    glBegin(GL_QUADS);
    for(j = 0; j < 6; ++j){
      glNormal3dv(normal[j]);
      for(i = 0; i < 4; ++i){
        glVertex3dv(vertex[face[j][i]]);
      }
    }
    glEnd();
}

void idle(void){
        glutPostRedisplay();
}

void display(void){
        int i;
        int j;
        static int r = 0;

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glLoadIdentity();

        gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

        glLightfv(GL_LIGHT0, GL_POSITION, light0pos);
        glLightfv(GL_LIGHT1, GL_POSITION, light1pos);

        glPushMatrix();

        glRotated((double)r, 0.0, 1.0, 0.0);

        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);

        cube();

        glPopMatrix();

        glBegin(GL_QUADS);
        for(j = 0; j < 6; ++j) {
                glNormal3dv(normal[j]);
                for(i = 0; i < 4; ++i) {
                        glVertex3dv(vertex[face[j][i]]);
                }
        }
        glEnd();

        glutSwapBuffers();

        if(++r >= 360) r = 0;
}

void resize(int w, int h){
        glViewport(0, 0, w, h);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void mouse(int button, int state, int x, int y){
        switch(button) {
        case GLUT_LEFT_BUTTON:
                if(state == GLUT_DOWN) {
                        glutIdleFunc(idle);
                } else {
                        glutIdleFunc(0);
                }
                break;
        case GLUT_RIGHT_BUTTON:
                if(state == GLUT_DOWN) {
                        glutPostRedisplay();
                }
                break;
        default:
                break;
        }
}

void keyboard(unsigned char key, int x, int y){
        switch(key) {
        case 'q':
        case 'Q':
        case '\033':
                exit(0);
        default:
                break;
        }
}

void init(void){
        glClearColor(1.0, 1.0, 1.0, 1.0);
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_CULL_FACE);
        glCullFace(GL_FRONT);

        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        glEnable(GL_LIGHT1);
        glLightfv(GL_LIGHT1, GL_DIFFUSE, green);
        glLightfv(GL_LIGHT1, GL_SPECULAR, green);
}

int main(int argc, char** argv){
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
        glutCreateWindow(argv[0]);
        glutDisplayFunc(display);
        glutReshapeFunc(resize);
        glutMouseFunc(mouse);
        glutKeyboardFunc(keyboard);
        init();
        glutMainLoop();
        return 0;
}

%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88-2016-11-15-16-21-37

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

GLdouble vertex[][3] = {
        { 0.0, 0.0, 0.0 },
        { 1.0, 0.0, 0.0 },
        { 1.0, 1.0, 0.0 },
        { 0.0, 1.0, 0.0 },
        { 0.0, 0.0, 1.0 },
        { 1.0, 0.0, 1.0 },
        { 1.0, 1.0, 1.0 },
        { 0.0, 1.0, 1.0 }
};

int face[][4] = {
        { 0, 1, 2, 3 },
        { 1, 5, 6, 2 },
        { 5, 4, 7, 6 },
        { 4, 0, 3, 7 },
        { 4, 5, 1, 0 },
        { 3, 2, 6, 7 }
};

GLdouble normal[][3] = {
        { 0.0, 0.0,-1.0 },
        { 1.0, 0.0, 0.0 },
        { 0.0, 0.0, 1.0 },
        {-1.0, 0.0, 0.0 },
        { 0.0,-1.0, 0.0 },
        { 0.0, 1.0, 0.0 }
};

GLfloat light0pos[] = { 0.0, 3.0, 5.0, 1.0 };
GLfloat light1pos[] = { 5.0, 3.0, 0.0, 1.0 };

GLfloat green[] = { 0.0, 1.0, 0.0, 1.0 };
GLfloat red[] = { 0.8, 0.2, 0.2, 1.0 };

void cube(void){
    int i;
    int j;

    glBegin(GL_QUADS);
    for(j = 0; j < 6; ++j){
      glNormal3dv(normal[j]);
      for(i = 0; i < 4; ++i){
        glVertex3dv(vertex[face[j][i]]);
      }
    }
    glEnd();
}

void idle(void){
        glutPostRedisplay();
}

void display(void){
        int i;
        int j;
        static int r = 0;

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glLoadIdentity();

        gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

        glLightfv(GL_LIGHT0, GL_POSITION, light0pos);
        glLightfv(GL_LIGHT1, GL_POSITION, light1pos);

        glPushMatrix();

        glRotated((double)r, 0.0, 1.0, 0.0);

        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);

        cube();

        glPushMatrix();
        glTranslated(1.0, 1.0, 1.0);
        cube();
        glPopMatrix();

        glPopMatrix();

        glBegin(GL_QUADS);
        for(j = 0; j < 6; ++j) {
                glNormal3dv(normal[j]);
                for(i = 0; i < 4; ++i) {
                        glVertex3dv(vertex[face[j][i]]);
                }
        }
        glEnd();

        glutSwapBuffers();

        if(++r >= 360) r = 0;
}

void resize(int w, int h){
        glViewport(0, 0, w, h);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void mouse(int button, int state, int x, int y){
        switch(button) {
        case GLUT_LEFT_BUTTON:
                if(state == GLUT_DOWN) {
                        glutIdleFunc(idle);
                } else {
                        glutIdleFunc(0);
                }
                break;
        case GLUT_RIGHT_BUTTON:
                if(state == GLUT_DOWN) {
                        glutPostRedisplay();
                }
                break;
        default:
                break;
        }
}

void keyboard(unsigned char key, int x, int y){
        switch(key) {
        case 'q':
        case 'Q':
        case '\033':
                exit(0);
        default:
                break;
        }
}

void init(void){
        glClearColor(1.0, 1.0, 1.0, 1.0);
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_CULL_FACE);
        glCullFace(GL_FRONT);

        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        glEnable(GL_LIGHT1);
        glLightfv(GL_LIGHT1, GL_DIFFUSE, green);
        glLightfv(GL_LIGHT1, GL_SPECULAR, green);
}

int main(int argc, char** argv){
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
        glutCreateWindow(argv[0]);
        glutDisplayFunc(display);
        glutReshapeFunc(resize);
        glutMouseFunc(mouse);
        glutKeyboardFunc(keyboard);
        init();
        glutMainLoop();
        return 0;
}

%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88-2016-11-15-16-25-53

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

GLdouble vertex[][3] = {
        { 0.0, 0.0, 0.0 },
        { 1.0, 0.0, 0.0 },
        { 1.0, 1.0, 0.0 },
        { 0.0, 1.0, 0.0 },
        { 0.0, 0.0, 1.0 },
        { 1.0, 0.0, 1.0 },
        { 1.0, 1.0, 1.0 },
        { 0.0, 1.0, 1.0 }
};

int face[][4] = {
        { 0, 1, 2, 3 },
        { 1, 5, 6, 2 },
        { 5, 4, 7, 6 },
        { 4, 0, 3, 7 },
        { 4, 5, 1, 0 },
        { 3, 2, 6, 7 }
};

GLdouble normal[][3] = {
        { 0.0, 0.0,-1.0 },
        { 1.0, 0.0, 0.0 },
        { 0.0, 0.0, 1.0 },
        {-1.0, 0.0, 0.0 },
        { 0.0,-1.0, 0.0 },
        { 0.0, 1.0, 0.0 }
};

GLfloat light0pos[] = { 0.0, 3.0, 5.0, 1.0 };
GLfloat light1pos[] = { 5.0, 3.0, 0.0, 1.0 };

GLfloat green[] = { 0.0, 1.0, 0.0, 1.0 };
GLfloat red[] = { 0.8, 0.2, 0.2, 1.0 };

void cube(void){
    int i;
    int j;

    glBegin(GL_QUADS);
    for(j = 0; j < 6; ++j){
      glNormal3dv(normal[j]);
      for(i = 0; i < 4; ++i){
        glVertex3dv(vertex[face[j][i]]);
      }
    }
    glEnd();
}

void idle(void){
        glutPostRedisplay();
}

void display(void){
        int i;
        int j;
        static int r = 0;

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glLoadIdentity();

        gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

        glLightfv(GL_LIGHT0, GL_POSITION, light0pos);
        glLightfv(GL_LIGHT1, GL_POSITION, light1pos);

        glPushMatrix();

        glRotated((double)r, 0.0, 1.0, 0.0);

        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);

        cube();

        glPushMatrix();
        glTranslated(1.0, 1.0, 1.0);
        glRotated((double)(2 * r), 0.0, 1.0, 0.0);
        cube();
        glPopMatrix();

        glPopMatrix();

        glBegin(GL_QUADS);
        for(j = 0; j < 6; ++j) {
                glNormal3dv(normal[j]);
                for(i = 0; i < 4; ++i) {
                        glVertex3dv(vertex[face[j][i]]);
                }
        }
        glEnd();

        glutSwapBuffers();

        if(++r >= 360) r = 0;
}

void resize(int w, int h){
        glViewport(0, 0, w, h);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void mouse(int button, int state, int x, int y){
        switch(button) {
        case GLUT_LEFT_BUTTON:
                if(state == GLUT_DOWN) {
                        glutIdleFunc(idle);
                } else {
                        glutIdleFunc(0);
                }
                break;
        case GLUT_RIGHT_BUTTON:
                if(state == GLUT_DOWN) {
                        glutPostRedisplay();
                }
                break;
        default:
                break;
        }
}

void keyboard(unsigned char key, int x, int y){
        switch(key) {
        case 'q':
        case 'Q':
        case '\033':
                exit(0);
        default:
                break;
        }
}

void init(void){
        glClearColor(1.0, 1.0, 1.0, 1.0);
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_CULL_FACE);
        glCullFace(GL_FRONT);

        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        glEnable(GL_LIGHT1);
        glLightfv(GL_LIGHT1, GL_DIFFUSE, green);
        glLightfv(GL_LIGHT1, GL_SPECULAR, green);
}

int main(int argc, char** argv){
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
        glutCreateWindow(argv[0]);
        glutDisplayFunc(display);
        glutReshapeFunc(resize);
        glutMouseFunc(mouse);
        glutKeyboardFunc(keyboard);
        init();
        glutMainLoop();
        return 0;
}

%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88-2016-11-15-16-29-08

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

GLdouble vertex[][3] = {
        { 0.0, 0.0, 0.0 },
        { 1.0, 0.0, 0.0 },
        { 1.0, 1.0, 0.0 },
        { 0.0, 1.0, 0.0 },
        { 0.0, 0.0, 1.0 },
        { 1.0, 0.0, 1.0 },
        { 1.0, 1.0, 1.0 },
        { 0.0, 1.0, 1.0 }
};

int face[][4] = {
        { 0, 1, 2, 3 },
        { 1, 5, 6, 2 },
        { 5, 4, 7, 6 },
        { 4, 0, 3, 7 },
        { 4, 5, 1, 0 },
        { 3, 2, 6, 7 }
};

GLdouble normal[][3] = {
        { 0.0, 0.0,-1.0 },
        { 1.0, 0.0, 0.0 },
        { 0.0, 0.0, 1.0 },
        {-1.0, 0.0, 0.0 },
        { 0.0,-1.0, 0.0 },
        { 0.0, 1.0, 0.0 }
};

GLfloat light0pos[] = { 0.0, 3.0, 5.0, 1.0 };
GLfloat light1pos[] = { 5.0, 3.0, 0.0, 1.0 };

GLfloat green[] = { 0.0, 1.0, 0.0, 1.0 };
GLfloat red[] = { 0.8, 0.2, 0.2, 1.0 };
GLfloat blue[] = { 0.2, 0.2, 0.8, 1.0 };

void cube(void){
    int i;
    int j;

    glBegin(GL_QUADS);
    for(j = 0; j < 6; ++j){
      glNormal3dv(normal[j]);
      for(i = 0; i < 4; ++i){
        glVertex3dv(vertex[face[j][i]]);
      }
    }
    glEnd();
}

void idle(void){
        glutPostRedisplay();
}

void display(void){
        int i;
        int j;
        static int r = 0;

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glLoadIdentity();

        gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

        glLightfv(GL_LIGHT0, GL_POSITION, light0pos);
        glLightfv(GL_LIGHT1, GL_POSITION, light1pos);

        glPushMatrix();

        glRotated((double)r, 0.0, 1.0, 0.0);

        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, red);

        cube();

        glPushMatrix();
        glTranslated(1.0, 1.0, 1.0);
        glRotated((double)(2 * r), 0.0, 1.0, 0.0);
        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blue);
        cube();
        glPopMatrix();

        glPopMatrix();

        glBegin(GL_QUADS);
        for(j = 0; j < 6; ++j) {
                glNormal3dv(normal[j]);
                for(i = 0; i < 4; ++i) {
                        glVertex3dv(vertex[face[j][i]]);
                }
        }
        glEnd();

        glutSwapBuffers();

        if(++r >= 360) r = 0;
}

void resize(int w, int h){
        glViewport(0, 0, w, h);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(30.0, (double)w / (double)h, 1.0, 100.0);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(3.0, 4.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void mouse(int button, int state, int x, int y){
        switch(button) {
        case GLUT_LEFT_BUTTON:
                if(state == GLUT_DOWN) {
                        glutIdleFunc(idle);
                } else {
                        glutIdleFunc(0);
                }
                break;
        case GLUT_RIGHT_BUTTON:
                if(state == GLUT_DOWN) {
                        glutPostRedisplay();
                }
                break;
        default:
                break;
        }
}

void keyboard(unsigned char key, int x, int y){
        switch(key) {
        case 'q':
        case 'Q':
        case '\033':
                exit(0);
        default:
                break;
        }
}

void init(void){
        glClearColor(1.0, 1.0, 1.0, 1.0);
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_CULL_FACE);
        glCullFace(GL_FRONT);

        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        glEnable(GL_LIGHT1);
        glLightfv(GL_LIGHT1, GL_DIFFUSE, green);
        glLightfv(GL_LIGHT1, GL_SPECULAR, green);
}

int main(int argc, char** argv){
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
        glutCreateWindow(argv[0]);
        glutDisplayFunc(display);
        glutReshapeFunc(resize);
        glutMouseFunc(mouse);
        glutKeyboardFunc(keyboard);
        init();
        glutMainLoop();
        return 0;
}

%e3%82%b9%e3%82%af%e3%83%aa%e3%83%bc%e3%83%b3%e3%82%b7%e3%83%a7%e3%83%83%e3%83%88-2016-11-15-16-31-28