dungeon_crawler

diff prototype/src/light.cc @ 23:fa8f89d06f6f

progress with light rendering
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 23 Aug 2012 00:10:10 +0300
parents 0588f8a1a351
children 527fede30057
line diff
     1.1 --- a/prototype/src/light.cc	Tue Aug 21 06:34:14 2012 +0300
     1.2 +++ b/prototype/src/light.cc	Thu Aug 23 00:10:10 2012 +0300
     1.3 @@ -5,6 +5,8 @@
     1.4  	: color(col)
     1.5  {
     1.6  	intensity = 1.0;
     1.7 +	vbo = 0;
     1.8 +	num_faces = 0;
     1.9  }
    1.10  
    1.11  Light::~Light() {}
    1.12 @@ -30,6 +32,29 @@
    1.13  	glLightfv(GL_LIGHT0 + id, GL_SPECULAR, &color.x);
    1.14  }
    1.15  
    1.16 +void Light::draw() const
    1.17 +{
    1.18 +	if(!vbo) {
    1.19 +		if(!((Light*)this)->create_mesh()) {
    1.20 +			return;
    1.21 +		}
    1.22 +	}
    1.23 +
    1.24 +	glEnableClientState(GL_VERTEX_ARRAY);
    1.25 +	glBindBuffer(GL_ARRAY_BUFFER, vbo);
    1.26 +	glVertexPointer(3, GL_FLOAT, 0, 0);
    1.27 +
    1.28 +	glDrawArrays(GL_TRIANGLES, 0, num_faces * 3);
    1.29 +
    1.30 +	glDisableClientState(GL_VERTEX_ARRAY);
    1.31 +}
    1.32 +
    1.33 +bool Light::create_mesh()
    1.34 +{
    1.35 +	fprintf(stderr, "%s: undefined\n", __FUNCTION__);
    1.36 +	return false;
    1.37 +}
    1.38 +
    1.39  
    1.40  PointLight::PointLight()
    1.41  {
    1.42 @@ -80,6 +105,67 @@
    1.43  	glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, atten[2]);
    1.44  }
    1.45  
    1.46 +void PointLight::draw() const
    1.47 +{
    1.48 +	//glMatrixMode(GL_MODELVIEW);
    1.49 +	//glPushMatrix();
    1.50 +	//glScalef(radius, radius, radius);
    1.51 +
    1.52 +	Light::draw();
    1.53 +
    1.54 +	//glPopMatrix();
    1.55 +}
    1.56 +
    1.57 +
    1.58 +
    1.59 +Vector3 sphvertex(float u, float v)
    1.60 +{
    1.61 +	float theta = u * M_PI * 2.0;
    1.62 +	float phi = v * M_PI;
    1.63 +
    1.64 +	Vector3 res;
    1.65 +	res.x = sin(theta) * cos(phi);
    1.66 +	res.y = sin(theta);
    1.67 +	res.z = cos(theta) * cos(phi);
    1.68 +	return res;
    1.69 +}
    1.70 +
    1.71 +
    1.72 +bool PointLight::create_mesh()
    1.73 +{
    1.74 +	const static int udiv = 8;
    1.75 +	const static int vdiv = 4;
    1.76 +
    1.77 +	int nquads = udiv * vdiv;
    1.78 +	num_faces = nquads * 2;
    1.79 +	int nverts = num_faces * 3;
    1.80 +
    1.81 +	float du = 1.0 / (float)udiv;
    1.82 +	float dv = 1.0 / (float)vdiv;
    1.83 +
    1.84 +	glGenBuffers(1, &vbo);
    1.85 +	glBindBuffer(GL_ARRAY_BUFFER, vbo);
    1.86 +	glBufferData(GL_ARRAY_BUFFER, nverts * sizeof(Vector3), 0, GL_STATIC_DRAW);
    1.87 +
    1.88 +	Vector3 *vptr = (Vector3*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
    1.89 +
    1.90 +	for(int i=0; i<vdiv; i++) {
    1.91 +		float v = (float)i / (float)vdiv;
    1.92 +		for(int j=0; j<udiv; j++) {
    1.93 +			float u = (float)j / (float)udiv;
    1.94 +
    1.95 +			*vptr++ = sphvertex(u, v);
    1.96 +			*vptr++ = sphvertex(u + du, v);
    1.97 +			*vptr++ = sphvertex(u, v + dv);
    1.98 +
    1.99 +			*vptr++ = sphvertex(u + du, v);
   1.100 +			*vptr++ = sphvertex(u + du, v + dv);
   1.101 +			*vptr++ = sphvertex(u, v + dv);
   1.102 +		}
   1.103 +	}
   1.104 +	glUnmapBuffer(GL_ARRAY_BUFFER);
   1.105 +	return true;
   1.106 +}
   1.107  
   1.108  void set_light(int id, const Light *lt)
   1.109  {