FS2_Open
Open source remastering of the Freespace 2 engine
gropenglstate.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) Volition, Inc. 1999. All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell
5  * or otherwise commercially exploit the source or things you created based on the
6  * source.
7  *
8 */
9 
10 
11 #ifndef _GROPENGLSTATE_H
12 #define _GROPENGLSTATE_H
13 
14 #include "globalincs/pstypes.h"
15 #include "graphics/gropengl.h"
18 
19 #define MAX_UNIFORM_BUFFERS 6
20 
22  GLboolean active; // unit is active
23  GLboolean enabled; // has texture target enabled
24 
27 
32 
37 
41 
44 
46 };
47 
49 {
50  private:
51  void default_values(GLint unit, GLenum target = GL_INVALID_ENUM);
52 
53  GLuint active_texture_unit;
54  GLboolean shader_mode;
55 
56  opengl_texture_unit *units;
57  GLuint num_texture_units;
58 
59 
60  public:
61  opengl_texture_state(): active_texture_unit(0), shader_mode(GL_FALSE), units(NULL), Current_texture_source(TEXTURE_SOURCE_NONE) {}
63 
65 
66  void init(GLuint n_units);
67 
68  GLboolean TexgenS(GLint state = -1);
69  GLboolean TexgenT(GLint state = -1);
70  GLboolean TexgenR(GLint state = -1);
71  GLboolean TexgenQ(GLint state = -1);
72  void SetTarget(GLenum tex_target);
73  void SetActiveUnit(GLuint id = 0);
74  void Enable(GLuint tex_id = 0);
75  void Disable();
76  void DisableUnused();
77  void DisableAll();
78  void ResetUsed();
79  void Delete(GLuint tex_id);
80  GLfloat AnisoFilter(GLfloat aniso = 0.0f);
81 
82  inline void SetRGBScale(GLfloat scale);
83  inline void SetAlphaScale(GLfloat scale);
84  inline void SetEnvMode(GLenum mode);
85  inline void SetEnvCombineMode(GLenum cmode, GLenum cfunc);
86  inline void SetWrapS(GLenum mode);
87  inline void SetWrapT(GLenum mode);
88  inline void SetWrapR(GLenum mode);
89  inline void SetTexgenModeS(GLenum mode);
90  inline void SetTexgenModeT(GLenum mode);
91  inline void SetTexgenModeR(GLenum mode);
92  inline void SetTexgenModeQ(GLenum mode);
93  inline GLenum GetTarget();
94  inline void SetShaderMode(GLboolean mode);
95 };
96 
98 {
99  Assert( ((scale == 1.0f) || (scale == 2.0f) || (scale == 4.0f)) );
101 }
102 
104 {
105  Assert( ((scale == 1.0f) || (scale == 2.0f) || (scale == 4.0f)) );
107 }
108 
110 {
111  if (mode != units[active_texture_unit].env_mode) {
113  units[active_texture_unit].env_mode = mode;
114  }
115 }
116 
118 {
120 
121  if (cmode == GL_COMBINE_RGB) {
123  } else if (cmode == GL_COMBINE_ALPHA) {
125  }
126 }
127 
129 {
130 // glTexParameteri(units[active_texture_unit].texture_target, GL_TEXTURE_WRAP_S, mode);
131 }
132 
134 {
135 // glTexParameteri(units[active_texture_unit].texture_target, GL_TEXTURE_WRAP_T, mode);
136 }
137 
139 {
140 // glTexParameteri(units[active_texture_unit].texture_target, GL_TEXTURE_WRAP_R, mode);
141 }
142 
144 {
146 }
147 
149 {
151 }
152 
154 {
156 }
157 
159 {
161 }
162 
164 {
165  return units[active_texture_unit].texture_target;
166 }
167 
169 {
170  shader_mode = mode;
171 }
172 
174 {
176 
182 
183  bool reset_ptr;
185 };
186 
188 {
190 
197 
199  bool ptr_init;
200 
201  bool reset_ptr;
203 };
204 
206 {
207  private:
208  GLuint active_client_texture_unit;
209  GLuint num_client_texture_units;
210 
211  opengl_client_texture_unit *client_texture_units;
212 
213  GLboolean color_array_Status;
214  GLuint color_array_Buffer;
215  GLint color_array_size;
216  GLenum color_array_type;
217  GLsizei color_array_stride;
218  GLvoid *color_array_pointer;
219  bool color_array_reset_ptr;
220  bool color_array_used_for_draw;
221 
222  GLboolean normal_array_Status;
223  GLuint normal_array_Buffer;
224  GLenum normal_array_Type;
225  GLsizei normal_array_Stride;
226  GLvoid *normal_array_Pointer;
227  bool normal_array_reset_ptr;
228  bool normal_array_used_for_draw;
229 
230  GLboolean vertex_array_Status;
231  GLuint vertex_array_Buffer;
232  GLint vertex_array_Size;
233  GLenum vertex_array_Type;
234  GLsizei vertex_array_Stride;
235  GLvoid *vertex_array_Pointer;
236  bool vertex_array_reset_ptr;
237  bool vertex_array_used_for_draw;
238 
239  SCP_map<GLuint, opengl_vertex_attrib_unit> vertex_attrib_units;
240 
241  GLuint array_buffer;
242  GLuint element_array_buffer;
243  GLuint texture_array_buffer;
244  GLuint uniform_buffer;
245 
246  GLuint uniform_buffer_index_bindings[MAX_UNIFORM_BUFFERS];
247  public:
248  opengl_array_state(): active_client_texture_unit(0), client_texture_units(NULL) {
249  for ( int i = 0; i < MAX_UNIFORM_BUFFERS; ++i ) {
250  uniform_buffer_index_bindings[i] = 0;
251  }
252  }
254 
255  void init(GLuint n_units);
256 
257  void SetActiveClientUnit(GLuint id);
258  void EnableClientTexture();
259  void DisableClientTexture();
261 
262  void EnableClientColor();
263  void DisableClientColor();
265 
266  void EnableClientNormal();
267  void DisableClientNormal();
269 
270  void EnableClientVertex();
271  void DisableClientVertex();
273 
277 
278  void BindPointersBegin();
279  void BindPointersEnd();
280 
281  void BindArrayBuffer(GLuint id);
282  void BindElementBuffer(GLuint id);
283  void BindTextureBuffer(GLuint id);
284  void BindUniformBuffer(GLuint id);
286 };
287 
289 {
291 
292  enum data_type {
299  };
300 
302  int index;
303 
304  int count;
305  int tranpose;
306 };
307 
309 {
310  SCP_vector<uniform_bind> uniforms;
311 
312  SCP_vector<int> uniform_data_ints;
313  SCP_vector<float> uniform_data_floats;
314  SCP_vector<vec2d> uniform_data_vec2d;
315  SCP_vector<vec3d> uniform_data_vec3d;
316  SCP_vector<vec4> uniform_data_vec4;
317  SCP_vector<matrix4> uniform_data_matrix4;
318 
319  SCP_map<SCP_string, int> uniform_lookup;
320 
321  int findUniform(const SCP_string &name);
322 public:
324 
325  void setUniformi(const SCP_string &name, const int value);
326  void setUniformf(const SCP_string &name, const float value);
327  void setUniform2f(const SCP_string &name, const float x, const float y);
328  void setUniform2f(const SCP_string &name, const vec2d &val);
329  void setUniform3f(const SCP_string &name, const float x, const float y, const float z);
330  void setUniform3f(const SCP_string &name, const vec3d &value);
331  void setUniform4f(const SCP_string &name, const vec4 &val);
332  void setUniformMatrix4fv(const SCP_string &name, const int count, const matrix4 *value);
333  void setUniformMatrix4f(const SCP_string &name, const matrix4 &val);
334 
335  void reset();
336 };
337 
339 {
340  friend class opengl_texture_state;
341 
342  private:
343  GLboolean fog_Status;
344  GLboolean blend_Status;
345  GLboolean alphatest_Status;
346  GLboolean depthtest_Status;
347  GLboolean scissortest_Status;
348  GLboolean stenciltest_Status;
349  GLboolean cullface_Status;
350  GLboolean polygonoffsetfill_Status;
351  GLboolean normalize_Status;
352  GLboolean clipplane_Status[6];
353  GLboolean clipdistance_Status[6];
354  GLboolean *light_Status;
355  GLboolean depthmask_Status;
356  GLboolean lighting_Status;
357  GLboolean colormask_Status;
358  GLubyte red_Status;
359  GLubyte blue_Status;
360  GLubyte green_Status;
361  GLubyte alpha_Status;
362  bool color_invalid;
363 
364  GLenum frontface_Value;
365  GLenum cullface_Value;
366  GLenum blendfunc_Value[2];
367  GLenum depthfunc_Value;
368 
369  GLenum polygon_mode_Face;
370  GLenum polygon_mode_Mode;
371 
372  GLfloat polygon_offset_Factor;
373  GLfloat polygon_offset_Unit;
374 
375  gr_alpha_blend Current_alpha_blend_mode;
376  gr_zbuffer_type Current_zbuffer_type;
377  gr_stencil_type Current_stencil_type;
378  public:
379  opengl_state() : light_Status(NULL) {}
380  ~opengl_state();
381 
382  void init();
383 
387 
392  void SetPolygonOffset(GLfloat factor, GLfloat units);
394 
395  // the GLboolean functions will return the current state if no argument
396  // and the previous state if an argument is passed
397  GLboolean Lighting(GLint state = -1);
398  GLboolean Fog(GLint state = -1);
399  GLboolean Blend(GLint state = -1);
400  GLboolean AlphaTest(GLint state = -1);
401  GLboolean DepthTest(GLint state = -1);
402  GLboolean ScissorTest(GLint state = -1);
403  GLboolean StencilTest(GLint state = -1);
404  GLboolean CullFace(GLint state = -1);
405  GLboolean PolygonOffsetFill(GLint state = -1);
406  GLboolean Normalize(GLint state = -1);
407  GLboolean Light(GLint num, GLint state = -1);
408  GLboolean ClipPlane(GLint num, GLint state = -1);
409  GLboolean ClipDistance(GLint num, GLint state = -1);
410  GLboolean DepthMask(GLint state = -1);
411  GLboolean ColorMask(GLint state = -1);
412 
413  inline GLenum FrontFaceValue(GLenum new_val = GL_INVALID_ENUM);
414  inline GLenum CullFaceValue(GLenum new_val = GL_INVALID_ENUM);
415  inline void BlendFunc(GLenum s_val, GLenum d_val);
416  inline GLenum BlendFuncSource();
417  inline GLenum BlendFuncDest();
418  inline GLenum DepthFunc(GLenum new_val = GL_INVALID_ENUM);
419  inline void AlphaFunc(GLenum f_val, GLclampf r_val);
420  inline void Color(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha = 255);
421  inline void InvalidateColor();
422 };
423 
425 {
426  if (new_val != frontface_Value) {
427  if (new_val != GL_INVALID_ENUM) {
428  glFrontFace(new_val);
429  frontface_Value = new_val;
430  }
431  }
432 
433  return frontface_Value;
434 }
435 
437 {
438  if (new_val != cullface_Value) {
439  if (new_val != GL_INVALID_ENUM) {
440  glCullFace(new_val);
441  cullface_Value = new_val;
442  }
443  }
444 
445  return cullface_Value;
446 }
447 
448 inline void opengl_state::BlendFunc(GLenum s_val, GLenum d_val)
449 {
450  if ( !((s_val == blendfunc_Value[0]) && (d_val == blendfunc_Value[1])) ) {
451  glBlendFunc(s_val, d_val);
452  blendfunc_Value[0] = s_val;
453  blendfunc_Value[1] = d_val;
454 
455  Current_alpha_blend_mode = (gr_alpha_blend)(-1);
456  }
457 }
458 
460 {
461  return blendfunc_Value[0];
462 }
463 
465 {
466  return blendfunc_Value[1];
467 }
468 
470 {
471  if (new_val != depthfunc_Value) {
472  if (new_val != GL_INVALID_ENUM) {
473  glDepthFunc(new_val);
474  depthfunc_Value = new_val;
475 
476  Current_zbuffer_type = (gr_zbuffer_type)(-1);
477  }
478  }
479 
480  return depthfunc_Value;
481 }
482 
483 inline void opengl_state::AlphaFunc(GLenum f_val, GLclampf r_val)
484 {
485  glAlphaFunc(f_val, r_val);
486 }
487 
489 {
490  if ( color_invalid || (red != red_Status) || (green != green_Status) || (blue != blue_Status) || (alpha != alpha_Status) ) {
491  glColor4ub(red, green, blue, alpha);
492  red_Status = red;
493  green_Status = green;
494  blue_Status = blue;
495  alpha_Status = alpha;
496  color_invalid = false;
497  }
498 }
499 
501 {
502  color_invalid = true;
503 }
504 
505 extern opengl_state GL_state;
506 
508 void opengl_setup_render_states(int &r,int &g,int &b,int &alpha, int &tmap_type, int flags, int is_scaler = 0);
509 
510 
511 #endif // _GROPENGLSTATE_H
unsigned int GLuint
Definition: Gl.h:52
GLboolean ColorMask(GLint state=-1)
float GLclampf
Definition: Gl.h:54
void EnableVertexAttrib(GLuint index)
int GLsizei
Definition: Gl.h:49
void setUniformMatrix4fv(const SCP_string &name, const int count, const matrix4 *value)
void SetPolygonMode(GLenum face, GLenum mode)
int i
Definition: multi_pxo.cpp:466
WINGDIAPI void APIENTRY glCullFace(GLenum mode)
void SetWrapS(GLenum mode)
uniform_bind::data_type type
WINGDIAPI void APIENTRY glTexGeni(GLenum coord, GLenum pname, GLint param)
void setUniformf(const SCP_string &name, const float value)
gr_texture_source
Definition: grinternal.h:59
void init(GLuint n_units)
void VertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer)
void setUniformi(const SCP_string &name, const int value)
void BindUniformBufferBindingIndex(GLuint id, GLuint index)
GLsizei const GLfloat * value
Definition: Glext.h:5646
GLuint index
Definition: Glext.h:5608
opengl_array_state Array
void Color(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha=255)
gr_stencil_type
Definition: grinternal.h:83
void SetTextureSource(gr_texture_source ts)
gr_alpha_blend
Definition: grinternal.h:66
GLboolean StencilTest(GLint state=-1)
void SetTexgenModeS(GLenum mode)
#define GL_ALPHA_SCALE
Definition: Gl.h:517
GLboolean ScissorTest(GLint state=-1)
Assert(pm!=NULL)
WINGDIAPI void APIENTRY glTexEnvf(GLenum target, GLenum pname, GLfloat param)
WINGDIAPI void APIENTRY glTexEnvi(GLenum target, GLenum pname, GLint param)
Definition: pstypes.h:88
GLboolean DepthTest(GLint state=-1)
GLenum BlendFuncDest()
GLboolean TexgenR(GLint state=-1)
GLclampf f
Definition: Glext.h:7097
void SetTexgenModeR(GLenum mode)
void SetZbufferType(gr_zbuffer_type zt)
void BindElementBuffer(GLuint id)
#define GL_Q
Definition: Gl.h:898
void setUniform3f(const SCP_string &name, const float x, const float y, const float z)
float factor
Definition: lua.cpp:440
GLenum mode
Definition: Glext.h:5794
opengl_texture_state Texture
GLboolean CullFace(GLint state=-1)
std::basic_string< char, std::char_traits< char >, std::allocator< char > > SCP_string
Definition: vmallocator.h:21
void SetAlphaScale(GLfloat scale)
GLenum GLenum GLenum GLenum GLenum scale
Definition: Glext.h:8503
GLsizeiptr size
Definition: Glext.h:5496
cmode
Definition: glcviewport.h:23
#define GL_RGB_SCALE
Definition: Glext.h:273
GLboolean TexgenQ(GLint state=-1)
GLenum CullFaceValue(GLenum new_val=GL_INVALID_ENUM)
GLenum type
Definition: Gl.h:1492
GLfloat AnisoFilter(GLfloat aniso=0.0f)
GLenum GLsizei GLsizei const GLvoid * pointer
Definition: Gl.h:1492
void SetEnvMode(GLenum mode)
void SetStencilType(gr_stencil_type st)
unsigned char GLubyte
Definition: Gl.h:50
opengl_state GL_state
GLint GLenum GLboolean normalized
Definition: Glext.h:5694
GLenum GLuint GLint GLenum face
Definition: Glext.h:6586
#define GL_FALSE
Definition: Gl.h:139
void opengl_setup_render_states(int &r, int &g, int &b, int &alpha, int &tmap_type, int flags, int is_scaler=0)
GLboolean DepthMask(GLint state=-1)
#define GL_S
Definition: Gl.h:895
GLdouble GLdouble GLdouble r
Definition: Glext.h:5337
Definition: pstypes.h:76
void gr_opengl_clear_states()
GLboolean GLboolean g
Definition: Glext.h:5781
GLclampf GLclampf blue
Definition: Glext.h:5177
void SetPolygonOffset(GLfloat factor, GLfloat units)
GLboolean Light(GLint num, GLint state=-1)
#define GL_COMBINE_RGB
Definition: Glext.h:259
GLboolean AlphaTest(GLint state=-1)
WINGDIAPI void APIENTRY glColor4ub(GLubyte red, GLubyte green, GLubyte blue, GLubyte alpha)
void SetAlphaBlendMode(gr_alpha_blend ab)
#define MAX_UNIFORM_BUFFERS
Definition: gropenglstate.h:19
GLdouble GLdouble z
Definition: Glext.h:5451
void VertexPointer(GLint size, GLenum type, GLsizei stride, GLvoid *pointer)
SCP_string name
WINGDIAPI void APIENTRY glDepthFunc(GLenum func)
#define GL_T
Definition: Gl.h:896
WINGDIAPI void APIENTRY glFrontFace(GLenum mode)
gr_zbuffer_type
Definition: grinternal.h:75
#define GL_COMBINE_ALPHA
Definition: Glext.h:260
GLenum BlendFuncSource()
void TexPointer(GLint size, GLenum type, GLsizei stride, GLvoid *pointer)
GLclampf green
Definition: Glext.h:5177
opengl_uniform_state Uniform
void BlendFunc(GLenum s_val, GLenum d_val)
void SetTarget(GLenum tex_target)
GLint GLint GLint GLint GLint x
Definition: Glext.h:5182
GLboolean Normalize(GLint state=-1)
void Enable(GLuint tex_id=0)
void NormalPointer(GLenum type, GLsizei stride, GLvoid *pointer)
void InvalidateColor()
void SetWrapR(GLenum mode)
GLboolean Blend(GLint state=-1)
GLboolean TexgenT(GLint state=-1)
void ColorPointer(GLint size, GLenum type, GLsizei stride, GLvoid *pointer)
GLbitfield flags
Definition: Glext.h:6722
unsigned char GLboolean
Definition: Gl.h:44
void init(GLuint n_units)
GLuint const GLchar * name
Definition: Glext.h:5608
void BindArrayBuffer(GLuint id)
void SetTexgenModeQ(GLenum mode)
GLuint GLfloat * val
Definition: Glext.h:6741
GLboolean GLboolean GLboolean b
Definition: Glext.h:5781
GLboolean PolygonOffsetFill(GLint state=-1)
void SetWrapT(GLenum mode)
void DisableVertexAttrib(GLuint index)
GLboolean ClipPlane(GLint num, GLint state=-1)
GLuint GLuint num
Definition: Glext.h:9089
#define GL_TEXTURE_ENV_MODE
Definition: Gl.h:913
GLboolean Fog(GLint state=-1)
WINGDIAPI void APIENTRY glBlendFunc(GLenum sfactor, GLenum dfactor)
Definition: pstypes.h:106
WINGDIAPI void APIENTRY glAlphaFunc(GLenum func, GLclampf ref)
GLenum target
Definition: Glext.h:6872
#define GL_TEXTURE_GEN_MODE
Definition: Gl.h:925
void SetRGBScale(GLfloat scale)
Definition: gropenglstate.h:97
void SetShaderMode(GLboolean mode)
void Delete(GLuint tex_id)
GLint GLsizei count
Definition: Gl.h:1491
void SetActiveClientUnit(GLuint id)
void setUniformMatrix4f(const SCP_string &name, const matrix4 &val)
unsigned int GLenum
Definition: Gl.h:43
GLenum DepthFunc(GLenum new_val=GL_INVALID_ENUM)
void AlphaFunc(GLenum f_val, GLclampf r_val)
void BindTextureBuffer(GLuint id)
#define GL_COMBINE
Definition: Glext.h:258
void BindUniformBuffer(GLuint id)
GLboolean TexgenS(GLint state=-1)
void SetTexgenModeT(GLenum mode)
void setUniform4f(const SCP_string &name, const vec4 &val)
#define GL_TEXTURE_ENV
Definition: Gl.h:917
GLclampf GLclampf GLclampf alpha
Definition: Glext.h:5177
gr_texture_source Current_texture_source
Definition: gropenglstate.h:64
GLenum FrontFaceValue(GLenum new_val=GL_INVALID_ENUM)
GLboolean ClipDistance(GLint num, GLint state=-1)
void GLvoid
Definition: Gl.h:57
#define GL_INVALID_ENUM
Definition: Gl.h:299
#define GL_R
Definition: Gl.h:897
void setUniform2f(const SCP_string &name, const float x, const float y)
void SetActiveUnit(GLuint id=0)
GLint y
Definition: Gl.h:1505
int GLint
Definition: Gl.h:48
float GLfloat
Definition: Gl.h:53
GLenum GLsizei stride
Definition: Gl.h:1492
GLboolean Lighting(GLint state=-1)
void SetEnvCombineMode(GLenum cmode, GLenum cfunc)