Guest User

Untitled

a guest
Aug 2nd, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.83 KB | None | 0 0
  1. package boilerplate.core;
  2.  
  3. import java.awt.image.BufferedImage;
  4. import java.io.BufferedReader;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.io.UnsupportedEncodingException;
  9. import java.net.URLDecoder;
  10. import java.nio.ByteBuffer;
  11. import java.nio.FloatBuffer;
  12. import java.util.HashMap;
  13.  
  14. import javax.imageio.ImageIO;
  15.  
  16. import net.jmatrix.Matrix4;
  17. import net.jmatrix.Vector3;
  18.  
  19. import org.lwjgl.BufferUtils;
  20. import org.lwjgl.opengl.GL11;
  21. import org.lwjgl.opengl.GL13;
  22. import org.lwjgl.opengl.GL15;
  23. import org.lwjgl.opengl.GL20;
  24. import org.lwjgl.util.glu.GLU;
  25.  
  26. public class Renderer {
  27.  
  28. public static final Renderer instance = new Renderer();
  29.  
  30. public static final Vector3 X1 = Vector3.fromValues(1, 0, 0), Y1 = Vector3.fromValues(0, 1, 0), Z1 = Vector3
  31. .fromValues(0, 0, 1);
  32.  
  33. private static HashMap<String, Integer> textures;
  34.  
  35. public Matrix4 projMatrix, viewMatrix, modelMatrix;
  36. private int uProjMat, uViewMat, uModelMat, uSampler, aPos, aColor, aTexCoord;
  37.  
  38. private int vsId = 0;
  39. private int fsId = 0;
  40. private int pId = 0;
  41.  
  42. private int currTexture;
  43.  
  44. public Renderer() {
  45. setupShaders();
  46. setupResources();
  47. }
  48.  
  49. private void setupResources() {
  50. projMatrix = Matrix4.create();
  51. GL20.glUniformMatrix4(uProjMat, false, asFlippedFloatBuffer(projMatrix));
  52. viewMatrix = Matrix4.create();
  53. GL20.glUniformMatrix4(uViewMat, false, asFlippedFloatBuffer(viewMatrix));
  54. modelMatrix = Matrix4.create();
  55. GL20.glUniformMatrix4(uModelMat, false, asFlippedFloatBuffer(modelMatrix));
  56.  
  57. GL13.glActiveTexture(GL13.GL_TEXTURE0);
  58.  
  59. textures = new HashMap<String, Integer>();
  60.  
  61. loadTexture("images/white.png", "blank");
  62. loadTexture("images/tiles.png", "tiles");
  63.  
  64. bindTexture("tiles");
  65.  
  66. GL20.glUniform1i(uSampler, 0);
  67. }
  68.  
  69. private void setupShaders() {
  70. int errorCheckValue = GL11.glGetError();
  71.  
  72. vsId = loadShader("/shaders/vertex.glsl", GL20.GL_VERTEX_SHADER);
  73. fsId = loadShader("/shaders/fragment.glsl", GL20.GL_FRAGMENT_SHADER);
  74.  
  75. pId = GL20.glCreateProgram();
  76. GL20.glAttachShader(pId, vsId);
  77. GL20.glAttachShader(pId, fsId);
  78. GL20.glLinkProgram(pId);
  79.  
  80. GL20.glValidateProgram(pId);
  81.  
  82. errorCheckValue = GL11.glGetError();
  83. if (errorCheckValue != GL11.GL_NO_ERROR) {
  84. System.out.println("ERROR - Could not create the shaders:" + GLU.gluErrorString(errorCheckValue));
  85. System.exit(-1);
  86. }
  87.  
  88. GL20.glUseProgram(pId);
  89.  
  90. uProjMat = GL20.glGetUniformLocation(pId, "uProjMatrix");
  91. System.out.println("uProjMat: " + uProjMat);
  92. uViewMat = GL20.glGetUniformLocation(pId, "uViewMatrix");
  93. System.out.println("uViewMatrix: " + uViewMat);
  94. uModelMat = GL20.glGetUniformLocation(pId, "uModelMatrix");
  95. System.out.println("uModelMatrix: " + uModelMat);
  96. uSampler = GL20.glGetUniformLocation(pId, "uSampler");
  97. System.out.println("uSampler: " + uSampler);
  98. aPos = GL20.glGetAttribLocation(pId, "aPos");
  99. System.out.println("aPos: " + aPos);
  100. aColor = GL20.glGetAttribLocation(pId, "aColor");
  101. System.out.println("aColor: " + aColor);
  102. aTexCoord = GL20.glGetAttribLocation(pId, "aTexCoord");
  103. System.out.println("aTexCoord: " + aTexCoord);
  104.  
  105. GL20.glEnableVertexAttribArray(aPos);
  106. GL20.glEnableVertexAttribArray(aColor);
  107. GL20.glEnableVertexAttribArray(aTexCoord);
  108. }
  109.  
  110. public void destroyResources() {
  111. GL20.glUseProgram(0);
  112. GL20.glDetachShader(pId, vsId);
  113. GL20.glDetachShader(pId, fsId);
  114.  
  115. GL20.glDeleteShader(vsId);
  116. GL20.glDeleteShader(fsId);
  117. GL20.glDeleteProgram(pId);
  118. }
  119.  
  120. public void setPerspective() {
  121. Matrix4.perspective(projMatrix, 70, Main.WIDTH / (float) Main.HEIGHT, 0.01, 100);
  122. GL20.glUniformMatrix4(uProjMat, false, asFlippedFloatBuffer(projMatrix));
  123. }
  124.  
  125. public void setOrtho() {
  126. Matrix4.ortho(projMatrix, 0, Main.WIDTH, Main.HEIGHT, 0, -1, 1);
  127. GL20.glUniformMatrix4(uProjMat, false, asFlippedFloatBuffer(projMatrix));
  128. }
  129.  
  130. public void setCamera(Vector3 pos, Vector3 rot) {
  131. Matrix4.identity(this.viewMatrix);
  132.  
  133. Matrix4.rotate(this.viewMatrix, this.viewMatrix, -rot.vec[0], X1);
  134. Matrix4.rotate(this.viewMatrix, this.viewMatrix, -rot.vec[1], Y1);
  135. Matrix4.rotate(this.viewMatrix, this.viewMatrix, -rot.vec[2], Z1);
  136.  
  137. Matrix4.translate(this.viewMatrix, this.viewMatrix, pos);
  138.  
  139. GL20.glUniformMatrix4(uViewMat, false, asFlippedFloatBuffer(viewMatrix));
  140. }
  141.  
  142. public void drawBuffer(VertexBuffer buffer) {
  143. GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, buffer.buffer);
  144.  
  145. GL20.glVertexAttribPointer(this.aPos, 3, GL11.GL_FLOAT, false, Vertex.SIZE_IN_BYTES, 0);
  146. GL20.glVertexAttribPointer(this.aColor, 4, GL11.GL_FLOAT, false, Vertex.SIZE_IN_BYTES, 3 * Vertex.ELEMENT_BYTES);
  147. GL20.glVertexAttribPointer(this.aTexCoord, 2, GL11.GL_FLOAT, false, Vertex.SIZE_IN_BYTES,
  148. 7 * Vertex.ELEMENT_BYTES);
  149.  
  150. GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, buffer.vertices.length);
  151. }
  152.  
  153. private static final int loadShader(String filename, int type) {
  154. StringBuilder shaderSource = new StringBuilder();
  155. int shaderID = 0;
  156.  
  157. try {
  158. BufferedReader reader = new BufferedReader(new InputStreamReader(Main.class.getResourceAsStream(filename)));
  159. String line;
  160. while ((line = reader.readLine()) != null) {
  161. shaderSource.append(line).append("\n");
  162. }
  163. reader.close();
  164. } catch (IOException e) {
  165. System.err.println("Could not read file.");
  166. e.printStackTrace();
  167. System.exit(-1);
  168. }
  169.  
  170. shaderID = GL20.glCreateShader(type);
  171. GL20.glShaderSource(shaderID, shaderSource);
  172. GL20.glCompileShader(shaderID);
  173.  
  174. if (GL20.glGetShader(shaderID, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
  175. System.err.println("Could not compile shader.");
  176. System.exit(-1);
  177. }
  178.  
  179. return shaderID;
  180. }
  181.  
  182. private static FloatBuffer asFlippedFloatBuffer(Matrix4 matrix4) {
  183. FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
  184.  
  185. buffer.put(toFloatArray(matrix4.mat));
  186.  
  187. buffer.flip();
  188.  
  189. return buffer;
  190. }
  191.  
  192. private static ByteBuffer asFlippedByteBuffer(byte[] arr) {
  193. ByteBuffer buffer = BufferUtils.createByteBuffer(arr.length);
  194.  
  195. buffer.put(arr);
  196.  
  197. buffer.flip();
  198.  
  199. return buffer;
  200. }
  201.  
  202. private static float[] toFloatArray(double[] array) {
  203. float[] fArray = new float[array.length];
  204.  
  205. for (int i = 0; i < array.length; i++) {
  206. fArray[i] = (float) array[i];
  207. }
  208.  
  209. return fArray;
  210. }
  211.  
  212. public void identity() {
  213. Matrix4.identity(modelMatrix);
  214. GL20.glUniformMatrix4(uModelMat, false, asFlippedFloatBuffer(modelMatrix));
  215. }
  216.  
  217. public void rotate(Vector3 v, double r) {
  218. Matrix4.rotate(Renderer.instance.modelMatrix, Renderer.instance.modelMatrix, r, v);
  219. GL20.glUniformMatrix4(uModelMat, false, asFlippedFloatBuffer(modelMatrix));
  220. }
  221.  
  222. public void translate(Vector3 v) {
  223. Matrix4.translate(Renderer.instance.modelMatrix, Renderer.instance.modelMatrix, v);
  224. GL20.glUniformMatrix4(uModelMat, false, asFlippedFloatBuffer(modelMatrix));
  225. }
  226.  
  227. public void scale(Vector3 v) {
  228. Matrix4.scale(Renderer.instance.modelMatrix, Renderer.instance.modelMatrix, v);
  229. GL20.glUniformMatrix4(uModelMat, false, asFlippedFloatBuffer(modelMatrix));
  230. }
  231.  
  232. public void bindTexture(String texture) {
  233. int tex = textures.get(texture);
  234.  
  235. if (currTexture != tex)
  236. GL11.glBindTexture(GL11.GL_TEXTURE_2D, tex);
  237. }
  238.  
  239. public void loadTexture(String path, String texture) {
  240. int tex = GL11.glGenTextures();
  241.  
  242. GL11.glBindTexture(GL11.GL_TEXTURE_2D, tex);
  243.  
  244. BufferedImage img = loadImage(path);
  245.  
  246. byte[] pixels = new byte[img.getWidth() * img.getHeight() * 4];
  247.  
  248. int currCol = 0;
  249. for (int i = 0; i < pixels.length; i++) {
  250. switch (i % 4) {
  251. case 0:
  252. currCol = img.getRGB((i / 4) % img.getWidth(), (i / 4) / img.getWidth());
  253.  
  254. pixels[i] = (byte) ((currCol >> 16) & 0xFF);
  255. break;
  256. case 1:
  257. pixels[i] = (byte) ((currCol >> 8) & 0xFF);
  258. break;
  259. case 2:
  260. pixels[i] = (byte) (currCol & 0xFF);
  261. break;
  262. case 3:
  263. pixels[i] = (byte) ((currCol >> 24) & 0xFF);
  264. break;
  265. }
  266. }
  267.  
  268. GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, img.getWidth(), img.getHeight(), 0, GL11.GL_RGBA,
  269. GL11.GL_UNSIGNED_BYTE, asFlippedByteBuffer(pixels));
  270.  
  271. GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
  272. GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
  273.  
  274. textures.put(texture, tex);
  275.  
  276. GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
  277. }
  278.  
  279. private static File getFile(String fileName) {
  280. String path = Renderer.class.getProtectionDomain().getCodeSource().getLocation().getPath();
  281.  
  282. String decodedPath = null;
  283.  
  284. try {
  285. decodedPath = URLDecoder.decode(path, "UTF-8");
  286. } catch (UnsupportedEncodingException e) {
  287. return null;
  288. }
  289.  
  290. return new File(decodedPath + fileName);
  291. }
  292.  
  293. private static BufferedImage loadImage(String fileName) {
  294. try {
  295. return ImageIO.read(getFile(fileName));
  296. } catch (Exception e) {
  297. throw new RuntimeException(e);
  298. }
  299. }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment