|
|
I use the following way to do much the same :
first i have a material class :
// kaae@daimi.au.dk
#pragma once
#include "textureimage.h"
class Material
{
protected:
enum
{
RENDER_TEXTUREMAP,
RENDER_ENVMAP,
RENDER_WIRE,
RENDER_PARTICLE,
RENDER_FLAT,
RENDER_TRANSPARENT,
RENDER_TRANSPARENT_ADDITIV,
RENDER_ALPHA_MASK,
RENDER_SEE_THROUGH,
CULL_FRONT,
CULL_BACK,
CULL_NONE
};
char RenderTexturemap;
char RenderEnvmap;
char RenderWire;
char RenderParticle;
char RenderFlat;
char RenderTransparent;
char RenderTransparentAdditiv;
char RenderAlphaMask;
char RenderSeeThrough;
char CullFront, CullBack, CullNone;
float *SolidColor; // rgba
float *WireColor;
float ParticleSize;
TextureImage *Texture;
TextureImage *Envmap;
public:
Material();
void Enable(int what); // enable one of the RENDER_? features
void Disable(int what); // disable - - - - -
void SetSolidColor(float r, float g, float b, float a); // set the solid color for this material
void SetWireColor(float r, float g, float b, float a);
void SetTexture(TextureImage *texture); // set texture
void SetEnvmap(TextureImage *envmap); // set envmap
void SetParticleSize(float s);
friend class Object;
};
secondly i (ofcourse) have my object class :
// kaae@daimi.au.dk
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "vector.h"
#include "material.h"
#include "texturecoord.h"
class Object
{
protected:
int displaylist;
Vector *normals;
Vector *vertices;
TextureCoord *mapping;
unsigned long numVertices;
unsigned long *faces;
unsigned long numFaces;
Vector *faceNormals;
Material *material;
Vector pivot;
Vector rotation;
Vector translate;
struct _bbox
{
Vector min, max;
} BoundingBox;
struct _bsphere
{
Vector center;
float radius;
} BoundingSphere;
Vector scale;
char name[255];
public:
Object();
Object(unsigned long nverts, unsigned long nfaces);
~Object();
void Clone(Object *o); // copy another object
void Allocate(unsigned long nverts, unsigned long nfaces);
void CalcPivot(); // finds the center of this object
void SetPivot(Vector pivot); // sets - - - - -
void SetRotation(Vector rotation); // sets the rotation
void SetRotation(float rx, float ry, float rz); // - - -
void SetScale(Vector scale); // scaling
void SetScale(float sx, float sy, float sz);
void SetScale(float s);
void SetTranslation(Vector trans); // set translation point
void SetTranslation(float x, float y, float z); // - - -
void SetMaterial(Material *mat); // set the material
void virtual Paint(); // paint!
void SetName(char *name); // set this object's name
char NameEquals(char *name); // ask if this object's name is the same as input
void CalculateBoundingSphere(); // sphere ;-)
void PaintBoundingBox();
void CalculateBoundingBox();
void CalculateFaceNormals();
void CalculateNormals();
private:
void UploadVertices();
void virtual PaintTexture();
void virtual PaintEnvmap();
void virtual PaintTextureEnvmap();
void virtual PaintParticle();
void virtual PaintFlat();
void virtual PaintWire();
void EnableCulling();
void DisableCulling();
void EnableTransparency();
void DisableTransparency();
void EnableRotation();
void DisableRotation();
void EnableScaling();
void DisableScaling();
void EnableTranslation();
void DisableTranslation();
void EnableAlphaMask();
void DisableAlphaMask();
void EnableSeeThrough();
void DisableSeeThrough();
};
My object::paint() looks like this :
void Object::Paint()
{
if (material==NULL) return;
EnableCulling();
EnableTranslation();
EnableRotation();
EnableScaling();
if (material->RenderSeeThrough) EnableSeeThrough();
if (material->RenderAlphaMask) EnableAlphaMask();
if (material->RenderTransparent || material->RenderTransparentAdditiv) EnableTransparency();
if (material->RenderWire) PaintWire();
if (material->RenderFlat) PaintFlat();
if (material->RenderTexturemap && material->RenderEnvmap) PaintTextureEnvmap();
else
{
if (material->RenderEnvmap) PaintEnvmap();
if (material->RenderTexturemap) PaintTexture();
}
if (material->RenderParticle) PaintParticle();
if (material->RenderTransparent || material->RenderTransparentAdditiv) DisableTransparency();
if (material->RenderAlphaMask) DisableAlphaMask();
if (material->RenderSeeThrough) DisableSeeThrough();
DisableScaling();
DisableRotation();
DisableTranslation();
DisableCulling();
}
And finally a little code snippet for using it :
...
Object *object = ObjectGenerator::Pyramid(Vector(0.0f,0.0f,0.0f), 50.0);
Material *material = new Material;
material->Enable(Material::RENDER_TEXTUREMAP);
material->Enable(Material::RENDER_ENVMAP);
material->Enable(Material::CULL_NONE);
material->Enable(Material::RENDER_TRANSPARENT);
material->SetTexture(TextureImage::DetectAndLoad("data/texture.jpg"));
material->SetTexture(TextureImage::DetectAndLoad("data/env.jpg"));
object->SetMaterial(material);
object->CalculateNormals();
...
while (1)
{
...
object->Paint();
...
}
i like it (sorry for the long post).
|