PolyRenderer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 "PolyMesh.h"
  21. using namespace Polycode;
  22. Renderer::Renderer() : clearColor(0.2f, 0.2f, 0.2f, 0.0), currentTexture(NULL), renderMode(0), lightingEnabled(false), orthoMode(false), xRes(0), yRes(0) {
  23. anisotropy = 0;
  24. textureFilteringMode = TEX_FILTERING_LINEAR;
  25. currentMaterial = NULL;
  26. numLights = 0;
  27. exposureLevel = 1;
  28. shadersEnabled = true;
  29. currentMaterial = NULL;
  30. numLights = 0;
  31. numAreaLights = 0;
  32. numSpotLights = 0;
  33. exposureLevel = 1;
  34. shadersEnabled = true;
  35. currentShaderModule = NULL;
  36. fov = 45.0;
  37. setAmbientColor(0.0,0.0,0.0);
  38. cullingFrontFaces = false;
  39. scissorEnabled = false;
  40. blendNormalAsPremultiplied = false;
  41. doClearBuffer = true;
  42. }
  43. Renderer::~Renderer() {
  44. }
  45. void Renderer::enableShaders(bool flag) {
  46. shadersEnabled = flag;
  47. }
  48. void Renderer::setCameraMatrix(const Matrix4& matrix) {
  49. cameraMatrix = matrix;
  50. }
  51. void Renderer::clearLights() {
  52. numLights = 0;
  53. numAreaLights = 0;
  54. numSpotLights = 0;
  55. lights.clear();
  56. areaLights.clear();
  57. spotLights.clear();
  58. // shadowMapTextures.clear();
  59. }
  60. /*
  61. void Renderer::addShadowMap(Texture *texture) {
  62. shadowMapTextures.push_back(texture);
  63. }
  64. */
  65. void Renderer::setExposureLevel(Number level) {
  66. exposureLevel = level;
  67. }
  68. bool Renderer::test2DCoordinateInPolygon(Number x, Number y, Polycode::Polygon *poly, const Matrix4 &matrix, bool ortho, bool testBackfacing, bool billboardMode, bool reverseDirection, Matrix4 *adjustMatrix) {
  69. Vector3 dirVec;
  70. Vector3 origin;
  71. if(ortho) {
  72. origin = Vector3(((x/(Number)xRes)*orthoSizeX) - (orthoSizeX*0.5), (((yRes-y)/(Number)yRes)*orthoSizeY) - (orthoSizeY*0.5), 0.0);
  73. origin = cameraMatrix * origin;
  74. dirVec = Vector3(0.0, 0.0, -1.0);
  75. dirVec = cameraMatrix.rotateVector(dirVec);
  76. } else {
  77. dirVec = projectRayFrom2DCoordinate(x, y);
  78. origin = cameraMatrix.getPosition();
  79. }
  80. Vector3 hitPoint;
  81. Matrix4 fullMatrix = matrix;
  82. if(billboardMode) {
  83. Matrix4 camInverse = cameraMatrix.Inverse();
  84. fullMatrix = fullMatrix * camInverse;
  85. fullMatrix.m[0][0] = 1;
  86. fullMatrix.m[0][1] = 0;
  87. fullMatrix.m[0][2] = 0;
  88. fullMatrix.m[1][0] = 0;
  89. fullMatrix.m[1][1] = 1;
  90. fullMatrix.m[1][2] = 0;
  91. fullMatrix.m[2][0] = 0;
  92. fullMatrix.m[2][1] = 0;
  93. fullMatrix.m[2][2] = 1;
  94. origin = camInverse * origin;
  95. dirVec = camInverse.rotateVector(dirVec);
  96. }
  97. if(adjustMatrix) {
  98. fullMatrix = (*adjustMatrix) * fullMatrix;
  99. }
  100. bool retStatus = false;
  101. if(poly->getVertexCount() == 3) {
  102. if(reverseDirection) {
  103. retStatus = rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(2)), fullMatrix * (*poly->getVertex(1)), fullMatrix * (*poly->getVertex(0)), &hitPoint);
  104. if(testBackfacing && !retStatus) {
  105. retStatus = rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(0)), fullMatrix * (*poly->getVertex(1)), fullMatrix * (*poly->getVertex(2)), &hitPoint);
  106. }
  107. } else {
  108. retStatus = rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(0)), fullMatrix * (*poly->getVertex(1)), fullMatrix * (*poly->getVertex(2)), &hitPoint);
  109. if(testBackfacing && !retStatus) {
  110. retStatus = rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(2)), fullMatrix * (*poly->getVertex(1)), fullMatrix * (*poly->getVertex(0)), &hitPoint);
  111. }
  112. }
  113. } else if(poly->getVertexCount() == 4) {
  114. if(reverseDirection) {
  115. retStatus = (rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(0)), fullMatrix * (*poly->getVertex(1)), fullMatrix * (*poly->getVertex(2)), &hitPoint) ||
  116. rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(2)), fullMatrix * (*poly->getVertex(3)), fullMatrix * (*poly->getVertex(0)), &hitPoint));
  117. if(testBackfacing && !retStatus) {
  118. retStatus = (rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(2)), fullMatrix * (*poly->getVertex(1)), fullMatrix * (*poly->getVertex(0)), &hitPoint) ||
  119. rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(0)), fullMatrix * (*poly->getVertex(3)), fullMatrix * (*poly->getVertex(2)), &hitPoint));
  120. }
  121. } else {
  122. retStatus = (rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(2)), fullMatrix * (*poly->getVertex(1)), fullMatrix * (*poly->getVertex(0)), &hitPoint) ||
  123. rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(0)), fullMatrix * (*poly->getVertex(3)), fullMatrix * (*poly->getVertex(2)), &hitPoint));
  124. if(testBackfacing && !retStatus) {
  125. retStatus = (rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(0)), fullMatrix * (*poly->getVertex(1)), fullMatrix * (*poly->getVertex(2)), &hitPoint) ||
  126. rayTriangleIntersect(origin, dirVec, fullMatrix * (*poly->getVertex(2)), fullMatrix * (*poly->getVertex(3)), fullMatrix * (*poly->getVertex(0)), &hitPoint));
  127. }
  128. }
  129. } else {
  130. retStatus = false;
  131. }
  132. return retStatus;
  133. }
  134. bool Renderer::rayTriangleIntersect(Vector3 ray_origin, Vector3 ray_direction, Vector3 vert0, Vector3 vert1, Vector3 vert2, Vector3 *hitPoint)
  135. {
  136. // printf("TESTING RAY\nORIGIN: %f,%f,%f\nDIR: %f,%f,%f\nVERT0: %f,%f,%f\nnVERT1: %f,%f,%f\nnVERT2: %f,%f,%f\n", ray_origin.x, ray_origin.y, ray_origin.z, ray_direction.x, ray_direction.y, ray_direction.z, vert0.x, vert0.y, vert0.z, vert1.x, vert1.y, vert1.z, vert2.x, vert2.y, vert2.z);
  137. Number t,u,v;
  138. t = 0; u = 0; v = 0;
  139. Vector3 edge1 = vert1 - vert0;
  140. Vector3 edge2 = vert2 - vert0;
  141. Vector3 tvec, pvec, qvec;
  142. Number det, inv_det;
  143. pvec = ray_direction.crossProduct(edge2);
  144. det = edge1.dot(pvec);
  145. if (det > -0.00001f)
  146. return false;
  147. inv_det = 1.0f / det;
  148. tvec = ray_origin - vert0;
  149. u = tvec.dot(pvec) * inv_det;
  150. if (u < -0.001f || u > 1.001f)
  151. return false;
  152. qvec = tvec.crossProduct(edge1);
  153. v = ray_direction.dot(qvec) * inv_det;
  154. if (v < -0.001f || u + v > 1.001f)
  155. return false;
  156. t = edge2.dot(qvec) * inv_det;
  157. if (t <= 0)
  158. return false;
  159. hitPoint->x = ray_origin.x+t*ray_direction.x;
  160. hitPoint->y = ray_origin.y+t*ray_direction.y;
  161. hitPoint->z = ray_origin.z+t*ray_direction.z;
  162. return true;
  163. }
  164. void Renderer::addShaderModule(PolycodeShaderModule *module) {
  165. shaderModules.push_back(module);
  166. }
  167. void Renderer::sortLights(){
  168. sorter.basePosition = (getModelviewMatrix()).getPosition();
  169. sorter.cameraMatrix = getCameraMatrix().Inverse();
  170. sort (areaLights.begin(), areaLights.end(), sorter);
  171. sort (spotLights.begin(), spotLights.end(), sorter);
  172. }
  173. void Renderer::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) {
  174. numLights++;
  175. LightInfo info;
  176. if(textureMatrix != NULL) {
  177. info.textureMatrix = *textureMatrix;
  178. }
  179. info.lightImportance = lightImportance;
  180. info.shadowMapTexture = shadowMapTexture;
  181. info.shadowsEnabled = shadowsEnabled;
  182. info.spotlightCutoff = spotlightCutoff;
  183. info.spotlightExponent = spotlightExponent;
  184. info.intensity = intensity;
  185. info.type = type;
  186. info.dir = direction;
  187. info.constantAttenuation = constantAttenuation;
  188. info.linearAttenuation = linearAttenuation;
  189. info.quadraticAttenuation = quadraticAttenuation;
  190. info.color.set(color.r, color.g, color.b);
  191. info.specularColor = specularColor;
  192. info.position = position;
  193. lights.push_back(info);
  194. switch(type) {
  195. case 0: //area light
  196. areaLights.push_back(info);
  197. numAreaLights++;
  198. break;
  199. case 1: //spot light
  200. spotLights.push_back(info);
  201. numSpotLights++;
  202. break;
  203. }
  204. }
  205. Number Renderer::getAnisotropyAmount() {
  206. return anisotropy;
  207. }
  208. void Renderer::setAnisotropyAmount(Number amount) {
  209. anisotropy = amount;
  210. }
  211. const Matrix4& Renderer::getCameraMatrix() const {
  212. return cameraMatrix;
  213. }
  214. void Renderer::setCameraPosition(Vector3 pos) {
  215. cameraPosition = pos;
  216. pos = pos * -1;
  217. this->translate3D(&pos);
  218. }
  219. void Renderer::enableScissor(bool val) {
  220. scissorEnabled = val;
  221. }
  222. void Renderer::setScissorBox(Polycode::Rectangle box) {
  223. scissorBox = box;
  224. }
  225. Polycode::Rectangle Renderer::getScissorBox() {
  226. return scissorBox;
  227. }
  228. bool Renderer::isScissorEnabled() {
  229. return scissorEnabled;
  230. }
  231. void Renderer::billboardMatrixWithScale(Vector3 scale) {
  232. Matrix4 matrix = getModelviewMatrix();
  233. matrix.m[0][0] = 1.0f*scale.x;
  234. matrix.m[0][1] = 0;
  235. matrix.m[0][2] = 0;
  236. matrix.m[1][0] = 0;
  237. matrix.m[1][1] = 1.0f*scale.y;
  238. matrix.m[1][2] = 0;
  239. matrix.m[2][0] = 0;
  240. matrix.m[2][1] = 0;
  241. matrix.m[2][2] = 1.0f*scale.z;
  242. setModelviewMatrix(matrix);
  243. }
  244. void Renderer::billboardMatrix() {
  245. Matrix4 matrix = getModelviewMatrix();
  246. matrix.m[0][0] = 1;
  247. matrix.m[0][1] = 0;
  248. matrix.m[0][2] = 0;
  249. matrix.m[1][0] = 0;
  250. matrix.m[1][1] = 1;
  251. matrix.m[1][2] = 0;
  252. matrix.m[2][0] = 0;
  253. matrix.m[2][1] = 0;
  254. matrix.m[2][2] = 1;
  255. setModelviewMatrix(matrix);
  256. }
  257. void *Renderer::getDataPointerForName(const String &name) {
  258. if(name == "ambient_color") {
  259. return (void*)&ambientColor;
  260. }
  261. return NULL;
  262. }
  263. void Renderer::setRendererShaderParams(Shader *shader, ShaderBinding *binding) {
  264. for(int i=0; i < shader->expectedParams.size(); i++) {
  265. void *dataPtr = getDataPointerForName(shader->expectedParams[i].name);
  266. if(dataPtr) {
  267. binding->addLocalParam(shader->expectedParams[i].name, dataPtr);
  268. }
  269. }
  270. }
  271. void Renderer::pushDataArrayForMesh(Mesh *mesh, int arrayType) {
  272. if(mesh->arrayDirtyMap[arrayType] == true || mesh->renderDataArrays[arrayType] == NULL) {
  273. if(mesh->renderDataArrays[arrayType] != NULL) {
  274. free(mesh->renderDataArrays[arrayType]->arrayPtr);
  275. delete mesh->renderDataArrays[arrayType];
  276. }
  277. mesh->renderDataArrays[arrayType] = createRenderDataArrayForMesh(mesh, arrayType);
  278. mesh->arrayDirtyMap[arrayType] = false;
  279. }
  280. pushRenderDataArray(mesh->renderDataArrays[arrayType]);
  281. }
  282. int Renderer::getXRes() {
  283. return xRes;
  284. }
  285. int Renderer::getYRes() {
  286. return yRes;
  287. }
  288. void Renderer::setAmbientColor(Number r, Number g, Number b) {
  289. ambientColor.setColor(r,g,b,1.0f);
  290. }
  291. void Renderer::setClearColor(Number r, Number g, Number b, Number a) {
  292. clearColor.setColor(r,g,b,a);
  293. }
  294. void Renderer::setClearColor(Color color) {
  295. setClearColor(color.r, color.g, color.b, color.a);
  296. }
  297. void Renderer::setRenderMode(int newRenderMode) {
  298. renderMode = newRenderMode;
  299. }
  300. void Renderer::setTextureFilteringMode(int mode) {
  301. textureFilteringMode = mode;
  302. }
  303. int Renderer::getRenderMode() {
  304. return renderMode;
  305. }
  306. void Renderer::setFOV(Number fov) {
  307. this->fov = fov;
  308. resetViewport();
  309. }
  310. void Renderer::setViewportSize(int w, int h) {
  311. viewportWidth = w;
  312. viewportHeight = h;
  313. resetViewport();
  314. }
  315. void Renderer::setViewportSizeAndFOV(int w, int h, Number fov) {
  316. this->fov = fov;
  317. viewportWidth = w;
  318. viewportHeight = h;
  319. resetViewport();
  320. }
  321. Number Renderer::getViewportWidth() {
  322. return viewportWidth;
  323. }
  324. Number Renderer::getViewportHeight() {
  325. return viewportHeight;
  326. }