dungeon_crawler

annotate prototype/src/light.cc @ 24:e122ba214ee1

implementing the command console
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 23 Aug 2012 18:03:11 +0300
parents 0588f8a1a351
children 527fede30057
rev   line source
nuclear@4 1 #include "opengl.h"
nuclear@4 2 #include "light.h"
nuclear@4 3
nuclear@4 4 Light::Light(const Color &col)
nuclear@4 5 : color(col)
nuclear@4 6 {
nuclear@4 7 intensity = 1.0;
nuclear@23 8 vbo = 0;
nuclear@23 9 num_faces = 0;
nuclear@4 10 }
nuclear@4 11
nuclear@4 12 Light::~Light() {}
nuclear@4 13
nuclear@4 14 void Light::set_intensity(float val)
nuclear@4 15 {
nuclear@4 16 intensity = val;
nuclear@4 17 }
nuclear@4 18
nuclear@4 19 void Light::set_color(const Color &col)
nuclear@4 20 {
nuclear@4 21 color = col;
nuclear@4 22 }
nuclear@4 23
nuclear@21 24 Color Light::get_color(bool with_intensity) const
nuclear@21 25 {
nuclear@21 26 return with_intensity ? color * intensity : color;
nuclear@21 27 }
nuclear@21 28
nuclear@4 29 void Light::use(int id) const
nuclear@4 30 {
nuclear@4 31 glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, &color.x);
nuclear@4 32 glLightfv(GL_LIGHT0 + id, GL_SPECULAR, &color.x);
nuclear@4 33 }
nuclear@4 34
nuclear@23 35 void Light::draw() const
nuclear@23 36 {
nuclear@23 37 if(!vbo) {
nuclear@23 38 if(!((Light*)this)->create_mesh()) {
nuclear@23 39 return;
nuclear@23 40 }
nuclear@23 41 }
nuclear@23 42
nuclear@23 43 glEnableClientState(GL_VERTEX_ARRAY);
nuclear@23 44 glBindBuffer(GL_ARRAY_BUFFER, vbo);
nuclear@23 45 glVertexPointer(3, GL_FLOAT, 0, 0);
nuclear@23 46
nuclear@23 47 glDrawArrays(GL_TRIANGLES, 0, num_faces * 3);
nuclear@23 48
nuclear@23 49 glDisableClientState(GL_VERTEX_ARRAY);
nuclear@23 50 }
nuclear@23 51
nuclear@23 52 bool Light::create_mesh()
nuclear@23 53 {
nuclear@23 54 fprintf(stderr, "%s: undefined\n", __FUNCTION__);
nuclear@23 55 return false;
nuclear@23 56 }
nuclear@23 57
nuclear@4 58
nuclear@4 59 PointLight::PointLight()
nuclear@4 60 {
nuclear@4 61 atten[0] = 1.0f;
nuclear@4 62 atten[1] = 0.0f;
nuclear@4 63 atten[2] = 0.0f;
nuclear@21 64 radius = 1.0;
nuclear@4 65 }
nuclear@4 66
nuclear@4 67 PointLight::PointLight(const Vector3 &pos, const Color &col)
nuclear@4 68 : Light(col)
nuclear@4 69 {
nuclear@4 70 this->pos = pos;
nuclear@4 71 atten[0] = 1.0f;
nuclear@4 72 atten[1] = 0.0f;
nuclear@4 73 atten[2] = 0.0f;
nuclear@21 74 radius = 1.0;
nuclear@4 75 }
nuclear@4 76
nuclear@4 77 void PointLight::set_position(const Vector3 &pos)
nuclear@4 78 {
nuclear@4 79 this->pos = pos;
nuclear@4 80 }
nuclear@4 81
nuclear@4 82 void PointLight::set_attenuation(float att_const, float att_lin, float att_quad)
nuclear@4 83 {
nuclear@4 84 atten[0] = att_const;
nuclear@4 85 atten[1] = att_lin;
nuclear@4 86 atten[2] = att_quad;
nuclear@4 87 }
nuclear@4 88
nuclear@21 89 void PointLight::set_radius(float rad)
nuclear@21 90 {
nuclear@21 91 radius = rad;
nuclear@21 92 }
nuclear@21 93
nuclear@21 94 float PointLight::get_radius() const
nuclear@21 95 {
nuclear@21 96 return radius;
nuclear@21 97 }
nuclear@21 98
nuclear@4 99 void PointLight::use(int id) const
nuclear@4 100 {
nuclear@4 101 float lpos[] = {pos.x, pos.y, pos.z, 1.0f};
nuclear@4 102 glLightfv(GL_LIGHT0 + id, GL_POSITION, lpos);
nuclear@4 103 glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, atten[0]);
nuclear@4 104 glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, atten[1]);
nuclear@4 105 glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, atten[2]);
nuclear@4 106 }
nuclear@4 107
nuclear@23 108 void PointLight::draw() const
nuclear@23 109 {
nuclear@23 110 //glMatrixMode(GL_MODELVIEW);
nuclear@23 111 //glPushMatrix();
nuclear@23 112 //glScalef(radius, radius, radius);
nuclear@23 113
nuclear@23 114 Light::draw();
nuclear@23 115
nuclear@23 116 //glPopMatrix();
nuclear@23 117 }
nuclear@23 118
nuclear@23 119
nuclear@23 120
nuclear@23 121 Vector3 sphvertex(float u, float v)
nuclear@23 122 {
nuclear@23 123 float theta = u * M_PI * 2.0;
nuclear@23 124 float phi = v * M_PI;
nuclear@23 125
nuclear@23 126 Vector3 res;
nuclear@23 127 res.x = sin(theta) * cos(phi);
nuclear@23 128 res.y = sin(theta);
nuclear@23 129 res.z = cos(theta) * cos(phi);
nuclear@23 130 return res;
nuclear@23 131 }
nuclear@23 132
nuclear@23 133
nuclear@23 134 bool PointLight::create_mesh()
nuclear@23 135 {
nuclear@23 136 const static int udiv = 8;
nuclear@23 137 const static int vdiv = 4;
nuclear@23 138
nuclear@23 139 int nquads = udiv * vdiv;
nuclear@23 140 num_faces = nquads * 2;
nuclear@23 141 int nverts = num_faces * 3;
nuclear@23 142
nuclear@23 143 float du = 1.0 / (float)udiv;
nuclear@23 144 float dv = 1.0 / (float)vdiv;
nuclear@23 145
nuclear@23 146 glGenBuffers(1, &vbo);
nuclear@23 147 glBindBuffer(GL_ARRAY_BUFFER, vbo);
nuclear@23 148 glBufferData(GL_ARRAY_BUFFER, nverts * sizeof(Vector3), 0, GL_STATIC_DRAW);
nuclear@23 149
nuclear@23 150 Vector3 *vptr = (Vector3*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
nuclear@23 151
nuclear@23 152 for(int i=0; i<vdiv; i++) {
nuclear@23 153 float v = (float)i / (float)vdiv;
nuclear@23 154 for(int j=0; j<udiv; j++) {
nuclear@23 155 float u = (float)j / (float)udiv;
nuclear@23 156
nuclear@23 157 *vptr++ = sphvertex(u, v);
nuclear@23 158 *vptr++ = sphvertex(u + du, v);
nuclear@23 159 *vptr++ = sphvertex(u, v + dv);
nuclear@23 160
nuclear@23 161 *vptr++ = sphvertex(u + du, v);
nuclear@23 162 *vptr++ = sphvertex(u + du, v + dv);
nuclear@23 163 *vptr++ = sphvertex(u, v + dv);
nuclear@23 164 }
nuclear@23 165 }
nuclear@23 166 glUnmapBuffer(GL_ARRAY_BUFFER);
nuclear@23 167 return true;
nuclear@23 168 }
nuclear@4 169
nuclear@4 170 void set_light(int id, const Light *lt)
nuclear@4 171 {
nuclear@4 172 if(lt) {
nuclear@4 173 glDisable(GL_LIGHT0 + id);
nuclear@4 174 } else {
nuclear@4 175 glEnable(GL_LIGHT0 + id);
nuclear@4 176 lt->use(id);
nuclear@4 177 }
nuclear@4 178 }