1 /************************************************************************
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 ************************************************************************/
25 framebuffer_t
*buffers
;
32 /* create a texture attachment for a fbo */
33 static int fbo_create_texture(int w
, int h
, fbo_buffer_t
*b
, uint8_t d
)
39 glGenTextures(1,&t
->glid
);
40 glBindTexture(GL_TEXTURE_2D
, t
->glid
);
42 glTexImage2D(GL_TEXTURE_2D
, 0, GL_DEPTH_COMPONENT32
, w
, h
, 0, GL_DEPTH_COMPONENT
, GL_FLOAT
, NULL
);
44 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGBA
, w
, h
, 0, GL_RGB
, GL_UNSIGNED_BYTE
, NULL
);
47 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
48 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
51 glFramebufferTexture(GL_FRAMEBUFFER
, GL_DEPTH_ATTACHMENT
, t
->glid
, 0);
53 glFramebufferTexture(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, t
->glid
, 0);
63 /* create a buffer attachment for a fbo */
64 static int fbo_create_buffer(int w
, int h
, fbo_buffer_t
*b
)
66 glGenRenderbuffers(1,&b
->glid
);
68 glBindRenderbuffer(GL_RENDERBUFFER
, b
->glid
);
69 glRenderbufferStorage(GL_RENDERBUFFER
, GL_DEPTH_COMPONENT
, w
, h
);
70 glFramebufferRenderbuffer(GL_FRAMEBUFFER
, GL_DEPTH_ATTACHMENT
, GL_RENDERBUFFER
, b
->glid
);
75 /* create a frame buffer object of width and height, with colour and depth buffers optionally bound to textures */
76 framebuffer_t
*fbo_create(int w
, int h
, uint8_t c_texture
, uint8_t d_texture
)
80 f
= malloc(sizeof(framebuffer_t
));
86 f
->id
= ++fbo_data
.ids
;
90 glGenFramebuffers(1,&f
->glid
);
92 glBindFramebuffer(GL_FRAMEBUFFER
, f
->glid
);
95 glDrawBuffer(GL_COLOR_ATTACHMENT0
);
96 fbo_create_texture(w
,h
,&f
->colour
,0);
98 glDrawBuffer(GL_NONE
);
102 fbo_create_texture(w
,h
,&f
->depth
,1);
104 fbo_create_buffer(w
,h
,&f
->depth
);
107 glBindFramebuffer(GL_FRAMEBUFFER
, 0);
109 fbo_data
.buffers
= list_append(&fbo_data
.buffers
,f
);
114 /* free a framebuffer */
115 void fbo_free(framebuffer_t
*f
)
120 /* get a framebuffer by id */
121 framebuffer_t
*fbo_get(int id
)
123 framebuffer_t
*f
= fbo_data
.buffers
;
134 /* use/bind a framebuffer */
135 void fbo_use(framebuffer_t
*f
)
137 glBindTexture(GL_TEXTURE_2D
, 0);
138 glBindFramebuffer(GL_FRAMEBUFFER
, f
->glid
);
139 glViewport(0, 0, f
->w
, f
->h
);
140 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
141 glEnable(GL_DEPTH_TEST
);
144 /* unbind framebuffers */
145 void fbo_unbind_all()
147 glBindFramebuffer(GL_FRAMEBUFFER
, 0);
148 glViewport(0, 0, wm_data
.size
.width
, wm_data
.size
.height
);