dungeon_crawler

changeset 4:158de53b4e18

tile work
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 11 Aug 2012 05:44:52 +0300
parents 31e53fd79c2d
children 252a00508411
files prototype/src/color.h prototype/src/light.cc prototype/src/light.h prototype/src/mesh.cc prototype/src/mesh.h prototype/src/tile.cc prototype/src/tile.h
diffstat 7 files changed, 225 insertions(+), 1 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/prototype/src/color.h	Sat Aug 11 05:44:52 2012 +0300
     1.3 @@ -0,0 +1,8 @@
     1.4 +#ifndef COLOR_H_
     1.5 +#define COLOR_H_
     1.6 +
     1.7 +#include "vmath.h"
     1.8 +
     1.9 +typedef Vector4 Color;
    1.10 +
    1.11 +#endif	// COLOR_H_
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/prototype/src/light.cc	Sat Aug 11 05:44:52 2012 +0300
     2.3 @@ -0,0 +1,75 @@
     2.4 +#include "opengl.h"
     2.5 +#include "light.h"
     2.6 +
     2.7 +Light::Light(const Color &col)
     2.8 +	: color(col)
     2.9 +{
    2.10 +	intensity = 1.0;
    2.11 +}
    2.12 +
    2.13 +Light::~Light() {}
    2.14 +
    2.15 +void Light::set_intensity(float val)
    2.16 +{
    2.17 +	intensity = val;
    2.18 +}
    2.19 +
    2.20 +void Light::set_color(const Color &col)
    2.21 +{
    2.22 +	color = col;
    2.23 +}
    2.24 +
    2.25 +void Light::use(int id) const
    2.26 +{
    2.27 +	glLightfv(GL_LIGHT0 + id, GL_DIFFUSE, &color.x);
    2.28 +	glLightfv(GL_LIGHT0 + id, GL_SPECULAR, &color.x);
    2.29 +}
    2.30 +
    2.31 +
    2.32 +PointLight::PointLight()
    2.33 +{
    2.34 +	atten[0] = 1.0f;
    2.35 +	atten[1] = 0.0f;
    2.36 +	atten[2] = 0.0f;
    2.37 +}
    2.38 +
    2.39 +PointLight::PointLight(const Vector3 &pos, const Color &col)
    2.40 +	: Light(col)
    2.41 +{
    2.42 +	this->pos = pos;
    2.43 +	atten[0] = 1.0f;
    2.44 +	atten[1] = 0.0f;
    2.45 +	atten[2] = 0.0f;
    2.46 +}
    2.47 +
    2.48 +void PointLight::set_position(const Vector3 &pos)
    2.49 +{
    2.50 +	this->pos = pos;
    2.51 +}
    2.52 +
    2.53 +void PointLight::set_attenuation(float att_const, float att_lin, float att_quad)
    2.54 +{
    2.55 +	atten[0] = att_const;
    2.56 +	atten[1] = att_lin;
    2.57 +	atten[2] = att_quad;
    2.58 +}
    2.59 +
    2.60 +void PointLight::use(int id) const
    2.61 +{
    2.62 +	float lpos[] = {pos.x, pos.y, pos.z, 1.0f};
    2.63 +	glLightfv(GL_LIGHT0 + id, GL_POSITION, lpos);
    2.64 +	glLightf(GL_LIGHT0 + id, GL_CONSTANT_ATTENUATION, atten[0]);
    2.65 +	glLightf(GL_LIGHT0 + id, GL_LINEAR_ATTENUATION, atten[1]);
    2.66 +	glLightf(GL_LIGHT0 + id, GL_QUADRATIC_ATTENUATION, atten[2]);
    2.67 +}
    2.68 +
    2.69 +
    2.70 +void set_light(int id, const Light *lt)
    2.71 +{
    2.72 +	if(lt) {
    2.73 +		glDisable(GL_LIGHT0 + id);
    2.74 +	} else {
    2.75 +		glEnable(GL_LIGHT0 + id);
    2.76 +		lt->use(id);
    2.77 +	}
    2.78 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/prototype/src/light.h	Sat Aug 11 05:44:52 2012 +0300
     3.3 @@ -0,0 +1,53 @@
     3.4 +#ifndef LIGHT_H_
     3.5 +#define LIGHT_H_
     3.6 +
     3.7 +#include "color.h"
     3.8 +
     3.9 +class Light {
    3.10 +protected:
    3.11 +	float intensity;
    3.12 +	Color color;
    3.13 +
    3.14 +public:
    3.15 +	Light(const Color &col = 1.0);
    3.16 +	virtual ~Light();
    3.17 +
    3.18 +	virtual void set_intensity(float val);
    3.19 +	virtual void set_color(const Color &col);
    3.20 +
    3.21 +	virtual void use(int id = 0) const;
    3.22 +};
    3.23 +
    3.24 +class PointLight : public Light {
    3.25 +protected:
    3.26 +	Vector3 pos;
    3.27 +	float atten[3];
    3.28 +
    3.29 +public:
    3.30 +	PointLight();
    3.31 +	PointLight(const Vector3 &pos, const Color &col = 1.0);
    3.32 +
    3.33 +	void set_position(const Vector3 &pos);
    3.34 +	void set_attenuation(float att_const, float att_lin, float att_quad);
    3.35 +
    3.36 +	virtual void use(int id = 0) const;
    3.37 +};
    3.38 +
    3.39 +class DirLight : public Light {
    3.40 +protected:
    3.41 +	Vector3 dir;
    3.42 +
    3.43 +public:
    3.44 +	DirLight();
    3.45 +	DirLight(const Vector3 &dir, const Color &col = 1.0);
    3.46 +
    3.47 +	void set_direction(const Vector3 &dir);
    3.48 +
    3.49 +	virtual void use(int id = 0) const;
    3.50 +};
    3.51 +
    3.52 +
    3.53 +void set_light(int id, const Light *lt);
    3.54 +
    3.55 +
    3.56 +#endif	// LIGHT_H_
     4.1 --- a/prototype/src/mesh.cc	Fri Aug 10 04:45:38 2012 +0300
     4.2 +++ b/prototype/src/mesh.cc	Sat Aug 11 05:44:52 2012 +0300
     4.3 @@ -16,6 +16,11 @@
     4.4  	destroy();
     4.5  }
     4.6  
     4.7 +const char *Mesh::get_name() const
     4.8 +{
     4.9 +	return name.c_str();
    4.10 +}
    4.11 +
    4.12  bool Mesh::create(const aiScene *scn, aiMesh *aim)
    4.13  {
    4.14  	if(vbo[MESH_ATTR_VERTEX]) {
    4.15 @@ -52,7 +57,7 @@
    4.16  	/* map the buffer and fill it up */
    4.17  	unsigned int *iptr = (unsigned int*)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
    4.18  
    4.19 -	for(int i=0; i<nfaces; i++) {
    4.20 +	for(int i=0; i<(int)nfaces; i++) {
    4.21  		for(int j=0; j<3; j++) {
    4.22  			*iptr++ = aim->mFaces[i].mIndices[j];
    4.23  		}
     5.1 --- a/prototype/src/mesh.h	Fri Aug 10 04:45:38 2012 +0300
     5.2 +++ b/prototype/src/mesh.h	Sat Aug 11 05:44:52 2012 +0300
     5.3 @@ -30,6 +30,8 @@
     5.4  	Mesh();
     5.5  	~Mesh();
     5.6  
     5.7 +	const char *get_name() const;
     5.8 +
     5.9  	bool create(const aiScene *scn, aiMesh *aim);
    5.10  	void destroy();
    5.11  
     6.1 --- a/prototype/src/tile.cc	Fri Aug 10 04:45:38 2012 +0300
     6.2 +++ b/prototype/src/tile.cc	Sat Aug 11 05:44:52 2012 +0300
     6.3 @@ -1,5 +1,8 @@
     6.4  #include <stdio.h>
     6.5  #include "opengl.h"
     6.6 +#include <assimp/assimp.h>
     6.7 +#include <assimp/aiScene.h>
     6.8 +#include <assimp/aiPostProcess.h>
     6.9  #include "tile.h"
    6.10  
    6.11  bool Tile::load(const char *fname)
    6.12 @@ -7,6 +10,7 @@
    6.13  	unsigned int proc_flags = aiProcess_JoinIdenticalVertices |
    6.14  		aiProcess_PreTransformVertices | aiProcess_Triangulate |
    6.15  		aiProcess_GenNormals | aiProcess_SortByPType | aiProcess_FlipUVs;
    6.16 +
    6.17  	const aiScene *scn = aiImportFile(fname, proc_flags);
    6.18  	if(!scn) {
    6.19  		fprintf(stderr, "failed to load tile: %s\n", fname);
    6.20 @@ -23,5 +27,76 @@
    6.21  
    6.22  void Tile::draw(unsigned int drawmask) const
    6.23  {
    6.24 +	for(size_t i=0; i<meshes.size(); i++) {
    6.25 +		if(mesh_side[i] & draw_mask) {
    6.26 +			meshes[i].draw();
    6.27 +		}
    6.28 +	}
    6.29 +}
    6.30  
    6.31 +int Tile::load_lights(const aiScene *scn)
    6.32 +{
    6.33 +	int count = 0;
    6.34 +
    6.35 +	for(int i=0; i<(int)scn->mNumLights; i++) {
    6.36 +		Light *lt;
    6.37 +		aiLight *ailt = scn->mLights[i];
    6.38 +
    6.39 +		switch(ailt->mType) {
    6.40 +		case aiLightSource_POINT:
    6.41 +			lt = new PointLight(Vector3(ailt->mPosition.x, ailt->mPosition.y, ailt->mPosition.z));
    6.42 +			((PointLight*)lt)->set_attenuation(ailt->mAttenuationConstant, ailt->mAttenuationLinear,
    6.43 +					ailt->mAttenuationQuadratic);
    6.44 +			break;
    6.45 +
    6.46 +		case aiLightSource_DIRECTIONAL:
    6.47 +			lt = new PointLight(Vector3(ailt->mDirection.x, ailt->mDirection.y, ailt->mDirection.z));
    6.48 +			break;
    6.49 +
    6.50 +		default:
    6.51 +			continue;
    6.52 +		}
    6.53 +
    6.54 +		lt->set_color(Color(ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b, 1.0));
    6.55 +
    6.56 +		lights.push_back(lt);
    6.57 +		count++;
    6.58 +	}
    6.59 +
    6.60 +	return count;
    6.61  }
    6.62 +
    6.63 +int Tile::load_meshes(const aiScene *scn)
    6.64 +{
    6.65 +	int count = 0;
    6.66 +
    6.67 +	for(int i=0; i<(int)scn->mNumMeshes; i++) {
    6.68 +		Mesh *mesh = new Mesh;
    6.69 +		if(!mesh->create(scn, scn->mMeshes[i])) {
    6.70 +			delete mesh;
    6.71 +			continue;
    6.72 +		}
    6.73 +
    6.74 +		meshes.push_back(mesh);
    6.75 +
    6.76 +		// find which side is this mesh on
    6.77 +		const char *name = mesh->get_name();
    6.78 +		unsigned int side;
    6.79 +
    6.80 +		if(strstr(name, "NORTH") == name) {
    6.81 +			side = TILE_NORTH;
    6.82 +		} else if(strstr(name, "SOUTH") == name) {
    6.83 +			side = TILE_SOUTH;
    6.84 +		} else if(strstr(name, "EAST") == name) {
    6.85 +			side = TILE_EAST;
    6.86 +		} else if(strstr(name, "WEST") == name) {
    6.87 +			side = TILE_WEST;
    6.88 +		} else {
    6.89 +			side = TILE_ALL;
    6.90 +		}
    6.91 +		mesh_side.push_back(side);
    6.92 +
    6.93 +		count++;
    6.94 +	}
    6.95 +	return count;
    6.96 +}
     7.1 --- a/prototype/src/tile.h	Fri Aug 10 04:45:38 2012 +0300
     7.2 +++ b/prototype/src/tile.h	Sat Aug 11 05:44:52 2012 +0300
     7.3 @@ -2,7 +2,9 @@
     7.4  #define TILE_H_
     7.5  
     7.6  #include <vector>
     7.7 +#include <assimp/aiScene.h>
     7.8  #include "mesh.h"
     7.9 +#include "light.h"
    7.10  
    7.11  enum {
    7.12  	TILE_NORTH	= 1,
    7.13 @@ -15,8 +17,12 @@
    7.14  class Tile {
    7.15  private:
    7.16  	std::vector<Mesh*> meshes;
    7.17 +	std::vector<unsigned int> mesh_side;
    7.18  	std::vector<Light*> lights;
    7.19  
    7.20 +	int load_lights(const aiScene *scn);
    7.21 +	int load_meshes(const aiScene *scn);
    7.22 +
    7.23  public:
    7.24  	bool load(const char *fname);
    7.25