bloboland

diff src/renderer.cc @ 1:cfe68befb7cc

some progress
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 15 Dec 2012 23:43:03 +0200
parents
children a39c301cdcce
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/renderer.cc	Sat Dec 15 23:43:03 2012 +0200
     1.3 @@ -0,0 +1,106 @@
     1.4 +#include "opengl.h"
     1.5 +#include "renderer.h"
     1.6 +
     1.7 +Renderer::Renderer()
     1.8 +{
     1.9 +	leveltex = 0;
    1.10 +	sdrprog = 0;
    1.11 +}
    1.12 +
    1.13 +Renderer::~Renderer()
    1.14 +{
    1.15 +	shutdown();
    1.16 +}
    1.17 +
    1.18 +bool Renderer::init()
    1.19 +{
    1.20 +	sdrprog = new SdrProg;
    1.21 +	/*if(!sdrprog->load("sdr/bloboray.v.glsl", "sdr/bloboray.p.glsl")) {
    1.22 +		return false;
    1.23 +	}*/
    1.24 +
    1.25 +	leveltex = new Texture3D;
    1.26 +
    1.27 +	return true;
    1.28 +}
    1.29 +
    1.30 +void Renderer::shutdown()
    1.31 +{
    1.32 +	delete leveltex;
    1.33 +	delete sdrprog;
    1.34 +}
    1.35 +
    1.36 +static void draw_cube(const Vector3 &pos, float sz);
    1.37 +
    1.38 +void Renderer::render(const Level *lvl) const
    1.39 +{
    1.40 +	Volume *vol = lvl->terrain;
    1.41 +
    1.42 +	glEnable(GL_COLOR_MATERIAL);
    1.43 +	glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
    1.44 +
    1.45 +	glBegin(GL_QUADS);
    1.46 +	for(int i=0; i<vol->get_size(0); i++) {
    1.47 +		for(int j=0; j<vol->get_size(1); j++) {
    1.48 +			for(int k=0; k<vol->get_size(2) - 1; k++) {
    1.49 +				Vector4 vox = vol->get_voxel(i, j, k);
    1.50 +				Vector4 next = vol->get_voxel(i, j, k + 1);
    1.51 +
    1.52 +				if(vox.w > 0.1 && next.w < 0.1) {
    1.53 +					glColor3f(vox.x, vox.y, vox.z);
    1.54 +
    1.55 +					float x = ((float)i - vol->get_size(0) / 2) * 0.1;
    1.56 +					float z = ((float)j - vol->get_size(1) / 2) * 0.1;
    1.57 +					float y = ((float)k - vol->get_size(2) / 2) * 0.1;
    1.58 +					draw_cube(Vector3(x, y, z), 0.1);
    1.59 +				}
    1.60 +			}
    1.61 +		}
    1.62 +	}
    1.63 +	glEnd();
    1.64 +
    1.65 +	glDisable(GL_COLOR_MATERIAL);
    1.66 +}
    1.67 +
    1.68 +
    1.69 +static void draw_cube(const Vector3 &pos, float sz)
    1.70 +{
    1.71 +	sz /= 2.0;
    1.72 +
    1.73 +	glNormal3f(0, 0, 1);
    1.74 +	glVertex3f(pos.x - sz, pos.y - sz, pos.z + sz);
    1.75 +	glVertex3f(pos.x + sz, pos.y - sz, pos.z + sz);
    1.76 +	glVertex3f(pos.x + sz, pos.y + sz, pos.z + sz);
    1.77 +	glVertex3f(pos.x - sz, pos.y + sz, pos.z + sz);
    1.78 +
    1.79 +	glNormal3f(1, 0, 0);
    1.80 +	glVertex3f(pos.x + sz, pos.y - sz, pos.z + sz);
    1.81 +	glVertex3f(pos.x + sz, pos.y - sz, pos.z - sz);
    1.82 +	glVertex3f(pos.x + sz, pos.y + sz, pos.z - sz);
    1.83 +	glVertex3f(pos.x + sz, pos.y + sz, pos.z + sz);
    1.84 +
    1.85 +	glNormal3f(0, 0, -1);
    1.86 +	glVertex3f(pos.x + sz, pos.y - sz, pos.z - sz);
    1.87 +	glVertex3f(pos.x - sz, pos.y - sz, pos.z - sz);
    1.88 +	glVertex3f(pos.x - sz, pos.y + sz, pos.z - sz);
    1.89 +	glVertex3f(pos.x + sz, pos.y + sz, pos.z - sz);
    1.90 +
    1.91 +	glNormal3f(-1, 0, 0);
    1.92 +	glVertex3f(pos.x - sz, pos.y - sz, pos.z - sz);
    1.93 +	glVertex3f(pos.x - sz, pos.y - sz, pos.z + sz);
    1.94 +	glVertex3f(pos.x - sz, pos.y + sz, pos.z + sz);
    1.95 +	glVertex3f(pos.x - sz, pos.y + sz, pos.z - sz);
    1.96 +
    1.97 +	glNormal3f(0, 1, 0);
    1.98 +	glVertex3f(pos.x - sz, pos.y + sz, pos.z + sz);
    1.99 +	glVertex3f(pos.x + sz, pos.y + sz, pos.z + sz);
   1.100 +	glVertex3f(pos.x + sz, pos.y + sz, pos.z - sz);
   1.101 +	glVertex3f(pos.x - sz, pos.y + sz, pos.z - sz);
   1.102 +
   1.103 +	glNormal3f(0, -1, 0);
   1.104 +	glVertex3f(pos.x - sz, pos.y - sz, pos.z - sz);
   1.105 +	glVertex3f(pos.x + sz, pos.y - sz, pos.z - sz);
   1.106 +	glVertex3f(pos.x + sz, pos.y - sz, pos.z + sz);
   1.107 +	glVertex3f(pos.x - sz, pos.y - sz, pos.z + sz);
   1.108 +
   1.109 +}