dungeon_crawler

view prototype/src/renderer.cc @ 67:2560a7ab0243

internalized libanim, libimago2, and libpsys
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 07 Oct 2012 02:04:00 +0300
parents f71381c9e245
children a27528035e20
line source
1 #include <limits.h>
2 #include "opengl.h"
3 #include "renderer.h"
4 #include "level.h"
5 #include "sdr.h"
6 #include "datapath.h"
8 static unsigned int load_sdr(const char *vfname, const char *pfname);
10 Renderer *rend;
13 Renderer::Renderer()
14 {
15 width = height = 0;
16 }
18 Renderer::~Renderer()
19 {
20 }
22 bool Renderer::init(int xsz, int ysz)
23 {
24 width = xsz;
25 height = ysz;
27 rend = this;
28 return true;
29 }
31 int Renderer::get_tangent_location() const
32 {
33 return -1;
34 }
36 unsigned int Renderer::get_current_program() const
37 {
38 return 0;
39 }
41 void Renderer::resize(int xsz, int ysz)
42 {
43 width = xsz;
44 height = ysz;
45 }
47 void Renderer::render_pre(const Level *level) const
48 {
49 glEnable(GL_FOG);
50 glFogi(GL_FOG_MODE, GL_LINEAR);
51 glFogf(GL_FOG_START, 3.0f);
52 glFogf(GL_FOG_END, 6.0f);
53 // TODO level->draw_pre();
54 }
56 void Renderer::render_post(const Level *level) const
57 {
58 level->draw_post();
59 }
62 // ---- fallback forward renderer ----
63 FwdRenderer::FwdRenderer()
64 {
65 sdrprog = 0;
66 tang_attr = -1;
67 }
69 FwdRenderer::~FwdRenderer()
70 {
71 if(sdrprog) {
72 free_program(sdrprog);
73 }
74 }
76 bool FwdRenderer::init(int xsz, int ysz)
77 {
78 width = xsz;
79 height = ysz;
81 if(glUseProgram && (sdrprog = load_sdr("fallback.v.glsl", "fallback.p.glsl"))) {
82 tang_attr = get_attrib_loc(sdrprog, "attr_tangent");
83 set_uniform_int(sdrprog, "tex_dif", 0);
84 set_uniform_int(sdrprog, "tex_norm", 1);
85 }
86 return true;
87 }
89 int FwdRenderer::get_tangent_location() const
90 {
91 return tang_attr;
92 }
94 unsigned int FwdRenderer::get_current_program() const
95 {
96 return sdrprog;
97 }
99 void FwdRenderer::render(const Level *level) const
100 {
101 glPushAttrib(GL_ENABLE_BIT);
102 glEnable(GL_LIGHTING);
104 render_pre(level);
106 glUseProgram(sdrprog);
107 level->draw();
108 glUseProgram(0);
110 render_post(level);
112 glPopAttrib();
113 }
115 static unsigned int load_sdr(const char *vfname, const char *pfname)
116 {
117 unsigned int prog;
119 std::string vsfile = datafile_path(vfname);
120 std::string psfile = datafile_path(pfname);
122 const char *vs = vsfile.empty() ? 0 : vsfile.c_str();
123 const char *ps = psfile.empty() ? 0 : psfile.c_str();
125 if(!(prog = create_program_load(vs, ps))) {
126 fprintf(stderr, "failed to load shader program (%s, %s)\n", vs, ps);
127 return 0;
128 }
129 return prog;
130 }