opengl学習

structure synthみたい。パーサとか作ってwxWidgetsと組み合わせれば似たようなものが作れそうだ。そうでもないか。

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

void
renderCube(float x, float y, float z, float sz)
{
    glPushMatrix();

    glTranslatef(x, y, z);
    glutSolidCube(sz);

    glPopMatrix();
}

void
display()
{
    int i, j;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    for (i = 0; i < 10; i++)
        for (j = 0; j < 10; j++)
            renderCube((float)i*2, (float)j*2, 0, 1.3);
    glutSwapBuffers();
}

void
reshape(int w, int h)
{
    glViewport(0, 0, w, h);
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60, (double)w/h, 1, 10000);
    gluLookAt(15, 20, 15, 5, 0, -20, 0, 1, 0);
    glMatrixMode(GL_MODELVIEW);
}

void
keyboard(unsigned char k, int x, int y)
{
    if (k == 'q')
        exit(0);
}

void
init()
{
    float pos0[] = {100, 200, 10, 0}; // 無限遠点
    float amb0[] = {1, 1, 1, 0};

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    glLightfv(GL_LIGHT0, GL_POSITION, pos0);
    glLightfv(GL_LIGHT0, GL_AMBIENT,  amb0);

    glEnable(GL_DEPTH_TEST);

    glClearColor(149./256, 156./256, 142./256, 0);
}
int
main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(450, 450);
    glutCreateWindow("arrayed cube");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);

    init();

    glutMainLoop();
    return 0;
}