dungeon_crawler
changeset 17:d98240a13793
lalala
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 20 Aug 2012 06:11:58 +0300 |
parents | 91180ee7b7d9 |
children | 5c41e6fcb300 |
files | prototype/Makefile prototype/sdr/deferred.p.glsl prototype/sdr/mrt.p.glsl prototype/sdr/mrt0.p.glsl prototype/src/main.cc prototype/src/renderer.cc prototype/src/renderer.h prototype/src/sdr.c |
diffstat | 8 files changed, 141 insertions(+), 54 deletions(-) [+] |
line diff
1.1 --- a/prototype/Makefile Mon Aug 20 03:40:51 2012 +0300 1.2 +++ b/prototype/Makefile Mon Aug 20 06:11:58 2012 +0300 1.3 @@ -4,7 +4,11 @@ 1.4 dep = $(obj:.o=.d) 1.5 bin = proto 1.6 1.7 -CFLAGS = -pedantic -Wall -g -Ivmath 1.8 +#opt = -O3 1.9 +dbg = -g 1.10 +warn = -Wall -Wno-format-extra-args -Wno-char-subscripts 1.11 + 1.12 +CFLAGS = -pedantic $(warn) $(dbg) $(opt) -Ivmath 1.13 CXXFLAGS = $(CFLAGS) -std=c++11 1.14 LDFLAGS = $(libgl) -lm -lassimpD -limago 1.15
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/prototype/sdr/deferred.p.glsl Mon Aug 20 06:11:58 2012 +0300 2.3 @@ -0,0 +1,4 @@ 2.4 +void main() 2.5 +{ 2.6 + gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); 2.7 +}
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/prototype/sdr/mrt.p.glsl Mon Aug 20 06:11:58 2012 +0300 3.3 @@ -0,0 +1,7 @@ 3.4 +uniform sampler2D tex_dif; 3.5 + 3.6 +void main() 3.7 +{ 3.8 + vec4 texel = texture2D(tex_dif, gl_TexCoord[0].st); 3.9 + gl_FragColor = texel; 3.10 +}
4.1 --- a/prototype/sdr/mrt0.p.glsl Mon Aug 20 03:40:51 2012 +0300 4.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 4.3 @@ -1,7 +0,0 @@ 4.4 -uniform sampler2D tex_dif; 4.5 - 4.6 -void main() 4.7 -{ 4.8 - vec4 texel = texture2D(tex_dif, gl_TexCoord[0].st); 4.9 - gl_FragColor = texel; 4.10 -}
5.1 --- a/prototype/src/main.cc Mon Aug 20 03:40:51 2012 +0300 5.2 +++ b/prototype/src/main.cc Mon Aug 20 06:11:58 2012 +0300 5.3 @@ -9,7 +9,7 @@ 5.4 #include "tileset.h" 5.5 #include "renderer.h" 5.6 5.7 -bool init(); 5.8 +bool init(int xsz, int ysz); 5.9 void cleanup(); 5.10 void idle(); 5.11 void disp(); 5.12 @@ -31,6 +31,8 @@ 5.13 5.14 int main(int argc, char **argv) 5.15 { 5.16 + int xsz, ysz; 5.17 + 5.18 glutInit(&argc, argv); 5.19 5.20 if(argc > 1) { 5.21 @@ -40,6 +42,8 @@ 5.22 glutInitWindowSize(800, 600); 5.23 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE); 5.24 glutCreateWindow("prototype"); 5.25 + xsz = glutGet(GLUT_WINDOW_WIDTH); 5.26 + ysz = glutGet(GLUT_WINDOW_HEIGHT); 5.27 5.28 glutIdleFunc(idle); 5.29 glutDisplayFunc(disp); 5.30 @@ -51,14 +55,14 @@ 5.31 5.32 glewInit(); 5.33 5.34 - if(!init()) { 5.35 + if(!init(xsz, ysz)) { 5.36 return 1; 5.37 } 5.38 5.39 glutMainLoop(); 5.40 } 5.41 5.42 -bool init() 5.43 +bool init(int xsz, int ysz) 5.44 { 5.45 glEnable(GL_LIGHTING); 5.46 glEnable(GL_LIGHT0); 5.47 @@ -73,7 +77,7 @@ 5.48 add_data_path("data"); 5.49 add_data_path("sdr"); 5.50 5.51 - if(!init_renderer()) { 5.52 + if(!init_renderer(xsz, ysz)) { 5.53 return false; 5.54 } 5.55
6.1 --- a/prototype/src/renderer.cc Mon Aug 20 03:40:51 2012 +0300 6.2 +++ b/prototype/src/renderer.cc Mon Aug 20 06:11:58 2012 +0300 6.3 @@ -7,40 +7,119 @@ 6.4 #include "sdr.h" 6.5 #include "datapath.h" 6.6 6.7 -#define MRT_COUNT (sizeof mrt_sdr_file / sizeof *mrt_sdr_file) 6.8 +static bool create_fbo(int xsz, int ysz); 6.9 +static bool load_shaders(); 6.10 +static int round_pow2(int x); 6.11 6.12 -static const char *mrt_sdr_file[][2] = { 6.13 - {0, "mrt0.p.glsl"}/*, 6.14 - {0, "mrt1.p.glsl"}, 6.15 - {0, "mrt2.p.glsl"}, 6.16 - {0, "mrt3.p.glsl"}*/ 6.17 + 6.18 +#define MRT_COUNT 4 6.19 +static unsigned int mrt_tex[MRT_COUNT]; 6.20 + 6.21 +static unsigned int mrt_prog; 6.22 +static unsigned int deferred_omni; 6.23 + 6.24 +static unsigned int fbo, rbuf_depth; 6.25 +static const char *fbstname[] = { 6.26 + "GL_FRAMEBUFFER_COMPLETE", 6.27 + "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT", 6.28 + "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT", 6.29 + "no such fbo error", 6.30 + "GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS", 6.31 + "GL_FRAMEBUFFER_INCOMPLETE_FORMATS", 6.32 + "GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER", 6.33 + "GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER", 6.34 + "GL_FRAMEBUFFER_UNSUPPORTED" 6.35 }; 6.36 6.37 -static unsigned int mrt_prog[MRT_COUNT]; 6.38 -static unsigned int deferred_prog; 6.39 +static int fb_xsz, fb_ysz, tex_xsz, tex_ysz; 6.40 6.41 -bool init_renderer() 6.42 +bool init_renderer(int xsz, int ysz) 6.43 +{ 6.44 + if(!create_fbo(xsz, ysz)) { 6.45 + return false; 6.46 + } 6.47 + if(!load_shaders()) { 6.48 + return false; 6.49 + } 6.50 + return true; 6.51 +} 6.52 + 6.53 +void destroy_renderer() 6.54 +{ 6.55 + free_program(mrt_prog); 6.56 + free_program(deferred_omni); 6.57 + 6.58 + glDeleteTextures(MRT_COUNT, mrt_tex); 6.59 + glDeleteFramebuffersEXT(1, &fbo); 6.60 +} 6.61 + 6.62 +void render_deferred(void (*draw_func)()) 6.63 +{ 6.64 + bind_program(mrt_prog); 6.65 + draw_func(); 6.66 + bind_program(0); 6.67 +} 6.68 + 6.69 +static bool create_fbo(int xsz, int ysz) 6.70 +{ 6.71 + unsigned int clamp = GL_ARB_texture_border_clamp ? GL_CLAMP_TO_EDGE : GL_CLAMP; 6.72 + 6.73 + tex_xsz = round_pow2(xsz); 6.74 + tex_ysz = round_pow2(ysz); 6.75 + fb_xsz = xsz; 6.76 + fb_ysz = ysz; 6.77 + 6.78 + if(!glGenFramebuffersEXT) { 6.79 + fprintf(stderr, "FBO support missing\n"); 6.80 + return false; 6.81 + } 6.82 + glGenFramebuffersEXT(1, &fbo); 6.83 + 6.84 + glGenRenderbuffersEXT(1, &rbuf_depth); 6.85 + glBindRenderbufferEXT(GL_RENDERBUFFER, rbuf_depth); 6.86 + glRenderbufferStorageEXT(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_xsz, tex_ysz); 6.87 + glFramebufferRenderbufferEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbuf_depth); 6.88 + 6.89 + glGenTextures(MRT_COUNT, mrt_tex); 6.90 + for(int i=0; i<MRT_COUNT; i++) { 6.91 + glBindTexture(GL_TEXTURE_2D, mrt_tex[i]); 6.92 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, clamp); 6.93 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, clamp); 6.94 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 6.95 + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 6.96 + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_xsz, tex_ysz, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); 6.97 + 6.98 + // attach to fbo 6.99 + glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, 6.100 + mrt_tex[i], 0); 6.101 + } 6.102 + 6.103 + int fbst = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER); 6.104 + if(fbst != GL_FRAMEBUFFER_COMPLETE) { 6.105 + fprintf(stderr, "incomplete fbo: %u (%s)\n", fbo, fbstname[fbst - GL_FRAMEBUFFER_COMPLETE]); 6.106 + return false; 6.107 + } 6.108 + return true; 6.109 +} 6.110 + 6.111 +static bool load_shaders() 6.112 { 6.113 char vsfile[PATH_MAX], psfile[PATH_MAX]; 6.114 const char *fname; 6.115 6.116 - for(int i=0; i<MRT_COUNT; i++) { 6.117 - if((fname = datafile_path(mrt_sdr_file[i][0]))) { 6.118 - strcpy(vsfile, mrt_sdr_file[i][0]); 6.119 - } else { 6.120 - vsfile[0] = 0; 6.121 - } 6.122 - 6.123 - if((fname = datafile_path(mrt_sdr_file[i][1]))) { 6.124 - strcpy(psfile, mrt_sdr_file[i][1]); 6.125 - } else { 6.126 - psfile[0] = 0; 6.127 - } 6.128 - 6.129 - if(!(mrt_prog[i] = create_program_load(vsfile, psfile))) { 6.130 - fprintf(stderr, "failed to load MRT program\n"); 6.131 - return false; 6.132 - } 6.133 + if((fname = datafile_path("mrt.v.glsl"))) { 6.134 + strcpy(vsfile, fname); 6.135 + } else { 6.136 + vsfile[0] = 0; 6.137 + } 6.138 + if((fname = datafile_path("mrt.p.glsl"))) { 6.139 + strcpy(psfile, fname); 6.140 + } else { 6.141 + psfile[0] = 0; 6.142 + } 6.143 + if(!(mrt_prog = create_program_load(vsfile, psfile))) { 6.144 + fprintf(stderr, "failed to load MRT program\n"); 6.145 + return false; 6.146 } 6.147 6.148 if((fname = datafile_path("deferred.v.glsl"))) { 6.149 @@ -54,24 +133,20 @@ 6.150 psfile[0] = 0; 6.151 } 6.152 6.153 - if(!(deferred_prog = create_program_load(vsfile, psfile))) { 6.154 + if(!(deferred_omni = create_program_load(vsfile, psfile))) { 6.155 fprintf(stderr, "failed to load deferred shader program\n"); 6.156 return false; 6.157 } 6.158 return true; 6.159 } 6.160 6.161 -void destroy_renderer() 6.162 +static int round_pow2(int x) 6.163 { 6.164 - for(int i=0; i<MRT_COUNT; i++) { 6.165 - free_program(mrt_prog[i]); 6.166 - } 6.167 - free_program(deferred_prog); 6.168 + x--; 6.169 + x = (x >> 1) | x; 6.170 + x = (x >> 2) | x; 6.171 + x = (x >> 4) | x; 6.172 + x = (x >> 8) | x; 6.173 + x = (x >> 16) | x; 6.174 + return x + 1; 6.175 } 6.176 - 6.177 -void render_deferred(void (*draw_func)()) 6.178 -{ 6.179 - 6.180 - 6.181 - draw_func(); 6.182 -}
7.1 --- a/prototype/src/renderer.h Mon Aug 20 03:40:51 2012 +0300 7.2 +++ b/prototype/src/renderer.h Mon Aug 20 06:11:58 2012 +0300 7.3 @@ -1,7 +1,7 @@ 7.4 #ifndef RENDERER_H_ 7.5 #define RENDERER_H_ 7.6 7.7 -bool init_renderer(); 7.8 +bool init_renderer(int xsz, int ysz); 7.9 void destroy_renderer(); 7.10 7.11 void render_deferred(void (*draw_func)());
8.1 --- a/prototype/src/sdr.c Mon Aug 20 03:40:51 2012 +0300 8.2 +++ b/prototype/src/sdr.c Mon Aug 20 06:11:58 2012 +0300 8.3 @@ -173,10 +173,10 @@ 8.4 { 8.5 unsigned int vs = 0, ps = 0; 8.6 8.7 - if(vfile && !(vs = get_vertex_shader(vfile))) { 8.8 + if(vfile && *vfile && !(vs = get_vertex_shader(vfile))) { 8.9 return 0; 8.10 } 8.11 - if(pfile && !(ps = get_pixel_shader(pfile))) { 8.12 + if(pfile && *pfile && !(ps = get_pixel_shader(pfile))) { 8.13 return 0; 8.14 } 8.15 return create_program_link(vs, ps);