Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (578)
games submitted by our members
Games in WIP (499)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: Gef (posted 2014-02-01 03:46:15, viewed 118 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292  
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.ARBShaderObjects.*;
import static org.lwjgl.opengl.ARBVertexShader.*;
import static org.lwjgl.opengl.ARBFragmentShader.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.HashMap;

import org.lwjgl.BufferUtils;

import com.productions.gef.blockyworld.system.Configuration;
import com.productions.gef.blockyworld.toolbox.Debug;

public class Shader3D {

   private final String _vertexName;
   private final String _fragmentName;
   private String
         _vertexSource = "",
         _fragmentSource = "";
   private int
         program = 0,
         vertexShader = 0,
         fragmentShader = 0;
   HashMap<String, Integer> vAttributs = new HashMap<String, Integer>();

   public Shader3D(String name) {
      this(name, name, null);
   }

   public Shader3D(String vertexName, String fragmentName) {
      this(vertexName, fragmentName, null);
   }

   public Shader3D(String name, String[] directives) {
      this(name, name, directives);
   }

   public Shader3D(String vertexName, String fragmentName, String[] directives) {
      _vertexName = vertexName;
      _fragmentName = fragmentName;
      if (Debug.DEBUG_SHADER)
         Debug.println("Création du shader [" + _fragmentName + "] :");
      addVertexSourceFile(vertexName, directives);
      addFragmentSourceFile(fragmentName, directives);
      validate();
   }

   public void dispose() {
      if (Debug.DEBUG_SHADER)
         Debug.print("Nettoyage du shader [" + _fragmentName + "] : ");
      try {
         glDetachObjectARB(program, vertexShader);
         glDeleteObjectARB(vertexShader);
         glDetachObjectARB(program, fragmentShader);
         glDeleteObjectARB(fragmentShader);
         glDeleteObjectARB(program);
         if (Debug.DEBUG_SHADER)
            Debug.println("Ok");
      } catch (Exception e) {
         if (Debug.DEBUG_SHADER)
            Debug.println_err("Erreur");
      }
   }

   private String addSourceFile(String fileName, String[] directives) {
      String source = "";
      String readLine;

      // Ajout d'un fichier source
      try {
         InputStream is = this.getClass().getResourceAsStream(Configuration.SHADERS_PATH + fileName);
         BufferedReader br = new BufferedReader(new InputStreamReader(is));

         // Ajout des directives de compilation
         if (directives != null) {
            for (int i = 0; i < directives.length; i++)
               if (directives[i].compareTo("") != 0)
                  source += "#define " + directives[i] + "\n";
         }

         // Creation de la source
         while ((readLine = br.readLine()) != null) {
            source += readLine + "\n";
         }
         if (Debug.DEBUG_SHADER)
            Debug.println("OK");
      } catch (Exception e) {
         if (Debug.DEBUG_SHADER) {
            Debug.println("ERREUR");
            // Debug.println_err(e.getMessage());
         }
      }

      return source;
   }

   private void addVertexSourceFile(String fileName, String[] directives) {
      if (Debug.DEBUG_SHADER)
         Debug.print(Debug.TAB + "Ajout d'un vertex source : ");
      _vertexSource += addSourceFile(fileName + ".vert", directives);
   }

   private void addFragmentSourceFile(String fileName, String[] directives) {
      if (Debug.DEBUG_SHADER)
         Debug.print(Debug.TAB + "Ajout d'un fragment source : ");
      _fragmentSource += addSourceFile(fileName + ".frag", directives);
   }

   private void validate() {
      try {

         // Compilation du fichier vert
         vertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
         glShaderSourceARB(vertexShader, _vertexSource);
         glCompileShaderARB(vertexShader);
         analyseInfoLog(vertexShader, "Compilation du vertex source " + _vertexName, GL_OBJECT_COMPILE_STATUS_ARB);

         // Compilation du fichier frag
         fragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
         glShaderSourceARB(fragmentShader, _fragmentSource);
         glCompileShaderARB(fragmentShader);
         analyseInfoLog(fragmentShader, "Compilation du fragment source " + _fragmentName, GL_OBJECT_COMPILE_STATUS_ARB);

         // Attachement et linkage des sources
         program = glCreateProgramObjectARB();
         glAttachObjectARB(program, vertexShader);
         glAttachObjectARB(program, fragmentShader);
         glLinkProgramARB(program);
         analyseInfoLog(program, "Link des programs", GL_OBJECT_LINK_STATUS_ARB);

         // Validation du programme
         glValidateProgramARB(program);
         analyseInfoLog(program, "Validation du Shader", GL_OBJECT_VALIDATE_STATUS_ARB);

         if (Debug.DEBUG_SHADER)
            Debug.println("Validation du shader : OK");

      } catch (Exception ex) {
         if (Debug.DEBUG_SHADER)
            Debug.println_err(ex.getMessage() + " : ERREUR");
         glDetachObjectARB(program, vertexShader);
         glDeleteObjectARB(vertexShader);
         glDetachObjectARB(program, fragmentShader);
         glDeleteObjectARB(fragmentShader);
         glDeleteObjectARB(program);
         program = 0;
      }

   }

   private void analyseInfoLog(int object, String subject, int status) throws Exception {

      if (glGetObjectParameteriARB(object, status) == GL_FALSE) {

         // Recuperation du logFile
         String errorLog = glGetInfoLogARB(object, glGetObjectParameteriARB(object, GL_OBJECT_INFO_LOG_LENGTH_ARB));

         // Remonte l'erreur
         throw new RuntimeException(subject + "\n" + errorLog);
      }
   }

   public void bind() {
      glUseProgramObjectARB(program);
   }

   public void unbind() {
      glUseProgramObjectARB(0);
   }

   public void addUniform(String name) {
      int v = glGetUniformLocationARB(program, name);
      if (v != -1)
         vAttributs.put(name, v);
      if (Debug.DEBUG_SHADER)
         Debug.println("Uniform [" + name + "] : " + v);
   }

   public void addUniform(String name, int textureUnit) {
      int v = glGetUniformLocationARB(program, name);
      if (v != -1) {
         vAttributs.put(name, v);
         bind();
         setUniform(name, textureUnit);
         unbind();
      }
      if (Debug.DEBUG_SHADER)
         Debug.println("Uniform [" + name + "] : " + v);
   }

   private int getUniform(String name) {
      if (vAttributs.containsKey(name))
         return vAttributs.get(name);
      else {
         if (Debug.DEBUG_SHADER)
            Debug.println("Shader " + _fragmentName + " : " + name + " non declare !");
         return -1;
      }
   }

   public void setUniform(String name, int value) {
      int v = getUniform(name);
      if (v != -1)
         glUniform1iARB(v, value);
   }

   public void setUniform(String name, int[] values) {
      int v = getUniform(name);
      IntBuffer buffer = BufferUtils.createIntBuffer(values.length);
      buffer.put(values);
      buffer.flip();
      if (v != -1)
         glUniform1ARB(v, buffer);
   }

   public void setUniform(String name, float value) {
      int v = getUniform(name);
      if (v != -1)
         glUniform1fARB(v, value);
   }

   private FloatBuffer getFloatBuffer(float[] values) {
      FloatBuffer fb = BufferUtils.createFloatBuffer(values.length);
      fb.put(values);
      fb.flip();
      return fb;
   }

   public void setUniform(String name, float[] values) {
      int v = getUniform(name);
      if (v != -1)
         glUniform1ARB(v, getFloatBuffer(values));
   }

   public void setUniform2fv(String name, float[] values) {
      int v = getUniform(name);
      if (v != -1)
         glUniform2ARB(v, getFloatBuffer(values));
   }

   public void setUniform3fv(String name, float[] values) {
      int v = getUniform(name);
      if (v != -1)
         glUniform3ARB(v, getFloatBuffer(values));
   }

   public void setUniform4fv(String name, float[] values) {
      int v = getUniform(name);
      if (v != -1)
         glUniform4ARB(v, getFloatBuffer(values));
   }

   public void setUniform(String name, boolean value) {
      int v = getUniform(name);
      if (v != -1)
         glUniform1iARB(v, value ? -1 : 0);
   }

   public void setUniform(String name, float v0, float v1) {
      int v = getUniform(name);
      if (v != -1)
         glUniform2fARB(v, v0, v1);
   }

   public void setUniformMatrix3(String name, float[] mat, boolean transpose) {
      int v = getUniform(name);
      if (v != -1)
         glUniformMatrix3ARB(v, transpose, getFloatBuffer(mat));
   }

   public void setUniformMatrix3(String name, FloatBuffer mat, boolean transpose) {
      int v = getUniform(name);
      if (v != -1)
         glUniformMatrix3ARB(v, transpose, mat);
   }

   public void setUniformMatrix4(String name, float[] mat, boolean transpose) {
      int v = getUniform(name);
      if (v != -1)
         glUniformMatrix4ARB(v, transpose, getFloatBuffer(mat));
   }

   public void setUniformMatrix4(String name, FloatBuffer mat, boolean transpose) {
      int v = getUniform(name);
      if (v != -1)
         glUniformMatrix4ARB(v, transpose, mat);
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (30 views)
2014-04-15 18:08:23

BurntPizza (27 views)
2014-04-15 03:46:01

UprightPath (42 views)
2014-04-14 17:39:50

UprightPath (24 views)
2014-04-14 17:35:47

Porlus (41 views)
2014-04-14 15:48:38

tom_mai78101 (63 views)
2014-04-10 04:04:31

BurntPizza (123 views)
2014-04-08 23:06:04

tom_mai78101 (222 views)
2014-04-05 13:34:39

trollwarrior1 (189 views)
2014-04-04 12:06:45

CJLetsGame (198 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!