dungeon_crawler
view prototype/src/renderer.cc @ 73:67d330038629
ok fixed the dissapearing particle system. Had forgotten to disable TEXTURE_3D after color grading pass
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 21 Oct 2012 02:11:23 +0300 |
parents | a27528035e20 |
children | 5981917093ff |
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_TEXTURE1);
140 glDisable(GL_TEXTURE_3D);
141 glActiveTextureARB(GL_TEXTURE0);
142 glDisable(GL_TEXTURE_2D);
144 CHECKGLERR;
145 }
147 static const char *fbstname[] = {
148 "GL_FRAMEBUFFER_COMPLETE",
149 "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT",
150 "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT",
151 "no such fbo error",
152 "GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS",
153 "GL_FRAMEBUFFER_INCOMPLETE_FORMATS",
154 "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER",
155 "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER",
156 "GL_FRAMEBUFFER_UNSUPPORTED"
157 };
159 bool Renderer::create_rtarg()
160 {
161 unsigned int clamp = GLEW_ARB_texture_border_clamp ? GL_CLAMP_TO_EDGE : GL_CLAMP;
163 tex_xsz = round_pow2(width);
164 tex_ysz = round_pow2(height);
166 if(!glGenFramebuffersEXT) {
167 fprintf(stderr, "FBO support missing!\n");
168 return false;
169 }
170 glGenFramebuffersEXT(1, &fbo);
171 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
173 // create the render target
174 glGenTextures(1, &rend_tex);
175 glBindTexture(GL_TEXTURE_2D, rend_tex);
176 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp);
177 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp);
178 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
179 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
180 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
182 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rend_tex, 0);
184 // create depth buffer
185 glGenRenderbuffersEXT(1, &rend_depth);
186 glBindRenderbufferEXT(GL_RENDERBUFFER, rend_depth);
187 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz);
189 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rend_depth);
191 int fbst = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER);
192 if(fbst != GL_FRAMEBUFFER_COMPLETE) {
193 fprintf(stderr, "incomplete fbo: %u (%s)\n", fbo, fbstname[fbst - GL_FRAMEBUFFER_COMPLETE]);
194 return false;
195 }
196 CHECKGLERR;
198 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
199 return true;
200 }
203 // ---- fallback forward renderer ----
204 FwdRenderer::FwdRenderer()
205 {
206 sdrprog = 0;
207 tang_attr = -1;
208 }
210 FwdRenderer::~FwdRenderer()
211 {
212 if(sdrprog) {
213 free_program(sdrprog);
214 }
215 }
217 bool FwdRenderer::init(int xsz, int ysz)
218 {
219 if(!Renderer::init(xsz, ysz)) {
220 return false;
221 }
223 if(glUseProgram && (sdrprog = load_sdr("fallback.v.glsl", "fallback.p.glsl"))) {
224 tang_attr = get_attrib_loc(sdrprog, "attr_tangent");
225 set_uniform_int(sdrprog, "tex_dif", 0);
226 set_uniform_int(sdrprog, "tex_norm", 1);
227 }
228 return true;
229 }
231 int FwdRenderer::get_tangent_location() const
232 {
233 return tang_attr;
234 }
236 unsigned int FwdRenderer::get_current_program() const
237 {
238 return sdrprog;
239 }
241 void FwdRenderer::render(const Level *level) const
242 {
243 glPushAttrib(GL_ENABLE_BIT);
244 glEnable(GL_LIGHTING);
246 render_pre(level);
248 glUseProgram(sdrprog);
249 level->draw();
250 glUseProgram(0);
252 render_post(level);
254 glPopAttrib();
255 }
257 static unsigned int load_sdr(const char *vfname, const char *pfname)
258 {
259 unsigned int prog;
261 std::string vsfile = datafile_path(vfname);
262 std::string psfile = datafile_path(pfname);
264 const char *vs = vsfile.empty() ? 0 : vsfile.c_str();
265 const char *ps = psfile.empty() ? 0 : psfile.c_str();
267 if(!(prog = create_program_load(vs, ps))) {
268 fprintf(stderr, "failed to load shader program (%s, %s)\n", vs, ps);
269 return 0;
270 }
271 return prog;
272 }
274 static int round_pow2(int x)
275 {
276 x--;
277 x = (x >> 1) | x;
278 x = (x >> 2) | x;
279 x = (x >> 4) | x;
280 x = (x >> 8) | x;
281 x = (x >> 16) | x;
282 return x + 1;
283 }