/Earth/src/com/zeph/android/earth/SkyBox.java

https://bitbucket.org/Zeph0/earth · Java · 161 lines · 112 code · 31 blank · 18 comment · 3 complexity · 5cf6d6979637ee501aa307aa8ce2e36c MD5 · raw file

  1. /*
  2. This file is part of Earth.
  3. Earth is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. Earth is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Earth. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. package com.zeph.android.earth;
  15. import java.nio.IntBuffer;
  16. import com.badlogic.gdx.Gdx;
  17. import com.badlogic.gdx.assets.AssetManager;
  18. import com.badlogic.gdx.graphics.GL20;
  19. import com.badlogic.gdx.graphics.Mesh;
  20. import com.badlogic.gdx.graphics.PerspectiveCamera;
  21. import com.badlogic.gdx.graphics.Pixmap;
  22. import com.badlogic.gdx.graphics.Texture;
  23. import com.badlogic.gdx.graphics.glutils.ShaderProgram;
  24. import com.badlogic.gdx.math.Matrix4;
  25. import com.badlogic.gdx.utils.BufferUtils;
  26. import com.badlogic.gdx.utils.GdxRuntimeException;
  27. public class SkyBox {
  28. private Mesh cube;
  29. // private Pixmap[] textures = new Pixmap[6];
  30. private String[] textures = new String[6];
  31. private ShaderProgram program;
  32. private int g_cubeTexture;
  33. private Matrix4 invView = new Matrix4();
  34. private Matrix4 mvp = new Matrix4();
  35. private AssetManager manager;
  36. private static final String fragmentShader = "data/SkyBox-fragO.glsl";
  37. private static final String vertexShader = "data/SkyBox-vertO.glsl";
  38. public SkyBox(AssetManager manager, String[] faces) {
  39. this.manager = manager;
  40. this.textures = faces;
  41. g_cubeTexture = Texture.createGLHandle();
  42. Gdx.gl20.glBindTexture(GL20.GL_TEXTURE_CUBE_MAP, g_cubeTexture);
  43. program = new ShaderProgram(Gdx.files.internal(vertexShader)
  44. .readString(), Gdx.files.internal(fragmentShader).readString());
  45. if (!program.isCompiled())
  46. throw new GdxRuntimeException("Couldn't compile skybox shader: "
  47. + program.getLog());
  48. cube = Shapes.genSkyBox();
  49. reloadCubemap();
  50. }
  51. public void reloadCubemap() {
  52. g_cubeTexture = Texture.createGLHandle();
  53. Gdx.gl20.glBindTexture(GL20.GL_TEXTURE_CUBE_MAP, g_cubeTexture);
  54. Pixmap temp = manager.get(textures[0], Pixmap.class);
  55. Gdx.gl.glTexImage2D(GL20.GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0,
  56. temp.getGLInternalFormat(), temp.getWidth(), temp.getHeight(),
  57. 0, temp.getGLFormat(), temp.getGLType(), temp.getPixels());
  58. temp = manager.get(textures[1], Pixmap.class);
  59. Gdx.gl.glTexImage2D(GL20.GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0,
  60. temp.getGLInternalFormat(), temp.getWidth(), temp.getHeight(),
  61. 0, temp.getGLFormat(), temp.getGLType(), temp.getPixels());
  62. temp = manager.get(textures[2], Pixmap.class);
  63. Gdx.gl.glTexImage2D(GL20.GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0,
  64. temp.getGLInternalFormat(), temp.getWidth(), temp.getHeight(),
  65. 0, temp.getGLFormat(), temp.getGLType(), temp.getPixels());
  66. temp = manager.get(textures[3], Pixmap.class);
  67. Gdx.gl.glTexImage2D(GL20.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0,
  68. temp.getGLInternalFormat(), temp.getWidth(), temp.getHeight(),
  69. 0, temp.getGLFormat(), temp.getGLType(), temp.getPixels());
  70. temp = manager.get(textures[4], Pixmap.class);
  71. Gdx.gl.glTexImage2D(GL20.GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0,
  72. temp.getGLInternalFormat(), temp.getWidth(), temp.getHeight(),
  73. 0, temp.getGLFormat(), temp.getGLType(), temp.getPixels());
  74. temp = manager.get(textures[5], Pixmap.class);
  75. Gdx.gl.glTexImage2D(GL20.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0,
  76. temp.getGLInternalFormat(), temp.getWidth(), temp.getHeight(),
  77. 0, temp.getGLFormat(), temp.getGLType(), temp.getPixels());
  78. Gdx.gl20.glBindTexture(GL20.GL_TEXTURE_CUBE_MAP, g_cubeTexture);
  79. Gdx.gl20.glTexParameterf(GL20.GL_TEXTURE_CUBE_MAP,
  80. GL20.GL_TEXTURE_MAG_FILTER, GL20.GL_LINEAR);
  81. Gdx.gl20.glTexParameterf(GL20.GL_TEXTURE_CUBE_MAP,
  82. GL20.GL_TEXTURE_MIN_FILTER, GL20.GL_NEAREST);
  83. Gdx.gl20.glTexParameteri(GL20.GL_TEXTURE_CUBE_MAP,
  84. GL20.GL_TEXTURE_WRAP_S, GL20.GL_CLAMP_TO_EDGE);
  85. Gdx.gl20.glTexParameteri(GL20.GL_TEXTURE_CUBE_MAP,
  86. GL20.GL_TEXTURE_WRAP_T, GL20.GL_CLAMP_TO_EDGE);
  87. }
  88. public void render(PerspectiveCamera camera) {
  89. invView.set(camera.view);
  90. // Remove translation
  91. invView.val[Matrix4.M03] = 0;
  92. invView.val[Matrix4.M13] = 0;
  93. invView.val[Matrix4.M23] = 0;
  94. invView.inv().tra();
  95. mvp.set(camera.projection);
  96. mvp.mul(invView);
  97. Gdx.gl.glEnable(GL20.GL_CULL_FACE);
  98. Gdx.gl.glCullFace(GL20.GL_FRONT);
  99. Gdx.gl.glFrontFace(GL20.GL_CCW);
  100. Gdx.gl20.glDisable(GL20.GL_BLEND);
  101. Gdx.gl20.glDisable(GL20.GL_DEPTH_TEST);
  102. Gdx.gl20.glDepthMask(false);
  103. Gdx.gl20.glActiveTexture(GL20.GL_TEXTURE0);
  104. Gdx.gl20.glBindTexture(GL20.GL_TEXTURE_CUBE_MAP, g_cubeTexture);
  105. program.begin();
  106. program.setUniformMatrix("u_mvpMatrix", mvp);
  107. program.setUniformi("s_cubemap", 0);
  108. cube.render(program, GL20.GL_TRIANGLE_STRIP);
  109. program.end();
  110. Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
  111. Gdx.gl.glDepthMask(true);
  112. }
  113. public void resume() {
  114. reloadCubemap();
  115. }
  116. public void dispose() {
  117. for (int i = 0; i < 6; i++) {
  118. if (manager.isLoaded(textures[i])) {
  119. manager.unload(textures[i]);
  120. }
  121. }
  122. cube.dispose();
  123. IntBuffer buffer = BufferUtils.newIntBuffer(1);
  124. buffer.put(0, g_cubeTexture);
  125. Gdx.gl.glDeleteTextures(1, buffer);
  126. }
  127. }