Shader.cpp 8.8 KB

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