# HG changeset patch # User John Tsiombikas # Date 1345335096 -10800 # Node ID e5567ddbf2efe2e644ded09a79333c90ee169649 # Parent 22562582c82de72792e45fc6b97f0f6eeac55ec4 - Texture set (texture manager) - materials diff -r 22562582c82d -r e5567ddbf2ef prototype/Makefile --- a/prototype/Makefile Sat Aug 18 03:47:13 2012 +0300 +++ b/prototype/Makefile Sun Aug 19 03:11:36 2012 +0300 @@ -6,7 +6,7 @@ CFLAGS = -pedantic -Wall -g -Ivmath CXXFLAGS = $(CFLAGS) -std=c++11 -LDFLAGS = $(libgl) -lm -lassimp +LDFLAGS = $(libgl) -lm -lassimp -limago ifeq ($(shell uname -s), Darwin) libgl = -framework OpenGL -framework GLUT -lglew diff -r 22562582c82d -r e5567ddbf2ef prototype/src/material.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/prototype/src/material.cc Sun Aug 19 03:11:36 2012 +0300 @@ -0,0 +1,71 @@ +#include +#include +#include "opengl.h" +#include "material.h" + +Material::Material() + : kd(1.0, 1.0, 1.0), ks(0.0, 0.0, 0.0) +{ + shin = 1.0; + memset(tex, 0, sizeof tex); +} + +void Material::load(const aiMaterial *assmat, TextureSet *texset) +{ + aiColor4D col; + float val; + + aiGetMaterialColor(assmat, AI_MATKEY_COLOR_DIFFUSE, &col); + kd = Color(col[0], col[1], col[2]); + aiGetMaterialColor(assmat, AI_MATKEY_COLOR_SPECULAR, &col); + ks = Color(col[0], col[1], col[2]); + + unsigned int sz = 1; + val = 60.0; + aiGetMaterialFloatArray(assmat, AI_MATKEY_SHININESS, &val, &sz); + if(val > 127) { + printf("GOT SHININESS: %f\n", val); + val = 127; + } + shin = val; + + sz = 1; + val = 1.0; + aiGetMaterialFloatArray(assmat, AI_MATKEY_SHININESS_STRENGTH, &val, &sz); + ks *= val; + + // must match TEXTYPE enum in material.h + unsigned int asstypes[] = { + aiTextureType_DIFFUSE, + aiTextureType_NORMALS, + aiTextureType_SPECULAR + }; + + for(int i=0; iget_texture(tex_name.data); + } else { + tex[i] = 0; + } + } +} + +void Material::setup() const +{ + float dcol[] = {kd.x, kd.y, kd.z, 1.0}; + float scol[] = {ks.x, ks.y, ks.z, 1.0}; + + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, dcol); + glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, scol); + glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin); + + if(tex[TEXTYPE_DIFFUSE]) { + glActiveTextureARB(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, tex[TEXTYPE_DIFFUSE]); + glEnable(GL_TEXTURE_2D); + } else { + glDisable(GL_TEXTURE_2D); + } + glActiveTextureARB(GL_TEXTURE0); +} diff -r 22562582c82d -r e5567ddbf2ef prototype/src/material.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/prototype/src/material.h Sun Aug 19 03:11:36 2012 +0300 @@ -0,0 +1,29 @@ +#ifndef MATERIAL_H_ +#define MATERIAL_H_ + +#include "color.h" +#include "texman.h" + +enum { + TEXTYPE_DIFFUSE, + TEXTYPE_NORMAL, + TEXTYPE_SPECULAR, + + NUM_TEXTURE_TYPES +}; + +class Material { +private: + Color kd, ks; + double shin; + unsigned int tex[NUM_TEXTURE_TYPES]; + +public: + Material(); + + void load(const aiMaterial *assmat, TextureSet *texset = 0); + + void setup() const; +}; + +#endif // MATERIAL_H_ diff -r 22562582c82d -r e5567ddbf2ef prototype/src/mesh.cc --- a/prototype/src/mesh.cc Sat Aug 18 03:47:13 2012 +0300 +++ b/prototype/src/mesh.cc Sun Aug 19 03:11:36 2012 +0300 @@ -105,6 +105,16 @@ return xform; } +void Mesh::set_material(const Material &mat) +{ + this->mat = mat; +} + +const Material &Mesh::get_material() const +{ + return mat; +} + void Mesh::set_attrib_location(int attr, int loc) { if(attr == MESH_ATTR_TANGENT) { @@ -119,6 +129,8 @@ void Mesh::draw() const { + mat.setup(); + glMatrixMode(GL_MODELVIEW); glPushMatrix(); mult_matrix(xform); diff -r 22562582c82d -r e5567ddbf2ef prototype/src/mesh.h --- a/prototype/src/mesh.h Sat Aug 18 03:47:13 2012 +0300 +++ b/prototype/src/mesh.h Sun Aug 19 03:11:36 2012 +0300 @@ -4,6 +4,7 @@ #include #include #include "vmath.h" +#include "material.h" enum { MESH_ATTR_VERTEX, @@ -26,6 +27,7 @@ int tang_loc; + Material mat; Matrix4x4 xform; public: @@ -41,6 +43,9 @@ void set_xform(const Matrix4x4 &mat); const Matrix4x4 &get_xform() const; + void set_material(const Material &mat); + const Material &get_material() const; + void set_attrib_location(int attr, int loc); int get_attrib_location(int attr) const; diff -r 22562582c82d -r e5567ddbf2ef prototype/src/texman.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/prototype/src/texman.cc Sun Aug 19 03:11:36 2012 +0300 @@ -0,0 +1,35 @@ +#include +#include "opengl.h" +#include "imago2.h" +#include "texman.h" +#include "datapath.h" + +TextureSet::~TextureSet() +{ + for(auto iter : textures) { + glDeleteTextures(1, &iter.second); + } +} + +unsigned int TextureSet::get_texture(const char *fname) const +{ + auto iter = textures.find(fname); + if(iter != textures.end()) { + return iter->second; + } + + const char *path, *slash; + if((slash = strrchr(fname, '/'))) { + path = slash + 1; + } + path = datafile_path(path); + + printf("loading texture: %s\n", path); + unsigned int tex = img_gltexture_load(path); + if(tex) { + textures[fname] = tex; + } else { + fprintf(stderr, "failed to load texture: %s\n", path); + } + return tex; +} diff -r 22562582c82d -r e5567ddbf2ef prototype/src/texman.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/prototype/src/texman.h Sun Aug 19 03:11:36 2012 +0300 @@ -0,0 +1,17 @@ +#ifndef TEXMAN_H_ +#define TEXMAN_H_ + +#include +#include + +class TextureSet { +private: + mutable std::map textures; + +public: + ~TextureSet(); + + unsigned int get_texture(const char *fname) const; +}; + +#endif // TEXMAN_H_ diff -r 22562582c82d -r e5567ddbf2ef prototype/src/tile.cc --- a/prototype/src/tile.cc Sat Aug 18 03:47:13 2012 +0300 +++ b/prototype/src/tile.cc Sun Aug 19 03:11:36 2012 +0300 @@ -5,11 +5,16 @@ #include #include #include "tile.h" +#include "tileset.h" using std::map; static void build_nodemap(map *nmap, const aiScene *scn, aiNode *node); +Tile::Tile(TileSet *tileset) +{ + tset = tileset; +} bool Tile::load(const char *fname) { @@ -90,6 +95,10 @@ continue; } + Material mat; + mat.load(scn->mMaterials[scn->mMeshes[i]->mMaterialIndex], tset->get_textures()); + mesh->set_material(mat); + // retrieve the node pointer const char *name = ""; diff -r 22562582c82d -r e5567ddbf2ef prototype/src/tile.h --- a/prototype/src/tile.h Sat Aug 18 03:47:13 2012 +0300 +++ b/prototype/src/tile.h Sun Aug 19 03:11:36 2012 +0300 @@ -15,8 +15,12 @@ TILE_ALL = 0xffff }; +class TileSet; + class Tile { private: + TileSet *tset; + std::vector meshes; std::vector mesh_side; std::vector lights; @@ -25,6 +29,8 @@ int load_meshes(const aiScene *scn, const std::map &nmap); public: + Tile(TileSet *tileset = 0); + bool load(const char *fname); void draw(unsigned int drawmask) const; diff -r 22562582c82d -r e5567ddbf2ef prototype/src/tileset.cc --- a/prototype/src/tileset.cc Sat Aug 18 03:47:13 2012 +0300 +++ b/prototype/src/tileset.cc Sun Aug 19 03:11:36 2012 +0300 @@ -42,7 +42,7 @@ *tilefile++ = 0; printf("Tileset %s, loading tile \"%s\" -> %s\n", fname, line, tilefile); - Tile *tile = new Tile; + Tile *tile = new Tile(this); if(!tile->load(datafile_path(tilefile))) { fprintf(stderr, "failed to load tile: %s\n", tilefile); delete tile; @@ -56,6 +56,16 @@ return true; } +TextureSet *TileSet::get_textures() +{ + return &texset; +} + +const TextureSet *TileSet::get_textures() const +{ + return &texset; +} + Tile *TileSet::get_tile(const char *name) const { auto res = tiles.find(name); diff -r 22562582c82d -r e5567ddbf2ef prototype/src/tileset.h --- a/prototype/src/tileset.h Sat Aug 18 03:47:13 2012 +0300 +++ b/prototype/src/tileset.h Sun Aug 19 03:11:36 2012 +0300 @@ -4,16 +4,22 @@ #include #include #include "tile.h" +#include "texman.h" class TileSet { private: std::map tiles; + TextureSet texset; + public: ~TileSet(); bool load(const char *fname); + TextureSet *get_textures(); + const TextureSet *get_textures() const; + Tile *get_tile(const char *name) const; };