dungeon_crawler
view prototype/src/renderer.cc @ 72:a27528035e20
- re-organized the renderer classes a bit wrt final render-target
- implemented identity color-grading palette for now
- broke particle systems....
- removed multipass renderer
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 19 Oct 2012 02:45:57 +0300 |
parents | 7f52d6310317 |
children | 67d330038629 |
line source
1 #include <limits.h>
2 #include "opengl.h"
3 #include "renderer.h"
4 #include "level.h"
5 #include "sdr.h"
6 #include "datapath.h"
8 static unsigned int load_sdr(const char *vfname, const char *pfname);
9 static int round_pow2(int x);
11 Renderer *rend;
14 Renderer::Renderer()
15 {
16 fbo = 0;
17 rend_tex = rend_depth = 0;
18 width = height = 0;
19 tex_xsz = tex_ysz = 0;
20 post_sdr = 0;
21 }
23 Renderer::~Renderer()
24 {
25 if(post_sdr) {
26 free_program(post_sdr);
27 }
28 if(rend_tex) {
29 glDeleteTextures(1, &rend_tex);
30 }
31 if(rend_depth) {
32 glDeleteRenderbuffersEXT(1, &rend_depth);
33 }
34 if(fbo) {
35 glDeleteFramebuffersEXT(1, &fbo);
36 }
37 }
39 bool Renderer::init(int xsz, int ysz)
40 {
41 width = xsz;
42 height = ysz;
44 if(!create_rtarg()) {
45 return false;
46 }
48 if(!(post_sdr = load_sdr("post.v.glsl", "post.p.glsl"))) {
49 return false;
50 }
52 if(!gradepal.create()) {
53 return false;
54 }
56 rend = this;
57 return true;
58 }
60 int Renderer::get_tangent_location() const
61 {
62 return -1;
63 }
65 unsigned int Renderer::get_current_program() const
66 {
67 return 0;
68 }
70 void Renderer::resize(int xsz, int ysz)
71 {
72 width = xsz;
73 height = ysz;
75 // if we need a bigger rendertarget ...
76 if(xsz > tex_xsz || ysz > tex_ysz) {
77 tex_xsz = round_pow2(xsz);
78 tex_ysz = round_pow2(ysz);
80 // ... resize the render target
81 glBindTexture(GL_TEXTURE_2D, rend_tex);
82 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
84 // ... resize the depth buffer
85 glBindRenderbufferEXT(GL_RENDERBUFFER, rend_depth);
86 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz);
87 }
88 }
90 void Renderer::render_pre(const Level *level) const
91 {
92 glEnable(GL_FOG);
93 glFogi(GL_FOG_MODE, GL_LINEAR);
94 glFogf(GL_FOG_START, 3.0f);
95 glFogf(GL_FOG_END, 6.0f);
96 // TODO level->draw_pre();
98 // bind the render target
99 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
100 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rend_tex, 0);
101 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
103 CHECKGLERR;
104 }
106 void Renderer::render_post(const Level *level) const
107 {
108 level->draw_post();
109 CHECKGLERR;
111 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
113 // draw the rendered output
114 glActiveTextureARB(GL_TEXTURE0);
115 glBindTexture(GL_TEXTURE_2D, rend_tex);
116 glEnable(GL_TEXTURE_2D);
118 // use the color-grading palette
119 glActiveTextureARB(GL_TEXTURE1);
120 glBindTexture(GL_TEXTURE_3D, gradepal.get_texture());
121 glEnable(GL_TEXTURE_3D);
123 glUseProgram(post_sdr);
124 set_uniform_int(post_sdr, "fbtex", 0);
125 set_uniform_int(post_sdr, "paltex", 1);
127 glBegin(GL_QUADS);
128 glTexCoord2f(0, 0);
129 glVertex2f(-1, -1);
130 glTexCoord2f((float)width / tex_xsz, 0);
131 glVertex2f(1, -1);
132 glTexCoord2f((float)width / tex_xsz, (float)height / tex_ysz);
133 glVertex2f(1, 1);
134 glTexCoord2f(0, (float)height / tex_ysz);
135 glVertex2f(-1, 1);
136 glEnd();
137 glUseProgram(0);
139 glActiveTextureARB(GL_TEXTURE0);
141 CHECKGLERR;
142 }
144 static const char *fbstname[] = {
145 "GL_FRAMEBUFFER_COMPLETE",
146 "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT",
147 "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT",
148 "no such fbo error",
149 "GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS",
150 "GL_FRAMEBUFFER_INCOMPLETE_FORMATS",
151 "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER",
152 "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER",
153 "GL_FRAMEBUFFER_UNSUPPORTED"
154 };
156 bool Renderer::create_rtarg()
157 {
158 unsigned int clamp = GLEW_ARB_texture_border_clamp ? GL_CLAMP_TO_EDGE : GL_CLAMP;
160 tex_xsz = round_pow2(width);
161 tex_ysz = round_pow2(height);
163 if(!glGenFramebuffersEXT) {
164 fprintf(stderr, "FBO support missing!\n");
165 return false;
166 }
167 glGenFramebuffersEXT(1, &fbo);
168 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
170 // create the render target
171 glGenTextures(1, &rend_tex);
172 glBindTexture(GL_TEXTURE_2D, rend_tex);
173 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp);
174 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp);
175 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
176 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
177 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
179 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rend_tex, 0);
181 // create depth buffer
182 glGenRenderbuffersEXT(1, &rend_depth);
183 glBindRenderbufferEXT(GL_RENDERBUFFER, rend_depth);
184 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz);
186 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rend_depth);
188 int fbst = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER);
189 if(fbst != GL_FRAMEBUFFER_COMPLETE) {
190 fprintf(stderr, "incomplete fbo: %u (%s)\n", fbo, fbstname[fbst - GL_FRAMEBUFFER_COMPLETE]);
191 return false;
192 }
193 CHECKGLERR;
195 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
196 return true;
197 }
200 // ---- fallback forward renderer ----
201 FwdRenderer::FwdRenderer()
202 {
203 sdrprog = 0;
204 tang_attr = -1;
205 }
207 FwdRenderer::~FwdRenderer()
208 {
209 if(sdrprog) {
210 free_program(sdrprog);
211 }
212 }
214 bool FwdRenderer::init(int xsz, int ysz)
215 {
216 if(!Renderer::init(xsz, ysz)) {
217 return false;
218 }
220 if(glUseProgram && (sdrprog = load_sdr("fallback.v.glsl", "fallback.p.glsl"))) {
221 tang_attr = get_attrib_loc(sdrprog, "attr_tangent");
222 set_uniform_int(sdrprog, "tex_dif", 0);
223 set_uniform_int(sdrprog, "tex_norm", 1);
224 }
225 return true;
226 }
228 int FwdRenderer::get_tangent_location() const
229 {
230 return tang_attr;
231 }
233 unsigned int FwdRenderer::get_current_program() const
234 {
235 return sdrprog;
236 }
238 void FwdRenderer::render(const Level *level) const
239 {
240 glPushAttrib(GL_ENABLE_BIT);
241 glEnable(GL_LIGHTING);
243 render_pre(level);
245 glUseProgram(sdrprog);
246 level->draw();
247 glUseProgram(0);
249 render_post(level);
251 glPopAttrib();
252 }
254 static unsigned int load_sdr(const char *vfname, const char *pfname)
255 {
256 unsigned int prog;
258 std::string vsfile = datafile_path(vfname);
259 std::string psfile = datafile_path(pfname);
261 const char *vs = vsfile.empty() ? 0 : vsfile.c_str();
262 const char *ps = psfile.empty() ? 0 : psfile.c_str();
264 if(!(prog = create_program_load(vs, ps))) {
265 fprintf(stderr, "failed to load shader program (%s, %s)\n", vs, ps);
266 return 0;
267 }
268 return prog;
269 }
271 static int round_pow2(int x)
272 {
273 x--;
274 x = (x >> 1) | x;
275 x = (x >> 2) | x;
276 x = (x >> 4) | x;
277 x = (x >> 8) | x;
278 x = (x >> 16) | x;
279 return x + 1;
280 }