nuclear@44: #include nuclear@15: #include "opengl.h" nuclear@15: #include "renderer.h" nuclear@23: #include "level.h" nuclear@15: #include "sdr.h" nuclear@15: #include "datapath.h" nuclear@15: nuclear@41: static unsigned int load_sdr(const char *vfname, const char *pfname); nuclear@35: nuclear@41: Renderer *rend; nuclear@18: nuclear@15: nuclear@41: Renderer::Renderer() nuclear@41: { nuclear@41: width = height = 0; nuclear@41: } nuclear@17: nuclear@41: Renderer::~Renderer() nuclear@41: { nuclear@41: } nuclear@17: nuclear@41: bool Renderer::init(int xsz, int ysz) nuclear@41: { nuclear@41: width = xsz; nuclear@41: height = ysz; nuclear@17: nuclear@41: rend = this; nuclear@41: return true; nuclear@41: } nuclear@15: nuclear@41: int Renderer::get_tangent_location() const nuclear@41: { nuclear@41: return -1; nuclear@41: } nuclear@15: nuclear@41: unsigned int Renderer::get_current_program() const nuclear@41: { nuclear@41: return 0; nuclear@41: } nuclear@21: nuclear@41: void Renderer::resize(int xsz, int ysz) nuclear@41: { nuclear@41: width = xsz; nuclear@41: height = ysz; nuclear@41: } nuclear@21: nuclear@62: void Renderer::render_pre(const Level *level) const nuclear@62: { nuclear@62: glEnable(GL_FOG); nuclear@62: glFogi(GL_FOG_MODE, GL_LINEAR); nuclear@62: glFogf(GL_FOG_START, 3.0f); nuclear@62: glFogf(GL_FOG_END, 6.0f); nuclear@62: // TODO level->draw_pre(); nuclear@62: } nuclear@62: nuclear@62: void Renderer::render_post(const Level *level) const nuclear@62: { nuclear@62: level->draw_post(); nuclear@62: } nuclear@62: nuclear@41: nuclear@41: // ---- fallback forward renderer ---- nuclear@41: FwdRenderer::FwdRenderer() nuclear@17: { nuclear@41: sdrprog = 0; nuclear@41: tang_attr = -1; nuclear@41: } nuclear@41: nuclear@41: FwdRenderer::~FwdRenderer() nuclear@41: { nuclear@41: if(sdrprog) { nuclear@41: free_program(sdrprog); nuclear@32: } nuclear@41: } nuclear@21: nuclear@41: bool FwdRenderer::init(int xsz, int ysz) nuclear@41: { nuclear@41: width = xsz; nuclear@41: height = ysz; nuclear@19: nuclear@41: if(glUseProgram && (sdrprog = load_sdr("fallback.v.glsl", "fallback.p.glsl"))) { nuclear@41: tang_attr = get_attrib_loc(sdrprog, "attr_tangent"); nuclear@41: set_uniform_int(sdrprog, "tex_dif", 0); nuclear@41: set_uniform_int(sdrprog, "tex_norm", 1); nuclear@23: } nuclear@17: return true; nuclear@17: } nuclear@17: nuclear@41: int FwdRenderer::get_tangent_location() const nuclear@17: { nuclear@41: return tang_attr; nuclear@17: } nuclear@17: nuclear@41: unsigned int FwdRenderer::get_current_program() const nuclear@33: { nuclear@41: return sdrprog; nuclear@33: } nuclear@33: nuclear@41: void FwdRenderer::render(const Level *level) const nuclear@35: { nuclear@41: glPushAttrib(GL_ENABLE_BIT); nuclear@51: glEnable(GL_LIGHTING); nuclear@62: nuclear@62: render_pre(level); nuclear@51: nuclear@41: glUseProgram(sdrprog); nuclear@62: level->draw(); nuclear@62: glUseProgram(0); nuclear@35: nuclear@62: render_post(level); nuclear@46: nuclear@41: glPopAttrib(); nuclear@17: } nuclear@17: nuclear@18: static unsigned int load_sdr(const char *vfname, const char *pfname) nuclear@15: { nuclear@18: unsigned int prog; nuclear@15: nuclear@63: std::string vsfile = datafile_path(vfname); nuclear@63: std::string psfile = datafile_path(pfname); nuclear@63: nuclear@63: const char *vs = vsfile.empty() ? 0 : vsfile.c_str(); nuclear@63: const char *ps = psfile.empty() ? 0 : psfile.c_str(); nuclear@63: nuclear@63: if(!(prog = create_program_load(vs, ps))) { nuclear@63: fprintf(stderr, "failed to load shader program (%s, %s)\n", vs, ps); nuclear@18: return 0; nuclear@15: } nuclear@18: return prog; nuclear@15: }