dungeon_crawler

view prototype/src/renderer.cc @ 21:0588f8a1a351

converting LIGHT meshes to lights
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 21 Aug 2012 06:33:36 +0300
parents 48cfd351a9be
children fa8f89d06f6f
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 4
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;
40 bool init_renderer(int xsz, int ysz)
41 {
42 if(!GLEW_ARB_draw_buffers) {
43 fprintf(stderr, "OpenGL implementation doesn't support multiple render targets\n");
44 return false;
45 }
47 int max_draw_buf;
48 glGetIntegerv(GL_MAX_DRAW_BUFFERS_ARB, &max_draw_buf);
49 printf("max draw buffers: %d\n", max_draw_buf);
50 if(max_draw_buf < MRT_COUNT) {
51 fprintf(stderr, "OpenGL implementation doesn't support enough draw buffers\n");
52 return false;
53 }
55 if(!create_fbo(xsz, ysz)) {
56 return false;
57 }
58 CHECKGLERR;
60 if(!(mrt_prog = load_sdr("mrt.v.glsl", "mrt.p.glsl"))) {
61 return false;
62 }
63 if(!(deferred_debug = load_sdr("deferred.v.glsl", "deferred.p.glsl"))) {
64 return false;
65 }
66 return true;
67 }
69 void destroy_renderer()
70 {
71 free_program(mrt_prog);
72 free_program(deferred_omni);
74 glDeleteTextures(MRT_COUNT, mrt_tex);
75 glDeleteFramebuffersEXT(1, &fbo);
76 }
78 void render_deferred(void (*draw_func)())
79 {
80 int loc;
82 // render into the MRT buffers
83 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
85 glUseProgram(mrt_prog);
86 if((loc = glGetUniformLocation(mrt_prog, "tex_dif")) != -1) {
87 glUniform1i(loc, 0);
88 }
89 if((loc = glGetUniformLocation(mrt_prog, "tex_norm")) != -1) {
90 glUniform1i(loc, 1);
91 }
92 draw_func();
94 glUseProgram(0);
95 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
98 // post-process lighting
99 glPushAttrib(GL_ENABLE_BIT);
101 glDisable(GL_LIGHTING);
102 glDisable(GL_DEPTH_TEST);
104 glUseProgram(deferred_debug);
105 for(int i=0; i<MRT_COUNT; i++) {
106 char uname[32];
108 sprintf(uname, "mrt%d", i);
109 if((loc = glGetUniformLocation(deferred_debug, uname)) != -1) {
110 glUniform1i(loc, i);
111 }
113 glActiveTexture(GL_TEXTURE0 + i);
114 glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
115 glEnable(GL_TEXTURE_2D);
116 }
117 if((loc = glGetUniformLocation(deferred_debug, "tex_scale")) != -1) {
118 glUniform2f(loc, (float)fb_xsz / tex_xsz, (float)fb_ysz / tex_ysz);
119 }
121 glBegin(GL_QUADS);
122 glTexCoord2f(0, 0);
123 glVertex2f(-1, -1);
124 glTexCoord2f(1, 0);
125 glVertex2f(1, -1);
126 glTexCoord2f(1, 1);
127 glVertex2f(1, 1);
128 glTexCoord2f(0, 1);
129 glVertex2f(-1, 1);
130 glEnd();
132 for(int i=0; i<MRT_COUNT; i++) {
133 glActiveTexture(GL_TEXTURE0 + MRT_COUNT - i - 1);
134 glDisable(GL_TEXTURE_2D);
135 }
137 glUseProgram(0);
138 glPopAttrib();
139 }
141 static bool create_fbo(int xsz, int ysz)
142 {
143 unsigned int clamp = GL_ARB_texture_border_clamp ? GL_CLAMP_TO_EDGE : GL_CLAMP;
145 tex_xsz = round_pow2(xsz);
146 tex_ysz = round_pow2(ysz);
147 fb_xsz = xsz;
148 fb_ysz = ysz;
150 if(!glGenFramebuffersEXT) {
151 fprintf(stderr, "FBO support missing\n");
152 return false;
153 }
154 glGenFramebuffersEXT(1, &fbo);
155 glBindFramebufferEXT(GL_FRAMEBUFFER, fbo);
157 glGenTextures(MRT_COUNT, mrt_tex);
158 for(int i=0; i<MRT_COUNT; i++) {
159 glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
160 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp);
161 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp);
162 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
163 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
164 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
166 // attach to fbo
167 glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i,
168 GL_TEXTURE_2D, mrt_tex[i], 0);
169 CHECKGLERR;
170 }
172 static GLenum draw_bufs[] = {
173 GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT,
174 GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT,
175 GL_COLOR_ATTACHMENT4_EXT, GL_COLOR_ATTACHMENT5_EXT,
176 GL_COLOR_ATTACHMENT6_EXT, GL_COLOR_ATTACHMENT7_EXT
177 };
178 glDrawBuffersARB(MRT_COUNT, draw_bufs);
180 glGenRenderbuffersEXT(1, &rbuf_depth);
181 glBindRenderbufferEXT(GL_RENDERBUFFER, rbuf_depth);
182 glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz);
183 glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth);
185 int fbst = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER);
186 if(fbst != GL_FRAMEBUFFER_COMPLETE) {
187 fprintf(stderr, "incomplete fbo: %u (%s)\n", fbo, fbstname[fbst - GL_FRAMEBUFFER_COMPLETE]);
188 return false;
189 }
190 CHECKGLERR;
192 glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
193 return true;
194 }
196 static unsigned int load_sdr(const char *vfname, const char *pfname)
197 {
198 char vsfile[PATH_MAX], psfile[PATH_MAX];
199 const char *fname;
200 unsigned int prog;
202 if((fname = datafile_path(vfname))) {
203 strcpy(vsfile, fname);
204 } else {
205 vsfile[0] = 0;
206 }
207 if((fname = datafile_path(pfname))) {
208 strcpy(psfile, fname);
209 } else {
210 psfile[0] = 0;
211 }
212 if(!(prog = create_program_load(vsfile, psfile))) {
213 fprintf(stderr, "failed to load shader program (%s, %s)\n", vsfile, psfile);
214 return 0;
215 }
216 return prog;
217 }
219 static int round_pow2(int x)
220 {
221 x--;
222 x = (x >> 1) | x;
223 x = (x >> 2) | x;
224 x = (x >> 4) | x;
225 x = (x >> 8) | x;
226 x = (x >> 16) | x;
227 return x + 1;
228 }