dungeon_crawler

annotate 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
rev   line source
nuclear@44 1 #include <limits.h>
nuclear@15 2 #include "opengl.h"
nuclear@15 3 #include "renderer.h"
nuclear@23 4 #include "level.h"
nuclear@15 5 #include "sdr.h"
nuclear@15 6 #include "datapath.h"
nuclear@15 7
nuclear@41 8 static unsigned int load_sdr(const char *vfname, const char *pfname);
nuclear@35 9
nuclear@41 10 Renderer *rend;
nuclear@18 11
nuclear@15 12
nuclear@41 13 Renderer::Renderer()
nuclear@41 14 {
nuclear@41 15 width = height = 0;
nuclear@41 16 }
nuclear@17 17
nuclear@41 18 Renderer::~Renderer()
nuclear@41 19 {
nuclear@41 20 }
nuclear@17 21
nuclear@41 22 bool Renderer::init(int xsz, int ysz)
nuclear@41 23 {
nuclear@41 24 width = xsz;
nuclear@41 25 height = ysz;
nuclear@17 26
nuclear@41 27 rend = this;
nuclear@41 28 return true;
nuclear@41 29 }
nuclear@15 30
nuclear@41 31 int Renderer::get_tangent_location() const
nuclear@41 32 {
nuclear@41 33 return -1;
nuclear@41 34 }
nuclear@15 35
nuclear@41 36 unsigned int Renderer::get_current_program() const
nuclear@41 37 {
nuclear@41 38 return 0;
nuclear@41 39 }
nuclear@21 40
nuclear@41 41 void Renderer::resize(int xsz, int ysz)
nuclear@41 42 {
nuclear@41 43 width = xsz;
nuclear@41 44 height = ysz;
nuclear@41 45 }
nuclear@21 46
nuclear@62 47 void Renderer::render_pre(const Level *level) const
nuclear@62 48 {
nuclear@62 49 glEnable(GL_FOG);
nuclear@62 50 glFogi(GL_FOG_MODE, GL_LINEAR);
nuclear@62 51 glFogf(GL_FOG_START, 3.0f);
nuclear@62 52 glFogf(GL_FOG_END, 6.0f);
nuclear@62 53 // TODO level->draw_pre();
nuclear@62 54 }
nuclear@62 55
nuclear@62 56 void Renderer::render_post(const Level *level) const
nuclear@62 57 {
nuclear@62 58 level->draw_post();
nuclear@62 59 }
nuclear@62 60
nuclear@41 61
nuclear@41 62 // ---- fallback forward renderer ----
nuclear@41 63 FwdRenderer::FwdRenderer()
nuclear@17 64 {
nuclear@41 65 sdrprog = 0;
nuclear@41 66 tang_attr = -1;
nuclear@41 67 }
nuclear@41 68
nuclear@41 69 FwdRenderer::~FwdRenderer()
nuclear@41 70 {
nuclear@41 71 if(sdrprog) {
nuclear@41 72 free_program(sdrprog);
nuclear@32 73 }
nuclear@41 74 }
nuclear@21 75
nuclear@41 76 bool FwdRenderer::init(int xsz, int ysz)
nuclear@41 77 {
nuclear@41 78 width = xsz;
nuclear@41 79 height = ysz;
nuclear@19 80
nuclear@41 81 if(glUseProgram && (sdrprog = load_sdr("fallback.v.glsl", "fallback.p.glsl"))) {
nuclear@41 82 tang_attr = get_attrib_loc(sdrprog, "attr_tangent");
nuclear@41 83 set_uniform_int(sdrprog, "tex_dif", 0);
nuclear@41 84 set_uniform_int(sdrprog, "tex_norm", 1);
nuclear@23 85 }
nuclear@17 86 return true;
nuclear@17 87 }
nuclear@17 88
nuclear@41 89 int FwdRenderer::get_tangent_location() const
nuclear@17 90 {
nuclear@41 91 return tang_attr;
nuclear@17 92 }
nuclear@17 93
nuclear@41 94 unsigned int FwdRenderer::get_current_program() const
nuclear@33 95 {
nuclear@41 96 return sdrprog;
nuclear@33 97 }
nuclear@33 98
nuclear@41 99 void FwdRenderer::render(const Level *level) const
nuclear@35 100 {
nuclear@41 101 glPushAttrib(GL_ENABLE_BIT);
nuclear@51 102 glEnable(GL_LIGHTING);
nuclear@62 103
nuclear@62 104 render_pre(level);
nuclear@51 105
nuclear@41 106 glUseProgram(sdrprog);
nuclear@62 107 level->draw();
nuclear@62 108 glUseProgram(0);
nuclear@35 109
nuclear@62 110 render_post(level);
nuclear@46 111
nuclear@41 112 glPopAttrib();
nuclear@17 113 }
nuclear@17 114
nuclear@18 115 static unsigned int load_sdr(const char *vfname, const char *pfname)
nuclear@15 116 {
nuclear@15 117 char vsfile[PATH_MAX], psfile[PATH_MAX];
nuclear@15 118 const char *fname;
nuclear@18 119 unsigned int prog;
nuclear@15 120
nuclear@18 121 if((fname = datafile_path(vfname))) {
nuclear@17 122 strcpy(vsfile, fname);
nuclear@17 123 } else {
nuclear@17 124 vsfile[0] = 0;
nuclear@17 125 }
nuclear@18 126 if((fname = datafile_path(pfname))) {
nuclear@17 127 strcpy(psfile, fname);
nuclear@17 128 } else {
nuclear@17 129 psfile[0] = 0;
nuclear@17 130 }
nuclear@18 131 if(!(prog = create_program_load(vsfile, psfile))) {
nuclear@18 132 fprintf(stderr, "failed to load shader program (%s, %s)\n", vsfile, psfile);
nuclear@18 133 return 0;
nuclear@15 134 }
nuclear@18 135 return prog;
nuclear@15 136 }