dungeon_crawler

view prototype/src/renderer.cc @ 44:c45c42c3d32e

fixed build on linux
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 30 Aug 2012 06:08:22 +0300
parents acfe0c0110fc
children f3030df27110
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 }
48 // ---- fallback forward renderer ----
49 FwdRenderer::FwdRenderer()
50 {
51 sdrprog = 0;
52 tang_attr = -1;
53 }
55 FwdRenderer::~FwdRenderer()
56 {
57 if(sdrprog) {
58 free_program(sdrprog);
59 }
60 }
62 bool FwdRenderer::init(int xsz, int ysz)
63 {
64 width = xsz;
65 height = ysz;
67 if(glUseProgram && (sdrprog = load_sdr("fallback.v.glsl", "fallback.p.glsl"))) {
68 tang_attr = get_attrib_loc(sdrprog, "attr_tangent");
69 set_uniform_int(sdrprog, "tex_dif", 0);
70 set_uniform_int(sdrprog, "tex_norm", 1);
71 }
72 return true;
73 }
75 int FwdRenderer::get_tangent_location() const
76 {
77 return tang_attr;
78 }
80 unsigned int FwdRenderer::get_current_program() const
81 {
82 return sdrprog;
83 }
85 void FwdRenderer::render(const Level *level) const
86 {
87 glPushAttrib(GL_ENABLE_BIT);
88 glUseProgram(sdrprog);
90 glEnable(GL_LIGHTING);
91 level->draw();
93 glPopAttrib();
95 glUseProgram(0);
96 }
98 static unsigned int load_sdr(const char *vfname, const char *pfname)
99 {
100 char vsfile[PATH_MAX], psfile[PATH_MAX];
101 const char *fname;
102 unsigned int prog;
104 if((fname = datafile_path(vfname))) {
105 strcpy(vsfile, fname);
106 } else {
107 vsfile[0] = 0;
108 }
109 if((fname = datafile_path(pfname))) {
110 strcpy(psfile, fname);
111 } else {
112 psfile[0] = 0;
113 }
114 if(!(prog = create_program_load(vsfile, psfile))) {
115 fprintf(stderr, "failed to load shader program (%s, %s)\n", vsfile, psfile);
116 return 0;
117 }
118 return prog;
119 }