GLUtil.h
Upload User: ghyvgy
Upload Date: 2009-05-26
Package Size: 547k
Code Size: 6k
Category:

Other Games

Development Platform:

Python

  1. /*
  2. s_p_oneil@hotmail.com
  3. Copyright (c) 2000, Sean O'Neil
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7. * Redistributions of source code must retain the above copyright notice,
  8.   this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright notice,
  10.   this list of conditions and the following disclaimer in the documentation
  11.   and/or other materials provided with the distribution.
  12. * Neither the name of this project nor the names of its contributors
  13.   may be used to endorse or promote products derived from this software
  14.   without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef __GLUtil_h__
  28. #define __GLUtil_h__
  29. // Set up the GL_NV_fence extension
  30. #ifndef GL_NV_fence
  31. #define GL_NV_fence
  32. #define GL_ALL_COMPLETED_NV               0x84F2
  33. #define GL_FENCE_STATUS_NV                0x84F3
  34. #define GL_FENCE_CONDITION_NV             0x84F4
  35. typedef void (APIENTRY * PFNGLDELETEFENCESNVPROC) (GLsizei n, const GLuint *fences);
  36. typedef void (APIENTRY * PFNGLGENFENCESNVPROC) (GLsizei n, GLuint *fences);
  37. typedef GLboolean (APIENTRY * PFNGLISFENCENVPROC) (GLuint fence);
  38. typedef GLboolean (APIENTRY * PFNGLTESTFENCENVPROC) (GLuint fence);
  39. typedef void (APIENTRY * PFNGLGETFENCEIVNVPROC) (GLuint fence, GLenum pname, GLint *params);
  40. typedef void (APIENTRY * PFNGLFINISHFENCENVPROC) (GLuint fence);
  41. typedef void (APIENTRY * PFNGLSETFENCENVPROC) (GLuint fence, GLenum condition);
  42. #endif
  43. #include "glh_genext.h"
  44. extern int glh_init_extension(const char* extension);
  45. #include "Texture.h"
  46. #include "PBuffer.h"
  47. // This must be defined before GL_NV_vertex_array_range
  48. class CVertexArray;
  49. class CVertex
  50. {
  51. // Attributes
  52. public:
  53. static CVertexArray Array;
  54. CVector m_vPosition; // The position of the vertex
  55. CVector m_vNormal; // The normal of the vertex (used for smooth shading)
  56. float m_fTexCoord[4]; // The texture coordinates of the vertex
  57. // Operations
  58. public:
  59. CVertex() {}
  60. CVertex(const CVertex &v) { *this = v; }
  61. void operator=(const CVertex &v)
  62. {
  63. m_vPosition = v.m_vPosition;
  64. m_vNormal = v.m_vNormal;
  65. m_fTexCoord[0] = v.m_fTexCoord[0];
  66. m_fTexCoord[1] = v.m_fTexCoord[1];
  67. m_fTexCoord[2] = v.m_fTexCoord[2];
  68. m_fTexCoord[3] = v.m_fTexCoord[3];
  69. }
  70. void Init(CVector &v)
  71. {
  72. m_vPosition = v;
  73. m_vNormal = v;
  74. m_fTexCoord[0] = m_fTexCoord[1] = 0;
  75. }
  76. };
  77. #define VERTEX_ARRAY_SIZE 65535 // Would use 65536, but unsigned short only goes up to 65535
  78. class CVertexArray : public CStackedArray<CVertex, unsigned short>
  79. {
  80. public:
  81. // Constructor/destructor methods
  82. CVertexArray() : CStackedArray<CVertex, unsigned short>(VERTEX_ARRAY_SIZE) {}
  83. void UpdateElement(unsigned short nElement) { SetFlags(nElement, Dirty); }
  84. };
  85. class CGLUtil
  86. {
  87. // Attributes
  88. protected:
  89. // Standard OpenGL members
  90. HDC m_hDC;
  91. HGLRC m_hGLRC;
  92. // Members to indicate whether certain OpenGL extensions are supported
  93. bool m_bVertexArrayRanges;
  94. bool m_bCompiledVertexArrays;
  95. bool m_bPBuffers;
  96. bool m_bRegisterCombiners;
  97. int m_nMaxTextureUnits;
  98. // Members for GL_NV_vertex_array_range and GL_NV_fence extensions
  99. CVertex *m_pArray;
  100. GLuint m_nFence;
  101. // Members for the WGL_ARB_pbuffer extension
  102. CPBuffer m_PBuffer;
  103. public:
  104. static CGLUtil *m_pMain;
  105. // Operations
  106. public:
  107. CGLUtil();
  108. ~CGLUtil();
  109. void Init();
  110. void Cleanup();
  111. void InitRenderContext(HDC hDC=NULL, HGLRC hGLRC=NULL);
  112. HDC GetHDC() { return m_hDC; }
  113. HGLRC GetHGLRC() { return m_hGLRC; }
  114. int GetVertexArraySize() { return VERTEX_ARRAY_SIZE * sizeof(CVertex); }
  115. bool HasVertexArrayRanges() { return m_bVertexArrayRanges; }
  116. bool HasCompiledVertexArrays() { return m_bCompiledVertexArrays; }
  117. bool HasPBuffers() { return m_bPBuffers; }
  118. bool HasRegisterCombiners() { return m_bRegisterCombiners; }
  119. int GetMaxTextureUnits() { return m_nMaxTextureUnits; }
  120. void EnableVertexArray()
  121. {
  122. glEnableClientState(GL_VERTEX_ARRAY);
  123. glVertexPointer(3, GL_FLOAT, sizeof(CVertex), &m_pArray->m_vPosition);
  124. }
  125. void DisableVertexArray()
  126. {
  127. glDisableClientState(GL_VERTEX_ARRAY);
  128. }
  129. void EnableNormalArray()
  130. {
  131. glEnableClientState(GL_NORMAL_ARRAY);
  132. glNormalPointer(GL_FLOAT, sizeof(CVertex), &m_pArray->m_vNormal);
  133. }
  134. void DisableNormalArray()
  135. {
  136. glDisableClientState(GL_NORMAL_ARRAY);
  137. }
  138. void EnableTextureCoordArray(int nTextureUnit=GL_TEXTURE0_ARB, int nOffset=0)
  139. {
  140. if(m_nMaxTextureUnits > 1)
  141. glClientActiveTextureARB(nTextureUnit);
  142. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  143. glTexCoordPointer(2, GL_FLOAT, sizeof(CVertex), &m_pArray->m_fTexCoord[nOffset]);
  144. }
  145. void DisableTextureCoordArray(int nTextureUnit=GL_TEXTURE0_ARB)
  146. {
  147. if(m_nMaxTextureUnits > 1)
  148. glClientActiveTextureARB(nTextureUnit);
  149. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  150. }
  151. void BeginVertexArray();
  152. void EndVertexArray();
  153. void BeginPBuffer() { m_PBuffer.MakeCurrent(); }
  154. void EndPBuffer() { wglMakeCurrent(m_hDC, m_hGLRC); }
  155. void BeginOrtho2D(int nWidth=640, int nHeight=480)
  156. {
  157. glDisable(GL_DEPTH_TEST);
  158. glDisable(GL_LIGHTING);
  159. glMatrixMode(GL_MODELVIEW);
  160. glPushMatrix();
  161. glLoadIdentity();
  162. glMatrixMode(GL_PROJECTION);
  163. glPushMatrix();
  164. glLoadIdentity();
  165. gluOrtho2D(0, nWidth, 0, nHeight);
  166. }
  167. void EndOrtho2D()
  168. {
  169. glPopMatrix();
  170. glMatrixMode(GL_MODELVIEW);
  171. glPopMatrix();
  172. glEnable(GL_LIGHTING);
  173. glEnable(GL_DEPTH_TEST);
  174. }
  175. };
  176. inline CGLUtil *GLUtil() { return CGLUtil::m_pMain; }
  177. #endif // __GLUtil_h__