GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: No Line Numbers


OpenGL Mengerschwamm - Simple Lighting by matthiasw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
 
#include <GL/glut.h>

int x_spin;
int y_spin;
int z_spin;
int dl_menger;

void menger(float left, float top, float far, float len, int level)
{
    if (level <= 1)
    {
        /* draw cube: */
        glPushMatrix();
            glTranslatef(left, top, far);
            glutSolidCube(len);
        glPopMatrix();
    }
    else
    {
        float i, j, k;
        
        left -= len / 3.0; top += len / 3.0; far -= len / 3.0;
        
        for (i = 0; i <= 2; i++)
        for (j = 0; j <= 2; j++)
        for (k = 0; k <= 2; k++)
            if ((i == 1 && (j == 1 || k == 1)) || (j == 1 && k == 1))
                continue;
            else
                menger(left + len * (i / 3.0), top  - len * (j / 3.0),
                       far  + len * (k / 3.0), len / 3.0, level - 1);
    }
}

void init(int level)
{
    if (level < 1) level = 4;

    glShadeModel(GL_SMOOTH);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_COLOR_MATERIAL);

    GLfloat position[] = { 150.0f, 150.0f, 150.0f, 1.0f };
    glLightfv(GL_LIGHT0, GL_POSITION, position);
    
    glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
    glClearColor(0, 0, 0, 0);
    glColor3f( 1.0, 1.0, 1.0 );
    
    dl_menger = glGenLists(1);
    glNewList(dl_menger, GL_COMPILE);
    menger(.0, .0, .0, 150.0, level);
    glEndList();
}

void reshape(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-150, 150, -150, 150, 150, -150);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glRotatef(x_spin, 1.0,  .0,  .0);
    glRotatef(y_spin,  .0, 1.0,  .0);
    glRotatef(z_spin,  .0,  .0, 1.0);
    glCallList(dl_menger);
        
    glutSwapBuffers();
}

void keyboard( unsigned char key, int x, int y )
{
    switch (key)
    {
        case 'w': x_spin -= 5; x_spin %= 360; break;
        case 's': x_spin += 5; x_spin %= 360; break;
        case 'a': y_spin -= 5; y_spin %= 360; break;
        case 'd': y_spin += 5; y_spin %= 360; break;
        case 'e': z_spin -= 5; z_spin %= 360; break;
        case 'q': z_spin += 5; z_spin %= 360; break;
    }
    
    glutPostRedisplay();
}

int main(int argc, char* *argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(500, 500);
    glutCreateWindow(argv[0]);
    
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    
    if (argc > 1) init(atoi(argv[1]));
    else          init(0);

    glutMainLoop();
}