/*     */ package org.renderer;
/*     */ 
/*     */ import java.awt.Dimension;
/*     */ import java.nio.IntBuffer;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import javax.swing.JOptionPane;
/*     */ import org.lwjgl.BufferUtils;
/*     */ import org.lwjgl.opengl.GL11C;
/*     */ import org.lwjgl.opengl.GL13C;
/*     */ import org.lwjgl.opengl.GL15C;
/*     */ import org.lwjgl.opengl.GL20C;
/*     */ import org.lwjgl.opengl.GL30C;
/*     */ import org.lwjgl.opengl.GL43C;
/*     */ import org.shader.ShaderProgram;
/*     */ import org.textures.Texture;
/*     */ import org.view.Camera;
/*     */ import org.world.World;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class SceneRenderer
/*     */   extends ShaderProgram
/*     */ {
/*  50 */   public static final String[] UNIFORM_NAMES = new String[] { 
/*  51 */       "resolution", 
/*  52 */       "time", 
/*  53 */       "cameraRotation", 
/*  54 */       "cameraZoom", 
/*  55 */       "worldTexture", 
/*  56 */       "environmentTexture", 
/*  57 */       "coinTexture", 
/*  58 */       "width", 
/*  59 */       "height", 
/*  60 */       "drawGytebot", 
/*  61 */       "gytebotLocation", 
/*  62 */       "gytebotRotation" };
/*     */ 
/*     */   
/*     */   private Texture worldTexture;
/*     */ 
/*     */   
/*     */   private Texture environmentTexture;
/*     */ 
/*     */   
/*     */   private Texture coinTexture;
/*     */ 
/*     */   
/*     */   private Texture testTexture;
/*     */   
/*     */   private int coinBuffer;
/*     */   
/*     */   private int coinSsbo;
/*     */   
/*     */   private int wallBuffer;
/*     */   
/*     */   private int wallSsbo;
/*     */   
/*     */   private Map<String, Integer> uniformLocations;
/*     */ 
/*     */   
/*     */   public SceneRenderer() throws AssertionError {
/*  88 */     super("/data/shader/SceneRenderer");
/*     */     
/*  90 */     this.uniformLocations = new HashMap<>();
/*     */ 
/*     */     
/*  93 */     this.coinSsbo = GL15C.glGenBuffers();
/*  94 */     this.wallSsbo = GL15C.glGenBuffers();
/*     */     
/*  96 */     createUniforms();
/*     */ 
/*     */     
/*     */     try {
/* 100 */       createTextures();
/*     */     }
/* 102 */     catch (Exception e) {
/*     */ 
/*     */       
/* 105 */       JOptionPane.showMessageDialog(null, "Unable to load texture(s)", "Error - loading textures", 0);
/* 106 */       e.printStackTrace();
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private void createTextures() throws Exception {
/* 116 */     this.worldTexture = new Texture("data/icons/atlas.png");
/* 117 */     this.environmentTexture = new Texture("data/icons/environment.png");
/* 118 */     this.coinTexture = new Texture("data/icons/coin.png");
/* 119 */     this.testTexture = new Texture("data/icons/test.png");
/*     */   }
/*     */ 
/*     */   
/*     */   private void createUniforms() {
/*     */     byte b;
/*     */     int i;
/*     */     String[] arrayOfString;
/* 127 */     for (i = (arrayOfString = UNIFORM_NAMES).length, b = 0; b < i; ) { String uniformName = arrayOfString[b];
/*     */       
/* 129 */       int location = GL20C.glGetUniformLocation(this.program, uniformName);
/*     */       
/* 131 */       if (location == -1)
/*     */       {
/* 133 */         System.err.println("Unable to find uniform: " + uniformName);
/*     */       }
/*     */ 
/*     */       
/* 137 */       this.uniformLocations.put(uniformName, Integer.valueOf(location));
/*     */       
/*     */       b++; }
/*     */     
/* 141 */     GL20C.glUseProgram(this.program);
/*     */ 
/*     */     
/* 144 */     IntBuffer props = BufferUtils.createIntBuffer(1);
/* 145 */     IntBuffer params = BufferUtils.createIntBuffer(1);
/* 146 */     props.put(0, 37634);
/*     */ 
/*     */     
/* 149 */     int coinBufferResourceIndex = GL43C.glGetProgramResourceIndex(this.program, 37606, "CoinBuffer");
/* 150 */     int wallBufferResourceIndex = GL43C.glGetProgramResourceIndex(this.program, 37606, "WallBuffer");
/*     */ 
/*     */     
/* 153 */     GL43C.glGetProgramResourceiv(this.program, 37606, coinBufferResourceIndex, props, null, params);
/* 154 */     this.coinBuffer = params.get(0);
/*     */ 
/*     */     
/* 157 */     GL43C.glGetProgramResourceiv(this.program, 37606, wallBufferResourceIndex, props, null, params);
/* 158 */     this.wallBuffer = params.get(0);
/*     */     
/* 160 */     GL20C.glUseProgram(0);
/*     */   }
/*     */ 
/*     */ 
/*     */   
/*     */   public void loadUniformData(Dimension size, float globalTime, Camera camera, World world) {
/* 166 */     if (world.updateCoinTextureData()) {
/*     */       
/* 168 */       GL15C.glBindBuffer(34962, this.coinSsbo);
/* 169 */       GL15C.glBufferData(34962, world.getCoinTextureData(), 35044);
/* 170 */       GL15C.glBindBuffer(34962, 0);
/*     */     } 
/*     */ 
/*     */     
/* 174 */     if (world.updateWallBufferData()) {
/*     */       
/* 176 */       GL15C.glBindBuffer(34962, this.wallSsbo);
/* 177 */       GL15C.glBufferData(34962, world.getWallBufferData(), 35044);
/* 178 */       GL15C.glBindBuffer(34962, 0);
/*     */     } 
/*     */ 
/*     */ 
/*     */ 
/*     */     
/* 184 */     GL20C.glUniform2f(((Integer)this.uniformLocations.get(UNIFORM_NAMES[0])).intValue(), size.width, size.height);
/* 185 */     GL20C.glUniform1f(((Integer)this.uniformLocations.get(UNIFORM_NAMES[1])).intValue(), globalTime);
/*     */     
/* 187 */     GL20C.glUniform2f(((Integer)this.uniformLocations.get(UNIFORM_NAMES[2])).intValue(), camera.getCameraRotationX(), camera.getCameraRotationY());
/* 188 */     GL20C.glUniform1f(((Integer)this.uniformLocations.get(UNIFORM_NAMES[3])).intValue(), camera.getCameraZoom());
/*     */     
/* 190 */     GL13C.glActiveTexture(33984);
/* 191 */     GL11C.glBindTexture(3553, this.worldTexture.getHandle());
/* 192 */     GL20C.glUniform1i(((Integer)this.uniformLocations.get(UNIFORM_NAMES[4])).intValue(), 0);
/*     */     
/* 194 */     GL13C.glActiveTexture(33985);
/* 195 */     GL11C.glBindTexture(3553, this.environmentTexture.getHandle());
/* 196 */     GL20C.glUniform1i(((Integer)this.uniformLocations.get(UNIFORM_NAMES[5])).intValue(), 1);
/*     */     
/* 198 */     GL13C.glActiveTexture(33986);
/* 199 */     GL11C.glBindTexture(3553, this.coinTexture.getHandle());
/* 200 */     GL20C.glUniform1i(((Integer)this.uniformLocations.get(UNIFORM_NAMES[6])).intValue(), 2);
/*     */     
/* 202 */     GL13C.glActiveTexture(33987);
/* 203 */     GL11C.glBindTexture(3553, this.testTexture.getHandle());
/* 204 */     GL20C.glUniform1i(GL20C.glGetUniformLocation(this.program, "testTexture"), 3);
/*     */     
/* 206 */     GL20C.glUniform1f(((Integer)this.uniformLocations.get(UNIFORM_NAMES[7])).intValue(), world.getWidth());
/* 207 */     GL20C.glUniform1f(((Integer)this.uniformLocations.get(UNIFORM_NAMES[8])).intValue(), world.getHeight());
/*     */     
/* 209 */     GL20C.glUniform1i(((Integer)this.uniformLocations.get(UNIFORM_NAMES[9])).intValue(), world.getDrawGytebotAsInt());
/*     */ 
/*     */     
/* 212 */     if (world.getGytebot() != null) {
/*     */       
/* 214 */       GL20C.glUniform2f(((Integer)this.uniformLocations.get(UNIFORM_NAMES[10])).intValue(), (world.getGytebot().getPosition()).x, (world.getGytebot().getPosition()).y);
/* 215 */       GL20C.glUniform1f(((Integer)this.uniformLocations.get(UNIFORM_NAMES[11])).intValue(), world.getGytebot().getViewAngle());
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public void bind() {
/* 225 */     super.bind();
/* 226 */     GL30C.glBindBufferBase(37074, this.coinBuffer, this.coinSsbo);
/* 227 */     GL30C.glBindBufferBase(37074, this.wallBuffer, this.wallSsbo);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public void unbind() {
/* 236 */     super.unbind();
/* 237 */     GL30C.glBindBufferBase(37074, this.coinBuffer, 0);
/* 238 */     GL30C.glBindBufferBase(37074, this.wallBuffer, 0);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public final void close() {
/* 246 */     super.close();
/*     */     
/* 248 */     GL15C.glDeleteBuffers(this.coinSsbo);
/* 249 */     GL15C.glDeleteBuffers(this.coinSsbo);
/*     */     
/* 251 */     this.worldTexture.close();
/* 252 */     this.environmentTexture.close();
/* 253 */     this.coinTexture.close();
/*     */   }
/*     */ }
