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

個人的自由ノート

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

OpenGLを学習

4回目です。

#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 edge[][2] = {
        { 0, 1 },
        { 1, 2 },
        { 2, 3 },
        { 3, 0 },
        { 4, 5 },
        { 5, 6 },
        { 6, 7 },
        { 7, 4 },
        { 0, 4 },
        { 1, 5 },
        { 2, 6 },
        { 3, 7 }
};

void idle(void){
        glutPostRedisplay();
}

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

        glClear(GL_COLOR_BUFFER_BIT);

        glLoadIdentity();

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

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

        glColor3d(0.0, 0.0, 0.0);
        glBegin(GL_LINES);
        for(i = 0; i < 12; ++i) {
                glVertex3dv(vertex[edge[i][0]]);
                glVertex3dv(vertex[edge[i][1]]);
        }
        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);
}

int main(int argc, char** argv){
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
        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-14-20-14-05

#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 color[][3] = {
        { 1.0, 0.0, 0.0 },
        { 0.0, 1.0, 0.0 },
        { 0.0, 0.0, 1.0 },
        { 1.0, 1.0, 0.0 },
        { 1.0, 0.0, 1.0 },
        { 0.0, 1.0, 1.0 }
};

void idle(void){
        glutPostRedisplay();
}

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

        glClear(GL_COLOR_BUFFER_BIT);

        glLoadIdentity();

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

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

        glColor3d(0.0, 0.0, 0.0);
        glBegin(GL_QUADS);
        for(j = 0; j < 6; ++j) {
                glColor3dv(color[j]);
                for(zi = 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);
}

int main(int argc, char** argv){
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
        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-14-20-20-19

#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 color[][3] = {
        { 1.0, 0.0, 0.0 },
        { 0.0, 1.0, 0.0 },
        { 0.0, 0.0, 1.0 },
        { 1.0, 1.0, 0.0 },
        { 1.0, 0.0, 1.0 },
        { 0.0, 1.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);

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

        glColor3d(0.0, 0.0, 0.0);
        glBegin(GL_QUADS);
        for(j = 0; j < 6; ++j) {
                glColor3dv(color[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);
}

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-14-20-24-56

#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 color[][3] = {
        { 1.0, 0.0, 0.0 },
        { 0.0, 1.0, 0.0 },
        { 0.0, 0.0, 1.0 },
        { 1.0, 1.0, 0.0 },
        { 1.0, 0.0, 1.0 },
        { 0.0, 1.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);

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

        glColor3d(0.0, 0.0, 0.0);
        glBegin(GL_QUADS);
        for(j = 0; j < 6; ++j) {
                glColor3dv(color[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_BACK);
}

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-14-20-31-00

#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 }
};

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);

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

        glColor3d(0.0, 0.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);
}

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-14-20-36-15