matrix.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. /* libs/opengles/matrix.cpp
  2. **
  3. ** Copyright 2006, The Android Open Source Project
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. */
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include "context.h"
  20. #include "fp.h"
  21. #include "state.h"
  22. #include "matrix.h"
  23. #include "vertex.h"
  24. #include "light.h"
  25. #if defined(__arm__) && defined(__thumb__)
  26. #warning "matrix.cpp should not be compiled in thumb on ARM."
  27. #endif
  28. #define I(_i, _j) ((_j)+ 4*(_i))
  29. namespace android {
  30. // ----------------------------------------------------------------------------
  31. static const GLfloat gIdentityf[16] = { 1,0,0,0,
  32. 0,1,0,0,
  33. 0,0,1,0,
  34. 0,0,0,1 };
  35. static const matrixx_t gIdentityx = {
  36. { 0x10000,0,0,0,
  37. 0,0x10000,0,0,
  38. 0,0,0x10000,0,
  39. 0,0,0,0x10000
  40. }
  41. };
  42. static void point2__nop(transform_t const*, vec4_t* c, vec4_t const* o);
  43. static void point3__nop(transform_t const*, vec4_t* c, vec4_t const* o);
  44. static void point4__nop(transform_t const*, vec4_t* c, vec4_t const* o);
  45. static void normal__nop(transform_t const*, vec4_t* c, vec4_t const* o);
  46. static void point2__generic(transform_t const*, vec4_t* c, vec4_t const* o);
  47. static void point3__generic(transform_t const*, vec4_t* c, vec4_t const* o);
  48. static void point4__generic(transform_t const*, vec4_t* c, vec4_t const* o);
  49. static void point3__mvui(transform_t const*, vec4_t* c, vec4_t const* o);
  50. static void point4__mvui(transform_t const*, vec4_t* c, vec4_t const* o);
  51. // ----------------------------------------------------------------------------
  52. #if 0
  53. #pragma mark -
  54. #endif
  55. void ogles_init_matrix(ogles_context_t* c)
  56. {
  57. c->transforms.modelview.init(OGLES_MODELVIEW_STACK_DEPTH);
  58. c->transforms.projection.init(OGLES_PROJECTION_STACK_DEPTH);
  59. for (int i=0; i<GGL_TEXTURE_UNIT_COUNT ; i++)
  60. c->transforms.texture[i].init(OGLES_TEXTURE_STACK_DEPTH);
  61. c->transforms.current = &c->transforms.modelview;
  62. c->transforms.matrixMode = GL_MODELVIEW;
  63. c->transforms.dirty = transform_state_t::VIEWPORT |
  64. transform_state_t::MVUI |
  65. transform_state_t::MVIT |
  66. transform_state_t::MVP;
  67. c->transforms.mvp.loadIdentity();
  68. c->transforms.mvp4.loadIdentity();
  69. c->transforms.mvit4.loadIdentity();
  70. c->transforms.mvui.loadIdentity();
  71. c->transforms.vpt.loadIdentity();
  72. c->transforms.vpt.zNear = 0.0f;
  73. c->transforms.vpt.zFar = 1.0f;
  74. }
  75. void ogles_uninit_matrix(ogles_context_t* c)
  76. {
  77. c->transforms.modelview.uninit();
  78. c->transforms.projection.uninit();
  79. for (int i=0; i<GGL_TEXTURE_UNIT_COUNT ; i++)
  80. c->transforms.texture[i].uninit();
  81. }
  82. static void validate_perspective(ogles_context_t* c, vertex_t* v)
  83. {
  84. const uint32_t enables = c->rasterizer.state.enables;
  85. c->arrays.perspective = (c->clipPlanes.enable) ?
  86. ogles_vertex_clipAllPerspective3D : ogles_vertex_perspective3D;
  87. if (enables & (GGL_ENABLE_DEPTH_TEST|GGL_ENABLE_FOG)) {
  88. c->arrays.perspective = ogles_vertex_perspective3DZ;
  89. if (c->clipPlanes.enable || (enables&GGL_ENABLE_FOG))
  90. c->arrays.perspective = ogles_vertex_clipAllPerspective3DZ;
  91. }
  92. if ((c->arrays.vertex.size != 4) &&
  93. (c->transforms.mvp4.flags & transform_t::FLAGS_2D_PROJECTION)) {
  94. c->arrays.perspective = ogles_vertex_perspective2D;
  95. }
  96. c->arrays.perspective(c, v);
  97. }
  98. void ogles_invalidate_perspective(ogles_context_t* c)
  99. {
  100. c->arrays.perspective = validate_perspective;
  101. }
  102. void ogles_validate_transform_impl(ogles_context_t* c, uint32_t want)
  103. {
  104. int dirty = c->transforms.dirty & want;
  105. // Validate the modelview
  106. if (dirty & transform_state_t::MODELVIEW) {
  107. c->transforms.modelview.validate();
  108. }
  109. // Validate the projection stack (in fact, it's never needed)
  110. if (dirty & transform_state_t::PROJECTION) {
  111. c->transforms.projection.validate();
  112. }
  113. // Validate the viewport transformation
  114. if (dirty & transform_state_t::VIEWPORT) {
  115. vp_transform_t& vpt = c->transforms.vpt;
  116. vpt.transform.matrix.load(vpt.matrix);
  117. vpt.transform.picker();
  118. }
  119. // We need to update the mvp (used to transform each vertex)
  120. if (dirty & transform_state_t::MVP) {
  121. c->transforms.update_mvp();
  122. // invalidate perspective (divide by W) and view volume clipping
  123. ogles_invalidate_perspective(c);
  124. }
  125. // Validate the mvui (for normal transformation)
  126. if (dirty & transform_state_t::MVUI) {
  127. c->transforms.update_mvui();
  128. ogles_invalidate_lighting_mvui(c);
  129. }
  130. // Validate the texture stack
  131. if (dirty & transform_state_t::TEXTURE) {
  132. for (int i=0; i<GGL_TEXTURE_UNIT_COUNT ; i++)
  133. c->transforms.texture[i].validate();
  134. }
  135. // Validate the mvit4 (user-clip planes)
  136. if (dirty & transform_state_t::MVIT) {
  137. c->transforms.update_mvit();
  138. }
  139. c->transforms.dirty &= ~want;
  140. }
  141. // ----------------------------------------------------------------------------
  142. #if 0
  143. #pragma mark -
  144. #pragma mark transform_t
  145. #endif
  146. void transform_t::loadIdentity() {
  147. matrix = gIdentityx;
  148. flags = 0;
  149. ops = OP_IDENTITY;
  150. point2 = point2__nop;
  151. point3 = point3__nop;
  152. point4 = point4__nop;
  153. }
  154. static inline
  155. int notZero(GLfixed v) {
  156. return abs(v) & ~0x3;
  157. }
  158. static inline
  159. int notOne(GLfixed v) {
  160. return notZero(v - 0x10000);
  161. }
  162. void transform_t::picker()
  163. {
  164. const GLfixed* const m = matrix.m;
  165. // XXX: picker needs to be smarter
  166. flags = 0;
  167. ops = OP_ALL;
  168. point2 = point2__generic;
  169. point3 = point3__generic;
  170. point4 = point4__generic;
  171. // find out if this is a 2D projection
  172. if (!(notZero(m[3]) | notZero(m[7]) | notZero(m[11]) | notOne(m[15]))) {
  173. flags |= FLAGS_2D_PROJECTION;
  174. }
  175. }
  176. void mvui_transform_t::picker()
  177. {
  178. flags = 0;
  179. ops = OP_ALL;
  180. point3 = point3__mvui;
  181. point4 = point4__mvui;
  182. }
  183. void transform_t::dump(const char* what)
  184. {
  185. GLfixed const * const m = matrix.m;
  186. ALOGD("%s:", what);
  187. for (int i=0 ; i<4 ; i++)
  188. ALOGD("[%08x %08x %08x %08x] [%f %f %f %f]\n",
  189. m[I(0,i)], m[I(1,i)], m[I(2,i)], m[I(3,i)],
  190. fixedToFloat(m[I(0,i)]),
  191. fixedToFloat(m[I(1,i)]),
  192. fixedToFloat(m[I(2,i)]),
  193. fixedToFloat(m[I(3,i)]));
  194. }
  195. // ----------------------------------------------------------------------------
  196. #if 0
  197. #pragma mark -
  198. #pragma mark matrixx_t
  199. #endif
  200. void matrixx_t::load(const matrixf_t& rhs) {
  201. GLfixed* xp = m;
  202. GLfloat const* fp = rhs.elements();
  203. unsigned int i = 16;
  204. do {
  205. const GLfloat f = *fp++;
  206. *xp++ = isZerof(f) ? 0 : gglFloatToFixed(f);
  207. } while (--i);
  208. }
  209. // ----------------------------------------------------------------------------
  210. #if 0
  211. #pragma mark -
  212. #pragma mark matrixf_t
  213. #endif
  214. void matrixf_t::multiply(matrixf_t& r, const matrixf_t& lhs, const matrixf_t& rhs)
  215. {
  216. GLfloat const* const m = lhs.m;
  217. for (int i=0 ; i<4 ; i++) {
  218. register const float rhs_i0 = rhs.m[ I(i,0) ];
  219. register float ri0 = m[ I(0,0) ] * rhs_i0;
  220. register float ri1 = m[ I(0,1) ] * rhs_i0;
  221. register float ri2 = m[ I(0,2) ] * rhs_i0;
  222. register float ri3 = m[ I(0,3) ] * rhs_i0;
  223. for (int j=1 ; j<4 ; j++) {
  224. register const float rhs_ij = rhs.m[ I(i,j) ];
  225. ri0 += m[ I(j,0) ] * rhs_ij;
  226. ri1 += m[ I(j,1) ] * rhs_ij;
  227. ri2 += m[ I(j,2) ] * rhs_ij;
  228. ri3 += m[ I(j,3) ] * rhs_ij;
  229. }
  230. r.m[ I(i,0) ] = ri0;
  231. r.m[ I(i,1) ] = ri1;
  232. r.m[ I(i,2) ] = ri2;
  233. r.m[ I(i,3) ] = ri3;
  234. }
  235. }
  236. void matrixf_t::dump(const char* what) {
  237. ALOGD("%s", what);
  238. ALOGD("[ %9f %9f %9f %9f ]", m[I(0,0)], m[I(1,0)], m[I(2,0)], m[I(3,0)]);
  239. ALOGD("[ %9f %9f %9f %9f ]", m[I(0,1)], m[I(1,1)], m[I(2,1)], m[I(3,1)]);
  240. ALOGD("[ %9f %9f %9f %9f ]", m[I(0,2)], m[I(1,2)], m[I(2,2)], m[I(3,2)]);
  241. ALOGD("[ %9f %9f %9f %9f ]", m[I(0,3)], m[I(1,3)], m[I(2,3)], m[I(3,3)]);
  242. }
  243. void matrixf_t::loadIdentity() {
  244. memcpy(m, gIdentityf, sizeof(m));
  245. }
  246. void matrixf_t::set(const GLfixed* rhs) {
  247. load(rhs);
  248. }
  249. void matrixf_t::set(const GLfloat* rhs) {
  250. load(rhs);
  251. }
  252. void matrixf_t::load(const GLfixed* rhs) {
  253. GLfloat* fp = m;
  254. unsigned int i = 16;
  255. do {
  256. *fp++ = fixedToFloat(*rhs++);
  257. } while (--i);
  258. }
  259. void matrixf_t::load(const GLfloat* rhs) {
  260. memcpy(m, rhs, sizeof(m));
  261. }
  262. void matrixf_t::load(const matrixf_t& rhs) {
  263. operator = (rhs);
  264. }
  265. void matrixf_t::multiply(const matrixf_t& rhs) {
  266. matrixf_t r;
  267. multiply(r, *this, rhs);
  268. operator = (r);
  269. }
  270. void matrixf_t::translate(GLfloat x, GLfloat y, GLfloat z) {
  271. for (int i=0 ; i<4 ; i++) {
  272. m[12+i] += m[i]*x + m[4+i]*y + m[8+i]*z;
  273. }
  274. }
  275. void matrixf_t::scale(GLfloat x, GLfloat y, GLfloat z) {
  276. for (int i=0 ; i<4 ; i++) {
  277. m[ i] *= x;
  278. m[4+i] *= y;
  279. m[8+i] *= z;
  280. }
  281. }
  282. void matrixf_t::rotate(GLfloat a, GLfloat x, GLfloat y, GLfloat z)
  283. {
  284. matrixf_t rotation;
  285. GLfloat* r = rotation.m;
  286. GLfloat c, s;
  287. r[3] = 0; r[7] = 0; r[11]= 0;
  288. r[12]= 0; r[13]= 0; r[14]= 0; r[15]= 1;
  289. a *= GLfloat(M_PI / 180.0f);
  290. sincosf(a, &s, &c);
  291. if (isOnef(x) && isZerof(y) && isZerof(z)) {
  292. r[5] = c; r[10]= c;
  293. r[6] = s; r[9] = -s;
  294. r[1] = 0; r[2] = 0;
  295. r[4] = 0; r[8] = 0;
  296. r[0] = 1;
  297. } else if (isZerof(x) && isOnef(y) && isZerof(z)) {
  298. r[0] = c; r[10]= c;
  299. r[8] = s; r[2] = -s;
  300. r[1] = 0; r[4] = 0;
  301. r[6] = 0; r[9] = 0;
  302. r[5] = 1;
  303. } else if (isZerof(x) && isZerof(y) && isOnef(z)) {
  304. r[0] = c; r[5] = c;
  305. r[1] = s; r[4] = -s;
  306. r[2] = 0; r[6] = 0;
  307. r[8] = 0; r[9] = 0;
  308. r[10]= 1;
  309. } else {
  310. const GLfloat len = sqrtf(x*x + y*y + z*z);
  311. if (!isOnef(len)) {
  312. const GLfloat recipLen = reciprocalf(len);
  313. x *= recipLen;
  314. y *= recipLen;
  315. z *= recipLen;
  316. }
  317. const GLfloat nc = 1.0f - c;
  318. const GLfloat xy = x * y;
  319. const GLfloat yz = y * z;
  320. const GLfloat zx = z * x;
  321. const GLfloat xs = x * s;
  322. const GLfloat ys = y * s;
  323. const GLfloat zs = z * s;
  324. r[ 0] = x*x*nc + c; r[ 4] = xy*nc - zs; r[ 8] = zx*nc + ys;
  325. r[ 1] = xy*nc + zs; r[ 5] = y*y*nc + c; r[ 9] = yz*nc - xs;
  326. r[ 2] = zx*nc - ys; r[ 6] = yz*nc + xs; r[10] = z*z*nc + c;
  327. }
  328. multiply(rotation);
  329. }
  330. // ----------------------------------------------------------------------------
  331. #if 0
  332. #pragma mark -
  333. #pragma mark matrix_stack_t
  334. #endif
  335. void matrix_stack_t::init(int depth) {
  336. stack = new matrixf_t[depth];
  337. ops = new uint8_t[depth];
  338. maxDepth = depth;
  339. depth = 0;
  340. dirty = 0;
  341. loadIdentity();
  342. }
  343. void matrix_stack_t::uninit() {
  344. delete [] stack;
  345. delete [] ops;
  346. }
  347. void matrix_stack_t::loadIdentity() {
  348. transform.loadIdentity();
  349. stack[depth].loadIdentity();
  350. ops[depth] = OP_IDENTITY;
  351. }
  352. void matrix_stack_t::load(const GLfixed* rhs)
  353. {
  354. memcpy(transform.matrix.m, rhs, sizeof(transform.matrix.m));
  355. stack[depth].load(rhs);
  356. ops[depth] = OP_ALL; // TODO: we should look at the matrix
  357. }
  358. void matrix_stack_t::load(const GLfloat* rhs)
  359. {
  360. stack[depth].load(rhs);
  361. ops[depth] = OP_ALL; // TODO: we should look at the matrix
  362. }
  363. void matrix_stack_t::multiply(const matrixf_t& rhs)
  364. {
  365. stack[depth].multiply(rhs);
  366. ops[depth] = OP_ALL; // TODO: we should look at the matrix
  367. }
  368. void matrix_stack_t::translate(GLfloat x, GLfloat y, GLfloat z)
  369. {
  370. stack[depth].translate(x,y,z);
  371. ops[depth] |= OP_TRANSLATE;
  372. }
  373. void matrix_stack_t::scale(GLfloat x, GLfloat y, GLfloat z)
  374. {
  375. stack[depth].scale(x,y,z);
  376. if (x==y && y==z) {
  377. ops[depth] |= OP_UNIFORM_SCALE;
  378. } else {
  379. ops[depth] |= OP_SCALE;
  380. }
  381. }
  382. void matrix_stack_t::rotate(GLfloat a, GLfloat x, GLfloat y, GLfloat z)
  383. {
  384. stack[depth].rotate(a,x,y,z);
  385. ops[depth] |= OP_ROTATE;
  386. }
  387. void matrix_stack_t::validate()
  388. {
  389. if (dirty & DO_FLOAT_TO_FIXED) {
  390. transform.matrix.load(top());
  391. }
  392. if (dirty & DO_PICKER) {
  393. transform.picker();
  394. }
  395. dirty = 0;
  396. }
  397. GLint matrix_stack_t::push()
  398. {
  399. if (depth >= (maxDepth-1)) {
  400. return GL_STACK_OVERFLOW;
  401. }
  402. stack[depth+1] = stack[depth];
  403. ops[depth+1] = ops[depth];
  404. depth++;
  405. return 0;
  406. }
  407. GLint matrix_stack_t::pop()
  408. {
  409. if (depth == 0) {
  410. return GL_STACK_UNDERFLOW;
  411. }
  412. depth--;
  413. return 0;
  414. }
  415. // ----------------------------------------------------------------------------
  416. #if 0
  417. #pragma mark -
  418. #pragma mark vp_transform_t
  419. #endif
  420. void vp_transform_t::loadIdentity() {
  421. transform.loadIdentity();
  422. matrix.loadIdentity();
  423. }
  424. // ----------------------------------------------------------------------------
  425. #if 0
  426. #pragma mark -
  427. #pragma mark transform_state_t
  428. #endif
  429. void transform_state_t::invalidate()
  430. {
  431. switch (matrixMode) {
  432. case GL_MODELVIEW: dirty |= MODELVIEW | MVP | MVUI | MVIT; break;
  433. case GL_PROJECTION: dirty |= PROJECTION | MVP; break;
  434. case GL_TEXTURE: dirty |= TEXTURE | MVP; break;
  435. }
  436. current->dirty = matrix_stack_t::DO_PICKER |
  437. matrix_stack_t::DO_FLOAT_TO_FIXED;
  438. }
  439. void transform_state_t::update_mvp()
  440. {
  441. matrixf_t temp_mvp;
  442. matrixf_t::multiply(temp_mvp, projection.top(), modelview.top());
  443. mvp4.matrix.load(temp_mvp);
  444. mvp4.picker();
  445. if (mvp4.flags & transform_t::FLAGS_2D_PROJECTION) {
  446. // the mvp matrix doesn't transform W, in this case we can
  447. // premultiply it with the viewport transformation. In addition to
  448. // being more efficient, this is also much more accurate and in fact
  449. // is needed for 2D drawing with a resulting 1:1 mapping.
  450. matrixf_t mvpv;
  451. matrixf_t::multiply(mvpv, vpt.matrix, temp_mvp);
  452. mvp.matrix.load(mvpv);
  453. mvp.picker();
  454. } else {
  455. mvp = mvp4;
  456. }
  457. }
  458. static inline
  459. GLfloat det22(GLfloat a, GLfloat b, GLfloat c, GLfloat d) {
  460. return a*d - b*c;
  461. }
  462. static inline
  463. GLfloat ndet22(GLfloat a, GLfloat b, GLfloat c, GLfloat d) {
  464. return b*c - a*d;
  465. }
  466. static __attribute__((noinline))
  467. void invert(GLfloat* inverse, const GLfloat* src)
  468. {
  469. double t;
  470. int i, j, k, swap;
  471. GLfloat tmp[4][4];
  472. memcpy(inverse, gIdentityf, sizeof(gIdentityf));
  473. memcpy(tmp, src, sizeof(GLfloat)*16);
  474. for (i = 0; i < 4; i++) {
  475. // look for largest element in column
  476. swap = i;
  477. for (j = i + 1; j < 4; j++) {
  478. if (fabs(tmp[j][i]) > fabs(tmp[i][i])) {
  479. swap = j;
  480. }
  481. }
  482. if (swap != i) {
  483. /* swap rows. */
  484. for (k = 0; k < 4; k++) {
  485. t = tmp[i][k];
  486. tmp[i][k] = tmp[swap][k];
  487. tmp[swap][k] = t;
  488. t = inverse[i*4+k];
  489. inverse[i*4+k] = inverse[swap*4+k];
  490. inverse[swap*4+k] = t;
  491. }
  492. }
  493. t = 1.0f / tmp[i][i];
  494. for (k = 0; k < 4; k++) {
  495. tmp[i][k] *= t;
  496. inverse[i*4+k] *= t;
  497. }
  498. for (j = 0; j < 4; j++) {
  499. if (j != i) {
  500. t = tmp[j][i];
  501. for (k = 0; k < 4; k++) {
  502. tmp[j][k] -= tmp[i][k]*t;
  503. inverse[j*4+k] -= inverse[i*4+k]*t;
  504. }
  505. }
  506. }
  507. }
  508. }
  509. void transform_state_t::update_mvit()
  510. {
  511. GLfloat r[16];
  512. const GLfloat* const mv = modelview.top().elements();
  513. invert(r, mv);
  514. // convert to fixed-point and transpose
  515. GLfixed* const x = mvit4.matrix.m;
  516. for (int i=0 ; i<4 ; i++)
  517. for (int j=0 ; j<4 ; j++)
  518. x[I(i,j)] = gglFloatToFixed(r[I(j,i)]);
  519. mvit4.picker();
  520. }
  521. void transform_state_t::update_mvui()
  522. {
  523. GLfloat r[16];
  524. const GLfloat* const mv = modelview.top().elements();
  525. /*
  526. When evaluating the lighting equation in eye-space, normals
  527. are transformed by the upper 3x3 modelview inverse-transpose.
  528. http://www.opengl.org/documentation/specs/version1.1/glspec1.1/node26.html
  529. (note that inverse-transpose is distributive).
  530. Also note that:
  531. l(obj) = inv(modelview).l(eye) for local light
  532. l(obj) = tr(modelview).l(eye) for infinite light
  533. */
  534. invert(r, mv);
  535. GLfixed* const x = mvui.matrix.m;
  536. #if OBJECT_SPACE_LIGHTING
  537. for (int i=0 ; i<4 ; i++)
  538. for (int j=0 ; j<4 ; j++)
  539. x[I(i,j)] = gglFloatToFixed(r[I(i,j)]);
  540. #else
  541. for (int i=0 ; i<4 ; i++)
  542. for (int j=0 ; j<4 ; j++)
  543. x[I(i,j)] = gglFloatToFixed(r[I(j,i)]);
  544. #endif
  545. mvui.picker();
  546. }
  547. // ----------------------------------------------------------------------------
  548. // transformation and matrices API
  549. // ----------------------------------------------------------------------------
  550. #if 0
  551. #pragma mark -
  552. #pragma mark transformation and matrices API
  553. #endif
  554. int ogles_surfaceport(ogles_context_t* c, GLint x, GLint y)
  555. {
  556. c->viewport.surfaceport.x = x;
  557. c->viewport.surfaceport.y = y;
  558. ogles_viewport(c,
  559. c->viewport.x,
  560. c->viewport.y,
  561. c->viewport.w,
  562. c->viewport.h);
  563. ogles_scissor(c,
  564. c->viewport.scissor.x,
  565. c->viewport.scissor.y,
  566. c->viewport.scissor.w,
  567. c->viewport.scissor.h);
  568. return 0;
  569. }
  570. void ogles_scissor(ogles_context_t* c,
  571. GLint x, GLint y, GLsizei w, GLsizei h)
  572. {
  573. if ((w|h) < 0) {
  574. ogles_error(c, GL_INVALID_VALUE);
  575. return;
  576. }
  577. c->viewport.scissor.x = x;
  578. c->viewport.scissor.y = y;
  579. c->viewport.scissor.w = w;
  580. c->viewport.scissor.h = h;
  581. x += c->viewport.surfaceport.x;
  582. y += c->viewport.surfaceport.y;
  583. y = c->rasterizer.state.buffers.color.height - (y + h);
  584. c->rasterizer.procs.scissor(c, x, y, w, h);
  585. }
  586. void ogles_viewport(ogles_context_t* c,
  587. GLint x, GLint y, GLsizei w, GLsizei h)
  588. {
  589. if ((w|h)<0) {
  590. ogles_error(c, GL_INVALID_VALUE);
  591. return;
  592. }
  593. c->viewport.x = x;
  594. c->viewport.y = y;
  595. c->viewport.w = w;
  596. c->viewport.h = h;
  597. x += c->viewport.surfaceport.x;
  598. y += c->viewport.surfaceport.y;
  599. GLint H = c->rasterizer.state.buffers.color.height;
  600. GLfloat sx = div2f(w);
  601. GLfloat ox = sx + x;
  602. GLfloat sy = div2f(h);
  603. GLfloat oy = sy - y + (H - h);
  604. GLfloat near = c->transforms.vpt.zNear;
  605. GLfloat far = c->transforms.vpt.zFar;
  606. GLfloat A = div2f(far - near);
  607. GLfloat B = div2f(far + near);
  608. // compute viewport matrix
  609. GLfloat* const f = c->transforms.vpt.matrix.editElements();
  610. f[0] = sx; f[4] = 0; f[ 8] = 0; f[12] = ox;
  611. f[1] = 0; f[5] =-sy; f[ 9] = 0; f[13] = oy;
  612. f[2] = 0; f[6] = 0; f[10] = A; f[14] = B;
  613. f[3] = 0; f[7] = 0; f[11] = 0; f[15] = 1;
  614. c->transforms.dirty |= transform_state_t::VIEWPORT;
  615. if (c->transforms.mvp4.flags & transform_t::FLAGS_2D_PROJECTION)
  616. c->transforms.dirty |= transform_state_t::MVP;
  617. }
  618. // ----------------------------------------------------------------------------
  619. #if 0
  620. #pragma mark -
  621. #pragma mark matrix * vertex
  622. #endif
  623. void point2__generic(transform_t const* mx, vec4_t* lhs, vec4_t const* rhs) {
  624. const GLfixed* const m = mx->matrix.m;
  625. const GLfixed rx = rhs->x;
  626. const GLfixed ry = rhs->y;
  627. lhs->x = mla2a(rx, m[ 0], ry, m[ 4], m[12]);
  628. lhs->y = mla2a(rx, m[ 1], ry, m[ 5], m[13]);
  629. lhs->z = mla2a(rx, m[ 2], ry, m[ 6], m[14]);
  630. lhs->w = mla2a(rx, m[ 3], ry, m[ 7], m[15]);
  631. }
  632. void point3__generic(transform_t const* mx, vec4_t* lhs, vec4_t const* rhs) {
  633. const GLfixed* const m = mx->matrix.m;
  634. const GLfixed rx = rhs->x;
  635. const GLfixed ry = rhs->y;
  636. const GLfixed rz = rhs->z;
  637. lhs->x = mla3a(rx, m[ 0], ry, m[ 4], rz, m[ 8], m[12]);
  638. lhs->y = mla3a(rx, m[ 1], ry, m[ 5], rz, m[ 9], m[13]);
  639. lhs->z = mla3a(rx, m[ 2], ry, m[ 6], rz, m[10], m[14]);
  640. lhs->w = mla3a(rx, m[ 3], ry, m[ 7], rz, m[11], m[15]);
  641. }
  642. void point4__generic(transform_t const* mx, vec4_t* lhs, vec4_t const* rhs) {
  643. const GLfixed* const m = mx->matrix.m;
  644. const GLfixed rx = rhs->x;
  645. const GLfixed ry = rhs->y;
  646. const GLfixed rz = rhs->z;
  647. const GLfixed rw = rhs->w;
  648. lhs->x = mla4(rx, m[ 0], ry, m[ 4], rz, m[ 8], rw, m[12]);
  649. lhs->y = mla4(rx, m[ 1], ry, m[ 5], rz, m[ 9], rw, m[13]);
  650. lhs->z = mla4(rx, m[ 2], ry, m[ 6], rz, m[10], rw, m[14]);
  651. lhs->w = mla4(rx, m[ 3], ry, m[ 7], rz, m[11], rw, m[15]);
  652. }
  653. void point3__mvui(transform_t const* mx, vec4_t* lhs, vec4_t const* rhs) {
  654. // this is used for transforming light positions back to object space.
  655. // w is used as a switch for directional lights, so we need
  656. // to preserve it.
  657. const GLfixed* const m = mx->matrix.m;
  658. const GLfixed rx = rhs->x;
  659. const GLfixed ry = rhs->y;
  660. const GLfixed rz = rhs->z;
  661. lhs->x = mla3(rx, m[ 0], ry, m[ 4], rz, m[ 8]);
  662. lhs->y = mla3(rx, m[ 1], ry, m[ 5], rz, m[ 9]);
  663. lhs->z = mla3(rx, m[ 2], ry, m[ 6], rz, m[10]);
  664. lhs->w = 0;
  665. }
  666. void point4__mvui(transform_t const* mx, vec4_t* lhs, vec4_t const* rhs) {
  667. // this is used for transforming light positions back to object space.
  668. // w is used as a switch for directional lights, so we need
  669. // to preserve it.
  670. const GLfixed* const m = mx->matrix.m;
  671. const GLfixed rx = rhs->x;
  672. const GLfixed ry = rhs->y;
  673. const GLfixed rz = rhs->z;
  674. const GLfixed rw = rhs->w;
  675. lhs->x = mla4(rx, m[ 0], ry, m[ 4], rz, m[ 8], rw, m[12]);
  676. lhs->y = mla4(rx, m[ 1], ry, m[ 5], rz, m[ 9], rw, m[13]);
  677. lhs->z = mla4(rx, m[ 2], ry, m[ 6], rz, m[10], rw, m[14]);
  678. lhs->w = rw;
  679. }
  680. void point2__nop(transform_t const*, vec4_t* lhs, vec4_t const* rhs) {
  681. lhs->z = 0;
  682. lhs->w = 0x10000;
  683. if (lhs != rhs) {
  684. lhs->x = rhs->x;
  685. lhs->y = rhs->y;
  686. }
  687. }
  688. void point3__nop(transform_t const*, vec4_t* lhs, vec4_t const* rhs) {
  689. lhs->w = 0x10000;
  690. if (lhs != rhs) {
  691. lhs->x = rhs->x;
  692. lhs->y = rhs->y;
  693. lhs->z = rhs->z;
  694. }
  695. }
  696. void point4__nop(transform_t const*, vec4_t* lhs, vec4_t const* rhs) {
  697. if (lhs != rhs)
  698. *lhs = *rhs;
  699. }
  700. static void frustumf(
  701. GLfloat left, GLfloat right,
  702. GLfloat bottom, GLfloat top,
  703. GLfloat zNear, GLfloat zFar,
  704. ogles_context_t* c)
  705. {
  706. if (cmpf(left,right) ||
  707. cmpf(top, bottom) ||
  708. cmpf(zNear, zFar) ||
  709. isZeroOrNegativef(zNear) ||
  710. isZeroOrNegativef(zFar))
  711. {
  712. ogles_error(c, GL_INVALID_VALUE);
  713. return;
  714. }
  715. const GLfloat r_width = reciprocalf(right - left);
  716. const GLfloat r_height = reciprocalf(top - bottom);
  717. const GLfloat r_depth = reciprocalf(zNear - zFar);
  718. const GLfloat x = mul2f(zNear * r_width);
  719. const GLfloat y = mul2f(zNear * r_height);
  720. const GLfloat A = mul2f((right + left) * r_width);
  721. const GLfloat B = (top + bottom) * r_height;
  722. const GLfloat C = (zFar + zNear) * r_depth;
  723. const GLfloat D = mul2f(zFar * zNear * r_depth);
  724. GLfloat f[16];
  725. f[ 0] = x;
  726. f[ 5] = y;
  727. f[ 8] = A;
  728. f[ 9] = B;
  729. f[10] = C;
  730. f[14] = D;
  731. f[11] = -1.0f;
  732. f[ 1] = f[ 2] = f[ 3] =
  733. f[ 4] = f[ 6] = f[ 7] =
  734. f[12] = f[13] = f[15] = 0.0f;
  735. matrixf_t rhs;
  736. rhs.set(f);
  737. c->transforms.current->multiply(rhs);
  738. c->transforms.invalidate();
  739. }
  740. static void orthof(
  741. GLfloat left, GLfloat right,
  742. GLfloat bottom, GLfloat top,
  743. GLfloat zNear, GLfloat zFar,
  744. ogles_context_t* c)
  745. {
  746. if (cmpf(left,right) ||
  747. cmpf(top, bottom) ||
  748. cmpf(zNear, zFar))
  749. {
  750. ogles_error(c, GL_INVALID_VALUE);
  751. return;
  752. }
  753. const GLfloat r_width = reciprocalf(right - left);
  754. const GLfloat r_height = reciprocalf(top - bottom);
  755. const GLfloat r_depth = reciprocalf(zFar - zNear);
  756. const GLfloat x = mul2f(r_width);
  757. const GLfloat y = mul2f(r_height);
  758. const GLfloat z = -mul2f(r_depth);
  759. const GLfloat tx = -(right + left) * r_width;
  760. const GLfloat ty = -(top + bottom) * r_height;
  761. const GLfloat tz = -(zFar + zNear) * r_depth;
  762. GLfloat f[16];
  763. f[ 0] = x;
  764. f[ 5] = y;
  765. f[10] = z;
  766. f[12] = tx;
  767. f[13] = ty;
  768. f[14] = tz;
  769. f[15] = 1.0f;
  770. f[ 1] = f[ 2] = f[ 3] =
  771. f[ 4] = f[ 6] = f[ 7] =
  772. f[ 8] = f[ 9] = f[11] = 0.0f;
  773. matrixf_t rhs;
  774. rhs.set(f);
  775. c->transforms.current->multiply(rhs);
  776. c->transforms.invalidate();
  777. }
  778. static void depthRangef(GLclampf zNear, GLclampf zFar, ogles_context_t* c)
  779. {
  780. zNear = clampToZerof(zNear > 1 ? 1 : zNear);
  781. zFar = clampToZerof(zFar > 1 ? 1 : zFar);
  782. GLfloat* const f = c->transforms.vpt.matrix.editElements();
  783. f[10] = div2f(zFar - zNear);
  784. f[14] = div2f(zFar + zNear);
  785. c->transforms.dirty |= transform_state_t::VIEWPORT;
  786. c->transforms.vpt.zNear = zNear;
  787. c->transforms.vpt.zFar = zFar;
  788. }
  789. // ----------------------------------------------------------------------------
  790. }; // namespace android
  791. using namespace android;
  792. void glMatrixMode(GLenum mode)
  793. {
  794. ogles_context_t* c = ogles_context_t::get();
  795. matrix_stack_t* stack = 0;
  796. switch (mode) {
  797. case GL_MODELVIEW:
  798. stack = &c->transforms.modelview;
  799. break;
  800. case GL_PROJECTION:
  801. stack = &c->transforms.projection;
  802. break;
  803. case GL_TEXTURE:
  804. stack = &c->transforms.texture[c->textures.active];
  805. break;
  806. default:
  807. ogles_error(c, GL_INVALID_ENUM);
  808. return;
  809. }
  810. c->transforms.matrixMode = mode;
  811. c->transforms.current = stack;
  812. }
  813. void glLoadIdentity()
  814. {
  815. ogles_context_t* c = ogles_context_t::get();
  816. c->transforms.current->loadIdentity(); // also loads the GLfixed transform
  817. c->transforms.invalidate();
  818. c->transforms.current->dirty = 0;
  819. }
  820. void glLoadMatrixf(const GLfloat* m)
  821. {
  822. ogles_context_t* c = ogles_context_t::get();
  823. c->transforms.current->load(m);
  824. c->transforms.invalidate();
  825. }
  826. void glLoadMatrixx(const GLfixed* m)
  827. {
  828. ogles_context_t* c = ogles_context_t::get();
  829. c->transforms.current->load(m); // also loads the GLfixed transform
  830. c->transforms.invalidate();
  831. c->transforms.current->dirty &= ~matrix_stack_t::DO_FLOAT_TO_FIXED;
  832. }
  833. void glMultMatrixf(const GLfloat* m)
  834. {
  835. ogles_context_t* c = ogles_context_t::get();
  836. matrixf_t rhs;
  837. rhs.set(m);
  838. c->transforms.current->multiply(rhs);
  839. c->transforms.invalidate();
  840. }
  841. void glMultMatrixx(const GLfixed* m)
  842. {
  843. ogles_context_t* c = ogles_context_t::get();
  844. matrixf_t rhs;
  845. rhs.set(m);
  846. c->transforms.current->multiply(rhs);
  847. c->transforms.invalidate();
  848. }
  849. void glPopMatrix()
  850. {
  851. ogles_context_t* c = ogles_context_t::get();
  852. GLint err = c->transforms.current->pop();
  853. if (ggl_unlikely(err)) {
  854. ogles_error(c, err);
  855. return;
  856. }
  857. c->transforms.invalidate();
  858. }
  859. void glPushMatrix()
  860. {
  861. ogles_context_t* c = ogles_context_t::get();
  862. GLint err = c->transforms.current->push();
  863. if (ggl_unlikely(err)) {
  864. ogles_error(c, err);
  865. return;
  866. }
  867. c->transforms.invalidate();
  868. }
  869. void glFrustumf(
  870. GLfloat left, GLfloat right,
  871. GLfloat bottom, GLfloat top,
  872. GLfloat zNear, GLfloat zFar)
  873. {
  874. ogles_context_t* c = ogles_context_t::get();
  875. frustumf(left, right, bottom, top, zNear, zFar, c);
  876. }
  877. void glFrustumx(
  878. GLfixed left, GLfixed right,
  879. GLfixed bottom, GLfixed top,
  880. GLfixed zNear, GLfixed zFar)
  881. {
  882. ogles_context_t* c = ogles_context_t::get();
  883. frustumf( fixedToFloat(left), fixedToFloat(right),
  884. fixedToFloat(bottom), fixedToFloat(top),
  885. fixedToFloat(zNear), fixedToFloat(zFar),
  886. c);
  887. }
  888. void glOrthof(
  889. GLfloat left, GLfloat right,
  890. GLfloat bottom, GLfloat top,
  891. GLfloat zNear, GLfloat zFar)
  892. {
  893. ogles_context_t* c = ogles_context_t::get();
  894. orthof(left, right, bottom, top, zNear, zFar, c);
  895. }
  896. void glOrthox(
  897. GLfixed left, GLfixed right,
  898. GLfixed bottom, GLfixed top,
  899. GLfixed zNear, GLfixed zFar)
  900. {
  901. ogles_context_t* c = ogles_context_t::get();
  902. orthof( fixedToFloat(left), fixedToFloat(right),
  903. fixedToFloat(bottom), fixedToFloat(top),
  904. fixedToFloat(zNear), fixedToFloat(zFar),
  905. c);
  906. }
  907. void glRotatef(GLfloat a, GLfloat x, GLfloat y, GLfloat z)
  908. {
  909. ogles_context_t* c = ogles_context_t::get();
  910. c->transforms.current->rotate(a, x, y, z);
  911. c->transforms.invalidate();
  912. }
  913. void glRotatex(GLfixed a, GLfixed x, GLfixed y, GLfixed z)
  914. {
  915. ogles_context_t* c = ogles_context_t::get();
  916. c->transforms.current->rotate(
  917. fixedToFloat(a), fixedToFloat(x),
  918. fixedToFloat(y), fixedToFloat(z));
  919. c->transforms.invalidate();
  920. }
  921. void glScalef(GLfloat x, GLfloat y, GLfloat z)
  922. {
  923. ogles_context_t* c = ogles_context_t::get();
  924. c->transforms.current->scale(x, y, z);
  925. c->transforms.invalidate();
  926. }
  927. void glScalex(GLfixed x, GLfixed y, GLfixed z)
  928. {
  929. ogles_context_t* c = ogles_context_t::get();
  930. c->transforms.current->scale(
  931. fixedToFloat(x), fixedToFloat(y), fixedToFloat(z));
  932. c->transforms.invalidate();
  933. }
  934. void glTranslatef(GLfloat x, GLfloat y, GLfloat z)
  935. {
  936. ogles_context_t* c = ogles_context_t::get();
  937. c->transforms.current->translate(x, y, z);
  938. c->transforms.invalidate();
  939. }
  940. void glTranslatex(GLfixed x, GLfixed y, GLfixed z)
  941. {
  942. ogles_context_t* c = ogles_context_t::get();
  943. c->transforms.current->translate(
  944. fixedToFloat(x), fixedToFloat(y), fixedToFloat(z));
  945. c->transforms.invalidate();
  946. }
  947. void glScissor(GLint x, GLint y, GLsizei w, GLsizei h)
  948. {
  949. ogles_context_t* c = ogles_context_t::get();
  950. ogles_scissor(c, x, y, w, h);
  951. }
  952. void glViewport(GLint x, GLint y, GLsizei w, GLsizei h)
  953. {
  954. ogles_context_t* c = ogles_context_t::get();
  955. ogles_viewport(c, x, y, w, h);
  956. }
  957. void glDepthRangef(GLclampf zNear, GLclampf zFar)
  958. {
  959. ogles_context_t* c = ogles_context_t::get();
  960. depthRangef(zNear, zFar, c);
  961. }
  962. void glDepthRangex(GLclampx zNear, GLclampx zFar)
  963. {
  964. ogles_context_t* c = ogles_context_t::get();
  965. depthRangef(fixedToFloat(zNear), fixedToFloat(zFar), c);
  966. }
  967. void glPolygonOffsetx(GLfixed factor, GLfixed units)
  968. {
  969. ogles_context_t* c = ogles_context_t::get();
  970. c->polygonOffset.factor = factor;
  971. c->polygonOffset.units = units;
  972. }
  973. void glPolygonOffset(GLfloat factor, GLfloat units)
  974. {
  975. ogles_context_t* c = ogles_context_t::get();
  976. c->polygonOffset.factor = gglFloatToFixed(factor);
  977. c->polygonOffset.units = gglFloatToFixed(units);
  978. }
  979. GLbitfield glQueryMatrixxOES(GLfixed* m, GLint* e)
  980. {
  981. ogles_context_t* c = ogles_context_t::get();
  982. GLbitfield status = 0;
  983. GLfloat const* f = c->transforms.current->top().elements();
  984. for (int i=0 ; i<16 ; i++) {
  985. if (isnan(f[i]) || isinf(f[i])) {
  986. status |= 1<<i;
  987. continue;
  988. }
  989. e[i] = exponent(f[i]) - 7;
  990. m[i] = mantissa(f[i]);
  991. }
  992. return status;
  993. }