dungeon_crawler

view prototype/src/renderer.cc @ 20:48cfd351a9be

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