PolyGLRenderer.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyString.h"
  21. #include "PolyLogger.h"
  22. #include "PolyGlobals.h"
  23. #include "PolyRenderer.h"
  24. #include "PolyTexture.h"
  25. #include "PolyGLTexture.h"
  26. #include "PolyCubemap.h"
  27. #include "PolyGLCubemap.h"
  28. #include "PolyGLVertexBuffer.h"
  29. #include "PolyFixedShader.h"
  30. #include "PolyMesh.h"
  31. #ifdef _WINDOWS
  32. #include <windows.h>
  33. #endif
  34. #if defined(__APPLE__) && defined(__MACH__)
  35. #include <OpenGL/gl.h>
  36. #include <OpenGL/glext.h>
  37. #include <OpenGL/glu.h>
  38. #else
  39. #include <GL/gl.h>
  40. #include <GL/glu.h>
  41. #include <GL/glext.h>
  42. #ifdef _WINDOWS
  43. #include <GL/wglext.h>
  44. #endif
  45. #endif
  46. /*
  47. #ifdef _WINDOWS
  48. #define GL_EXT_framebuffer_object 1
  49. #if GL_EXT_framebuffer_object
  50. #define GL_FRAMEBUFFER_EXT 0x8D40
  51. #define GL_RENDERBUFFER_EXT 0x8D41
  52. #define GL_STENCIL_INDEX1_EXT 0x8D46
  53. #define GL_STENCIL_INDEX4_EXT 0x8D47
  54. #define GL_STENCIL_INDEX8_EXT 0x8D48
  55. #define GL_STENCIL_INDEX16_EXT 0x8D49
  56. #define GL_RENDERBUFFER_WIDTH_EXT 0x8D42
  57. #define GL_RENDERBUFFER_HEIGHT_EXT 0x8D43
  58. #define GL_RENDERBUFFER_INTERNAL_FORMAT_EXT 0x8D44
  59. #define GL_RENDERBUFFER_RED_SIZE_EXT 0x8D50
  60. #define GL_RENDERBUFFER_GREEN_SIZE_EXT 0x8D51
  61. #define GL_RENDERBUFFER_BLUE_SIZE_EXT 0x8D52
  62. #define GL_RENDERBUFFER_ALPHA_SIZE_EXT 0x8D53
  63. #define GL_RENDERBUFFER_DEPTH_SIZE_EXT 0x8D54
  64. #define GL_RENDERBUFFER_STENCIL_SIZE_EXT 0x8D55
  65. #define GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT 0x8CD0
  66. #define GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME_EXT 0x8CD1
  67. #define GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL_EXT 0x8CD2
  68. #define GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE_EXT 0x8CD3
  69. #define GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_3D_ZOFFSET_EXT 0x8CD4
  70. #define GL_COLOR_ATTACHMENT0_EXT 0x8CE0
  71. #define GL_COLOR_ATTACHMENT1_EXT 0x8CE1
  72. #define GL_COLOR_ATTACHMENT2_EXT 0x8CE2
  73. #define GL_COLOR_ATTACHMENT3_EXT 0x8CE3
  74. #define GL_COLOR_ATTACHMENT4_EXT 0x8CE4
  75. #define GL_COLOR_ATTACHMENT5_EXT 0x8CE5
  76. #define GL_COLOR_ATTACHMENT6_EXT 0x8CE6
  77. #define GL_COLOR_ATTACHMENT7_EXT 0x8CE7
  78. #define GL_COLOR_ATTACHMENT8_EXT 0x8CE8
  79. #define GL_COLOR_ATTACHMENT9_EXT 0x8CE9
  80. #define GL_COLOR_ATTACHMENT10_EXT 0x8CEA
  81. #define GL_COLOR_ATTACHMENT11_EXT 0x8CEB
  82. #define GL_COLOR_ATTACHMENT12_EXT 0x8CEC
  83. #define GL_COLOR_ATTACHMENT13_EXT 0x8CED
  84. #define GL_COLOR_ATTACHMENT14_EXT 0x8CEE
  85. #define GL_COLOR_ATTACHMENT15_EXT 0x8CEF
  86. #define GL_DEPTH_ATTACHMENT_EXT 0x8D00
  87. #define GL_STENCIL_ATTACHMENT_EXT 0x8D20
  88. #define GL_FRAMEBUFFER_COMPLETE_EXT 0x8CD5
  89. #define GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT 0x8CD6
  90. #define GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT 0x8CD7
  91. #define GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT 0x8CD8
  92. #define GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT 0x8CD9
  93. #define GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT 0x8CDA
  94. #define GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT 0x8CDB
  95. #define GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT 0x8CDC
  96. #define GL_FRAMEBUFFER_UNSUPPORTED_EXT 0x8CDD
  97. #define GL_FRAMEBUFFER_BINDING_EXT 0x8CA6
  98. #define GL_RENDERBUFFER_BINDING_EXT 0x8CA7
  99. #define GL_MAX_COLOR_ATTACHMENTS_EXT 0x8CDF
  100. #define GL_MAX_RENDERBUFFER_SIZE_EXT 0x84E8
  101. #define GL_INVALID_FRAMEBUFFER_OPERATION_EXT 0x0506
  102. #endif
  103. #endif
  104. */
  105. namespace Polycode {
  106. class _PolyExport OpenGLRenderer : public Renderer {
  107. public:
  108. OpenGLRenderer();
  109. ~OpenGLRenderer();
  110. void Resize(int xRes, int yRes);
  111. void BeginRender();
  112. void EndRender();
  113. Cubemap *createCubemap(Texture *t0, Texture *t1, Texture *t2, Texture *t3, Texture *t4, Texture *t5);
  114. Texture *createTexture(unsigned int width, unsigned int height, char *textureData, bool clamp, int type = Image::IMAGE_RGBA);
  115. Texture *createFramebufferTexture(unsigned int width, unsigned int height);
  116. void createRenderTextures(Texture **colorBuffer, Texture **depthBuffer, int width, int height);
  117. void enableAlphaTest(bool val);
  118. void createVertexBufferForMesh(Mesh *mesh);
  119. void drawVertexBuffer(VertexBuffer *buffer);
  120. void bindFrameBufferTexture(Texture *texture);
  121. void unbindFramebuffers();
  122. void cullFrontFaces(bool val);
  123. void pushRenderDataArray(RenderDataArray *array);
  124. RenderDataArray *createRenderDataArrayForMesh(Mesh *mesh, int arrayType);
  125. RenderDataArray *createRenderDataArray(int arrayType);
  126. void setRenderArrayData(RenderDataArray *array, Number *arrayData);
  127. void drawArrays(int drawType);
  128. void setOrthoMode(Number xSize=0.0f, Number ySize=0.0f);
  129. void _setOrthoMode();
  130. void setPerspectiveMode();
  131. void enableBackfaceCulling(bool val);
  132. void setViewportSize(int w, int h, Number fov=45.0f);
  133. void setLineSmooth(bool val);
  134. void loadIdentity();
  135. void setClearColor(Number r, Number g, Number b);
  136. void setTexture(Texture *texture);
  137. void renderToTexture(Texture *targetTexture);
  138. void renderZBufferToTexture(Texture *targetTexture);
  139. void clearScreen();
  140. void translate2D(Number x, Number y);
  141. void rotate2D(Number angle);
  142. void scale2D(Vector2 *scale);
  143. Vector3 projectRayFrom2DCoordinate(Number x, Number y);
  144. void initOSSpecific();
  145. void setLineSize(Number lineSize);
  146. void setVertexColor(Number r, Number g, Number b, Number a);
  147. void setBlendingMode(int blendingMode);
  148. void enableLighting(bool enable);
  149. void enableFog(bool enable);
  150. void setFogProperties(int fogMode, Color color, Number density, Number startDepth, Number endDepth);
  151. void translate3D(Vector3 *position);
  152. void translate3D(Number x, Number y, Number z);
  153. void scale3D(Vector3 *scale);
  154. Matrix4 getProjectionMatrix();
  155. Matrix4 getModelviewMatrix();
  156. void setModelviewMatrix(Matrix4 m);
  157. void multModelviewMatrix(Matrix4 m);
  158. void enableDepthTest(bool val);
  159. void enableDepthWrite(bool val);
  160. void setClippingPlanes(Number near, Number far);
  161. void clearBuffer(bool colorBuffer, bool depthBuffer);
  162. void drawToColorBuffer(bool val);
  163. void drawScreenQuad(Number qx, Number qy);
  164. void pushMatrix();
  165. void popMatrix();
  166. bool test2DCoordinate(Number x, Number y, Polycode::Polygon *poly, const Matrix4 &matrix, bool billboardMode);
  167. void setFOV(Number fov);
  168. Vector3 Unproject(Number x, Number y);
  169. void setDepthFunction(int depthFunction);
  170. void clearShader();
  171. void applyMaterial(Material *material, ShaderBinding *localOptions, unsigned int shaderIndex);
  172. protected:
  173. Number nearPlane;
  174. Number farPlane;
  175. int verticesToDraw;
  176. GLdouble sceneProjectionMatrix[16];
  177. };
  178. }