PolyRenderer.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. #include "PolyRenderer.h"
  20. #include "PolyFixedShader.h"
  21. #include "PolyMaterial.h"
  22. #include "PolyModule.h"
  23. #include "PolyMesh.h"
  24. using namespace Polycode;
  25. Renderer::Renderer() : clearColor(0.2, 0.2, 0.2, 0.0), currentTexture(NULL), lightingEnabled(false), xRes(0), yRes(0) {
  26. anisotropy = 0;
  27. textureFilteringMode = TEX_FILTERING_LINEAR;
  28. currentMaterial = NULL;
  29. numLights = 0;
  30. exposureLevel = 1;
  31. shadersEnabled = true;
  32. currentMaterial = NULL;
  33. numLights = 0;
  34. numPointLights = 0;
  35. numSpotLights = 0;
  36. exposureLevel = 1;
  37. shadersEnabled = true;
  38. currentShaderModule = NULL;
  39. setAmbientColor(0.0,0.0,0.0);
  40. cullingFrontFaces = false;
  41. scissorEnabled = false;
  42. blendNormalAsPremultiplied = false;
  43. alphaTestValue = 0.01;
  44. doClearBuffer = true;
  45. backingResolutionScaleX = 1.0;
  46. backingResolutionScaleY = 1.0;
  47. overrideMaterial = NULL;
  48. }
  49. void Renderer::setOverrideMaterial(Material *material) {
  50. overrideMaterial = material;
  51. }
  52. Number Renderer::getBackingResolutionScaleX() {
  53. return backingResolutionScaleX;
  54. }
  55. Number Renderer::getBackingResolutionScaleY() {
  56. return backingResolutionScaleY;
  57. }
  58. Renderer::~Renderer() {
  59. }
  60. bool Renderer::Init() {
  61. initOSSpecific();
  62. return true;
  63. }
  64. void Renderer::pushVertexColor() {
  65. vertexColorStack.push(currentVertexColor);
  66. }
  67. void Renderer::popVertexColor() {
  68. currentVertexColor = vertexColorStack.top();
  69. vertexColorStack.pop();
  70. }
  71. void Renderer::multiplyVertexColor(const Color &color) {
  72. currentVertexColor = currentVertexColor * color;
  73. setVertexColor(currentVertexColor.r, currentVertexColor.g, currentVertexColor.b, currentVertexColor.a);
  74. }
  75. void Renderer::enableShaders(bool flag) {
  76. shadersEnabled = flag;
  77. }
  78. void Renderer::setCameraMatrix(const Matrix4& matrix) {
  79. cameraMatrix = matrix;
  80. }
  81. void Renderer::clearLights() {
  82. numLights = 0;
  83. numPointLights = 0;
  84. numSpotLights = 0;
  85. lights.clear();
  86. pointLights.clear();
  87. spotLights.clear();
  88. }
  89. void Renderer::bindFrameBufferTexture(Texture *texture) {
  90. framebufferStackColor.push(texture);
  91. }
  92. void Renderer::bindFrameBufferTextureDepth(Texture *texture) {
  93. framebufferStackDepth.push(texture);
  94. }
  95. void Renderer::unbindFramebuffers() {
  96. if(framebufferStackColor.size() > 0) {
  97. framebufferStackColor.pop();
  98. }
  99. if(framebufferStackDepth.size() > 0) {
  100. framebufferStackDepth.pop();
  101. }
  102. if(framebufferStackColor.size() > 0) {
  103. Texture *rebindTexture = framebufferStackColor.top();
  104. framebufferStackColor.pop();
  105. bindFrameBufferTexture(rebindTexture);
  106. }
  107. if(framebufferStackDepth.size() > 0) {
  108. Texture *rebindTexture = framebufferStackDepth.top();
  109. framebufferStackDepth.pop();
  110. bindFrameBufferTextureDepth(rebindTexture);
  111. }
  112. }
  113. void Renderer::setExposureLevel(Number level) {
  114. exposureLevel = level;
  115. }
  116. void Renderer::addShaderModule(PolycodeShaderModule *module) {
  117. shaderModules.push_back(module);
  118. }
  119. void Renderer::sortLights(){
  120. sorter.basePosition = (getModelviewMatrix() * cameraMatrix).getPosition();
  121. sort (pointLights.begin(), pointLights.end(), sorter);
  122. sort (spotLights.begin(), spotLights.end(), sorter);
  123. }
  124. void Renderer::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) {
  125. numLights++;
  126. LightInfo info;
  127. if(textureMatrix != NULL) {
  128. info.textureMatrix = *textureMatrix;
  129. }
  130. info.lightImportance = lightImportance;
  131. info.shadowMapTexture = shadowMapTexture;
  132. info.shadowsEnabled = shadowsEnabled;
  133. info.spotlightCutoff = spotlightCutoff;
  134. info.spotlightExponent = spotlightExponent;
  135. info.intensity = intensity;
  136. info.type = type;
  137. info.dir = direction;
  138. info.constantAttenuation = constantAttenuation;
  139. info.linearAttenuation = linearAttenuation;
  140. info.quadraticAttenuation = quadraticAttenuation;
  141. info.color.set(color.r, color.g, color.b);
  142. info.specularColor = specularColor;
  143. info.position = position;
  144. lights.push_back(info);
  145. switch(type) {
  146. case 0: //point light
  147. pointLights.push_back(info);
  148. numPointLights++;
  149. break;
  150. case 1: //spot light
  151. spotLights.push_back(info);
  152. numSpotLights++;
  153. break;
  154. }
  155. }
  156. Number Renderer::getAnisotropyAmount() {
  157. return anisotropy;
  158. }
  159. void Renderer::setAnisotropyAmount(Number amount) {
  160. anisotropy = amount;
  161. }
  162. const Matrix4& Renderer::getCameraMatrix() const {
  163. return cameraMatrix;
  164. }
  165. void Renderer::enableScissor(bool val) {
  166. scissorEnabled = val;
  167. }
  168. void Renderer::setScissorBox(Polycode::Rectangle box) {
  169. scissorBox = box;
  170. }
  171. Polycode::Rectangle Renderer::getScissorBox() {
  172. return scissorBox;
  173. }
  174. void Renderer::setViewportShift(Number shiftX, Number shiftY) {
  175. viewportShift.x = shiftX;
  176. viewportShift.y = shiftY;
  177. resetViewport();
  178. }
  179. bool Renderer::isScissorEnabled() {
  180. return scissorEnabled;
  181. }
  182. void Renderer::billboardMatrixWithScale(Vector3 scale) {
  183. Matrix4 matrix = getModelviewMatrix();
  184. matrix.m[0][0] = 1.0f*scale.x;
  185. matrix.m[0][1] = 0;
  186. matrix.m[0][2] = 0;
  187. matrix.m[1][0] = 0;
  188. matrix.m[1][1] = 1.0f*scale.y;
  189. matrix.m[1][2] = 0;
  190. matrix.m[2][0] = 0;
  191. matrix.m[2][1] = 0;
  192. matrix.m[2][2] = 1.0f*scale.z;
  193. setModelviewMatrix(matrix);
  194. }
  195. void Renderer::billboardMatrix() {
  196. Matrix4 matrix = getModelviewMatrix();
  197. matrix.m[0][0] = 1;
  198. matrix.m[0][1] = 0;
  199. matrix.m[0][2] = 0;
  200. matrix.m[1][0] = 0;
  201. matrix.m[1][1] = 1;
  202. matrix.m[1][2] = 0;
  203. matrix.m[2][0] = 0;
  204. matrix.m[2][1] = 0;
  205. matrix.m[2][2] = 1;
  206. setModelviewMatrix(matrix);
  207. }
  208. void *Renderer::getDataPointerForName(const String &name) {
  209. if(name == "ambient_color") {
  210. return (void*)&ambientColor;
  211. }
  212. return NULL;
  213. }
  214. void Renderer::setRendererShaderParams(Shader *shader, ShaderBinding *binding) {
  215. for(int i=0; i < shader->expectedParams.size(); i++) {
  216. void *dataPtr = getDataPointerForName(shader->expectedParams[i].name);
  217. if(dataPtr) {
  218. binding->addParamPointer(shader->expectedParams[i].type, shader->expectedParams[i].name, dataPtr);
  219. }
  220. }
  221. }
  222. void Renderer::pushDataArrayForMesh(Mesh *mesh, int arrayType) {
  223. if(mesh->arrayDirtyMap[arrayType] == true || mesh->renderDataArrays[arrayType] == NULL) {
  224. if(mesh->renderDataArrays[arrayType] != NULL) {
  225. free(mesh->renderDataArrays[arrayType]->arrayPtr);
  226. delete mesh->renderDataArrays[arrayType];
  227. }
  228. mesh->renderDataArrays[arrayType] = createRenderDataArrayForMesh(mesh, arrayType);
  229. mesh->arrayDirtyMap[arrayType] = false;
  230. }
  231. pushRenderDataArray(mesh->renderDataArrays[arrayType]);
  232. }
  233. void Renderer::applyMaterial(Material *material, ShaderBinding *localOptions,unsigned int shaderIndex, bool forceMaterial) {
  234. if(overrideMaterial) {
  235. if(!forceMaterial) {
  236. material = overrideMaterial;
  237. }
  238. }
  239. if(!material->getShader(shaderIndex) || !shadersEnabled) {
  240. setTexture(NULL);
  241. return;
  242. }
  243. FixedShaderBinding *fBinding;
  244. Shader *shader = material->getShader(shaderIndex);
  245. if(shader->numPointLights + shader->numSpotLights > 0) {
  246. sortLights();
  247. }
  248. switch(material->getShader(shaderIndex)->getType()) {
  249. case Shader::FIXED_SHADER:
  250. fBinding = (FixedShaderBinding*)material->getShaderBinding(shaderIndex);
  251. setTexture(fBinding->getDiffuseTexture());
  252. break;
  253. case Shader::MODULE_SHADER:
  254. currentMaterial = material;
  255. if(material->shaderModule == NULL) {
  256. for(int m=0; m < shaderModules.size(); m++) {
  257. PolycodeShaderModule *shaderModule = shaderModules[m];
  258. if(shaderModule->hasShader(material->getShader(shaderIndex))) {
  259. material->shaderModule = (void*)shaderModule;
  260. }
  261. }
  262. } else {
  263. PolycodeShaderModule *shaderModule = (PolycodeShaderModule*)material->shaderModule;
  264. shaderModule->applyShaderMaterial(this, material, localOptions, shaderIndex);
  265. currentShaderModule = shaderModule;
  266. }
  267. break;
  268. }
  269. setBlendingMode(material->blendingMode);
  270. setWireframePolygonMode(material->wireframe);
  271. }
  272. void Renderer::setBackingResolutionScale(Number xScale, Number yScale) {
  273. backingResolutionScaleX = xScale;
  274. backingResolutionScaleY = yScale;
  275. }
  276. int Renderer::getXRes() {
  277. return xRes;
  278. }
  279. int Renderer::getYRes() {
  280. return yRes;
  281. }
  282. void Renderer::setAmbientColor(Number r, Number g, Number b) {
  283. ambientColor.setColor(r,g,b,1.0f);
  284. }
  285. void Renderer::setClearColor(Number r, Number g, Number b, Number a) {
  286. clearColor.setColor(r,g,b,a);
  287. }
  288. void Renderer::setClearColor(Color color) {
  289. setClearColor(color.r, color.g, color.b, color.a);
  290. }
  291. void Renderer::setTextureFilteringMode(int mode) {
  292. textureFilteringMode = mode;
  293. }
  294. void Renderer::setViewportSize(int w, int h) {
  295. viewportWidth = w;
  296. viewportHeight = h;
  297. resetViewport();
  298. }
  299. Number Renderer::getViewportWidth() {
  300. return viewportWidth;
  301. }
  302. Number Renderer::getViewportHeight() {
  303. return viewportHeight;
  304. }