#ifndef __GG_SIMPLESHADER_H__ #define __GG_SIMPLESHADER_H__ /* ** 単純な陰影付けを行うシェーダ */ #include "Gg.h" namespace gg { // データの格納 inline void set(GLfloat *p, GLfloat p0, GLfloat p1, GLfloat p2, GLfloat p3) { p[0] = p0; p[1] = p1; p[2] = p2; p[3] = p3; } inline void set(GLfloat *p, const GLfloat *q) { p[0] = q[0]; p[1] = q[1]; p[2] = q[2]; p[3] = q[3]; } // 光源 class GgSimpleLight { // 光源パラメータ GLfloat ldiff[4]; // 光源の拡散反射光成分 GLfloat lspec[4]; // 光源の鏡面反射光成分 GLfloat lamb[4]; // 光源の環境光成分 GLfloat lpos[4]; // 光源の位置 public: // デストラクタ virtual ~GgSimpleLight(void) {} // コンストラクタ GgSimpleLight(void) {} // 光源パラメータの設定 void setDiffuse(const GLfloat *diff) { set(ldiff, diff); } void setDiffuse(GLfloat r, GLfloat g, GLfloat b, GLfloat a = 1.0f) { set(ldiff, r, g, b, a); } void setSpecular(const GLfloat *spec) { set(lspec, spec); } void setSpecular(GLfloat r, GLfloat g, GLfloat b, GLfloat a = 1.0f) { set(lspec, r, g, b, a); } void setAmbient(const GLfloat *amb) { set(lamb, amb); } void setAmbient(GLfloat r, GLfloat g, GLfloat b, GLfloat a = 1.0f) { set(lamb, r, g, b, a); } void setPosition(const GLfloat *pos) { set(lpos, pos); } void setPosition(GLfloat x, GLfloat y, GLfloat z, GLfloat w = 1.0f) { set(lpos, x, y, z, w); } // 光源パラメータの取得 const GLfloat *getDiffuse(void) const { return ldiff; } const GLfloat *getSpecular(void) const { return lspec; } const GLfloat *getAmbient(void) const { return lamb; } const GLfloat *getPosition(void) const { return lpos; } }; // 材質 class GgSimpleMaterial { // 材質パラメータ GLfloat kdiff[4]; // 材質の拡散反射係数 GLfloat kspec[4]; // 材質の鏡面反射係数 GLfloat kamb[4]; // 材質の環境光に対する反射係数 GLfloat kshi; // 材質の輝き係数 public: // デストラクタ virtual ~GgSimpleMaterial(void) {} // コンストラクタ GgSimpleMaterial(void) {} // 光源パラメータの設定 void setDiffuse(const GLfloat *diff) { set(kdiff, diff); } void setDiffuse(GLfloat r, GLfloat g, GLfloat b, GLfloat a = 1.0f) { set(kdiff, r, g, b, a); } void setSpecular(const GLfloat *spec) { set(kspec, spec); } void setSpecular(GLfloat r, GLfloat g, GLfloat b, GLfloat a = 1.0f) { set(kspec, r, g, b, a); } void setAmbient(const GLfloat *amb) { set(kamb, amb); } void setAmbient(GLfloat r, GLfloat g, GLfloat b, GLfloat a = 1.0f) { set(kamb, r, g, b, a); } void setShininess(const GLfloat *shi) { kshi = *shi; } void setShininess(GLfloat shi) { kshi = shi; } // 材質パラメータの適用 const GLfloat *getDiffuse(void) const { return kdiff; } const GLfloat *getSpecular(void) const { return kspec; } const GLfloat *getAmbient(void) const { return kamb; } const GLfloat getShininess(void) const { return kshi; } }; // シェーダ class GgSimpleShader : public GgShader { // attribute 変数の場所 GLint pvLoc; // 位置 GLint nvLoc; // 法線 // 変換行列の uniform 変数の場所 GLint mwLoc; // モデルビュー変換行列 GLint mcLoc; // モデルビュー投影変換行列 GLint mgLoc; // 法線変換行列 // 光源パラメータの uniform 変数の場所 GLint ldiffLoc; // 拡散反射光成分 GLint lspecLoc; // 鏡面反射光成分 GLint lambLoc; // 環境光成分 GLint lposLoc; // 位置 // 材質パラメータの uniform 変数の場所 GLint kdiffLoc; // 拡散反射係数 GLint kspecLoc; // 鏡面反射係数 GLint kambLoc; // 環境光に対する反射係数 GLint kshiLoc; // 輝き係数 public: // デストラクタ virtual ~GgSimpleShader(void) {} // コンストラクタ GgSimpleShader( const GLchar *vert = "GgSimpleShader.vert", const GLchar *frag = "GgSimpleShader.frag", const GLchar *pv = "pv", const GLchar *nv = "nv", const GLchar *mw = "mw", const GLchar *mc = "mc", const GLchar *mg = "mg", const GLchar *ldiff = "ldiff", const GLchar *lspec = "lspec", const GLchar *lamb = "lamb", const GLchar *lpos = "lpos", const GLchar *kdiff = "kdiff", const GLchar *kspec = "kspec", const GLchar *kamb = "kamb", const GLchar *kshi = "kshi" ); // 光源 const GgSimpleLight *light; // 材質 const GgSimpleMaterial *material; // attribute 変数 pv の場所を得る GLint getPvLoc(void) { return pvLoc; } // attribute 変数 nv の場所を得る GLint getNvLoc(void) { return nvLoc; } // シェーダプログラムの適用 void use(GgMatrix &projection, GgMatrix &modelview) const; }; } #endif