dungeon_crawler

annotate prototype/src/renderer.cc @ 46:f3030df27110

debugging the particles
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 13 Sep 2012 06:33:51 +0300
parents c45c42c3d32e
children d57df51f6b50
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@41 88 glUseProgram(sdrprog);
nuclear@35 89
nuclear@41 90 glEnable(GL_LIGHTING);
nuclear@23 91 level->draw();
nuclear@20 92
nuclear@46 93 glUseProgram(0);
nuclear@46 94 level->draw_post();
nuclear@46 95
nuclear@41 96 glPopAttrib();
nuclear@17 97 }
nuclear@17 98
nuclear@18 99 static unsigned int load_sdr(const char *vfname, const char *pfname)
nuclear@15 100 {
nuclear@15 101 char vsfile[PATH_MAX], psfile[PATH_MAX];
nuclear@15 102 const char *fname;
nuclear@18 103 unsigned int prog;
nuclear@15 104
nuclear@18 105 if((fname = datafile_path(vfname))) {
nuclear@17 106 strcpy(vsfile, fname);
nuclear@17 107 } else {
nuclear@17 108 vsfile[0] = 0;
nuclear@17 109 }
nuclear@18 110 if((fname = datafile_path(pfname))) {
nuclear@17 111 strcpy(psfile, fname);
nuclear@17 112 } else {
nuclear@17 113 psfile[0] = 0;
nuclear@17 114 }
nuclear@18 115 if(!(prog = create_program_load(vsfile, psfile))) {
nuclear@18 116 fprintf(stderr, "failed to load shader program (%s, %s)\n", vsfile, psfile);
nuclear@18 117 return 0;
nuclear@15 118 }
nuclear@18 119 return prog;
nuclear@15 120 }