dungeon_crawler

diff prototype/src/light.cc @ 4:158de53b4e18

tile work
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 11 Aug 2012 05:44:52 +0300
parents
children 0588f8a1a351
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/prototype/src/light.cc	Sat Aug 11 05:44:52 2012 +0300
     1.3 @@ -0,0 +1,75 @@
     1.4 +#include "opengl.h"
     1.5 +#include "light.h"
     1.6 +
     1.7 +Light::Light(const Color &col)
     1.8 +	: color(col)
     1.9 +{
    1.10 +	intensity = 1.0;
    1.11 +}
    1.12 +
    1.13 +Light::~Light() {}
    1.14 +
    1.15 +void Light::set_intensity(float val)
    1.16 +{
    1.17 +	intensity = val;
    1.18 +}
    1.19 +
    1.20 +void Light::set_color(const Color &col)
    1.21 +{
    1.22 +	color = col;
    1.23 +}
    1.24 +
    1.25 +void Light::use(int id) const
    1.26 +{
    1.27 +	glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, &color.x);
    1.28 +	glLightfv(GL_LIGHT0 + id, GL_SPECULAR, &color.x);
    1.29 +}
    1.30 +
    1.31 +
    1.32 +PointLight::PointLight()
    1.33 +{
    1.34 +	atten[0] = 1.0f;
    1.35 +	atten[1] = 0.0f;
    1.36 +	atten[2] = 0.0f;
    1.37 +}
    1.38 +
    1.39 +PointLight::PointLight(const Vector3 &pos, const Color &col)
    1.40 +	: Light(col)
    1.41 +{
    1.42 +	this->pos = pos;
    1.43 +	atten[0] = 1.0f;
    1.44 +	atten[1] = 0.0f;
    1.45 +	atten[2] = 0.0f;
    1.46 +}
    1.47 +
    1.48 +void PointLight::set_position(const Vector3 &pos)
    1.49 +{
    1.50 +	this->pos = pos;
    1.51 +}
    1.52 +
    1.53 +void PointLight::set_attenuation(float att_const, float att_lin, float att_quad)
    1.54 +{
    1.55 +	atten[0] = att_const;
    1.56 +	atten[1] = att_lin;
    1.57 +	atten[2] = att_quad;
    1.58 +}
    1.59 +
    1.60 +void PointLight::use(int id) const
    1.61 +{
    1.62 +	float lpos[] = {pos.x, pos.y, pos.z, 1.0f};
    1.63 +	glLightfv(GL_LIGHT0 + id, GL_POSITION, lpos);
    1.64 +	glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, atten[0]);
    1.65 +	glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, atten[1]);
    1.66 +	glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, atten[2]);
    1.67 +}
    1.68 +
    1.69 +
    1.70 +void set_light(int id, const Light *lt)
    1.71 +{
    1.72 +	if(lt) {
    1.73 +		glDisable(GL_LIGHT0 + id);
    1.74 +	} else {
    1.75 +		glEnable(GL_LIGHT0 + id);
    1.76 +		lt->use(id);
    1.77 +	}
    1.78 +}