summaryrefslogtreecommitdiff
path: root/glclear-depth.c
blob: 4f1948d0affecf343528b2b72552340462d7a41d (plain)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
 * Copyright (C) 2012 Red Hat, Inc. All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * author: Jerome Glisse
 */
#define MAXFRAME        5
#define PFINISH         1
#define MAX_PARTICLES   256

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define GL_GLEXT_PROTOTYPES
#include <GL/glew.h>
#include <GL/glut.h>

static GLuint win_width = 1024;
static GLuint win_height = 1024;

static GLuint fbo;
static GLuint renderbuffers[3];
static GLuint nrenderbuffers = 0;

GLuint generate_simple_fbo(unsigned width, unsigned height)
{
    GLuint fb;
    GLenum status;
    unsigned color, stencil, depth, packed;

    color = stencil = depth = packed = 1;

#if 0
    /* texture */
    glGenTextures(1, &gears_tex);
    glBindTexture(GL_TEXTURE_2D, gears_tex);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
#endif

    glGenFramebuffers(1, &fb);
    glBindFramebuffer(GL_FRAMEBUFFER, fb);


    if (color) {
#if 1
        glGenRenderbuffers(1, &renderbuffers[nrenderbuffers]);
        glBindRenderbuffer(GL_RENDERBUFFER, renderbuffers[nrenderbuffers]);
        glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, width, height);
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffers[nrenderbuffers]);
        nrenderbuffers++;
#else
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gears_tex, 0);
#endif
    }

    if (stencil) {
        const GLenum format = (packed)
            ? GL_DEPTH24_STENCIL8
            : GL_STENCIL_INDEX8;
        const GLenum attachment = (packed)
            ? GL_DEPTH_STENCIL_ATTACHMENT
            : GL_STENCIL_ATTACHMENT;

        glGenRenderbuffers(1, &renderbuffers[nrenderbuffers]);
        glBindRenderbuffer(GL_RENDERBUFFER, renderbuffers[nrenderbuffers]);
        glRenderbufferStorage(GL_RENDERBUFFER, format, width, height);
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, renderbuffers[nrenderbuffers]);
        nrenderbuffers++;
    }

    if (!packed && depth) {
        glGenRenderbuffers(1, &renderbuffers[nrenderbuffers]);
        glBindRenderbuffer(GL_RENDERBUFFER, renderbuffers[nrenderbuffers]);
        glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
        glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, renderbuffers[nrenderbuffers]);
        nrenderbuffers++;
    }

    status = glGetError();
    if (status != GL_NO_ERROR) {
        fprintf(stderr, "Unexpected GL error: 0x%x\n", status);
        exit(1);
    }

    /* All of the possible combinations that we can generate are required
     * to be supported by all OpenGL 3.0 implementations, with one
     * exception.  As far as I can tell, implementations are not required
     * to support separate depth and stencil.  That one option is handled
     * specially.
     */
    status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    if (status != GL_FRAMEBUFFER_COMPLETE
        && !(status == GL_FRAMEBUFFER_UNSUPPORTED && stencil && !packed)) {
        fprintf(stderr,
            "Framebuffer %s color, %s stencil (%s) was not "
            "complete: 0x%04x\n",
            color ? "with" : "without",
            stencil ? "with" : "without",
            packed ? "packed" : "separate",
            status);
        exit(1);
    }

    if (status == GL_FRAMEBUFFER_UNSUPPORTED) {
        glDeleteRenderbuffers(nrenderbuffers, renderbuffers);
        glDeleteFramebuffers(1, &fb);
        fprintf(stderr, "Unsupported framebuffer\n");
        exit(1);
    }
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

#if 0
    glClearColor(0.2, 0.4, 0.6, 1.0);
    glClearDepth(0.5);
    glClearStencil(0x7a);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    glFinish();
#endif
    return fb;
}

void vsimple_probe(void)
{
    GLfloat *pixels = malloc(256*256*sizeof(float));
    glReadPixels(0, 0, 256, 256, GL_DEPTH_COMPONENT, GL_FLOAT, pixels);
    fprintf(stderr, "probed %f\n", pixels[100]);
    free(pixels);
}

static void draw(void)
{
    static const float first[4]  = { 0.5, 1.0, 1.0, 1.0 };
    static const float second[4] = { 0.8, 0.0, 0.0, 0.0 };

#if PFINISH
    fprintf(stderr, "EE CLEAR 0.5 -------------------------------------------------\n");
#endif
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    glClearColor(0.2, 0.4, 0.6, 1.0);
    glClearDepth(0.5);
    glClearStencil(0x7a);
    glClearBufferfv(GL_DEPTH, 0, first);
#if PFINISH
    glFinish();
    fprintf(stderr, "EE CLEAR 0.5 _________________________________________________\n");
#endif

#if PFINISH
    fprintf(stderr, "EE CLEAR PROBE -----------------------------------------------\n");
#endif
    vsimple_probe();
#if PFINISH
    fprintf(stderr, "EE CLEAR PROBE _______________________________________________\n");
#endif

#if PFINISH
    fprintf(stderr, "EE CLEAR 0.8 -------------------------------------------------\n");
#endif
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    glClearColor(0.2, 0.4, 0.6, 1.0);
    glClearDepth(0.8);
    glClearStencil(0x7a);
    glClearBufferfv(GL_DEPTH, 0, second);
#if PFINISH
    glFinish();
    fprintf(stderr, "EE CLEAR 0.8 _________________________________________________\n");
#endif

#if PFINISH
    fprintf(stderr, "EE CLEAR PROBE -----------------------------------------------\n");
#endif
    vsimple_probe();
#if PFINISH
    fprintf(stderr, "EE CLEAR PROBE _______________________________________________\n");
#endif

    exit(0);
}

static void idle(void)
{
}

/* new window size or exposure */
static void reshape(int width, int height)
{
    GLfloat h = (GLfloat) height / (GLfloat) width;

    glViewport(0, 0, (GLint) width, (GLint) height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-1.0, 1.0, -h, h, 5.0, 60.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0, 0.0, -40.0);

    win_width = width;
    win_height = height;
}

static void init(void)
{
    fbo = generate_simple_fbo(512, 512);
}

static void key(unsigned char k, int x, int y)
{
  switch (k) {
  case 27:  /* Escape */
      exit(0);
      break;
  default:
      return;
  }
  glutPostRedisplay();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_STENCIL);
    glutInitWindowSize(win_width, win_height);
    glutCreateWindow("glgears");

    GLenum err = glewInit();
    if (GLEW_OK != err) {
        fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
        return -1;
    }

    glutKeyboardFunc(key);
    glutIdleFunc(idle);
    glutReshapeFunc(reshape);
    glutDisplayFunc(draw);

    init();
    reshape(win_width, win_height);
    glutMainLoop();
    return 0;
}