Shader.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. #include "Shader.h"
  2. #include "Utils.h"
  3. #include <cmath>
  4. #include "Wrapper.h"
  5. Shader::Shader()
  6. {
  7. unifProjMatrix = Engine::getUniformLocation("projMatrix");
  8. unifViewMatrix = Engine::getUniformLocation("viewMatrix");
  9. unifModelMatrix = Engine::getUniformLocation("modelMatrix");
  10. unifUseTexture = Engine::getUniformLocation("useTexture");
  11. unifUseColor = Engine::getUniformLocation("useColor");
  12. unifUseMixColor = Engine::getUniformLocation("useMixColor");
  13. unifMixColorLoc = Engine::getUniformLocation("mixColor");
  14. unifUseNormals = Engine::getUniformLocation("useNormals");
  15. }
  16. Shader::Shader(const Shader& orig)
  17. {
  18. }
  19. Shader::~Shader()
  20. {
  21. }
  22. void Shader::set3DMode(float lag)
  23. {
  24. float tan = tanf((0.5f * fovY) * M_PI / 180.0f);
  25. float q = 1.0f / tan;
  26. float aspect = (float) Engine::getWidth() / Engine::getHeight();
  27. proj.set(0, 0, q / aspect);
  28. proj.set(1, 1, q);
  29. proj.set(2, 2, (nearClip + farClip) / (nearClip - farClip));
  30. proj.set(3, 2, -1.0f);
  31. proj.set(2, 3, (2.0f * nearClip * farClip) / (nearClip - farClip));
  32. proj.set(3, 3, 0);
  33. Engine::setMatrix(unifProjMatrix, proj.getValues());
  34. Engine::testMat.set(proj);
  35. // -------------------------------------------------------------------------
  36. // calculate vectors for the view matrix
  37. // -------------------------------------------------------------------------
  38. // front
  39. front.setAngles(interpolate(lag, oldLengthAngle, lengthAngle), interpolate(lag, oldWidthAngle, widthAngle));
  40. // back
  41. back.setInverse(front);
  42. // right
  43. right.set(front);
  44. right.cross(0.0f, 1.0f, 0.0f);
  45. right.normalize();
  46. // left
  47. left.setInverse(right);
  48. // up
  49. up.set(front);
  50. up.cross(left);
  51. up.normalize();
  52. // down
  53. down.setInverse(up);
  54. Vector3D interCamera = oldCamera;
  55. interCamera.addMul(camera, lag);
  56. interCamera.addMul(oldCamera, -lag);
  57. Engine::testX = interCamera.getX();
  58. Engine::testY = interCamera.getY();
  59. Engine::testZ = interCamera.getZ();
  60. view.set(0, 0, right.getX());
  61. view.set(0, 1, right.getY());
  62. view.set(0, 2, right.getZ());
  63. view.set(0, 3, right.dotInverse(interCamera));
  64. view.set(1, 0, up.getX());
  65. view.set(1, 1, up.getY());
  66. view.set(1, 2, up.getZ());
  67. view.set(1, 3, up.dotInverse(interCamera));
  68. view.set(2, 0, back.getX());
  69. view.set(2, 1, back.getY());
  70. view.set(2, 2, back.getZ());
  71. view.set(2, 3, back.dotInverse(interCamera));
  72. view.set(3, 0, 0.0f);
  73. view.set(3, 1, 0.0f);
  74. view.set(3, 0, 0.0f);
  75. view.set(3, 3, 1.0f);
  76. Engine::setMatrix(unifViewMatrix, view.getValues());
  77. // -------------------------------------------------------------------------
  78. // update frustum planes
  79. // -------------------------------------------------------------------------
  80. // http://cgvr.informatik.uni-bremen.de/teaching/cg_literatur/lighthouse3d_view_frustum_culling/index.html
  81. float nearHigh = tan * nearClip;
  82. float nearWidth = nearHigh * aspect;
  83. float farHigh = tan * farClip;
  84. float farWidth = farHigh * aspect;
  85. Vector3D fc = interCamera;
  86. fc.addMul(front, farClip);
  87. Vector3D ftl = fc;
  88. ftl.addMul(left, farWidth);
  89. ftl.addMul(up, farHigh);
  90. Vector3D fbl = fc;
  91. fbl.addMul(left, farWidth);
  92. fbl.addMul(down, farHigh);
  93. Vector3D fbr = fc;
  94. fbr.addMul(right, farWidth);
  95. fbr.addMul(down, farHigh);
  96. Vector3D nc = interCamera;
  97. nc.addMul(front, nearClip);
  98. Vector3D ntl = nc;
  99. ntl.addMul(left, nearWidth);
  100. ntl.addMul(down, nearHigh);
  101. Vector3D ntr = nc;
  102. ntr.addMul(right, nearWidth);
  103. ntr.addMul(up, nearHigh);
  104. Vector3D nbr = nc;
  105. nbr.addMul(right, nearWidth);
  106. nbr.addMul(down, nearHigh);
  107. // generating planes with counter clockwise vector order
  108. frustumPlanes[0].set(fbl, ftl, fbr); // far
  109. frustumPlanes[1].set(ntl, ftl, fbl); // left
  110. frustumPlanes[2].set(fbr, ntr, nbr); // right
  111. frustumPlanes[3].set(fbl, fbr, nbr); // bottom
  112. frustumPlanes[4].set(ntr, ftl, ntl); // top
  113. frustumPlanes[5].set(nbr, ntr, ntl); // near
  114. // -------------------------------------------------------------------------
  115. // calculate vectors for movement
  116. // -------------------------------------------------------------------------
  117. // front
  118. front.setY(0.0f);
  119. front.normalize();
  120. // back
  121. back.setInverse(front);
  122. // right
  123. right.set(front);
  124. right.cross(0.0f, 1.0f, 0.0f);
  125. right.normalize();
  126. // left
  127. left.setInverse(right);
  128. // up
  129. up.set(0.0f, 1.0f, 0.0f);
  130. // down
  131. down.setInverse(up);
  132. }
  133. void Shader::set2DMode()
  134. {
  135. proj.setToIdentity();
  136. Engine::setMatrix(unifProjMatrix, proj.getValues());
  137. int scale = Engine::getScale();
  138. view.set(0, 0, (2.0f * scale) / Engine::getWidth());
  139. view.set(0, 1, 0.0f);
  140. view.set(0, 2, 0.0f);
  141. view.set(0, 3, -1.0f);
  142. view.set(1, 0, 0.0f);
  143. view.set(1, 1, (-2.0f * scale) / Engine::getHeight());
  144. view.set(1, 2, 0.0f);
  145. view.set(1, 3, 1.0f);
  146. view.set(2, 0, 0.0f);
  147. view.set(2, 1, 0.0f);
  148. view.set(2, 2, (-1.0f * scale) / Engine::getHeight());
  149. view.set(2, 3, 0.5f);
  150. view.set(3, 0, 0.0f);
  151. view.set(3, 1, 0.0f);
  152. view.set(3, 0, 0.0f);
  153. view.set(3, 3, 1.0f);
  154. Engine::setMatrix(unifViewMatrix, view.getValues());
  155. }
  156. void Shader::storeCamera()
  157. {
  158. oldCamera.set(camera);
  159. oldLengthAngle = lengthAngle;
  160. oldWidthAngle = widthAngle;
  161. }
  162. void Shader::setCamera(float x, float y, float z, float length, float width)
  163. {
  164. camera.set(x, y, z);
  165. lengthAngle = length;
  166. widthAngle = width;
  167. }
  168. float Shader::distanceFromCamera(float x, float y, float z, float lag) const
  169. {
  170. Vector3D cam = oldCamera;
  171. cam.addMul(camera, lag);
  172. cam.addMul(oldCamera, -lag);
  173. return sqrtf((x - cam.getX()) * (x - cam.getX()) + (y - cam.getY()) * (y - cam.getY()) + (z - cam.getZ()) * (z - cam.getZ()));
  174. }
  175. bool Shader::isInFrustum(float x, float y, float z, float x2, float y2, float z2) const
  176. {
  177. //return true;
  178. // http://cgvr.informatik.uni-bremen.de/teaching/cg_literatur/lighthouse3d_view_frustum_culling/index.html
  179. // for each plane do ...
  180. for(int fp = 0; fp < 6; fp++)
  181. {
  182. // reset counters for corners in and out
  183. int out = 0;
  184. int in = 0;
  185. // for each corner of the box do ...
  186. // get out of the cycle as soon as a box as corners
  187. // both inside and out of the frustum
  188. for(int i = 0; i < 8 && (in == 0 || out == 0); i++)
  189. {
  190. // is the corner outside or inside
  191. if(frustumPlanes[fp].getSignedDistance(((i >> 2) & 1) ? x : x2, ((i >> 1) & 1) ? y : y2, (i & 1) ? z : z2) > 0)
  192. {
  193. out++;
  194. }
  195. else
  196. {
  197. in++;
  198. }
  199. }
  200. //if all corners are out
  201. if(in == 0)
  202. {
  203. return false;
  204. }
  205. }
  206. return true;
  207. }
  208. const Vector3D& Shader::getFront() const
  209. {
  210. return front;
  211. }
  212. const Vector3D& Shader::getBack() const
  213. {
  214. return back;
  215. }
  216. const Vector3D& Shader::getRight() const
  217. {
  218. return right;
  219. }
  220. const Vector3D& Shader::getLeft() const
  221. {
  222. return left;
  223. }
  224. const Vector3D& Shader::getUp() const
  225. {
  226. return up;
  227. }
  228. const Vector3D& Shader::getDown() const
  229. {
  230. return down;
  231. }
  232. void Shader::setTextureEnabled(bool use)
  233. {
  234. Engine::setInt(unifUseTexture, use);
  235. }
  236. void Shader::setColorEnabled(bool use)
  237. {
  238. Engine::setInt(unifUseColor, use);
  239. }
  240. void Shader::setMixColorEnabled(bool use)
  241. {
  242. Engine::setInt(unifUseMixColor, use);
  243. }
  244. void Shader::setMixColor(float r, float g, float b, float a)
  245. {
  246. Engine::setFloat(unifUseMixColor, r, g, b, a);
  247. }
  248. void Shader::setTextMode()
  249. {
  250. setTextureEnabled(true);
  251. setColorEnabled(true);
  252. setUseBlending(true);
  253. setMixColorEnabled(false);
  254. setNormalsEnabled(false);
  255. }
  256. void Shader::setNormalsEnabled(bool use)
  257. {
  258. Engine::setInt(unifUseNormals, use);
  259. }
  260. void Shader::setUseBlending(bool use)
  261. {
  262. if(use)
  263. {
  264. glEnable(GL_BLEND);
  265. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  266. glBlendEquation(GL_FUNC_ADD);
  267. }
  268. else
  269. {
  270. glDisable(GL_BLEND);
  271. }
  272. }
  273. void Shader::pop()
  274. {
  275. model.pop();
  276. }
  277. void Shader::push()
  278. {
  279. model.push();
  280. }
  281. void Shader::setToIdentity()
  282. {
  283. model.get().setToIdentity();
  284. }
  285. void Shader::scale(float sx, float sy, float sz)
  286. {
  287. model.get().scale(sx, sy, sz);
  288. }
  289. void Shader::translate(float tx, float ty, float tz)
  290. {
  291. model.get().translate(tx, ty, tz);
  292. }
  293. void Shader::translateX(float tx)
  294. {
  295. model.get().translateX(tx);
  296. }
  297. void Shader::translateY(float ty)
  298. {
  299. model.get().translateY(ty);
  300. }
  301. void Shader::translateZ(float tz)
  302. {
  303. model.get().translateZ(tz);
  304. }
  305. void Shader::translateTo(float tx, float ty, float tz)
  306. {
  307. model.get().translateTo(tx, ty, tz);
  308. }
  309. void Shader::rotate(float xDegrees, float yDegrees, float zDegrees)
  310. {
  311. model.get().rotate(xDegrees, yDegrees, zDegrees);
  312. }
  313. void Shader::rotateX(float degrees)
  314. {
  315. model.get().rotateX(degrees);
  316. }
  317. void Shader::rotateY(float degrees)
  318. {
  319. model.get().rotateY(degrees);
  320. }
  321. void Shader::rotateZ(float degrees)
  322. {
  323. model.get().rotateZ(degrees);
  324. }
  325. void Shader::updateModelMatrix()
  326. {
  327. Engine::setMatrix(unifModelMatrix, model.get().getValues());
  328. }