dungeon_crawler

view prototype/src/renderer.cc @ 62:f71381c9e245

gamo to xristo
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 25 Sep 2012 06:59:11 +0300
parents d57df51f6b50
children 7f52d6310317
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 char vsfile[PATH_MAX], psfile[PATH_MAX];
118 const char *fname;
119 unsigned int prog;
121 if((fname = datafile_path(vfname))) {
122 strcpy(vsfile, fname);
123 } else {
124 vsfile[0] = 0;
125 }
126 if((fname = datafile_path(pfname))) {
127 strcpy(psfile, fname);
128 } else {
129 psfile[0] = 0;
130 }
131 if(!(prog = create_program_load(vsfile, psfile))) {
132 fprintf(stderr, "failed to load shader program (%s, %s)\n", vsfile, psfile);
133 return 0;
134 }
135 return prog;
136 }