Renderer.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. package min3d.core;
  2. import java.nio.FloatBuffer;
  3. import java.nio.IntBuffer;
  4. import javax.microedition.khronos.egl.EGLConfig;
  5. import javax.microedition.khronos.opengles.GL10;
  6. import javax.microedition.khronos.opengles.GL11;
  7. import min3d.Min3d;
  8. import min3d.Shared;
  9. import min3d.animation.AnimationObject3d;
  10. import min3d.vos.FrustumManaged;
  11. import min3d.vos.Light;
  12. import min3d.vos.RenderType;
  13. import min3d.vos.TextureVo;
  14. import android.app.ActivityManager;
  15. import android.content.Context;
  16. import android.graphics.Bitmap;
  17. import android.opengl.GLSurfaceView;
  18. import android.opengl.GLU;
  19. import android.opengl.GLUtils;
  20. import android.util.Log;
  21. public class Renderer implements GLSurfaceView.Renderer
  22. {
  23. public static final int NUM_GLLIGHTS = 8;
  24. private GL10 _gl;
  25. private Scene _scene;
  26. private TextureManager _textureManager;
  27. private float _surfaceAspectRatio;
  28. private IntBuffer _scratchIntBuffer;
  29. private FloatBuffer _scratchFloatBuffer;
  30. private boolean _scratchB;
  31. // stats-related
  32. public static final int FRAMERATE_SAMPLEINTERVAL_MS = 1000;
  33. private boolean _logFps = false;
  34. private long _frameCount = 0;
  35. private float _fps = 0;
  36. private long _timeLastSample;
  37. private ActivityManager _activityManager;
  38. private ActivityManager.MemoryInfo _memoryInfo;
  39. public Renderer(Scene $scene)
  40. {
  41. _scene = $scene;
  42. _scratchIntBuffer = IntBuffer.allocate(4);
  43. _scratchFloatBuffer = FloatBuffer.allocate(4);
  44. _textureManager = new TextureManager();
  45. Shared.textureManager(_textureManager);
  46. _activityManager = (ActivityManager) Shared.context().getSystemService( Context.ACTIVITY_SERVICE );
  47. _memoryInfo = new ActivityManager.MemoryInfo();
  48. }
  49. public void onSurfaceCreated(GL10 $gl, EGLConfig eglConfig)
  50. {
  51. Log.i(Min3d.TAG, "Renderer.onSurfaceCreated()");
  52. RenderCaps.setRenderCaps($gl);
  53. setGl($gl);
  54. reset();
  55. _scene.init();
  56. }
  57. public void clearScene()
  58. {
  59. _scene.clear();
  60. }
  61. public void onSurfaceChanged(GL10 gl, int w, int h)
  62. {
  63. Log.i(Min3d.TAG, "Renderer.onSurfaceChanged()");
  64. Log.i("-----", "Renderer.onSurfaceChanged()");
  65. setGl(_gl);
  66. _surfaceAspectRatio = (float)w / (float)h;
  67. _gl.glViewport(0, 0, w, h);
  68. _gl.glMatrixMode(GL10.GL_PROJECTION);
  69. _gl.glLoadIdentity();
  70. updateViewFrustrum();
  71. }
  72. public void onDrawFrame(GL10 gl)
  73. {
  74. // Update 'model'
  75. _scene.update();
  76. // Update 'view'
  77. drawSetup();
  78. drawScene();
  79. if (_logFps) doFps();
  80. }
  81. //
  82. /**
  83. * Accessor to the GL object, in case anything outside this class wants to do
  84. * bad things with it :)
  85. */
  86. public GL10 gl()
  87. {
  88. return _gl;
  89. }
  90. /**
  91. * Returns last sampled framerate (logFps must be set to true)
  92. */
  93. public float fps()
  94. {
  95. return _fps;
  96. }
  97. /**
  98. * Return available system memory in bytes
  99. */
  100. public long availMem()
  101. {
  102. _activityManager.getMemoryInfo(_memoryInfo);
  103. return _memoryInfo.availMem;
  104. }
  105. protected void drawSetup()
  106. {
  107. // View frustrum
  108. if (_scene.camera().frustum.isDirty()) {
  109. updateViewFrustrum();
  110. }
  111. // Camera
  112. _gl.glMatrixMode(GL10.GL_MODELVIEW);
  113. _gl.glLoadIdentity();
  114. GLU.gluLookAt(_gl,
  115. _scene.camera().position.x,_scene.camera().position.y,_scene.camera().position.z,
  116. _scene.camera().target.x,_scene.camera().target.y,_scene.camera().target.z,
  117. _scene.camera().upAxis.x,_scene.camera().upAxis.y,_scene.camera().upAxis.z);
  118. // Background color
  119. if (_scene.backgroundColor().isDirty())
  120. {
  121. _gl.glClearColor(
  122. (float)_scene.backgroundColor().r() / 255f,
  123. (float)_scene.backgroundColor().g() / 255f,
  124. (float)_scene.backgroundColor().b() / 255f,
  125. (float)_scene.backgroundColor().a() / 255f);
  126. _scene.backgroundColor().clearDirtyFlag();
  127. }
  128. _gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  129. drawSetupLights();
  130. // Always on:
  131. _gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  132. }
  133. protected void drawSetupLights()
  134. {
  135. // GL_LIGHTS enabled/disabled based on enabledDirty list
  136. for (int glIndex = 0; glIndex < NUM_GLLIGHTS; glIndex++)
  137. {
  138. if (_scene.lights().glIndexEnabledDirty()[glIndex] == true)
  139. {
  140. if (_scene.lights().glIndexEnabled()[glIndex] == true)
  141. {
  142. _gl.glEnable(GL10.GL_LIGHT0 + glIndex);
  143. // make light's properties dirty to force update
  144. _scene.lights().getLightByGlIndex(glIndex).setAllDirty();
  145. }
  146. else
  147. {
  148. _gl.glDisable(GL10.GL_LIGHT0 + glIndex);
  149. }
  150. _scene.lights().glIndexEnabledDirty()[glIndex] = false; // clear dirtyflag
  151. }
  152. }
  153. // Lights' properties
  154. Light[] lights = _scene.lights().toArray();
  155. for (int i = 0; i < lights.length; i++)
  156. {
  157. Light light = lights[i];
  158. if (light.isDirty()) // .. something has changed
  159. {
  160. // Check all of Light's properties for dirty
  161. int glLightId = GL10.GL_LIGHT0 + _scene.lights().getGlIndexByLight(light);
  162. if (light.position.isDirty())
  163. {
  164. light.commitPositionAndTypeBuffer();
  165. _gl.glLightfv(glLightId, GL10.GL_POSITION, light._positionAndTypeBuffer);
  166. light.position.clearDirtyFlag();
  167. }
  168. if (light.ambient.isDirty())
  169. {
  170. light.ambient.commitToFloatBuffer();
  171. _gl.glLightfv(glLightId, GL10.GL_AMBIENT, light.ambient.floatBuffer());
  172. light.ambient.clearDirtyFlag();
  173. }
  174. if (light.diffuse.isDirty())
  175. {
  176. light.diffuse.commitToFloatBuffer();
  177. _gl.glLightfv(glLightId, GL10.GL_DIFFUSE, light.diffuse.floatBuffer());
  178. light.diffuse.clearDirtyFlag();
  179. }
  180. if (light.specular.isDirty())
  181. {
  182. light.specular.commitToFloatBuffer();
  183. _gl.glLightfv(glLightId, GL10.GL_SPECULAR, light.specular.floatBuffer());
  184. light.specular.clearDirtyFlag();
  185. }
  186. if (light.emissive.isDirty())
  187. {
  188. light.emissive.commitToFloatBuffer();
  189. _gl.glLightfv(glLightId, GL10.GL_EMISSION, light.emissive.floatBuffer());
  190. light.emissive.clearDirtyFlag();
  191. }
  192. if (light.direction.isDirty())
  193. {
  194. light.direction.commitToFloatBuffer();
  195. _gl.glLightfv(glLightId, GL10.GL_SPOT_DIRECTION, light.direction.floatBuffer());
  196. light.direction.clearDirtyFlag();
  197. }
  198. if (light._spotCutoffAngle.isDirty())
  199. {
  200. _gl.glLightf(glLightId, GL10.GL_SPOT_CUTOFF, light._spotCutoffAngle.get());
  201. }
  202. if (light._spotExponent.isDirty())
  203. {
  204. _gl.glLightf(glLightId, GL10.GL_SPOT_EXPONENT, light._spotExponent.get());
  205. }
  206. if (light._isVisible.isDirty())
  207. {
  208. if (light.isVisible()) {
  209. _gl.glEnable(glLightId);
  210. } else {
  211. _gl.glDisable(glLightId);
  212. }
  213. light._isVisible.clearDirtyFlag();
  214. }
  215. if (light._attenuation.isDirty())
  216. {
  217. _gl.glLightf(glLightId, GL10.GL_CONSTANT_ATTENUATION, light._attenuation.getX());
  218. _gl.glLightf(glLightId, GL10.GL_LINEAR_ATTENUATION, light._attenuation.getY());
  219. _gl.glLightf(glLightId, GL10.GL_QUADRATIC_ATTENUATION, light._attenuation.getZ());
  220. }
  221. light.clearDirtyFlag();
  222. }
  223. }
  224. }
  225. protected void drawScene()
  226. {
  227. if(_scene.fogEnabled() == true) {
  228. _gl.glFogf(GL10.GL_FOG_MODE, _scene.fogType().glValue());
  229. _gl.glFogf(GL10.GL_FOG_START, _scene.fogNear());
  230. _gl.glFogf(GL10.GL_FOG_END, _scene.fogFar());
  231. _gl.glFogfv(GL10.GL_FOG_COLOR, _scene.fogColor().toFloatBuffer() );
  232. _gl.glEnable(GL10.GL_FOG);
  233. } else {
  234. _gl.glDisable(GL10.GL_FOG);
  235. }
  236. for (int i = 0; i < _scene.children().size(); i++)
  237. {
  238. Object3d o = _scene.children().get(i);
  239. if(o.animationEnabled())
  240. {
  241. ((AnimationObject3d)o).update();
  242. }
  243. drawObject(o);
  244. }
  245. }
  246. //boolean customResult = o.customRenderer(_gl);
  247. //if (customResult) return;
  248. protected void drawObject(Object3d $o)
  249. {
  250. if ($o.isVisible() == false) return;
  251. // Various per-object settings:
  252. // Normals
  253. if ($o.hasNormals() && $o.normalsEnabled()) {
  254. $o.vertices().normals().buffer().position(0);
  255. _gl.glNormalPointer(GL10.GL_FLOAT, 0, $o.vertices().normals().buffer());
  256. _gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
  257. }
  258. else {
  259. _gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
  260. }
  261. // Is lighting enabled for object...
  262. /*
  263. // *** this version not working properly on emulator - why not? ***
  264. _scratchIntBuffer.position(0);
  265. _gl.glGetIntegerv(GL10.GL_LIGHTING, _scratchIntBuffer);
  266. if (useLighting != _scratchIntBuffer.get(0))
  267. {
  268. if (useLighting == 1) {
  269. _gl.glEnable(GL10.GL_LIGHTING);
  270. } else {
  271. _gl.glDisable(GL10.GL_LIGHTING);
  272. }
  273. }
  274. */
  275. boolean useLighting = (_scene.lightingEnabled() && $o.hasNormals() && $o.normalsEnabled() && $o.lightingEnabled());
  276. if (useLighting) {
  277. _gl.glEnable(GL10.GL_LIGHTING);
  278. } else {
  279. _gl.glDisable(GL10.GL_LIGHTING);
  280. }
  281. // Shademodel
  282. _gl.glGetIntegerv(GL11.GL_SHADE_MODEL, _scratchIntBuffer);
  283. if ($o.shadeModel().glConstant() != _scratchIntBuffer.get(0)) {
  284. _gl.glShadeModel($o.shadeModel().glConstant());
  285. }
  286. // Colors: either per-vertex, or per-object
  287. if ($o.hasVertexColors() && $o.vertexColorsEnabled()) {
  288. $o.vertices().colors().buffer().position(0);
  289. _gl.glColorPointer(4, GL10.GL_UNSIGNED_BYTE, 0, $o.vertices().colors().buffer());
  290. _gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
  291. }
  292. else {
  293. _gl.glColor4f(
  294. (float)$o.defaultColor().r / 255f,
  295. (float)$o.defaultColor().g / 255f,
  296. (float)$o.defaultColor().b / 255f,
  297. (float)$o.defaultColor().a / 255f
  298. );
  299. _gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
  300. }
  301. // Colormaterial
  302. _gl.glGetIntegerv(GL10.GL_COLOR_MATERIAL, _scratchIntBuffer);
  303. _scratchB = (_scratchIntBuffer.get(0) != 0);
  304. if ($o.colorMaterialEnabled() != _scratchB) {
  305. if ($o.colorMaterialEnabled())
  306. _gl.glEnable(GL10.GL_COLOR_MATERIAL);
  307. else
  308. _gl.glDisable(GL10.GL_COLOR_MATERIAL);
  309. }
  310. // Point size
  311. if ($o.renderType() == RenderType.POINTS)
  312. {
  313. if ($o.pointSmoothing())
  314. _gl.glEnable(GL10.GL_POINT_SMOOTH);
  315. else
  316. _gl.glDisable(GL10.GL_POINT_SMOOTH);
  317. _gl.glPointSize($o.pointSize());
  318. }
  319. // Line properties
  320. if ($o.renderType() == RenderType.LINES || $o.renderType() == RenderType.LINE_STRIP || $o.renderType() == RenderType.LINE_LOOP)
  321. {
  322. if ( $o.lineSmoothing() == true) {
  323. _gl.glEnable(GL10.GL_LINE_SMOOTH);
  324. }
  325. else {
  326. _gl.glDisable(GL10.GL_LINE_SMOOTH);
  327. }
  328. _gl.glLineWidth($o.lineWidth());
  329. }
  330. // Backface culling
  331. if ($o.doubleSidedEnabled()) {
  332. _gl.glDisable(GL10.GL_CULL_FACE);
  333. }
  334. else {
  335. _gl.glEnable(GL10.GL_CULL_FACE);
  336. }
  337. drawObject_textures($o);
  338. // Matrix operations in modelview
  339. _gl.glPushMatrix();
  340. _gl.glTranslatef($o.position().x, $o.position().y, $o.position().z);
  341. _gl.glRotatef($o.rotation().x, 1,0,0);
  342. _gl.glRotatef($o.rotation().y, 0,1,0);
  343. _gl.glRotatef($o.rotation().z, 0,0,1);
  344. _gl.glScalef($o.scale().x, $o.scale().y, $o.scale().z);
  345. // Draw
  346. $o.vertices().points().buffer().position(0);
  347. _gl.glVertexPointer(3, GL10.GL_FLOAT, 0, $o.vertices().points().buffer());
  348. if (! $o.ignoreFaces())
  349. {
  350. int pos, len;
  351. if (! $o.faces().renderSubsetEnabled()) {
  352. pos = 0;
  353. len = $o.faces().size();
  354. }
  355. else {
  356. pos = $o.faces().renderSubsetStartIndex() * FacesBufferedList.PROPERTIES_PER_ELEMENT;
  357. len = $o.faces().renderSubsetLength();
  358. }
  359. $o.faces().buffer().position(pos);
  360. _gl.glDrawElements(
  361. $o.renderType().glValue(),
  362. len * FacesBufferedList.PROPERTIES_PER_ELEMENT,
  363. GL10.GL_UNSIGNED_SHORT,
  364. $o.faces().buffer());
  365. }
  366. else
  367. {
  368. _gl.glDrawArrays($o.renderType().glValue(), 0, $o.vertices().size());
  369. }
  370. //
  371. // Recurse on children
  372. //
  373. if ($o instanceof Object3dContainer)
  374. {
  375. Object3dContainer container = (Object3dContainer)$o;
  376. for (int i = 0; i < container.children().size(); i++)
  377. {
  378. Object3d o = container.children().get(i);
  379. drawObject(o);
  380. }
  381. }
  382. // Restore matrix
  383. _gl.glPopMatrix();
  384. }
  385. private void drawObject_textures(Object3d $o)
  386. {
  387. // iterate thru object's textures
  388. for (int i = 0; i < RenderCaps.maxTextureUnits(); i++)
  389. {
  390. _gl.glActiveTexture(GL10.GL_TEXTURE0 + i);
  391. _gl.glClientActiveTexture(GL10.GL_TEXTURE0 + i);
  392. if ($o.hasUvs() && $o.texturesEnabled())
  393. {
  394. $o.vertices().uvs().buffer().position(0);
  395. _gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, $o.vertices().uvs().buffer());
  396. TextureVo textureVo = ((i < $o.textures().size())) ? textureVo = $o.textures().get(i) : null;
  397. if (textureVo != null)
  398. {
  399. // activate texture
  400. int glId = _textureManager.getGlTextureId(textureVo.textureId);
  401. _gl.glBindTexture(GL10.GL_TEXTURE_2D, glId);
  402. _gl.glEnable(GL10.GL_TEXTURE_2D);
  403. _gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
  404. int minFilterType = _textureManager.hasMipMap(textureVo.textureId) ? GL10.GL_LINEAR_MIPMAP_NEAREST : GL10.GL_NEAREST;
  405. _gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, minFilterType);
  406. _gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); // (OpenGL default)
  407. // do texture environment settings
  408. for (int j = 0; j < textureVo.textureEnvs.size(); j++)
  409. {
  410. _gl.glTexEnvx(GL10.GL_TEXTURE_ENV, textureVo.textureEnvs.get(j).pname, textureVo.textureEnvs.get(j).param);
  411. }
  412. // texture wrapping settings
  413. _gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, (textureVo.repeatU ? GL10.GL_REPEAT : GL10.GL_CLAMP_TO_EDGE));
  414. _gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, (textureVo.repeatV ? GL10.GL_REPEAT : GL10.GL_CLAMP_TO_EDGE));
  415. // texture offset, if any
  416. if (textureVo.offsetU != 0 || textureVo.offsetV != 0)
  417. {
  418. _gl.glMatrixMode(GL10.GL_TEXTURE);
  419. _gl.glLoadIdentity();
  420. _gl.glTranslatef(textureVo.offsetU, textureVo.offsetV, 0);
  421. _gl.glMatrixMode(GL10.GL_MODELVIEW); // .. restore matrixmode
  422. }
  423. }
  424. else
  425. {
  426. _gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
  427. _gl.glDisable(GL10.GL_TEXTURE_2D);
  428. _gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
  429. }
  430. }
  431. else
  432. {
  433. _gl.glBindTexture(GL10.GL_TEXTURE_2D, 0);
  434. _gl.glDisable(GL10.GL_TEXTURE_2D);
  435. _gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
  436. }
  437. }
  438. }
  439. /**
  440. * Used by TextureManager
  441. */
  442. int uploadTextureAndReturnId(Bitmap $bitmap, boolean $generateMipMap) /*package-private*/
  443. {
  444. int glTextureId;
  445. int[] a = new int[1];
  446. _gl.glGenTextures(1, a, 0); // create a 'texture name' and put it in array element 0
  447. glTextureId = a[0];
  448. _gl.glBindTexture(GL10.GL_TEXTURE_2D, glTextureId);
  449. if($generateMipMap && _gl instanceof GL11) {
  450. _gl.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_GENERATE_MIPMAP, GL11.GL_TRUE);
  451. } else {
  452. _gl.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_GENERATE_MIPMAP, GL11.GL_FALSE);
  453. }
  454. // 'upload' to gpu
  455. GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, $bitmap, 0);
  456. return glTextureId;
  457. }
  458. /**
  459. * Used by TextureManager
  460. */
  461. void deleteTexture(int $glTextureId) /*package-private*/
  462. {
  463. int[] a = new int[1];
  464. a[0] = $glTextureId;
  465. _gl.glDeleteTextures(1, a, 0);
  466. }
  467. protected void updateViewFrustrum()
  468. {
  469. FrustumManaged vf = _scene.camera().frustum;
  470. float n = vf.shortSideLength() / 2f;
  471. float lt, rt, btm, top;
  472. lt = vf.horizontalCenter() - n*_surfaceAspectRatio;
  473. rt = vf.horizontalCenter() + n*_surfaceAspectRatio;
  474. btm = vf.verticalCenter() - n*1;
  475. top = vf.verticalCenter() + n*1;
  476. if (_surfaceAspectRatio > 1) {
  477. lt *= 1f/_surfaceAspectRatio;
  478. rt *= 1f/_surfaceAspectRatio;
  479. btm *= 1f/_surfaceAspectRatio;
  480. top *= 1f/_surfaceAspectRatio;
  481. }
  482. _gl.glMatrixMode(GL10.GL_PROJECTION);
  483. _gl.glLoadIdentity();
  484. _gl.glFrustumf(lt,rt, btm,top, vf.zNear(), vf.zFar());
  485. vf.clearDirtyFlag();
  486. }
  487. /**
  488. * If true, framerate and memory is periodically calculated and Log'ed,
  489. * and gettable thru fps()
  490. */
  491. public void logFps(boolean $b)
  492. {
  493. _logFps = $b;
  494. if (_logFps) { // init
  495. _timeLastSample = System.currentTimeMillis();
  496. _frameCount = 0;
  497. }
  498. }
  499. private void setGl(GL10 $gl)
  500. {
  501. _gl = $gl;
  502. }
  503. private void doFps()
  504. {
  505. _frameCount++;
  506. long now = System.currentTimeMillis();
  507. long delta = now - _timeLastSample;
  508. if (delta >= FRAMERATE_SAMPLEINTERVAL_MS)
  509. {
  510. _fps = _frameCount / (delta/1000f);
  511. _activityManager.getMemoryInfo(_memoryInfo);
  512. Log.v(Min3d.TAG, "FPS: " + Math.round(_fps) + ", availMem: " + Math.round(_memoryInfo.availMem/1048576) + "MB");
  513. _timeLastSample = now;
  514. _frameCount = 0;
  515. }
  516. }
  517. private void reset()
  518. {
  519. // Reset TextureManager
  520. Shared.textureManager().reset();
  521. // Do OpenGL settings which we are using as defaults, or which we will not be changing on-draw
  522. // Explicit depth settings
  523. _gl.glEnable(GL10.GL_DEPTH_TEST);
  524. _gl.glClearDepthf(1.0f);
  525. _gl.glDepthFunc(GL10.GL_LESS);
  526. _gl.glDepthRangef(0,1f);
  527. _gl.glDepthMask(true);
  528. // Alpha enabled
  529. _gl.glEnable(GL10.GL_BLEND);
  530. _gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
  531. // "Transparency is best implemented using glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  532. // with primitives sorted from farthest to nearest."
  533. // Texture
  534. _gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST); // (OpenGL default is GL_NEAREST_MIPMAP)
  535. _gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); // (is OpenGL default)
  536. // CCW frontfaces only, by default
  537. _gl.glFrontFace(GL10.GL_CCW);
  538. _gl.glCullFace(GL10.GL_BACK);
  539. _gl.glEnable(GL10.GL_CULL_FACE);
  540. // Disable lights by default
  541. for (int i = GL10.GL_LIGHT0; i < GL10.GL_LIGHT0 + NUM_GLLIGHTS; i++) {
  542. _gl.glDisable(i);
  543. }
  544. //
  545. // Scene object init only happens here, when we get GL for the first time
  546. //
  547. }
  548. }