dungeon_crawler

annotate prototype/src/renderer.cc @ 18:5c41e6fcb300

- commandline arguments - stereoscopic rendering - FBO fixed
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 21 Aug 2012 03:17:48 +0300
parents d98240a13793
children 8a0ae6b4aa9b
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@15 8 #include "sdr.h"
nuclear@15 9 #include "datapath.h"
nuclear@15 10
nuclear@18 11
nuclear@17 12 static bool create_fbo(int xsz, int ysz);
nuclear@18 13 static unsigned int load_sdr(const char *vfname, const char *pfname);
nuclear@17 14 static int round_pow2(int x);
nuclear@15 15
nuclear@17 16
nuclear@18 17 #define MRT_COUNT 1
nuclear@17 18 static unsigned int mrt_tex[MRT_COUNT];
nuclear@17 19
nuclear@17 20 static unsigned int mrt_prog;
nuclear@18 21 static unsigned int deferred_omni, deferred_debug;
nuclear@17 22
nuclear@17 23 static unsigned int fbo, rbuf_depth;
nuclear@17 24 static const char *fbstname[] = {
nuclear@17 25 "GL_FRAMEBUFFER_COMPLETE",
nuclear@17 26 "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT",
nuclear@17 27 "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT",
nuclear@17 28 "no such fbo error",
nuclear@17 29 "GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS",
nuclear@17 30 "GL_FRAMEBUFFER_INCOMPLETE_FORMATS",
nuclear@17 31 "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER",
nuclear@17 32 "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER",
nuclear@17 33 "GL_FRAMEBUFFER_UNSUPPORTED"
nuclear@15 34 };
nuclear@15 35
nuclear@17 36 static int fb_xsz, fb_ysz, tex_xsz, tex_ysz;
nuclear@15 37
nuclear@17 38 bool init_renderer(int xsz, int ysz)
nuclear@17 39 {
nuclear@17 40 if(!create_fbo(xsz, ysz)) {
nuclear@17 41 return false;
nuclear@17 42 }
nuclear@18 43
nuclear@18 44 if(!(mrt_prog = load_sdr("mrt.v.glsl", "mrt.p.glsl"))) {
nuclear@18 45 return false;
nuclear@18 46 }
nuclear@18 47 if(!(deferred_debug = load_sdr("deferred.v.glsl", "deferred.p.glsl"))) {
nuclear@17 48 return false;
nuclear@17 49 }
nuclear@17 50 return true;
nuclear@17 51 }
nuclear@17 52
nuclear@17 53 void destroy_renderer()
nuclear@17 54 {
nuclear@17 55 free_program(mrt_prog);
nuclear@17 56 free_program(deferred_omni);
nuclear@17 57
nuclear@17 58 glDeleteTextures(MRT_COUNT, mrt_tex);
nuclear@17 59 glDeleteFramebuffersEXT(1, &fbo);
nuclear@17 60 }
nuclear@17 61
nuclear@17 62 void render_deferred(void (*draw_func)())
nuclear@17 63 {
nuclear@18 64 int loc;
nuclear@18 65
nuclear@18 66 // render into the MRT buffers
nuclear@18 67 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
nuclear@18 68 glUseProgram(mrt_prog);
nuclear@17 69 draw_func();
nuclear@18 70 glUseProgram(0);
nuclear@18 71 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
nuclear@18 72
nuclear@18 73
nuclear@18 74 // post-process lighting
nuclear@18 75 glPushAttrib(GL_ENABLE_BIT);
nuclear@18 76
nuclear@18 77 glDisable(GL_LIGHTING);
nuclear@18 78 glDisable(GL_DEPTH_TEST);
nuclear@18 79 glBindTexture(GL_TEXTURE_2D, mrt_tex[0]);
nuclear@18 80 glEnable(GL_TEXTURE_2D);
nuclear@18 81
nuclear@18 82 glUseProgram(deferred_debug);
nuclear@18 83 if((loc = glGetUniformLocation(deferred_debug, "tex0")) != -1) {
nuclear@18 84 glUniform1i(loc, 0);
nuclear@18 85 }
nuclear@18 86 if((loc = glGetUniformLocation(deferred_debug, "tex_scale")) != -1) {
nuclear@18 87 glUniform2f(loc, (float)fb_xsz / tex_xsz, (float)fb_ysz / tex_ysz);
nuclear@18 88 }
nuclear@18 89
nuclear@18 90 glBegin(GL_QUADS);
nuclear@18 91 glTexCoord2f(0, 0);
nuclear@18 92 glVertex2f(-1, -1);
nuclear@18 93 glTexCoord2f(1, 0);
nuclear@18 94 glVertex2f(1, -1);
nuclear@18 95 glTexCoord2f(1, 1);
nuclear@18 96 glVertex2f(1, 1);
nuclear@18 97 glTexCoord2f(0, 1);
nuclear@18 98 glVertex2f(-1, 1);
nuclear@18 99 glEnd();
nuclear@18 100
nuclear@18 101 glUseProgram(0);
nuclear@18 102 glPopAttrib();
nuclear@17 103 }
nuclear@17 104
nuclear@17 105 static bool create_fbo(int xsz, int ysz)
nuclear@17 106 {
nuclear@17 107 unsigned int clamp = GL_ARB_texture_border_clamp ? GL_CLAMP_TO_EDGE : GL_CLAMP;
nuclear@17 108
nuclear@17 109 tex_xsz = round_pow2(xsz);
nuclear@17 110 tex_ysz = round_pow2(ysz);
nuclear@17 111 fb_xsz = xsz;
nuclear@17 112 fb_ysz = ysz;
nuclear@17 113
nuclear@17 114 if(!glGenFramebuffersEXT) {
nuclear@17 115 fprintf(stderr, "FBO support missing\n");
nuclear@17 116 return false;
nuclear@17 117 }
nuclear@17 118 glGenFramebuffersEXT(1, &fbo);
nuclear@18 119 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
nuclear@17 120
nuclear@17 121 glGenTextures(MRT_COUNT, mrt_tex);
nuclear@17 122 for(int i=0; i<MRT_COUNT; i++) {
nuclear@17 123 glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
nuclear@17 124 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp);
nuclear@17 125 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp);
nuclear@17 126 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@17 127 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@17 128 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
nuclear@17 129
nuclear@17 130 // attach to fbo
nuclear@17 131 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D,
nuclear@17 132 mrt_tex[i], 0);
nuclear@18 133 CHECKGLERR;
nuclear@17 134 }
nuclear@17 135
nuclear@18 136 glGenRenderbuffersEXT(1, &rbuf_depth);
nuclear@18 137 glBindRenderbufferEXT(GL_RENDERBUFFER, rbuf_depth);
nuclear@18 138 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz);
nuclear@18 139 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth);
nuclear@18 140
nuclear@17 141 int fbst = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER);
nuclear@17 142 if(fbst != GL_FRAMEBUFFER_COMPLETE) {
nuclear@17 143 fprintf(stderr, "incomplete fbo: %u (%s)\n", fbo, fbstname[fbst - GL_FRAMEBUFFER_COMPLETE]);
nuclear@17 144 return false;
nuclear@17 145 }
nuclear@18 146 CHECKGLERR;
nuclear@18 147
nuclear@18 148 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
nuclear@17 149 return true;
nuclear@17 150 }
nuclear@17 151
nuclear@18 152 static unsigned int load_sdr(const char *vfname, const char *pfname)
nuclear@15 153 {
nuclear@15 154 char vsfile[PATH_MAX], psfile[PATH_MAX];
nuclear@15 155 const char *fname;
nuclear@18 156 unsigned int prog;
nuclear@15 157
nuclear@18 158 if((fname = datafile_path(vfname))) {
nuclear@17 159 strcpy(vsfile, fname);
nuclear@17 160 } else {
nuclear@17 161 vsfile[0] = 0;
nuclear@17 162 }
nuclear@18 163 if((fname = datafile_path(pfname))) {
nuclear@17 164 strcpy(psfile, fname);
nuclear@17 165 } else {
nuclear@17 166 psfile[0] = 0;
nuclear@17 167 }
nuclear@18 168 if(!(prog = create_program_load(vsfile, psfile))) {
nuclear@18 169 fprintf(stderr, "failed to load shader program (%s, %s)\n", vsfile, psfile);
nuclear@18 170 return 0;
nuclear@15 171 }
nuclear@18 172 return prog;
nuclear@15 173 }
nuclear@15 174
nuclear@17 175 static int round_pow2(int x)
nuclear@15 176 {
nuclear@17 177 x--;
nuclear@17 178 x = (x >> 1) | x;
nuclear@17 179 x = (x >> 2) | x;
nuclear@17 180 x = (x >> 4) | x;
nuclear@17 181 x = (x >> 8) | x;
nuclear@17 182 x = (x >> 16) | x;
nuclear@17 183 return x + 1;
nuclear@15 184 }