# HG changeset patch # User John Tsiombikas # Date 1344653092 -10800 # Node ID 158de53b4e181da620e4e74e3f892393082edc2b # Parent 31e53fd79c2da3e6f9a81a1f25e22ab15bd65a8f tile work diff -r 31e53fd79c2d -r 158de53b4e18 prototype/src/color.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/prototype/src/color.h Sat Aug 11 05:44:52 2012 +0300 @@ -0,0 +1,8 @@ +#ifndef COLOR_H_ +#define COLOR_H_ + +#include "vmath.h" + +typedef Vector4 Color; + +#endif // COLOR_H_ diff -r 31e53fd79c2d -r 158de53b4e18 prototype/src/light.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/prototype/src/light.cc Sat Aug 11 05:44:52 2012 +0300 @@ -0,0 +1,75 @@ +#include "opengl.h" +#include "light.h" + +Light::Light(const Color &col) + : color(col) +{ + intensity = 1.0; +} + +Light::~Light() {} + +void Light::set_intensity(float val) +{ + intensity = val; +} + +void Light::set_color(const Color &col) +{ + color = col; +} + +void Light::use(int id) const +{ + glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, &color.x); + glLightfv(GL_LIGHT0 + id, GL_SPECULAR, &color.x); +} + + +PointLight::PointLight() +{ + atten[0] = 1.0f; + atten[1] = 0.0f; + atten[2] = 0.0f; +} + +PointLight::PointLight(const Vector3 &pos, const Color &col) + : Light(col) +{ + this->pos = pos; + atten[0] = 1.0f; + atten[1] = 0.0f; + atten[2] = 0.0f; +} + +void PointLight::set_position(const Vector3 &pos) +{ + this->pos = pos; +} + +void PointLight::set_attenuation(float att_const, float att_lin, float att_quad) +{ + atten[0] = att_const; + atten[1] = att_lin; + atten[2] = att_quad; +} + +void PointLight::use(int id) const +{ + float lpos[] = {pos.x, pos.y, pos.z, 1.0f}; + glLightfv(GL_LIGHT0 + id, GL_POSITION, lpos); + glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, atten[0]); + glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, atten[1]); + glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, atten[2]); +} + + +void set_light(int id, const Light *lt) +{ + if(lt) { + glDisable(GL_LIGHT0 + id); + } else { + glEnable(GL_LIGHT0 + id); + lt->use(id); + } +} diff -r 31e53fd79c2d -r 158de53b4e18 prototype/src/light.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/prototype/src/light.h Sat Aug 11 05:44:52 2012 +0300 @@ -0,0 +1,53 @@ +#ifndef LIGHT_H_ +#define LIGHT_H_ + +#include "color.h" + +class Light { +protected: + float intensity; + Color color; + +public: + Light(const Color &col = 1.0); + virtual ~Light(); + + virtual void set_intensity(float val); + virtual void set_color(const Color &col); + + virtual void use(int id = 0) const; +}; + +class PointLight : public Light { +protected: + Vector3 pos; + float atten[3]; + +public: + PointLight(); + PointLight(const Vector3 &pos, const Color &col = 1.0); + + void set_position(const Vector3 &pos); + void set_attenuation(float att_const, float att_lin, float att_quad); + + virtual void use(int id = 0) const; +}; + +class DirLight : public Light { +protected: + Vector3 dir; + +public: + DirLight(); + DirLight(const Vector3 &dir, const Color &col = 1.0); + + void set_direction(const Vector3 &dir); + + virtual void use(int id = 0) const; +}; + + +void set_light(int id, const Light *lt); + + +#endif // LIGHT_H_ diff -r 31e53fd79c2d -r 158de53b4e18 prototype/src/mesh.cc --- a/prototype/src/mesh.cc Fri Aug 10 04:45:38 2012 +0300 +++ b/prototype/src/mesh.cc Sat Aug 11 05:44:52 2012 +0300 @@ -16,6 +16,11 @@ destroy(); } +const char *Mesh::get_name() const +{ + return name.c_str(); +} + bool Mesh::create(const aiScene *scn, aiMesh *aim) { if(vbo[MESH_ATTR_VERTEX]) { @@ -52,7 +57,7 @@ /* map the buffer and fill it up */ unsigned int *iptr = (unsigned int*)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY); - for(int i=0; imFaces[i].mIndices[j]; } diff -r 31e53fd79c2d -r 158de53b4e18 prototype/src/mesh.h --- a/prototype/src/mesh.h Fri Aug 10 04:45:38 2012 +0300 +++ b/prototype/src/mesh.h Sat Aug 11 05:44:52 2012 +0300 @@ -30,6 +30,8 @@ Mesh(); ~Mesh(); + const char *get_name() const; + bool create(const aiScene *scn, aiMesh *aim); void destroy(); diff -r 31e53fd79c2d -r 158de53b4e18 prototype/src/tile.cc --- a/prototype/src/tile.cc Fri Aug 10 04:45:38 2012 +0300 +++ b/prototype/src/tile.cc Sat Aug 11 05:44:52 2012 +0300 @@ -1,5 +1,8 @@ #include #include "opengl.h" +#include +#include +#include #include "tile.h" bool Tile::load(const char *fname) @@ -7,6 +10,7 @@ unsigned int proc_flags = aiProcess_JoinIdenticalVertices | aiProcess_PreTransformVertices | aiProcess_Triangulate | aiProcess_GenNormals | aiProcess_SortByPType | aiProcess_FlipUVs; + const aiScene *scn = aiImportFile(fname, proc_flags); if(!scn) { fprintf(stderr, "failed to load tile: %s\n", fname); @@ -23,5 +27,76 @@ void Tile::draw(unsigned int drawmask) const { + for(size_t i=0; imNumLights; i++) { + Light *lt; + aiLight *ailt = scn->mLights[i]; + + switch(ailt->mType) { + case aiLightSource_POINT: + lt = new PointLight(Vector3(ailt->mPosition.x, ailt->mPosition.y, ailt->mPosition.z)); + ((PointLight*)lt)->set_attenuation(ailt->mAttenuationConstant, ailt->mAttenuationLinear, + ailt->mAttenuationQuadratic); + break; + + case aiLightSource_DIRECTIONAL: + lt = new PointLight(Vector3(ailt->mDirection.x, ailt->mDirection.y, ailt->mDirection.z)); + break; + + default: + continue; + } + + lt->set_color(Color(ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b, 1.0)); + + lights.push_back(lt); + count++; + } + + return count; } + +int Tile::load_meshes(const aiScene *scn) +{ + int count = 0; + + for(int i=0; i<(int)scn->mNumMeshes; i++) { + Mesh *mesh = new Mesh; + if(!mesh->create(scn, scn->mMeshes[i])) { + delete mesh; + continue; + } + + meshes.push_back(mesh); + + // find which side is this mesh on + const char *name = mesh->get_name(); + unsigned int side; + + if(strstr(name, "NORTH") == name) { + side = TILE_NORTH; + } else if(strstr(name, "SOUTH") == name) { + side = TILE_SOUTH; + } else if(strstr(name, "EAST") == name) { + side = TILE_EAST; + } else if(strstr(name, "WEST") == name) { + side = TILE_WEST; + } else { + side = TILE_ALL; + } + mesh_side.push_back(side); + + count++; + } + return count; +} diff -r 31e53fd79c2d -r 158de53b4e18 prototype/src/tile.h --- a/prototype/src/tile.h Fri Aug 10 04:45:38 2012 +0300 +++ b/prototype/src/tile.h Sat Aug 11 05:44:52 2012 +0300 @@ -2,7 +2,9 @@ #define TILE_H_ #include +#include #include "mesh.h" +#include "light.h" enum { TILE_NORTH = 1, @@ -15,8 +17,12 @@ class Tile { private: std::vector meshes; + std::vector mesh_side; std::vector lights; + int load_lights(const aiScene *scn); + int load_meshes(const aiScene *scn); + public: bool load(const char *fname);