dungeon_crawler
view prototype/src/renderer.cc @ 41:acfe0c0110fc
- cleaned up the renderer
- implemented fallback (non-deferred renderer)
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 30 Aug 2012 05:35:00 +0300 |
parents | 84a56fb24850 |
children | c45c42c3d32e |
line source
1 #include "opengl.h"
2 #include "renderer.h"
3 #include "level.h"
4 #include "sdr.h"
5 #include "datapath.h"
7 static unsigned int load_sdr(const char *vfname, const char *pfname);
9 Renderer *rend;
12 Renderer::Renderer()
13 {
14 width = height = 0;
15 }
17 Renderer::~Renderer()
18 {
19 }
21 bool Renderer::init(int xsz, int ysz)
22 {
23 width = xsz;
24 height = ysz;
26 rend = this;
27 return true;
28 }
30 int Renderer::get_tangent_location() const
31 {
32 return -1;
33 }
35 unsigned int Renderer::get_current_program() const
36 {
37 return 0;
38 }
40 void Renderer::resize(int xsz, int ysz)
41 {
42 width = xsz;
43 height = ysz;
44 }
47 // ---- fallback forward renderer ----
48 FwdRenderer::FwdRenderer()
49 {
50 sdrprog = 0;
51 tang_attr = -1;
52 }
54 FwdRenderer::~FwdRenderer()
55 {
56 if(sdrprog) {
57 free_program(sdrprog);
58 }
59 }
61 bool FwdRenderer::init(int xsz, int ysz)
62 {
63 width = xsz;
64 height = ysz;
66 if(glUseProgram && (sdrprog = load_sdr("fallback.v.glsl", "fallback.p.glsl"))) {
67 tang_attr = get_attrib_loc(sdrprog, "attr_tangent");
68 set_uniform_int(sdrprog, "tex_dif", 0);
69 set_uniform_int(sdrprog, "tex_norm", 1);
70 }
71 return true;
72 }
74 int FwdRenderer::get_tangent_location() const
75 {
76 return tang_attr;
77 }
79 unsigned int FwdRenderer::get_current_program() const
80 {
81 return sdrprog;
82 }
84 void FwdRenderer::render(const Level *level) const
85 {
86 glPushAttrib(GL_ENABLE_BIT);
87 glUseProgram(sdrprog);
89 glEnable(GL_LIGHTING);
90 level->draw();
92 glPopAttrib();
94 glUseProgram(0);
95 }
97 static unsigned int load_sdr(const char *vfname, const char *pfname)
98 {
99 char vsfile[PATH_MAX], psfile[PATH_MAX];
100 const char *fname;
101 unsigned int prog;
103 if((fname = datafile_path(vfname))) {
104 strcpy(vsfile, fname);
105 } else {
106 vsfile[0] = 0;
107 }
108 if((fname = datafile_path(pfname))) {
109 strcpy(psfile, fname);
110 } else {
111 psfile[0] = 0;
112 }
113 if(!(prog = create_program_load(vsfile, psfile))) {
114 fprintf(stderr, "failed to load shader program (%s, %s)\n", vsfile, psfile);
115 return 0;
116 }
117 return prog;
118 }