bloboland
diff src/renderer.cc @ 3:a39c301cdcce
terrain raytracing pretty much done
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 16 Dec 2012 14:24:16 +0200 |
parents | cfe68befb7cc |
children | 9021a906c5d3 |
line diff
1.1 --- a/src/renderer.cc Sun Dec 16 00:37:35 2012 +0200 1.2 +++ b/src/renderer.cc Sun Dec 16 14:24:16 2012 +0200 1.3 @@ -1,10 +1,16 @@ 1.4 #include "opengl.h" 1.5 #include "renderer.h" 1.6 +#include "sdr.h" 1.7 1.8 Renderer::Renderer() 1.9 { 1.10 leveltex = 0; 1.11 sdrprog = 0; 1.12 + 1.13 + level = 0; 1.14 + 1.15 + fov = M_PI / 4.0; 1.16 + aspect = 1.0; 1.17 } 1.18 1.19 Renderer::~Renderer() 1.20 @@ -12,14 +18,18 @@ 1.21 shutdown(); 1.22 } 1.23 1.24 -bool Renderer::init() 1.25 +bool Renderer::init(Level *level) 1.26 { 1.27 - sdrprog = new SdrProg; 1.28 - /*if(!sdrprog->load("sdr/bloboray.v.glsl", "sdr/bloboray.p.glsl")) { 1.29 + if(!(sdrprog = create_program_load("sdr/bloboray.v.glsl", "sdr/bloboray.p.glsl"))) { 1.30 return false; 1.31 - }*/ 1.32 + } 1.33 + 1.34 + Volume *vol = level->terrain; 1.35 1.36 leveltex = new Texture3D; 1.37 + leveltex->create(vol->get_size(0), vol->get_size(1), vol->get_size(2)); 1.38 + 1.39 + this->level = level; 1.40 1.41 return true; 1.42 } 1.43 @@ -27,16 +37,48 @@ 1.44 void Renderer::shutdown() 1.45 { 1.46 delete leveltex; 1.47 - delete sdrprog; 1.48 + if(sdrprog) { 1.49 + free_program(sdrprog); 1.50 + } 1.51 +} 1.52 + 1.53 +void Renderer::set_fov(float fov) 1.54 +{ 1.55 + this->fov = fov; 1.56 +} 1.57 + 1.58 +void Renderer::set_aspect(float aspect) 1.59 +{ 1.60 + this->aspect = aspect; 1.61 } 1.62 1.63 static void draw_cube(const Vector3 &pos, float sz); 1.64 1.65 -void Renderer::render(const Level *lvl) const 1.66 +void Renderer::render() const 1.67 { 1.68 - Volume *vol = lvl->terrain; 1.69 + leveltex->update((float*)level->terrain->get_data_ptr()); 1.70 + bind_texture(leveltex); 1.71 1.72 - glEnable(GL_COLOR_MATERIAL); 1.73 + set_uniform_float3(sdrprog, "volsize", level->terrain->get_size(0), 1.74 + level->terrain->get_size(1), level->terrain->get_size(2)); 1.75 + set_uniform_float4(sdrprog, "camprop", fov, aspect, 0, 0); 1.76 + bind_program(sdrprog); 1.77 + 1.78 + glBegin(GL_QUADS); 1.79 + glTexCoord2f(0, 0); 1.80 + glVertex2f(-1, -1); 1.81 + glTexCoord2f(1, 0); 1.82 + glVertex2f(1, -1); 1.83 + glTexCoord2f(1, 1); 1.84 + glVertex2f(1, 1); 1.85 + glTexCoord2f(0, 1); 1.86 + glVertex2f(-1, 1); 1.87 + glEnd(); 1.88 + 1.89 + bind_program(0); 1.90 + bind_texture(0); 1.91 + 1.92 + /*glEnable(GL_COLOR_MATERIAL); 1.93 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); 1.94 1.95 glBegin(GL_QUADS); 1.96 @@ -60,6 +102,7 @@ 1.97 glEnd(); 1.98 1.99 glDisable(GL_COLOR_MATERIAL); 1.100 + */ 1.101 } 1.102 1.103