MGCL V10  V10
MGCL V10
 全て クラス 名前空間 関数 変数 型定義 列挙型 列挙値 フレンド グループ ページ
glslprogram.h
1 #ifndef _MGGLSLPROGRAM_H
2 #define _MGGLSLPROGRAM_H
3 
4 class mgStaticGLAttrib;
5 class MGColor;
6 class mgGLSLProgram;
7 
8 #include "mg/MGCL.h"
9 #include <string>
10 #include <glm/glm.hpp>
11 #include <iostream>
18 namespace mgGLSL{
19 
20 // function ID
25 typedef enum {
26  Primitive = 0,
27  Texture = 11,
29 } DrawType;
30 
35 typedef enum {
37  Select = 1,
38  Analysis = 5,
39 } FuncType;
40 
46 typedef enum {
47  World = 0,
52 
56 typedef enum {
59 } ShadeMode;
60 
64 typedef enum {
65  ZebraVert = 0,
67 }ZebraType;
68 
69 
71 MG_DLL_DECLR void setColorAsSelectionName(unsigned name);
72 
74 MG_DLL_DECLR void printOpenGLError(int errorCode);
75 
77 MG_DLL_DECLR int checkForOpenGLError(const char*, int);
78 MG_DLL_DECLR void dumpGLInfo(bool dumpExtensions = false);
79 
80 // get LightEnable (Shade On/Off)
81 MG_DLL_DECLR bool LightEnabled();
82 
83 MG_DLL_DECLR void CALLBACK debugCallback(GLenum source,
84  GLenum type,
85  GLuint id,
86  GLenum severity,
87  GLsizei length,
88  const GLchar* message,
89  void* userParam);
90 
91 MG_DLL_DECLR void initializeStaticGLAttribStack();
92 MG_DLL_DECLR std::stack<mgStaticGLAttrib>& getStaticGLAttribStack();
94 MG_DLL_DECLR void execStaticGLAttrib(const mgStaticGLAttrib& attrib);
95 MG_DLL_DECLR void execStaticColorAttrib(const MGColor& color);
96 MG_DLL_DECLR void execStaticColorAttrib(const float color[4]);
97 MG_DLL_DECLR void execStaticLineWidth(float lineWidth);
98 MG_DLL_DECLR void execStaticLineStipple(short int factor, GLuint pattern);
99 MG_DLL_DECLR void execLightMode(// set Light(Shading On/Off)
100  int mode
101 );
102 
103 MG_DLL_DECLR void pushStaticGLAttrib();
104 MG_DLL_DECLR void popStaticGLAttrib();
105 
106 };
107 
108 
110 
113 class MG_DLL_DECLR mgFuncTypeSwitcher{
114 public:
116 
118 
119 private:
120  mgGLSLProgram* m_pGLSL;//The current glsl program is save.
121  int m_orgFuncType;//The original function type is save.
122 };
123 
124 
126 
151 class MG_DLL_DECLR mgGLSLProgram{
152 public:
153 
155  typedef enum{
156  VERTEX, FRAGMENT, GEOMETRY,
157  TESS_CONTROL, TESS_EVALUATION
158  } GLSLShaderType;
159 
160  //Vertex attrib変数定義用
161  typedef enum {
162  vPosition=0,
165  vTextureCoord
166  } VertexAttribId;
167 
168  union SELECT_NAME{
169  unsigned uiName;
170  unsigned short usName[2];
171  unsigned char ucName[4];//
172  };
173 
174  mgGLSLProgram();
175  ~mgGLSLProgram();
176 
178  bool compileShaderFromFile(const char* fileName, GLSLShaderType type);
179 
181  bool compileShaderFromString(const std::string& source, GLSLShaderType type);
182 
184  void freeProgram();
185 
187  bool link();
188 
190  void use();
191 
193  std::string& log();
194 
196  int getHandle();
197 
199  bool isLinked();
200 
202  void bindAttribLocation( GLuint location, const char * name);
203 
205  static void setCurrentGLSLProgram(mgGLSLProgram* glsl);
206 
208  static mgGLSLProgram* getCurrentGLSLProgram();
209 
211  void bindFragDataLocation( GLuint location, const char * name );
212 
214  int getvPositionLocation()const;
215  int getvColorLocation()const;
216  int getvNormalLocation()const;
217  int getvTextureCoordLocation()const;
218 
219  // Uniform変数まわり
220  GLint getUniformLocation(const char* name)const;
221  GLint getUniformLocation(const std::string& name)const{
222  return getUniformLocation(name.c_str());
223  };
224 
225  // Uniformに値をセットする関数群。locが負の値のときはAssertを吐きます。
226  // locのチェックを行ったうえでこれらの関数を呼び出してください。
227  void setUniform( GLint loc, float x, float y, float z);
228  void setUniform( GLint loc, const glm::vec3& v);
229  void setUniform( GLint loc, const glm::vec4& v);
230  void setUniform( GLint loc, const glm::mat4& m);
231  void setUniform( GLint loc, const glm::mat3& m);
232  void setUniform( GLint loc, float val);
233  void setUniform( GLint loc, int val);
234  void setUniform( GLint loc, bool val);
235 
236 public:
237  void printActiveUniforms();
238  void printActiveAttribs();
239 
240 
241  void EnableLights(bool bEnabled = true);
242  bool LightEnabled();
243 
244 
245  static const size_t MAX_ATTRIB_LOC = 20;
246  //Last name of UNIFORM_ATTRIB_ID must be less than MAX_ATTRIB_LOC.
247  typedef enum{
248  modelViewProjMatrix=0, // mat4
250  projMatrix, // mat4
251  normalMatrix, // mat3
252 
253  ndcMarix, // mat4
254  ndcScaleMatrix, // mat3
255  dpiFactor, // float
256 
257  FuncType, // draw function(Draw/Select/Zebra Analysis) FuncTypeでEnum定義
258  DrawType, // func typeがDrawのときのみ有効 DrawTypeでEnum定義
259  ShaderMode, // int 0:NoShading 1:Shading
260 
261  CoordinateType, // CoordinateType (World/NDC/AnchorPoint/AnchorPointScreen) CoordinateTypeでEnum定義
262 
263  texture2D, // texture.
264  pointSize, // Vertexの表示サイズ。カーソルなどのPOINT_SPLITE表示に使う
265 
266  anchorPoint, // AnchorPoint。Billboard表示(拡大。縮小してもサイズが変わらない描画)の基準点
267 
268  LightTwoSides, // boolean True:TwoSide, False:片面
269  ForceLight, // falseのときは、有効なLightが無い場合は、NO_SHADINGモードで描画。
270 
271  ZebraAxis, // ゼブラ方向 ZebraTypeで指定
272  ZebraSize, // ゼブラの幅。 ゼブラのステップ刻みは step(0.5f, fract(1.0f / ZebraSize * atan(r.y, r.x)));
273  }UniformName;
274 
275  void setUniform( UniformName name, float x, float y, float z);
276  void setUniform( UniformName name, const glm::vec3& v);
277  void setUniform( UniformName name, const glm::vec4& v);
278  void setUniform( UniformName name, const glm::mat4& m);
279  void setUniform( UniformName name, const glm::mat3& m);
280  void setUniform( UniformName name, float val);
281  void setUniform( UniformName name, int val);
282  void setUniform( UniformName name, bool val);
283 
284  // Lightまわり
285  static const GLint LIGHT_NUM = 10;
286  static const GLint PROP_NUM = 11;
287 
288  typedef enum {
289  isEnabled=0, // bool
290  ambientColor, // vec4 ambient Color
291  diffuseColor, // vec4 diffulse color
292  specularColor, // vec4 specular color
293  position, // vec4 position
294  spotDirection, // vec3
295  spotExponent, // float
296  spotCutoff, // float
299  quadraticAttenuation // float
300  }LightProps;
301 
302  void setUniformLights( GLint lightNo, LightProps name, float x, float y, float z);
303  void setUniformLights( GLint lightNo, LightProps name, const glm::vec3& v);
304  void setUniformLights( GLint lightNo, LightProps name, const glm::vec4& v);
305  void setUniformLights( GLint lightNo, LightProps name, const glm::mat4& m);
306  void setUniformLights( GLint lightNo, LightProps name, const glm::mat3& m);
307  void setUniformLights( GLint lightNo, LightProps name, float val);
308  void setUniformLights( GLint lightNo, LightProps name, int val);
309  void setUniformLights( GLint lightNo, LightProps name, bool val);
310 
311  void setOpenGLVersion();
312 
314  void getOpenGLVerion(GLint& major, GLint& minor)const;
315 
316  void printPrjMatrix();
317 
318  void printLightProps();
319 
320  void printLightProps(GLint lightNo);
321 
322  void setFuncType(mgGLSL::FuncType type);
323  int getFuncType();
324 
325 private:
326  int m_handle;
327  GLint m_VersionMajor, m_VersionMinor;
328  bool m_linked;
329  std::string m_logString;
330  static mgGLSLProgram* m_CurrrentGLSL;
331 
332  GLint m_uniform_locations[MAX_ATTRIB_LOC];
333 
334  GLint m_LightPropsIds[LIGHT_NUM][PROP_NUM];
335 
336  //m_uniform_locations[]に値をセットする(link()内の処理)
337  void build_attribLocations();
338 
339  //m_uniform_locationsLights[]に値をセットする(ling()内の処理)
340  void build_attribLocationsLight();
341 
342  int getAttribLocation(const char* name)const;
343  bool fileExists(const std::string& fileName)const;
344 };
345  // end of DisplayHandling group
347 
348 #endif // _MGGLSLPROGRAM_H
Definition: glslprogram.h:26
MG_DLL_DECLR void printOpenGLError(int errorCode)
change error code to string and print it.
Definition: glslprogram.h:297
MG_DLL_DECLR void CALLBACK debugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, void *userParam)
MG_DLL_DECLR void initializeStaticGLAttribStack()
Definition: glslprogram.h:49
Definition: glslprogram.h:257
Definition: glslprogram.h:295
Definition: glslprogram.h:263
GLint getUniformLocation(const std::string &name) const
Definition: glslprogram.h:221
MG_DLL_DECLR void pushStaticGLAttrib()
Definition: glslprogram.h:290
MG_DLL_DECLR bool LightEnabled()
MG_DLL_DECLR std::stack< mgStaticGLAttrib > & getStaticGLAttribStack()
Definition: glslprogram.h:156
mgStaticGLAttrib defines MGColor and line width data of OpenGL.
Definition: StaticGLAttrib.h:16
Definition: glslprogram.h:36
FuncType
Definition: glslprogram.h:35
Definition: glslprogram.h:298
Definition: glslprogram.h:264
Definition: glslprogram.h:271
GLSLShaderType
CreateShaderのタイプ列挙子
Definition: glslprogram.h:155
MG_DLL_DECLR int checkForOpenGLError(const char *, int)
Returns 1 if an OpenGL error occurred, 0 otherwise.
Definition: glslprogram.h:269
MG_DLL_DECLR mgStaticGLAttrib & getCurrentStaticGLAttrib()
Definition: glslprogram.h:258
Definition: glslprogram.h:272
Definition: glslprogram.h:65
Definition: glslprogram.h:27
Definition: glslprogram.h:292
Definition: glslprogram.h:50
Definition: glslprogram.h:164
Definition: glslprogram.h:253
MGColor defines the OpenGL color (R,G,B,A).
Definition: Color.h:26
Definition: glslprogram.h:296
DrawType
Definition: glslprogram.h:25
Definition: glslprogram.h:268
Definition: glslprogram.h:294
Definition: glslprogram.h:58
Definition: glslprogram.h:249
Definition: glslprogram.h:47
Definition: glslprogram.h:293
Definition: glslprogram.h:66
Definition: glslprogram.h:266
MG_DLL_DECLR void execStaticLineWidth(float lineWidth)
Definition: glslprogram.h:261
Definition: glslprogram.h:254
MG_DLL_DECLR void execLightMode(int mode)
Definition: glslprogram.h:18
mgGLSLProgramはOpenGL Shader Programをcompile , linkしてそのuniform変数の管理を行います.
Definition: glslprogram.h:151
MG_DLL_DECLR void dumpGLInfo(bool dumpExtensions=false)
unsigned uiName
Definition: glslprogram.h:169
MG_DLL_DECLR void setColorAsSelectionName(unsigned name)
Set static color as selection name.
MG_DLL_DECLR void execStaticLineStipple(short int factor, GLuint pattern)
Definition: glslprogram.h:37
Definition: glslprogram.h:255
MG_DLL_DECLR void execStaticColorAttrib(const MGColor &color)
Definition: glslprogram.h:57
Definition: glslprogram.h:168
ZebraType
Definition: glslprogram.h:64
Definition: glslprogram.h:291
Definition: glslprogram.h:163
Definition: glslprogram.h:259
Definition: glslprogram.h:38
MG_DLL_DECLR void execStaticGLAttrib(const mgStaticGLAttrib &attrib)
CoordinateType
Definition: glslprogram.h:46
ShadeMode
Definition: glslprogram.h:56
MG_DLL_DECLR void popStaticGLAttrib()
Utility class to invoke glsl's setFuncType.
Definition: glslprogram.h:113
Definition: glslprogram.h:251
Definition: glslprogram.h:48
Definition: glslprogram.h:250
Definition: glslprogram.h:28