dungeon_crawler

annotate prototype/src/renderer.cc @ 51:d57df51f6b50

- fixed audio panning (listener direction) - particles had no fog - sound sources were not destroyed properly
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 18 Sep 2012 09:40:56 +0300
parents f3030df27110
children f71381c9e245
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@41 47
nuclear@41 48 // ---- fallback forward renderer ----
nuclear@41 49 FwdRenderer::FwdRenderer()
nuclear@17 50 {
nuclear@41 51 sdrprog = 0;
nuclear@41 52 tang_attr = -1;
nuclear@41 53 }
nuclear@41 54
nuclear@41 55 FwdRenderer::~FwdRenderer()
nuclear@41 56 {
nuclear@41 57 if(sdrprog) {
nuclear@41 58 free_program(sdrprog);
nuclear@32 59 }
nuclear@41 60 }
nuclear@21 61
nuclear@41 62 bool FwdRenderer::init(int xsz, int ysz)
nuclear@41 63 {
nuclear@41 64 width = xsz;
nuclear@41 65 height = ysz;
nuclear@19 66
nuclear@41 67 if(glUseProgram && (sdrprog = load_sdr("fallback.v.glsl", "fallback.p.glsl"))) {
nuclear@41 68 tang_attr = get_attrib_loc(sdrprog, "attr_tangent");
nuclear@41 69 set_uniform_int(sdrprog, "tex_dif", 0);
nuclear@41 70 set_uniform_int(sdrprog, "tex_norm", 1);
nuclear@23 71 }
nuclear@17 72 return true;
nuclear@17 73 }
nuclear@17 74
nuclear@41 75 int FwdRenderer::get_tangent_location() const
nuclear@17 76 {
nuclear@41 77 return tang_attr;
nuclear@17 78 }
nuclear@17 79
nuclear@41 80 unsigned int FwdRenderer::get_current_program() const
nuclear@33 81 {
nuclear@41 82 return sdrprog;
nuclear@33 83 }
nuclear@33 84
nuclear@41 85 void FwdRenderer::render(const Level *level) const
nuclear@35 86 {
nuclear@41 87 glPushAttrib(GL_ENABLE_BIT);
nuclear@51 88 glEnable(GL_LIGHTING);
nuclear@51 89 glEnable(GL_FOG);
nuclear@51 90 glFogi(GL_FOG_MODE, GL_LINEAR);
nuclear@51 91 glFogf(GL_FOG_START, 3.0f);
nuclear@51 92 glFogf(GL_FOG_END, 6.0f);
nuclear@51 93
nuclear@41 94 glUseProgram(sdrprog);
nuclear@35 95
nuclear@23 96 level->draw();
nuclear@20 97
nuclear@46 98 glUseProgram(0);
nuclear@46 99 level->draw_post();
nuclear@46 100
nuclear@41 101 glPopAttrib();
nuclear@17 102 }
nuclear@17 103
nuclear@18 104 static unsigned int load_sdr(const char *vfname, const char *pfname)
nuclear@15 105 {
nuclear@15 106 char vsfile[PATH_MAX], psfile[PATH_MAX];
nuclear@15 107 const char *fname;
nuclear@18 108 unsigned int prog;
nuclear@15 109
nuclear@18 110 if((fname = datafile_path(vfname))) {
nuclear@17 111 strcpy(vsfile, fname);
nuclear@17 112 } else {
nuclear@17 113 vsfile[0] = 0;
nuclear@17 114 }
nuclear@18 115 if((fname = datafile_path(pfname))) {
nuclear@17 116 strcpy(psfile, fname);
nuclear@17 117 } else {
nuclear@17 118 psfile[0] = 0;
nuclear@17 119 }
nuclear@18 120 if(!(prog = create_program_load(vsfile, psfile))) {
nuclear@18 121 fprintf(stderr, "failed to load shader program (%s, %s)\n", vsfile, psfile);
nuclear@18 122 return 0;
nuclear@15 123 }
nuclear@18 124 return prog;
nuclear@15 125 }