#include "material.h" Material::Material() { init(); setName (""); } Material::Material (char* mtlName) { init(); setName (mtlName); } Material* Material::addMaterial(char* mtlName) { if (verbose >=2) printf ("adding Material %s\n", mtlName); Material* newMat = new Material(mtlName); Material* tmpMat = this; while (tmpMat->nextMat != NULL) { tmpMat = tmpMat->nextMat; } tmpMat->nextMat = newMat; return newMat; } void Material::init(void) { if (verbose >= 3) printf ("initializing new Material\n"); nextMat = NULL; setIllum(1); setDiffuse(0,0,0); setAmbient(0,0,0); setSpecular(0,0,0); setTransparency(0.0); } void Material::setName (char* mtlName) { if (verbose >= 3) printf("setting Material Name to %s", mtlName); strcpy(name, mtlName); // printf ("adding new Material: %s, %p\n", this->getName(), this); } char* Material::getName (void) { return name; } void Material::setIllum (int illum) { if (verbose >= 3) printf("setting illumModel of Material %s to %i", name, illum); illumModel = illum; // printf ("setting illumModel to: %i\n", illumModel); } void Material::setIllum (char* illum) { setIllum (atoi(illum)); } void Material::setDiffuse (float r, float g, float b) { if (verbose >= 3) printf ("setting Diffuse Color of Material %s to r=%f g=%f b=%f\n", name, r, g, b); diffuse[0] = r; diffuse[1] = g; diffuse[2] = b; diffuse[3] = 1.0; } void Material::setDiffuse (char* rgb) { char r[20],g[20],b[20]; sscanf (rgb, "%s %s %s", r, g, b); setDiffuse (atof(r), atof(g), atof(b)); } void Material::setAmbient (float r, float g, float b) { if (verbose >=3) printf ("setting Ambient Color of Material %s to r=%f g=%f b=%f\n", name, r, g, b); ambient[0] = r; ambient[1] = g; ambient[2] = b; ambient[3] = 1.0; } void Material::setAmbient (char* rgb) { char r[20],g[20],b[20]; sscanf (rgb, "%s %s %s", r, g, b); setAmbient (atof(r), atof(g), atof(b)); } void Material::setSpecular (float r, float g, float b) { if (verbose >= 3) printf ("setting Specular Color of Material %s to r=%f g=%f b=%f\n", name, r, g, b); specular[0] = r; specular[1] = g; specular[2] = b; specular[3] = 1.0; } void Material::setSpecular (char* rgb) { char r[20],g[20],b[20]; sscanf (rgb, "%s %s %s", r, g, b); setSpecular (atof(r), atof(g), atof(b)); } void Material::setTransparency (float trans) { if (verbose >= 3) printf ("setting Transparency of Material %s to %f\n", name, trans); transparency = trans; } void Material::setTransparency (char* trans) { char tr[20]; sscanf (trans, "%s", tr); setTransparency (atof(tr)); } Material* Material::search (char* mtlName) { if (verbose >=3) printf ("Searching for material %s", mtlName); Material* searcher = this; while (searcher != NULL) { if (verbose >= 3) printf ("."); if (!strcmp (searcher->getName(), mtlName)) { if (verbose >= 3) printf ("found.\n"); return searcher; } searcher = searcher->nextMat; } return NULL; } bool Material::select (void) { // setting diffuse color // glColor3f (diffuse[0], diffuse[1], diffuse[2]); glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse); // setting ambient color glMaterialfv(GL_FRONT, GL_AMBIENT, ambient); // setting up Sprecular glMaterialfv(GL_FRONT, GL_SPECULAR, specular); // setting illumination Model if (illumModel == 1) glShadeModel(GL_FLAT); else if (illumModel >= 2) glShadeModel(GL_SMOOTH); }