PolyRenderer.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. #include <stack>
  28. namespace Polycode {
  29. class Cubemap;
  30. class Material;
  31. class Mesh;
  32. class PolycodeShaderBinding;
  33. class PolycodeShaderModule;
  34. class Polygon;
  35. class RenderDataArray;
  36. class IndexDataArray;
  37. class ShaderBinding;
  38. class Texture;
  39. class VertexBuffer;
  40. class _PolyExport LightInfo : public PolyBase {
  41. public:
  42. Vector3 position;
  43. Vector3 color;
  44. Color specularColor;
  45. Vector3 dir;
  46. Number constantAttenuation;
  47. Number linearAttenuation;
  48. Number quadraticAttenuation;
  49. Number intensity;
  50. Number spotlightCutoff;
  51. Number spotlightExponent;
  52. int type;
  53. bool shadowsEnabled;
  54. Matrix4 textureMatrix;
  55. Texture* shadowMapTexture;
  56. int lightImportance;
  57. };
  58. class _PolyExport LightSorter : public PolyBase {
  59. public:
  60. Vector3 basePosition;
  61. bool operator() (LightInfo i,LightInfo j) {
  62. if(i.lightImportance > j.lightImportance)
  63. return true;
  64. if(i.lightImportance == j.lightImportance)
  65. return i.position.distance(basePosition) < j.position.distance(basePosition);
  66. return false;
  67. }
  68. };
  69. /**
  70. * Provides low-level settings for the main renderer.
  71. *
  72. * The methods and settings in this class are closely related to OpenGL.
  73. * If you have trouble understanding anything in this class, it is thus suggested to brush up on your OpenGL knowledge.
  74. *
  75. * 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.
  76. *
  77. */
  78. class _PolyExport Renderer : public PolyBase {
  79. public:
  80. Renderer();
  81. virtual ~Renderer();
  82. virtual bool Init();
  83. virtual void Resize(int xRes, int yRes) = 0;
  84. virtual void BeginRender();
  85. virtual void EndRender();
  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 destroyVertexBuffer(VertexBuffer *buffer) = 0;
  90. virtual void createRenderTextures(Texture **colorBuffer, Texture **depthBuffer, int width, int height, bool floatingPointBuffer) = 0;
  91. virtual Texture *createFramebufferTexture(unsigned int width, unsigned int height) = 0;
  92. virtual void bindFrameBufferTexture(Texture *texture);
  93. virtual void bindFrameBufferTextureDepth(Texture *texture);
  94. virtual void unbindFramebuffers();
  95. virtual Image *renderScreenToImage() = 0;
  96. virtual Image *renderBufferToImage(Texture *texture) = 0;
  97. void setViewportSize(int w, int h);
  98. virtual void resetViewport() = 0;
  99. virtual Polycode::Rectangle getViewport() = 0;
  100. virtual void loadIdentity() = 0;
  101. virtual void setProjectionOrtho(Number xSize=0.0f, Number ySize=0.0f, Number near=-256.0f, Number far=256.0f, bool centered = false) = 0;
  102. virtual void setPerspectiveDefaults() = 0;
  103. virtual void setProjectionMatrix(Matrix4 matrix) = 0;
  104. virtual void setTexture(Texture *texture) = 0;
  105. virtual void enableBackfaceCulling(bool val) = 0;
  106. virtual void setClearColor(Number r, Number g, Number b, Number a = 1.0);
  107. virtual void setClearColor(Color color);
  108. virtual void setAmbientColor(Number r, Number g, Number b);
  109. virtual void clearScreen(bool clearColor = true, bool clearDepth = true) = 0;
  110. virtual void translate2D(Number x, Number y) = 0;
  111. virtual void rotate2D(Number angle) = 0;
  112. virtual void scale2D(const Vector2 &scale) = 0;
  113. virtual void setVertexColor(Number r, Number g, Number b, Number a) = 0;
  114. virtual void pushRenderDataArray(RenderDataArray *array) = 0;
  115. virtual void drawArrays(int drawType, IndexDataArray *indexArray) = 0;
  116. virtual void translate3D(const Vector3 &position) = 0;
  117. virtual void translate3D(Number x, Number y, Number z) = 0;
  118. virtual void scale3D(const Vector3 &scale) = 0;
  119. virtual void pushMatrix() = 0;
  120. virtual void popMatrix() = 0;
  121. virtual void setLineSmooth(bool val) = 0;
  122. virtual void setLineSize(Number lineSize) = 0;
  123. virtual void setPointSize(Number pointSize) = 0;
  124. virtual void setPointSmooth(bool val) = 0;
  125. virtual void enableLighting(bool enable) = 0;
  126. virtual void enableFog(bool enable) = 0;
  127. virtual void setFogProperties(int fogMode, Color color, Number density, Number startDepth, Number endDepth) = 0;
  128. virtual void multModelviewMatrix(Matrix4 m) = 0;
  129. virtual void setModelviewMatrix(Matrix4 m) = 0;
  130. virtual void setBlendingMode(int blendingMode) = 0;
  131. virtual void applyMaterial(Material *material, ShaderBinding *localOptions, unsigned int shaderIndex, bool forceMaterial);
  132. virtual void clearShader() = 0;
  133. virtual void setDepthFunction(int depthFunction) = 0;
  134. virtual VertexBuffer *createVertexBufferForMesh(Mesh *mesh) = 0;
  135. virtual void drawVertexBuffer(VertexBuffer *buffer, bool enableColorBuffer) = 0;
  136. virtual void enableDepthTest(bool val) = 0;
  137. virtual void enableDepthWrite(bool val) = 0;
  138. virtual void setWireframePolygonMode(bool val) = 0;
  139. void billboardMatrix();
  140. void billboardMatrixWithScale(Vector3 scale);
  141. void setTextureFilteringMode(int mode);
  142. /**
  143. * Set the frustum clipping planes.
  144. *
  145. * Please check the supplied external links for more information
  146. * about the problems of a high farPlane/nearPlane setting.
  147. *
  148. * @see http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml
  149. */
  150. virtual void setProjectionFromFrustum(Number left, Number right, Number bottom, Number top, Number front, Number back) = 0;
  151. virtual void setProjectionFromFoV(Number fov, Number near, Number far) = 0;
  152. /**
  153. * Enable/disable alpha tests.
  154. *
  155. * If alpha tests are enabled, drawn pixels of textures will
  156. * be "mixed" with framebuffer pixels based on the drawn pixel's
  157. * alpha value. If alpha tests are disabled, they will be drawn
  158. * as solid color.
  159. *
  160. * @param val Whether to enable or disable alpha tests.
  161. */
  162. virtual void enableAlphaTest(bool val) = 0;
  163. virtual void clearBuffer(bool colorBuffer, bool depthBuffer) = 0;
  164. virtual void drawToColorBuffer(bool val) = 0;
  165. const Matrix4& getCameraMatrix() const;
  166. void setCameraMatrix(const Matrix4& matrix);
  167. virtual void drawScreenQuad(Number qx, Number qy) = 0;
  168. int getXRes();
  169. int getYRes();
  170. bool isScissorEnabled();
  171. virtual void enableScissor(bool val);
  172. virtual void setScissorBox(Polycode::Rectangle box);
  173. Polycode::Rectangle getScissorBox();
  174. void setAnisotropyAmount(Number amount);
  175. Number getAnisotropyAmount();
  176. virtual void cullFrontFaces(bool val) = 0;
  177. void clearLights();
  178. void addLight(int lightImportance, const Vector3 &position, const Vector3 &direction, int type, const Color &color, const Color &specularColor, Number constantAttenuation, Number linearAttenuation, Number quadraticAttenuation, Number intensity, Number spotlightCutoff, Number spotlightExponent, bool shadowsEnabled, Matrix4 *textureMatrix, Texture *shadowMapTexture);
  179. void setExposureLevel(Number level);
  180. virtual Vector3 projectRayFrom2DCoordinate(Number x, Number y, const Matrix4 &cameraMatrix, const Matrix4 &projectionMatrix, const Polycode::Rectangle &viewport) = 0;
  181. virtual Vector2 Project(const Matrix4 &cameraMatrix, const Matrix4 &projectionMatrix, const Polycode::Rectangle &viewport, const Vector3 &coordiante) const = 0;
  182. void enableShaders(bool flag);
  183. Number getViewportWidth();
  184. Number getViewportHeight();
  185. void setViewportShift(Number shiftX, Number shiftY);
  186. void *getDataPointerForName(const String &name);
  187. void setRendererShaderParams(Shader *shader, ShaderBinding *binding);
  188. void addShaderModule(PolycodeShaderModule *module);
  189. virtual Matrix4 getProjectionMatrix() = 0;
  190. virtual Matrix4 getModelviewMatrix() = 0;
  191. static const int BLEND_MODE_NONE = 0;
  192. static const int BLEND_MODE_NORMAL = 1;
  193. static const int BLEND_MODE_LIGHTEN = 2;
  194. static const int BLEND_MODE_COLOR = 3;
  195. static const int BLEND_MODE_PREMULTIPLIED = 4;
  196. static const int BLEND_MODE_MULTIPLY = 5;
  197. static const int FOG_LINEAR = 0;
  198. static const int FOG_EXP = 1;
  199. static const int FOG_EXP2 = 2;
  200. static const int DEPTH_FUNCTION_GREATER = 0;
  201. static const int DEPTH_FUNCTION_LEQUAL = 1;
  202. static const int TEX_FILTERING_NEAREST = 0;
  203. static const int TEX_FILTERING_LINEAR = 1;
  204. virtual Vector3 Unproject(Number x, Number y, const Matrix4 &cameraMatrix, const Matrix4 &projectionMatrix, const Polycode::Rectangle &viewport) = 0;
  205. Color ambientColor;
  206. Color clearColor;
  207. Number exposureLevel;
  208. Vector3 cameraPosition;
  209. void sortLights();
  210. int getNumPointLights() { return numPointLights; }
  211. int getNumSpotLights() { return numSpotLights; }
  212. int getNumLights() { return numLights; }
  213. std::vector<LightInfo> getPointLights() { return pointLights; }
  214. std::vector<LightInfo> getSpotLights() { return spotLights; }
  215. bool doClearBuffer;
  216. bool blendNormalAsPremultiplied;
  217. Number alphaTestValue;
  218. void setBackingResolutionScale(Number xScale, Number yScale);
  219. Number getBackingResolutionScaleX();
  220. Number getBackingResolutionScaleY();
  221. void setOverrideMaterial(Material *material);
  222. void pushVertexColor();
  223. void popVertexColor();
  224. void loadVertexColorIdentity();
  225. void multiplyVertexColor(const Color &color);
  226. void setRenderToGlobalFramebuffer(bool val);
  227. bool getRenderToGlobalFramebuffer() const;
  228. Texture *getGlobalColorFramebuffer() const;
  229. Texture *getGlobalDepthFramebuffer() const;
  230. protected:
  231. virtual void initOSSpecific() {};
  232. Number backingResolutionScaleX;
  233. Number backingResolutionScaleY;
  234. std::stack<Color> vertexColorStack;
  235. Color currentVertexColor;
  236. std::stack<Texture*> framebufferStackColor;
  237. std::stack<Texture*> framebufferStackDepth;
  238. bool scissorEnabled;
  239. Polycode::Rectangle scissorBox;
  240. Number anisotropy;
  241. LightSorter sorter;
  242. Number viewportWidth;
  243. Number viewportHeight;
  244. Vector2 viewportShift;
  245. bool cullingFrontFaces;
  246. Texture *currentTexture;
  247. Material *currentMaterial;
  248. int textureFilteringMode;
  249. Matrix4 cameraMatrix;
  250. Material *overrideMaterial;
  251. PolycodeShaderModule* currentShaderModule;
  252. std::vector <PolycodeShaderModule*> shaderModules;
  253. bool renderToGlobalFramebuffer;
  254. Texture *globalColorFramebuffer;
  255. Texture *globalDepthFramebuffer;
  256. std::vector<LightInfo> lights;
  257. std::vector<LightInfo> pointLights;
  258. std::vector<LightInfo> spotLights;
  259. int numLights;
  260. int numPointLights;
  261. int numSpotLights;
  262. bool shadersEnabled;
  263. Number fov;
  264. bool lightingEnabled;
  265. int xRes;
  266. int yRes;
  267. };
  268. }