File: complexity.c

package info (click to toggle)
glut 3.7-14
  • links: PTS
  • area: main
  • in suites: woody
  • size: 12,556 kB
  • ctags: 45,170
  • sloc: ansic: 148,716; makefile: 35,208; ada: 2,062; yacc: 473; fortran: 290; lex: 131; csh: 51; sed: 49; sh: 33
file content (234 lines) | stat: -rw-r--r-- 5,625 bytes parent folder | download | duplicates (3)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* This program demonstrates how to use the stencil buffer to visualize
** the depth complexity of a scene.
*/

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

/* show contents of stencil buffer */
int winwid = 512;
int winht = 512;
GLubyte *stencil = 0; /* so realloc works the first time */

void resize(int wid, int ht)
{
    winwid = wid;
    winht = ht;
    stencil = (GLubyte *)realloc((void*)stencil,
                                 winwid * winht * sizeof(GLubyte));
    glViewport(0, 0, wid, ht);
}

/* ARGSUSED1 */
void key(unsigned char key, int x, int y)
{
    if(key == '\033')
        exit(0);
}

int rotate = 0;
GLfloat udangle = 0.f;
GLfloat lrangle = 0.f;

void motion(int x, int y)
{
    if(rotate)
    {
        udangle = (y - winht/2) * 360./winht;
        lrangle = (x - winwid/2) * 360./winwid;
    }
    glutPostRedisplay();
}

void mouse(int button, int state, int x, int y)
{
    if(state == GLUT_DOWN)
    switch(button)
    {
    case GLUT_LEFT_BUTTON: /* rotate the scene up and down */
    case GLUT_MIDDLE_BUTTON: /* rotate the scene left and right */
        rotate = 1;
        motion(x, y);
        break;
    }
    else
        rotate = 0; /* overkill; cover right button too */
}


/* read back stencil buffer, store in memory, draw back colorized */
void showstencil(void)
{
    glReadPixels(0, 0, winwid, winht, GL_STENCIL_INDEX, 
                 GL_UNSIGNED_BYTE, stencil);

    glRasterPos2i(-1, -1);
    glDrawPixels(winwid, winht, GL_COLOR_INDEX, GL_UNSIGNED_BYTE, stencil);
}


int showdepth = 0;
int depthtest = 1;
/* Called when window needs to be redrawn */
void redraw(void)
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);

    glEnable(GL_STENCIL_TEST);
    if(depthtest)
        glEnable(GL_DEPTH_TEST);

    glPushMatrix();
    glRotatef(lrangle, 0.f, 1.f, 0.f);
    glRotatef(udangle, 1.f, 0.f, 0.f);
    glCallList(1); /* draw scene */
    glPopMatrix();

    glDisable(GL_STENCIL_TEST);
    glFlush(); /* high end machines may need this */

    if(depthtest)
        glDisable(GL_DEPTH_TEST);

    if(showdepth)
        showstencil();

    if(glGetError()) /* to catch programming errors; should never happen */
       printf("Oops! I screwed up my OpenGL calls somewhere\n");
    glutSwapBuffers();
}

/* menu entries mapped to actions */
enum {RENDER, SHOW_STENCIL, DEPTH_TEST};
void menu(int choice)
{
    switch(choice)
    {
    case RENDER:
        showdepth = 0;
        break;
    case SHOW_STENCIL:
        showdepth = 1;
        break;
    case DEPTH_TEST:
        depthtest = !depthtest;
        if(depthtest)
            /* show how many pixels were discarded by depth test */
            glStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
        else
            /* show how many pixels were written to frame buffer */
            glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
        break;
    }
    glutPostRedisplay();
}

typedef struct {
    GLfloat r;
    GLfloat g;
    GLfloat b;
} Color;
 
/* color map to indicate different depth complexities */
Color map[] = {
    {0.f, 0.f, 0.f,},
    {0.f, .25f, 0.f,},
    {0.f, .5f, 0.f,},
    {0.f, .75f, 0.f,},
    {0.f, 1.f, 0.f,},
    {.25f, 1.f, 0.f,},
    {.5f, 1.f, 0.f,},
    {.75f, 1.f, 0.f,},
    {1.f, 1.f, 0.f,},
    {1.f, .75f, 0.f,},
    {1.f, .5f, 0.f,},
    {1.f, .25f, 0.f,},
    {1.f, .0f, 0.f,},
    {1.f, .0f, 0.f,},
    {1.f, .0f, 0.f,},
    {1.f, .0f, 0.f,}
};

/* mapsize should be a power of two */
#define mapsize 16
GLfloat lightpos[4] = {.5f, .5f, -1.f, 1.f};
main(int argc, char *argv[])
{
    GLfloat rmap[mapsize], gmap[mapsize], bmap[mapsize];
    int i;

    glutInit(&argc, argv);
    glutInitWindowSize(winwid, winht);
    glutInitDisplayMode(GLUT_RGBA|GLUT_STENCIL|GLUT_DOUBLE|GLUT_DEPTH);
    (void)glutCreateWindow("visualizing depth complexity");
    glutDisplayFunc(redraw);
    glutKeyboardFunc(key);
    glutReshapeFunc(resize);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutCreateMenu(menu);
    glutAddMenuEntry("Draw Scene", RENDER);
    glutAddMenuEntry("Show Stencil", SHOW_STENCIL);
    glutAddMenuEntry("Toggle Depth Test", DEPTH_TEST);
    glutAttachMenu(GLUT_RIGHT_BUTTON);

    glStencilFunc(GL_ALWAYS, ~0, ~0);
    glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);

    /* draw an interesting scene */
    glNewList(1, GL_COMPILE);
    /* center */
    glPushMatrix();
    glScalef(.2f, .2f, .2f);
    glutSolidTetrahedron();
    glPopMatrix();
    /* right */
    glTranslatef(.4f, 0.f, 0.f);
    glutSolidSphere(.25, 8, 8);
    /* left */
    glTranslatef(-.8f, 0.f, 0.f);
    glutSolidSphere(.25, 8, 8);
    /* bottom */
    glTranslatef(.4f, -.4f, 0.f);
    glutSolidSphere(.25, 8, 8);
    /* top */
    glTranslatef(0.f, .8f, 0.f);
    glutSolidSphere(.25, 8, 8);

    /* lefttop */
    glTranslatef(-.5f, .1f, 0.f);
    glutSolidCube(.3);
    /* righttop */
    glTranslatef(1.f, 0.f, 0.f);
    glutSolidCube(.3);
    /* rightbot */
    glTranslatef(0.f, -1.f, 0.f);
    glutSolidCube(.3);
    /* rightbot */
    glTranslatef(-1.f, 0.f, 0.f);
    glutSolidCube(.3);
    glEndList();

    /* color ramp to show increasing complexity */
    /* black shading to green to yellow to red */
    for(i = 0; i < mapsize; i++)
    {
        rmap[i] = map[i].r;
        gmap[i] = map[i].g;
        bmap[i] = map[i].b;
    }

    glPixelMapfv(GL_PIXEL_MAP_I_TO_R, mapsize, rmap);
    glPixelMapfv(GL_PIXEL_MAP_I_TO_G, mapsize, gmap);
    glPixelMapfv(GL_PIXEL_MAP_I_TO_B, mapsize, bmap);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0, GL_POSITION, lightpos);

    glutMainLoop();
    return 0;
}