Shader.cpp 8.9 KB

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