dungeon_crawler

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