dungeon_crawler

changeset 11:e5567ddbf2ef

- Texture set (texture manager) - materials
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 19 Aug 2012 03:11:36 +0300
parents 22562582c82d
children e95462632f9a
files prototype/Makefile prototype/src/material.cc prototype/src/material.h prototype/src/mesh.cc prototype/src/mesh.h prototype/src/texman.cc prototype/src/texman.h prototype/src/tile.cc prototype/src/tile.h prototype/src/tileset.cc prototype/src/tileset.h
diffstat 11 files changed, 202 insertions(+), 2 deletions(-) [+]
line diff
     1.1 --- a/prototype/Makefile	Sat Aug 18 03:47:13 2012 +0300
     1.2 +++ b/prototype/Makefile	Sun Aug 19 03:11:36 2012 +0300
     1.3 @@ -6,7 +6,7 @@
     1.4  
     1.5  CFLAGS = -pedantic -Wall -g -Ivmath
     1.6  CXXFLAGS = $(CFLAGS) -std=c++11
     1.7 -LDFLAGS = $(libgl) -lm -lassimp
     1.8 +LDFLAGS = $(libgl) -lm -lassimp -limago
     1.9  
    1.10  ifeq ($(shell uname -s), Darwin)
    1.11  	libgl = -framework OpenGL -framework GLUT -lglew
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/prototype/src/material.cc	Sun Aug 19 03:11:36 2012 +0300
     2.3 @@ -0,0 +1,71 @@
     2.4 +#include <assert.h>
     2.5 +#include <assimp/material.h>
     2.6 +#include "opengl.h"
     2.7 +#include "material.h"
     2.8 +
     2.9 +Material::Material()
    2.10 +	: kd(1.0, 1.0, 1.0), ks(0.0, 0.0, 0.0)
    2.11 +{
    2.12 +	shin = 1.0;
    2.13 +	memset(tex, 0, sizeof tex);
    2.14 +}
    2.15 +
    2.16 +void Material::load(const aiMaterial *assmat, TextureSet *texset)
    2.17 +{
    2.18 +	aiColor4D col;
    2.19 +	float val;
    2.20 +
    2.21 +	aiGetMaterialColor(assmat, AI_MATKEY_COLOR_DIFFUSE, &col);
    2.22 +	kd = Color(col[0], col[1], col[2]);
    2.23 +	aiGetMaterialColor(assmat, AI_MATKEY_COLOR_SPECULAR, &col);
    2.24 +	ks = Color(col[0], col[1], col[2]);
    2.25 +
    2.26 +	unsigned int sz = 1;
    2.27 +	val = 60.0;
    2.28 +	aiGetMaterialFloatArray(assmat, AI_MATKEY_SHININESS, &val, &sz);
    2.29 +	if(val > 127) {
    2.30 +		printf("GOT SHININESS: %f\n", val);
    2.31 +		val = 127;
    2.32 +	}
    2.33 +	shin = val;
    2.34 +
    2.35 +	sz = 1;
    2.36 +	val = 1.0;
    2.37 +	aiGetMaterialFloatArray(assmat, AI_MATKEY_SHININESS_STRENGTH, &val, &sz);
    2.38 +	ks *= val;
    2.39 +
    2.40 +	// must match TEXTYPE enum in material.h
    2.41 +	unsigned int asstypes[] = {
    2.42 +		aiTextureType_DIFFUSE,
    2.43 +		aiTextureType_NORMALS,
    2.44 +		aiTextureType_SPECULAR
    2.45 +	};
    2.46 +
    2.47 +	for(int i=0; i<NUM_TEXTURE_TYPES; i++) {
    2.48 +		aiString tex_name;
    2.49 +		if(aiGetMaterialString(assmat, AI_MATKEY_TEXTURE(asstypes[i], 0), &tex_name) == 0) {
    2.50 +			tex[i] = texset->get_texture(tex_name.data);
    2.51 +		} else {
    2.52 +			tex[i] = 0;
    2.53 +		}
    2.54 +	}
    2.55 +}
    2.56 +
    2.57 +void Material::setup() const
    2.58 +{
    2.59 +	float dcol[] = {kd.x, kd.y, kd.z, 1.0};
    2.60 +	float scol[] = {ks.x, ks.y, ks.z, 1.0};
    2.61 +
    2.62 +	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol);
    2.63 +	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol);
    2.64 +	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
    2.65 +
    2.66 +	if(tex[TEXTYPE_DIFFUSE]) {
    2.67 +		glActiveTextureARB(GL_TEXTURE0);
    2.68 +		glBindTexture(GL_TEXTURE_2D, tex[TEXTYPE_DIFFUSE]);
    2.69 +		glEnable(GL_TEXTURE_2D);
    2.70 +	} else {
    2.71 +		glDisable(GL_TEXTURE_2D);
    2.72 +	}
    2.73 +	glActiveTextureARB(GL_TEXTURE0);
    2.74 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/prototype/src/material.h	Sun Aug 19 03:11:36 2012 +0300
     3.3 @@ -0,0 +1,29 @@
     3.4 +#ifndef MATERIAL_H_
     3.5 +#define MATERIAL_H_
     3.6 +
     3.7 +#include "color.h"
     3.8 +#include "texman.h"
     3.9 +
    3.10 +enum {
    3.11 +	TEXTYPE_DIFFUSE,
    3.12 +	TEXTYPE_NORMAL,
    3.13 +	TEXTYPE_SPECULAR,
    3.14 +
    3.15 +	NUM_TEXTURE_TYPES
    3.16 +};
    3.17 +
    3.18 +class Material {
    3.19 +private:
    3.20 +	Color kd, ks;
    3.21 +	double shin;
    3.22 +	unsigned int tex[NUM_TEXTURE_TYPES];
    3.23 +
    3.24 +public:
    3.25 +	Material();
    3.26 +
    3.27 +	void load(const aiMaterial *assmat, TextureSet *texset = 0);
    3.28 +
    3.29 +	void setup() const;
    3.30 +};
    3.31 +
    3.32 +#endif	// MATERIAL_H_
     4.1 --- a/prototype/src/mesh.cc	Sat Aug 18 03:47:13 2012 +0300
     4.2 +++ b/prototype/src/mesh.cc	Sun Aug 19 03:11:36 2012 +0300
     4.3 @@ -105,6 +105,16 @@
     4.4  	return xform;
     4.5  }
     4.6  
     4.7 +void Mesh::set_material(const Material &mat)
     4.8 +{
     4.9 +	this->mat = mat;
    4.10 +}
    4.11 +
    4.12 +const Material &Mesh::get_material() const
    4.13 +{
    4.14 +	return mat;
    4.15 +}
    4.16 +
    4.17  void Mesh::set_attrib_location(int attr, int loc)
    4.18  {
    4.19  	if(attr == MESH_ATTR_TANGENT) {
    4.20 @@ -119,6 +129,8 @@
    4.21  
    4.22  void Mesh::draw() const
    4.23  {
    4.24 +	mat.setup();
    4.25 +
    4.26  	glMatrixMode(GL_MODELVIEW);
    4.27  	glPushMatrix();
    4.28  	mult_matrix(xform);
     5.1 --- a/prototype/src/mesh.h	Sat Aug 18 03:47:13 2012 +0300
     5.2 +++ b/prototype/src/mesh.h	Sun Aug 19 03:11:36 2012 +0300
     5.3 @@ -4,6 +4,7 @@
     5.4  #include <string>
     5.5  #include <assimp/scene.h>
     5.6  #include "vmath.h"
     5.7 +#include "material.h"
     5.8  
     5.9  enum {
    5.10  	MESH_ATTR_VERTEX,
    5.11 @@ -26,6 +27,7 @@
    5.12  
    5.13  	int tang_loc;
    5.14  
    5.15 +	Material mat;
    5.16  	Matrix4x4 xform;
    5.17  
    5.18  public:
    5.19 @@ -41,6 +43,9 @@
    5.20  	void set_xform(const Matrix4x4 &mat);
    5.21  	const Matrix4x4 &get_xform() const;
    5.22  
    5.23 +	void set_material(const Material &mat);
    5.24 +	const Material &get_material() const;
    5.25 +
    5.26  	void set_attrib_location(int attr, int loc);
    5.27  	int get_attrib_location(int attr) const;
    5.28  
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/prototype/src/texman.cc	Sun Aug 19 03:11:36 2012 +0300
     6.3 @@ -0,0 +1,35 @@
     6.4 +#include <string.h>
     6.5 +#include "opengl.h"
     6.6 +#include "imago2.h"
     6.7 +#include "texman.h"
     6.8 +#include "datapath.h"
     6.9 +
    6.10 +TextureSet::~TextureSet()
    6.11 +{
    6.12 +	for(auto iter : textures) {
    6.13 +		glDeleteTextures(1, &iter.second);
    6.14 +	}
    6.15 +}
    6.16 +
    6.17 +unsigned int TextureSet::get_texture(const char *fname) const
    6.18 +{
    6.19 +	auto iter = textures.find(fname);
    6.20 +	if(iter != textures.end()) {
    6.21 +		return iter->second;
    6.22 +	}
    6.23 +
    6.24 +	const char *path, *slash;
    6.25 +	if((slash = strrchr(fname, '/'))) {
    6.26 +		path = slash + 1;
    6.27 +	}
    6.28 +	path = datafile_path(path);
    6.29 +
    6.30 +	printf("loading texture: %s\n", path);
    6.31 +	unsigned int tex = img_gltexture_load(path);
    6.32 +	if(tex) {
    6.33 +		textures[fname] = tex;
    6.34 +	} else {
    6.35 +		fprintf(stderr, "failed to load texture: %s\n", path);
    6.36 +	}
    6.37 +	return tex;
    6.38 +}
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/prototype/src/texman.h	Sun Aug 19 03:11:36 2012 +0300
     7.3 @@ -0,0 +1,17 @@
     7.4 +#ifndef TEXMAN_H_
     7.5 +#define TEXMAN_H_
     7.6 +
     7.7 +#include <string>
     7.8 +#include <map>
     7.9 +
    7.10 +class TextureSet {
    7.11 +private:
    7.12 +	mutable std::map<std::string, unsigned int> textures;
    7.13 +
    7.14 +public:
    7.15 +	~TextureSet();
    7.16 +
    7.17 +	unsigned int get_texture(const char *fname) const;
    7.18 +};
    7.19 +
    7.20 +#endif	// TEXMAN_H_
     8.1 --- a/prototype/src/tile.cc	Sat Aug 18 03:47:13 2012 +0300
     8.2 +++ b/prototype/src/tile.cc	Sun Aug 19 03:11:36 2012 +0300
     8.3 @@ -5,11 +5,16 @@
     8.4  #include <assimp/scene.h>
     8.5  #include <assimp/postprocess.h>
     8.6  #include "tile.h"
     8.7 +#include "tileset.h"
     8.8  
     8.9  using std::map;
    8.10  
    8.11  static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node);
    8.12  
    8.13 +Tile::Tile(TileSet *tileset)
    8.14 +{
    8.15 +	tset = tileset;
    8.16 +}
    8.17  
    8.18  bool Tile::load(const char *fname)
    8.19  {
    8.20 @@ -90,6 +95,10 @@
    8.21  			continue;
    8.22  		}
    8.23  
    8.24 +		Material mat;
    8.25 +		mat.load(scn->mMaterials[scn->mMeshes[i]->mMaterialIndex], tset->get_textures());
    8.26 +		mesh->set_material(mat);
    8.27 +
    8.28  		// retrieve the node pointer
    8.29  		const char *name = "<unknown>";
    8.30  
     9.1 --- a/prototype/src/tile.h	Sat Aug 18 03:47:13 2012 +0300
     9.2 +++ b/prototype/src/tile.h	Sun Aug 19 03:11:36 2012 +0300
     9.3 @@ -15,8 +15,12 @@
     9.4  	TILE_ALL	= 0xffff
     9.5  };
     9.6  
     9.7 +class TileSet;
     9.8 +
     9.9  class Tile {
    9.10  private:
    9.11 +	TileSet *tset;
    9.12 +
    9.13  	std::vector<Mesh*> meshes;
    9.14  	std::vector<unsigned int> mesh_side;
    9.15  	std::vector<Light*> lights;
    9.16 @@ -25,6 +29,8 @@
    9.17  	int load_meshes(const aiScene *scn, const std::map<aiMesh*, aiNode*> &nmap);
    9.18  
    9.19  public:
    9.20 +	Tile(TileSet *tileset = 0);
    9.21 +
    9.22  	bool load(const char *fname);
    9.23  
    9.24  	void draw(unsigned int drawmask) const;
    10.1 --- a/prototype/src/tileset.cc	Sat Aug 18 03:47:13 2012 +0300
    10.2 +++ b/prototype/src/tileset.cc	Sun Aug 19 03:11:36 2012 +0300
    10.3 @@ -42,7 +42,7 @@
    10.4  		*tilefile++ = 0;
    10.5  
    10.6  		printf("Tileset %s, loading tile \"%s\" -> %s\n", fname, line, tilefile);
    10.7 -		Tile *tile = new Tile;
    10.8 +		Tile *tile = new Tile(this);
    10.9  		if(!tile->load(datafile_path(tilefile))) {
   10.10  			fprintf(stderr, "failed to load tile: %s\n", tilefile);
   10.11  			delete tile;
   10.12 @@ -56,6 +56,16 @@
   10.13  	return true;
   10.14  }
   10.15  
   10.16 +TextureSet *TileSet::get_textures()
   10.17 +{
   10.18 +	return &texset;
   10.19 +}
   10.20 +
   10.21 +const TextureSet *TileSet::get_textures() const
   10.22 +{
   10.23 +	return &texset;
   10.24 +}
   10.25 +
   10.26  Tile *TileSet::get_tile(const char *name) const
   10.27  {
   10.28  	auto res = tiles.find(name);
    11.1 --- a/prototype/src/tileset.h	Sat Aug 18 03:47:13 2012 +0300
    11.2 +++ b/prototype/src/tileset.h	Sun Aug 19 03:11:36 2012 +0300
    11.3 @@ -4,16 +4,22 @@
    11.4  #include <string>
    11.5  #include <map>
    11.6  #include "tile.h"
    11.7 +#include "texman.h"
    11.8  
    11.9  class TileSet {
   11.10  private:
   11.11  	std::map<std::string, Tile*> tiles;
   11.12  
   11.13 +	TextureSet texset;
   11.14 +
   11.15  public:
   11.16  	~TileSet();
   11.17  
   11.18  	bool load(const char *fname);
   11.19  
   11.20 +	TextureSet *get_textures();
   11.21 +	const TextureSet *get_textures() const;
   11.22 +
   11.23  	Tile *get_tile(const char *name) const;
   11.24  };
   11.25