PolyRenderer.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 "PolyGlobals.h"
  22. #include "PolyMatrix4.h"
  23. #include "PolyVector2.h"
  24. #include "PolyShader.h"
  25. #include "PolyImage.h"
  26. #include "PolyRectangle.h"
  27. namespace Polycode {
  28. class Cubemap;
  29. class Material;
  30. class Mesh;
  31. class PolycodeShaderBinding;
  32. class PolycodeShaderModule;
  33. class Polygon;
  34. class RenderDataArray;
  35. class ShaderBinding;
  36. class Texture;
  37. class VertexBuffer;
  38. class _PolyExport LightInfo : public PolyBase {
  39. public:
  40. Vector3 position;
  41. Vector3 color;
  42. Color specularColor;
  43. Vector3 dir;
  44. Number constantAttenuation;
  45. Number linearAttenuation;
  46. Number quadraticAttenuation;
  47. Number intensity;
  48. Number spotlightCutoff;
  49. Number spotlightExponent;
  50. int type;
  51. bool shadowsEnabled;
  52. Matrix4 textureMatrix;
  53. Texture* shadowMapTexture;
  54. int lightImportance;
  55. };
  56. class _PolyExport LightSorter : public PolyBase {
  57. public:
  58. Vector3 basePosition;
  59. Matrix4 cameraMatrix;
  60. bool operator() (LightInfo i,LightInfo j) {
  61. if(i.lightImportance > j.lightImportance)
  62. return true;
  63. if(i.lightImportance == j.lightImportance)
  64. return ((cameraMatrix*i.position).distance(basePosition)<(cameraMatrix*j.position).distance(basePosition));
  65. return false;
  66. }
  67. };
  68. /**
  69. * Provides low-level settings for the main renderer.
  70. *
  71. * The methods and settings in this class are closely related to OpenGL.
  72. * If you have trouble understanding anything in this class, it is thus suggested to brush up on your OpenGL knowledge.
  73. *
  74. * The renderer should only be accessed from the CoreServices singleton. Renderer operations should only be called from within Render methods of entities so that they can be properly managed.
  75. *
  76. * @see http://www.glprogramming.com/red/
  77. * @see http://nehe.gamedev.net/tutorial/lessons_01__05/22004/
  78. */
  79. class _PolyExport Renderer : public PolyBase {
  80. public:
  81. Renderer();
  82. virtual ~Renderer();
  83. virtual void Resize(int xRes, int yRes) = 0;
  84. virtual void BeginRender() = 0;
  85. virtual void EndRender() = 0;
  86. virtual Cubemap *createCubemap(Texture *t0, Texture *t1, Texture *t2, Texture *t3, Texture *t4, Texture *t5) = 0;
  87. virtual Texture *createTexture(unsigned int width, unsigned int height, char *textureData, bool clamp, bool createMipmaps, int type=Image::IMAGE_RGBA) = 0;
  88. virtual void destroyTexture(Texture *texture) = 0;
  89. virtual void createRenderTextures(Texture **colorBuffer, Texture **depthBuffer, int width, int height, bool floatingPointBuffer) = 0;
  90. virtual Texture *createFramebufferTexture(unsigned int width, unsigned int height) = 0;
  91. virtual void bindFrameBufferTexture(Texture *texture) = 0;
  92. virtual void bindFrameBufferTextureDepth(Texture *texture) = 0;
  93. virtual void unbindFramebuffers() = 0;
  94. virtual Image *renderScreenToImage() = 0;
  95. void setFOV(Number fov);
  96. void setViewportSize(int w, int h);
  97. void setViewportSizeAndFOV(int w, int h, Number fov);
  98. virtual void resetViewport() = 0;
  99. virtual void loadIdentity() = 0;
  100. virtual void setOrthoMode(Number xSize=0.0f, Number ySize=0.0f, bool centered = false) = 0;
  101. virtual void _setOrthoMode(Number orthoSizeX, Number orthoSizeY) = 0;
  102. virtual void setPerspectiveMode() = 0;
  103. virtual void setTexture(Texture *texture) = 0;
  104. virtual void enableBackfaceCulling(bool val) = 0;
  105. virtual void setClearColor(Number r, Number g, Number b, Number a = 1.0);
  106. virtual void setClearColor(Color color);
  107. virtual void setAmbientColor(Number r, Number g, Number b);
  108. virtual void clearScreen() = 0;
  109. virtual void translate2D(Number x, Number y) = 0;
  110. virtual void rotate2D(Number angle) = 0;
  111. virtual void scale2D(Vector2 *scale) = 0;
  112. virtual void setVertexColor(Number r, Number g, Number b, Number a) = 0;
  113. void pushDataArrayForMesh(Mesh *mesh, int arrayType);
  114. virtual void pushRenderDataArray(RenderDataArray *array) = 0;
  115. virtual RenderDataArray *createRenderDataArrayForMesh(Mesh *mesh, int arrayType) = 0;
  116. virtual RenderDataArray *createRenderDataArray(int arrayType) = 0;
  117. virtual void setRenderArrayData(RenderDataArray *array, Number *arrayData) = 0;
  118. virtual void drawArrays(int drawType) = 0;
  119. virtual void translate3D(Vector3 *position) = 0;
  120. virtual void translate3D(Number x, Number y, Number z) = 0;
  121. virtual void scale3D(Vector3 *scale) = 0;
  122. virtual void pushMatrix() = 0;
  123. virtual void popMatrix() = 0;
  124. virtual void setLineSmooth(bool val) = 0;
  125. virtual void setLineSize(Number lineSize) = 0;
  126. virtual void enableLighting(bool enable) = 0;
  127. virtual void enableFog(bool enable) = 0;
  128. virtual void setFogProperties(int fogMode, Color color, Number density, Number startDepth, Number endDepth) = 0;
  129. virtual void multModelviewMatrix(Matrix4 m) = 0;
  130. virtual void setModelviewMatrix(Matrix4 m) = 0;
  131. void setCurrentModelMatrix(Matrix4 m) { currentModelMatrix = m; }
  132. Matrix4 getCurrentModelMatrix() { return currentModelMatrix; }
  133. virtual void setBlendingMode(int blendingMode) = 0;
  134. virtual void applyMaterial(Material *material, ShaderBinding *localOptions, unsigned int shaderIndex) = 0;
  135. virtual void clearShader() = 0;
  136. virtual void setDepthFunction(int depthFunction) = 0;
  137. virtual void createVertexBufferForMesh(Mesh *mesh) = 0;
  138. virtual void drawVertexBuffer(VertexBuffer *buffer, bool enableColorBuffer) = 0;
  139. void setRenderMode(int newRenderMode);
  140. int getRenderMode();
  141. virtual void enableDepthTest(bool val) = 0;
  142. virtual void enableDepthWrite(bool val) = 0;
  143. void billboardMatrix();
  144. void billboardMatrixWithScale(Vector3 scale);
  145. void setTextureFilteringMode(int mode);
  146. /**
  147. * Set the near and far clipping planes for the visible frustum.
  148. *
  149. * Please check the supplied external links for more information
  150. * about the problems of a high farPlane/nearPlane setting.
  151. *
  152. * @param nearPlane The new near clipping plane.
  153. * @param farPlane The new far clipping plane.
  154. *
  155. * @see http://en.wikipedia.org/wiki/Viewing_frustum
  156. * @see http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml
  157. */
  158. virtual void setClippingPlanes(Number nearPlane, Number farPlane) = 0;
  159. /**
  160. * Enable/disable alpha tests.
  161. *
  162. * If alpha tests are enabled, drawn pixels of textures will
  163. * be "mixed" with framebuffer pixels based on the drawn pixel's
  164. * alpha value. If alpha tests are disabled, they will be drawn
  165. * as solid color.
  166. *
  167. * @param val Whether to enable or disable alpha tests.
  168. */
  169. virtual void enableAlphaTest(bool val) = 0;
  170. virtual void clearBuffer(bool colorBuffer, bool depthBuffer) = 0;
  171. virtual void drawToColorBuffer(bool val) = 0;
  172. const Matrix4& getCameraMatrix() const;
  173. void setCameraMatrix(const Matrix4& matrix);
  174. void setCameraPosition(Vector3 pos);
  175. virtual void drawScreenQuad(Number qx, Number qy) = 0;
  176. int getXRes();
  177. int getYRes();
  178. bool isScissorEnabled();
  179. virtual void enableScissor(bool val);
  180. virtual void setScissorBox(Polycode::Rectangle box);
  181. Polycode::Rectangle getScissorBox();
  182. void setAnisotropyAmount(Number amount);
  183. Number getAnisotropyAmount();
  184. virtual void cullFrontFaces(bool val) = 0;
  185. void clearLights();
  186. void addLight(int lightImportance, Vector3 position, Vector3 direction, int type, Color color, Color specularColor, Number constantAttenuation, Number linearAttenuation, Number quadraticAttenuation, Number intensity, Number spotlightCutoff, Number spotlightExponent, bool shadowsEnabled, Matrix4 *textureMatrix, Texture *shadowMapTexture);
  187. void setExposureLevel(Number level);
  188. bool rayTriangleIntersect(Vector3 ray_origin, Vector3 ray_direction, Vector3 vert0, Vector3 vert1, Vector3 vert2, Vector3 *hitPoint);
  189. virtual Vector3 projectRayFrom2DCoordinate(Number x, Number y) = 0;
  190. void enableShaders(bool flag);
  191. Number getViewportWidth();
  192. Number getViewportHeight();
  193. void *getDataPointerForName(const String &name);
  194. void setRendererShaderParams(Shader *shader, ShaderBinding *binding);
  195. virtual void initOSSpecific() {};
  196. void addShaderModule(PolycodeShaderModule *module);
  197. virtual bool test2DCoordinateInPolygon(Number x, Number y, Polygon *poly, const Matrix4 &matrix, bool ortho, bool testBackfacing, bool billboardMode, bool reverseDirection = false, Matrix4 *adjustMatrix = NULL);
  198. virtual Matrix4 getProjectionMatrix() = 0;
  199. virtual Matrix4 getModelviewMatrix() = 0;
  200. static const int RENDER_MODE_NORMAL = 0;
  201. static const int RENDER_MODE_WIREFRAME = 1;
  202. static const int BLEND_MODE_NORMAL = 0;
  203. static const int BLEND_MODE_LIGHTEN = 1;
  204. static const int BLEND_MODE_COLOR = 2;
  205. static const int BLEND_MODE_PREMULTIPLIED = 3;
  206. static const int BLEND_MODE_MULTIPLY = 4;
  207. static const int FOG_LINEAR = 0;
  208. static const int FOG_EXP = 1;
  209. static const int FOG_EXP2 = 2;
  210. static const int DEPTH_FUNCTION_GREATER = 0;
  211. static const int DEPTH_FUNCTION_LEQUAL = 1;
  212. static const int TEX_FILTERING_NEAREST = 0;
  213. static const int TEX_FILTERING_LINEAR = 1;
  214. // void addShadowMap(Texture *texture);
  215. // vector<Texture*> getShadowMapTextures(){ return shadowMapTextures; };
  216. virtual Vector3 Unproject(Number x, Number y) = 0;
  217. Color ambientColor;
  218. Color clearColor;
  219. Number exposureLevel;
  220. Vector3 cameraPosition;
  221. void sortLights();
  222. int getNumAreaLights() { return numAreaLights; }
  223. int getNumSpotLights() { return numSpotLights; }
  224. int getNumLights() { return numLights; }
  225. std::vector<LightInfo> getAreaLights() { return areaLights; }
  226. std::vector<LightInfo> getSpotLights() { return spotLights; }
  227. bool doClearBuffer;
  228. bool blendNormalAsPremultiplied;
  229. protected:
  230. bool scissorEnabled;
  231. Polycode::Rectangle scissorBox;
  232. Number anisotropy;
  233. Matrix4 currentModelMatrix;
  234. LightSorter sorter;
  235. Number viewportWidth;
  236. Number viewportHeight;
  237. bool cullingFrontFaces;
  238. Texture *currentTexture;
  239. Material *currentMaterial;
  240. int textureFilteringMode;
  241. int renderMode;
  242. Matrix4 cameraMatrix;
  243. PolycodeShaderModule* currentShaderModule;
  244. std::vector <PolycodeShaderModule*> shaderModules;
  245. std::vector<LightInfo> lights;
  246. std::vector<LightInfo> areaLights;
  247. std::vector<LightInfo> spotLights;
  248. int numLights;
  249. int numAreaLights;
  250. int numSpotLights;
  251. bool shadersEnabled;
  252. Number fov;
  253. Number orthoSizeX;
  254. Number orthoSizeY;
  255. bool lightingEnabled;
  256. bool orthoMode;
  257. int xRes;
  258. int yRes;
  259. };
  260. }