Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package boilerplate.core;
- import java.awt.image.BufferedImage;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.net.URLDecoder;
- import java.nio.ByteBuffer;
- import java.nio.FloatBuffer;
- import java.util.HashMap;
- import javax.imageio.ImageIO;
- import net.jmatrix.Matrix4;
- import net.jmatrix.Vector3;
- import org.lwjgl.BufferUtils;
- import org.lwjgl.opengl.GL11;
- import org.lwjgl.opengl.GL13;
- import org.lwjgl.opengl.GL15;
- import org.lwjgl.opengl.GL20;
- import org.lwjgl.util.glu.GLU;
- public class Renderer {
- public static final Renderer instance = new Renderer();
- public static final Vector3 X1 = Vector3.fromValues(1, 0, 0), Y1 = Vector3.fromValues(0, 1, 0), Z1 = Vector3
- .fromValues(0, 0, 1);
- private static HashMap<String, Integer> textures;
- public Matrix4 projMatrix, viewMatrix, modelMatrix;
- private int uProjMat, uViewMat, uModelMat, uSampler, aPos, aColor, aTexCoord;
- private int vsId = 0;
- private int fsId = 0;
- private int pId = 0;
- private int currTexture;
- public Renderer() {
- setupShaders();
- setupResources();
- }
- private void setupResources() {
- projMatrix = Matrix4.create();
- GL20.glUniformMatrix4(uProjMat, false, asFlippedFloatBuffer(projMatrix));
- viewMatrix = Matrix4.create();
- GL20.glUniformMatrix4(uViewMat, false, asFlippedFloatBuffer(viewMatrix));
- modelMatrix = Matrix4.create();
- GL20.glUniformMatrix4(uModelMat, false, asFlippedFloatBuffer(modelMatrix));
- GL13.glActiveTexture(GL13.GL_TEXTURE0);
- textures = new HashMap<String, Integer>();
- loadTexture("images/white.png", "blank");
- loadTexture("images/tiles.png", "tiles");
- bindTexture("tiles");
- GL20.glUniform1i(uSampler, 0);
- }
- private void setupShaders() {
- int errorCheckValue = GL11.glGetError();
- vsId = loadShader("/shaders/vertex.glsl", GL20.GL_VERTEX_SHADER);
- fsId = loadShader("/shaders/fragment.glsl", GL20.GL_FRAGMENT_SHADER);
- pId = GL20.glCreateProgram();
- GL20.glAttachShader(pId, vsId);
- GL20.glAttachShader(pId, fsId);
- GL20.glLinkProgram(pId);
- GL20.glValidateProgram(pId);
- errorCheckValue = GL11.glGetError();
- if (errorCheckValue != GL11.GL_NO_ERROR) {
- System.out.println("ERROR - Could not create the shaders:" + GLU.gluErrorString(errorCheckValue));
- System.exit(-1);
- }
- GL20.glUseProgram(pId);
- uProjMat = GL20.glGetUniformLocation(pId, "uProjMatrix");
- System.out.println("uProjMat: " + uProjMat);
- uViewMat = GL20.glGetUniformLocation(pId, "uViewMatrix");
- System.out.println("uViewMatrix: " + uViewMat);
- uModelMat = GL20.glGetUniformLocation(pId, "uModelMatrix");
- System.out.println("uModelMatrix: " + uModelMat);
- uSampler = GL20.glGetUniformLocation(pId, "uSampler");
- System.out.println("uSampler: " + uSampler);
- aPos = GL20.glGetAttribLocation(pId, "aPos");
- System.out.println("aPos: " + aPos);
- aColor = GL20.glGetAttribLocation(pId, "aColor");
- System.out.println("aColor: " + aColor);
- aTexCoord = GL20.glGetAttribLocation(pId, "aTexCoord");
- System.out.println("aTexCoord: " + aTexCoord);
- GL20.glEnableVertexAttribArray(aPos);
- GL20.glEnableVertexAttribArray(aColor);
- GL20.glEnableVertexAttribArray(aTexCoord);
- }
- public void destroyResources() {
- GL20.glUseProgram(0);
- GL20.glDetachShader(pId, vsId);
- GL20.glDetachShader(pId, fsId);
- GL20.glDeleteShader(vsId);
- GL20.glDeleteShader(fsId);
- GL20.glDeleteProgram(pId);
- }
- public void setPerspective() {
- Matrix4.perspective(projMatrix, 70, Main.WIDTH / (float) Main.HEIGHT, 0.01, 100);
- GL20.glUniformMatrix4(uProjMat, false, asFlippedFloatBuffer(projMatrix));
- }
- public void setOrtho() {
- Matrix4.ortho(projMatrix, 0, Main.WIDTH, Main.HEIGHT, 0, -1, 1);
- GL20.glUniformMatrix4(uProjMat, false, asFlippedFloatBuffer(projMatrix));
- }
- public void setCamera(Vector3 pos, Vector3 rot) {
- Matrix4.identity(this.viewMatrix);
- Matrix4.rotate(this.viewMatrix, this.viewMatrix, -rot.vec[0], X1);
- Matrix4.rotate(this.viewMatrix, this.viewMatrix, -rot.vec[1], Y1);
- Matrix4.rotate(this.viewMatrix, this.viewMatrix, -rot.vec[2], Z1);
- Matrix4.translate(this.viewMatrix, this.viewMatrix, pos);
- GL20.glUniformMatrix4(uViewMat, false, asFlippedFloatBuffer(viewMatrix));
- }
- public void drawBuffer(VertexBuffer buffer) {
- GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, buffer.buffer);
- GL20.glVertexAttribPointer(this.aPos, 3, GL11.GL_FLOAT, false, Vertex.SIZE_IN_BYTES, 0);
- GL20.glVertexAttribPointer(this.aColor, 4, GL11.GL_FLOAT, false, Vertex.SIZE_IN_BYTES, 3 * Vertex.ELEMENT_BYTES);
- GL20.glVertexAttribPointer(this.aTexCoord, 2, GL11.GL_FLOAT, false, Vertex.SIZE_IN_BYTES,
- 7 * Vertex.ELEMENT_BYTES);
- GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, buffer.vertices.length);
- }
- private static final int loadShader(String filename, int type) {
- StringBuilder shaderSource = new StringBuilder();
- int shaderID = 0;
- try {
- BufferedReader reader = new BufferedReader(new InputStreamReader(Main.class.getResourceAsStream(filename)));
- String line;
- while ((line = reader.readLine()) != null) {
- shaderSource.append(line).append("\n");
- }
- reader.close();
- } catch (IOException e) {
- System.err.println("Could not read file.");
- e.printStackTrace();
- System.exit(-1);
- }
- shaderID = GL20.glCreateShader(type);
- GL20.glShaderSource(shaderID, shaderSource);
- GL20.glCompileShader(shaderID);
- if (GL20.glGetShader(shaderID, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
- System.err.println("Could not compile shader.");
- System.exit(-1);
- }
- return shaderID;
- }
- private static FloatBuffer asFlippedFloatBuffer(Matrix4 matrix4) {
- FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
- buffer.put(toFloatArray(matrix4.mat));
- buffer.flip();
- return buffer;
- }
- private static ByteBuffer asFlippedByteBuffer(byte[] arr) {
- ByteBuffer buffer = BufferUtils.createByteBuffer(arr.length);
- buffer.put(arr);
- buffer.flip();
- return buffer;
- }
- private static float[] toFloatArray(double[] array) {
- float[] fArray = new float[array.length];
- for (int i = 0; i < array.length; i++) {
- fArray[i] = (float) array[i];
- }
- return fArray;
- }
- public void identity() {
- Matrix4.identity(modelMatrix);
- GL20.glUniformMatrix4(uModelMat, false, asFlippedFloatBuffer(modelMatrix));
- }
- public void rotate(Vector3 v, double r) {
- Matrix4.rotate(Renderer.instance.modelMatrix, Renderer.instance.modelMatrix, r, v);
- GL20.glUniformMatrix4(uModelMat, false, asFlippedFloatBuffer(modelMatrix));
- }
- public void translate(Vector3 v) {
- Matrix4.translate(Renderer.instance.modelMatrix, Renderer.instance.modelMatrix, v);
- GL20.glUniformMatrix4(uModelMat, false, asFlippedFloatBuffer(modelMatrix));
- }
- public void scale(Vector3 v) {
- Matrix4.scale(Renderer.instance.modelMatrix, Renderer.instance.modelMatrix, v);
- GL20.glUniformMatrix4(uModelMat, false, asFlippedFloatBuffer(modelMatrix));
- }
- public void bindTexture(String texture) {
- int tex = textures.get(texture);
- if (currTexture != tex)
- GL11.glBindTexture(GL11.GL_TEXTURE_2D, tex);
- }
- public void loadTexture(String path, String texture) {
- int tex = GL11.glGenTextures();
- GL11.glBindTexture(GL11.GL_TEXTURE_2D, tex);
- BufferedImage img = loadImage(path);
- byte[] pixels = new byte[img.getWidth() * img.getHeight() * 4];
- int currCol = 0;
- for (int i = 0; i < pixels.length; i++) {
- switch (i % 4) {
- case 0:
- currCol = img.getRGB((i / 4) % img.getWidth(), (i / 4) / img.getWidth());
- pixels[i] = (byte) ((currCol >> 16) & 0xFF);
- break;
- case 1:
- pixels[i] = (byte) ((currCol >> 8) & 0xFF);
- break;
- case 2:
- pixels[i] = (byte) (currCol & 0xFF);
- break;
- case 3:
- pixels[i] = (byte) ((currCol >> 24) & 0xFF);
- break;
- }
- }
- GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, img.getWidth(), img.getHeight(), 0, GL11.GL_RGBA,
- GL11.GL_UNSIGNED_BYTE, asFlippedByteBuffer(pixels));
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
- textures.put(texture, tex);
- GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
- }
- private static File getFile(String fileName) {
- String path = Renderer.class.getProtectionDomain().getCodeSource().getLocation().getPath();
- String decodedPath = null;
- try {
- decodedPath = URLDecoder.decode(path, "UTF-8");
- } catch (UnsupportedEncodingException e) {
- return null;
- }
- return new File(decodedPath + fileName);
- }
- private static BufferedImage loadImage(String fileName) {
- try {
- return ImageIO.read(getFile(fileName));
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment