1 /*
2  * Copyright (C) 2013 Robert Kooima
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14 
15 #include <string.h>
16 
17 #include "fbo.h"
18 
19 /*---------------------------------------------------------------------------*/
20 
21 #if ENABLE_OPENGLES
22 
23 /* OpenGL ES support in Neverball is targeted toward OpenGL ES version 1.1.  */
24 /* This version of ES has no support for framebuffer objects.                */
25 
fbo_create(fbo * F,GLsizei w,GLsizei h)26 GLboolean fbo_create(fbo *F, GLsizei w, GLsizei h)
27 {
28     return GL_FALSE;
29 }
30 
fbo_delete(fbo * F)31 void fbo_delete(fbo *F)
32 {
33 }
34 
35 #else
36 
37 /*---------------------------------------------------------------------------*/
38 
fbo_create(fbo * F,GLsizei w,GLsizei h)39 GLboolean fbo_create(fbo *F, GLsizei w, GLsizei h)
40 {
41     if (gli.framebuffer_object == 0) return GL_FALSE;
42 
43     F->width  = w;
44     F->height = h;
45 
46     glGenTextures     (1, &F->color_texture);
47     glGenTextures     (1, &F->depth_texture);
48     glGenFramebuffers_(1, &F->framebuffer);
49 
50     glBindTexture  (GL_TEXTURE_2D, F->color_texture);
51     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
52     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
53     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
54     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
55     glTexImage2D   (GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0,
56                     GL_RGBA, GL_UNSIGNED_INT, NULL);
57 
58     glBindTexture  (GL_TEXTURE_2D, F->depth_texture);
59     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
60     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
61     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
62     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
63     glTexImage2D   (GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, w, h, 0,
64                     GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL);
65 
66     glBindFramebuffer_     (GL_FRAMEBUFFER, F->framebuffer);
67     glFramebufferTexture2D_(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
68                             GL_TEXTURE_2D, F->color_texture, 0);
69     glFramebufferTexture2D_(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT,
70                             GL_TEXTURE_2D, F->depth_texture, 0);
71     glFramebufferTexture2D_(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT,
72                             GL_TEXTURE_2D, F->depth_texture, 0);
73 
74     if (glCheckFramebufferStatus_(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE)
75     {
76         glBindFramebuffer_(GL_FRAMEBUFFER, 0);
77         return GL_TRUE;
78     }
79     else
80     {
81         glBindFramebuffer_(GL_FRAMEBUFFER, 0);
82         return GL_FALSE;
83     }
84 }
85 
fbo_delete(fbo * F)86 void fbo_delete(fbo *F)
87 {
88     if (gli.framebuffer_object == 0) return;
89 
90     if (F->color_texture) glDeleteTextures     (1, &F->color_texture);
91     if (F->depth_texture) glDeleteTextures     (1, &F->depth_texture);
92     if (F->framebuffer)   glDeleteFramebuffers_(1, &F->framebuffer);
93 
94     memset(F, 0, sizeof (fbo));
95 }
96 
97 #endif
98