dungeon_crawler

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