rev |
line source |
nuclear@15
|
1 #include <stdio.h>
|
nuclear@15
|
2 #include <stdlib.h>
|
nuclear@15
|
3 #include <string.h>
|
nuclear@15
|
4 #include <limits.h>
|
nuclear@18
|
5 #include <assert.h>
|
nuclear@15
|
6 #include "opengl.h"
|
nuclear@15
|
7 #include "renderer.h"
|
nuclear@23
|
8 #include "level.h"
|
nuclear@15
|
9 #include "sdr.h"
|
nuclear@15
|
10 #include "datapath.h"
|
nuclear@15
|
11
|
nuclear@18
|
12
|
nuclear@17
|
13 static bool create_fbo(int xsz, int ysz);
|
nuclear@18
|
14 static unsigned int load_sdr(const char *vfname, const char *pfname);
|
nuclear@17
|
15 static int round_pow2(int x);
|
nuclear@15
|
16
|
nuclear@17
|
17
|
nuclear@19
|
18 #define MRT_COUNT 4
|
nuclear@17
|
19 static unsigned int mrt_tex[MRT_COUNT];
|
nuclear@17
|
20
|
nuclear@17
|
21 static unsigned int mrt_prog;
|
nuclear@18
|
22 static unsigned int deferred_omni, deferred_debug;
|
nuclear@17
|
23
|
nuclear@17
|
24 static unsigned int fbo, rbuf_depth;
|
nuclear@17
|
25 static const char *fbstname[] = {
|
nuclear@17
|
26 "GL_FRAMEBUFFER_COMPLETE",
|
nuclear@17
|
27 "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT",
|
nuclear@17
|
28 "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT",
|
nuclear@17
|
29 "no such fbo error",
|
nuclear@17
|
30 "GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS",
|
nuclear@17
|
31 "GL_FRAMEBUFFER_INCOMPLETE_FORMATS",
|
nuclear@17
|
32 "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER",
|
nuclear@17
|
33 "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER",
|
nuclear@17
|
34 "GL_FRAMEBUFFER_UNSUPPORTED"
|
nuclear@15
|
35 };
|
nuclear@15
|
36
|
nuclear@17
|
37 static int fb_xsz, fb_ysz, tex_xsz, tex_ysz;
|
nuclear@15
|
38
|
nuclear@21
|
39
|
nuclear@21
|
40
|
nuclear@17
|
41 bool init_renderer(int xsz, int ysz)
|
nuclear@17
|
42 {
|
nuclear@21
|
43 if(!GLEW_ARB_draw_buffers) {
|
nuclear@21
|
44 fprintf(stderr, "OpenGL implementation doesn't support multiple render targets\n");
|
nuclear@21
|
45 return false;
|
nuclear@21
|
46 }
|
nuclear@21
|
47
|
nuclear@19
|
48 int max_draw_buf;
|
nuclear@19
|
49 glGetIntegerv(GL_MAX_DRAW_BUFFERS_ARB, &max_draw_buf);
|
nuclear@19
|
50 printf("max draw buffers: %d\n", max_draw_buf);
|
nuclear@19
|
51 if(max_draw_buf < MRT_COUNT) {
|
nuclear@19
|
52 fprintf(stderr, "OpenGL implementation doesn't support enough draw buffers\n");
|
nuclear@19
|
53 return false;
|
nuclear@19
|
54 }
|
nuclear@19
|
55
|
nuclear@17
|
56 if(!create_fbo(xsz, ysz)) {
|
nuclear@17
|
57 return false;
|
nuclear@17
|
58 }
|
nuclear@19
|
59 CHECKGLERR;
|
nuclear@18
|
60
|
nuclear@18
|
61 if(!(mrt_prog = load_sdr("mrt.v.glsl", "mrt.p.glsl"))) {
|
nuclear@18
|
62 return false;
|
nuclear@18
|
63 }
|
nuclear@23
|
64 set_uniform_int(mrt_prog, "tex_dif", 0);
|
nuclear@23
|
65 set_uniform_int(mrt_prog, "tex_norm", 1);
|
nuclear@23
|
66
|
nuclear@18
|
67 if(!(deferred_debug = load_sdr("deferred.v.glsl", "deferred.p.glsl"))) {
|
nuclear@17
|
68 return false;
|
nuclear@17
|
69 }
|
nuclear@23
|
70 for(int i=0; i<MRT_COUNT; i++) {
|
nuclear@23
|
71 char uname[32];
|
nuclear@23
|
72 sprintf(uname, "mrt%d", i);
|
nuclear@23
|
73 set_uniform_int(deferred_debug, uname, i);
|
nuclear@23
|
74 }
|
nuclear@23
|
75
|
nuclear@23
|
76 if(!(deferred_omni = load_sdr("deferred.v.glsl", "deferred_omni.p.glsl"))) {
|
nuclear@23
|
77 return false;
|
nuclear@23
|
78 }
|
nuclear@23
|
79 for(int i=0; i<MRT_COUNT; i++) {
|
nuclear@23
|
80 char uname[32];
|
nuclear@23
|
81 sprintf(uname, "mrt%d", i);
|
nuclear@23
|
82 set_uniform_int(deferred_omni, uname, i);
|
nuclear@23
|
83 }
|
nuclear@17
|
84 return true;
|
nuclear@17
|
85 }
|
nuclear@17
|
86
|
nuclear@17
|
87 void destroy_renderer()
|
nuclear@17
|
88 {
|
nuclear@17
|
89 free_program(mrt_prog);
|
nuclear@17
|
90 free_program(deferred_omni);
|
nuclear@17
|
91
|
nuclear@17
|
92 glDeleteTextures(MRT_COUNT, mrt_tex);
|
nuclear@17
|
93 glDeleteFramebuffersEXT(1, &fbo);
|
nuclear@17
|
94 }
|
nuclear@17
|
95
|
nuclear@23
|
96 void resize_renderer(int xsz, int ysz)
|
nuclear@17
|
97 {
|
nuclear@23
|
98 fb_xsz = xsz;
|
nuclear@23
|
99 fb_ysz = ysz;
|
nuclear@18
|
100
|
nuclear@23
|
101 // if we need a bigger rendertarget resize all colorbuffer textures and the depth buffer
|
nuclear@23
|
102 if(xsz > tex_xsz || ysz > tex_ysz) {
|
nuclear@23
|
103 tex_xsz = round_pow2(xsz);
|
nuclear@23
|
104 tex_ysz = round_pow2(ysz);
|
nuclear@23
|
105
|
nuclear@23
|
106 for(int i=0; i<MRT_COUNT; i++) {
|
nuclear@23
|
107 glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
|
nuclear@23
|
108 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA,
|
nuclear@23
|
109 GL_UNSIGNED_BYTE, 0);
|
nuclear@23
|
110 }
|
nuclear@23
|
111
|
nuclear@23
|
112 glBindRenderbufferEXT(GL_RENDERBUFFER, rbuf_depth);
|
nuclear@23
|
113 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz);
|
nuclear@23
|
114 }
|
nuclear@23
|
115
|
nuclear@23
|
116
|
nuclear@23
|
117 // update the texture coordinate scaling factors
|
nuclear@23
|
118 float tex_scale_x = (float)fb_xsz / tex_xsz;
|
nuclear@23
|
119 float tex_scale_y = (float)fb_ysz / tex_ysz;
|
nuclear@23
|
120
|
nuclear@23
|
121 set_uniform_float2(deferred_omni, "tex_scale", tex_scale_x, tex_scale_y);
|
nuclear@23
|
122 set_uniform_float2(deferred_debug, "tex_scale", tex_scale_x, tex_scale_y);
|
nuclear@23
|
123 }
|
nuclear@23
|
124
|
nuclear@23
|
125 void render_deferred(const Level *level)
|
nuclear@23
|
126 {
|
nuclear@18
|
127 // render into the MRT buffers
|
nuclear@23
|
128 glUseProgram(mrt_prog);
|
nuclear@18
|
129 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
|
nuclear@19
|
130
|
nuclear@23
|
131 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@23
|
132 level->draw();
|
nuclear@20
|
133
|
nuclear@18
|
134 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
|
nuclear@18
|
135
|
nuclear@18
|
136
|
nuclear@18
|
137 // post-process lighting
|
nuclear@18
|
138 glPushAttrib(GL_ENABLE_BIT);
|
nuclear@18
|
139
|
nuclear@18
|
140 glDisable(GL_LIGHTING);
|
nuclear@18
|
141
|
nuclear@23
|
142 glUseProgram(deferred_omni);
|
nuclear@19
|
143 for(int i=0; i<MRT_COUNT; i++) {
|
nuclear@19
|
144 glActiveTexture(GL_TEXTURE0 + i);
|
nuclear@19
|
145 glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
|
nuclear@19
|
146 glEnable(GL_TEXTURE_2D);
|
nuclear@18
|
147 }
|
nuclear@18
|
148
|
nuclear@23
|
149 glDepthMask(0);
|
nuclear@23
|
150 level->draw_lights();
|
nuclear@23
|
151 glDepthMask(1);
|
nuclear@18
|
152
|
nuclear@19
|
153 for(int i=0; i<MRT_COUNT; i++) {
|
nuclear@19
|
154 glActiveTexture(GL_TEXTURE0 + MRT_COUNT - i - 1);
|
nuclear@19
|
155 glDisable(GL_TEXTURE_2D);
|
nuclear@19
|
156 }
|
nuclear@19
|
157
|
nuclear@18
|
158 glUseProgram(0);
|
nuclear@18
|
159 glPopAttrib();
|
nuclear@17
|
160 }
|
nuclear@17
|
161
|
nuclear@17
|
162 static bool create_fbo(int xsz, int ysz)
|
nuclear@17
|
163 {
|
nuclear@17
|
164 unsigned int clamp = GL_ARB_texture_border_clamp ? GL_CLAMP_TO_EDGE : GL_CLAMP;
|
nuclear@17
|
165
|
nuclear@17
|
166 tex_xsz = round_pow2(xsz);
|
nuclear@17
|
167 tex_ysz = round_pow2(ysz);
|
nuclear@17
|
168 fb_xsz = xsz;
|
nuclear@17
|
169 fb_ysz = ysz;
|
nuclear@17
|
170
|
nuclear@17
|
171 if(!glGenFramebuffersEXT) {
|
nuclear@17
|
172 fprintf(stderr, "FBO support missing\n");
|
nuclear@17
|
173 return false;
|
nuclear@17
|
174 }
|
nuclear@17
|
175 glGenFramebuffersEXT(1, &fbo);
|
nuclear@18
|
176 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
|
nuclear@17
|
177
|
nuclear@17
|
178 glGenTextures(MRT_COUNT, mrt_tex);
|
nuclear@17
|
179 for(int i=0; i<MRT_COUNT; i++) {
|
nuclear@17
|
180 glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
|
nuclear@17
|
181 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp);
|
nuclear@17
|
182 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp);
|
nuclear@17
|
183 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
nuclear@17
|
184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
nuclear@17
|
185 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
|
nuclear@17
|
186
|
nuclear@17
|
187 // attach to fbo
|
nuclear@20
|
188 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i,
|
nuclear@20
|
189 GL_TEXTURE_2D, mrt_tex[i], 0);
|
nuclear@18
|
190 CHECKGLERR;
|
nuclear@17
|
191 }
|
nuclear@17
|
192
|
nuclear@20
|
193 static GLenum draw_bufs[] = {
|
nuclear@20
|
194 GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT,
|
nuclear@20
|
195 GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT,
|
nuclear@20
|
196 GL_COLOR_ATTACHMENT4_EXT, GL_COLOR_ATTACHMENT5_EXT,
|
nuclear@20
|
197 GL_COLOR_ATTACHMENT6_EXT, GL_COLOR_ATTACHMENT7_EXT
|
nuclear@20
|
198 };
|
nuclear@20
|
199 glDrawBuffersARB(MRT_COUNT, draw_bufs);
|
nuclear@20
|
200
|
nuclear@18
|
201 glGenRenderbuffersEXT(1, &rbuf_depth);
|
nuclear@18
|
202 glBindRenderbufferEXT(GL_RENDERBUFFER, rbuf_depth);
|
nuclear@18
|
203 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz);
|
nuclear@18
|
204 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth);
|
nuclear@18
|
205
|
nuclear@17
|
206 int fbst = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER);
|
nuclear@17
|
207 if(fbst != GL_FRAMEBUFFER_COMPLETE) {
|
nuclear@17
|
208 fprintf(stderr, "incomplete fbo: %u (%s)\n", fbo, fbstname[fbst - GL_FRAMEBUFFER_COMPLETE]);
|
nuclear@17
|
209 return false;
|
nuclear@17
|
210 }
|
nuclear@18
|
211 CHECKGLERR;
|
nuclear@18
|
212
|
nuclear@18
|
213 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
|
nuclear@17
|
214 return true;
|
nuclear@17
|
215 }
|
nuclear@17
|
216
|
nuclear@18
|
217 static unsigned int load_sdr(const char *vfname, const char *pfname)
|
nuclear@15
|
218 {
|
nuclear@15
|
219 char vsfile[PATH_MAX], psfile[PATH_MAX];
|
nuclear@15
|
220 const char *fname;
|
nuclear@18
|
221 unsigned int prog;
|
nuclear@15
|
222
|
nuclear@18
|
223 if((fname = datafile_path(vfname))) {
|
nuclear@17
|
224 strcpy(vsfile, fname);
|
nuclear@17
|
225 } else {
|
nuclear@17
|
226 vsfile[0] = 0;
|
nuclear@17
|
227 }
|
nuclear@18
|
228 if((fname = datafile_path(pfname))) {
|
nuclear@17
|
229 strcpy(psfile, fname);
|
nuclear@17
|
230 } else {
|
nuclear@17
|
231 psfile[0] = 0;
|
nuclear@17
|
232 }
|
nuclear@18
|
233 if(!(prog = create_program_load(vsfile, psfile))) {
|
nuclear@18
|
234 fprintf(stderr, "failed to load shader program (%s, %s)\n", vsfile, psfile);
|
nuclear@18
|
235 return 0;
|
nuclear@15
|
236 }
|
nuclear@18
|
237 return prog;
|
nuclear@15
|
238 }
|
nuclear@15
|
239
|
nuclear@17
|
240 static int round_pow2(int x)
|
nuclear@15
|
241 {
|
nuclear@17
|
242 x--;
|
nuclear@17
|
243 x = (x >> 1) | x;
|
nuclear@17
|
244 x = (x >> 2) | x;
|
nuclear@17
|
245 x = (x >> 4) | x;
|
nuclear@17
|
246 x = (x >> 8) | x;
|
nuclear@17
|
247 x = (x >> 16) | x;
|
nuclear@17
|
248 return x + 1;
|
nuclear@15
|
249 }
|