Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  GLSL Example(s) in Jogl  (Read 11923 times)
0 Members and 1 Guest are viewing this topic.
Offline kaffiene
« Posted 2010-02-07 21:11:38 »

Can anyone point me to (or supply!) an example of GLSL shaders in JOGL please?

Thanks in advance!
Offline mortennobel

Junior Newbie





« Reply #1 - Posted 2010-02-08 22:12:35 »

Here is a small example for JOGL 1.1.1. I hope this is enough to get you started.

I'm actually currently working on a blog-post about mandelbrot and JOGL (vs. pure Java) for my blog: http://blog.nobel-joergensen.com/
The blog post should be ready in about a week :-)

Kind regards,

Morten

import com.sun.opengl.util.Animator;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;

public class SimpleShaderExample extends GLCanvas implements GLEventListener {

   private boolean updateUniformVars = true;
   private int vertexShaderProgram;
   private int fragmentShaderProgram;
   private int shaderprogram;
   private final Animator animator = new Animator(this);
   private float x = -2;
   private float y = -2;
   private float height = 4;
   private float width = 4;
   private int iterations = 255;

   public SimpleShaderExample() {
      addGLEventListener(this);
        animator.start();
    }

    public void init(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();

        // Enable VSync
        gl.setSwapInterval(1);
      gl.glShadeModel(GL.GL_FLAT);
      try {
         attachShaders(gl);
      } catch (Exception e) {
         e.printStackTrace();
      }
   }

   private String[] loadShaderSrc(String name){
      StringBuilder sb = new StringBuilder();
      try{
         InputStream is = getClass().getResourceAsStream(name);
         BufferedReader br = new BufferedReader(new InputStreamReader(is));
         String line;
         while ((line = br.readLine())!=null){
            sb.append(line);
            sb.append('\n');
         }
         is.close();
      }
      catch (Exception e){
         e.printStackTrace();
      }
      System.out.println("Shader is "+sb.toString());
      return new String[]{sb.toString()};
   }

   private void attachShaders(GL gl) throws Exception {
      vertexShaderProgram = gl.glCreateShader(GL.GL_VERTEX_SHADER);
      fragmentShaderProgram = gl.glCreateShader(GL.GL_FRAGMENT_SHADER);

      String[] vsrc = new String[]{"uniform float mandel_x;\n" +
            "uniform float mandel_y;\n" +
            "uniform float mandel_width;\n" +
            "uniform float mandel_height; \n" +
            "uniform float mandel_iterations;\n" +
            "\n" +
            "void main()\n" +
            "{\n" +
            "\tgl_TexCoord[0] = gl_MultiTexCoord0;\n" +
            "\tgl_Position = ftransform();\n" +
            "}"};
      gl.glShaderSource(vertexShaderProgram, 1, vsrc, null, 0);
      gl.glCompileShader(vertexShaderProgram);

      String[] fsrc = new String[]{"uniform float mandel_x;\n" +
            "uniform float mandel_y;\n" +
            "uniform float mandel_width;\n" +
            "uniform float mandel_height; \n" +
            "uniform float mandel_iterations;\n" +
            "\n" +
            "float calculateMandelbrotIterations(float x, float y) {\n" +
            "\tfloat xx = 0.0;\n" +
            "    float yy = 0.0;\n" +
            "    float iter = 0.0;\n" +
            "    while (xx * xx + yy * yy <= 4.0 && iter<mandel_iterations) {\n" +
            "        float temp = xx*xx - yy*yy + x;\n" +
            "        yy = 2.0*xx*yy + y;\n" +
            "\n" +
            "        xx = temp;\n" +
            "\n" +
            "        iter ++;\n" +
            "    }\n" +
            "    return iter;\n" +
            "}\n" +
            "\n" +
            "vec4 getColor(float iterations) {\n" +
            "\tfloat oneThirdMandelIterations = mandel_iterations/3.0;\n" +
            "\tfloat green = iterations/oneThirdMandelIterations;\n" +
            "\tfloat blue = (iterations-1.3*oneThirdMandelIterations)/oneThirdMandelIterations;\n" +
            "\tfloat red = (iterations-2.2*oneThirdMandelIterations)/oneThirdMandelIterations;\n" +
            "\treturn vec4(red,green,blue,1.0);\n" +
            "}\n" +
            "\n" +
            "void main()\n" +
            "{\n" +
            "\tfloat x = mandel_x+gl_TexCoord[0].x*mandel_width;\n" +
            "\tfloat y = mandel_y+gl_TexCoord[0].y*mandel_height;\n" +
            "\tfloat iterations = calculateMandelbrotIterations(x,y);\n" +
            "\tgl_FragColor = getColor(iterations);\n" +
            "}"};
      gl.glShaderSource(fragmentShaderProgram, 1, fsrc, null, 0);
      gl.glCompileShader(fragmentShaderProgram);

      shaderprogram = gl.glCreateProgram();
      gl.glAttachShader(shaderprogram, vertexShaderProgram);
      gl.glAttachShader(shaderprogram, fragmentShaderProgram);
      gl.glLinkProgram(shaderprogram);
      gl.glValidateProgram(shaderprogram);
      IntBuffer intBuffer = IntBuffer.allocate(1);
      gl.glGetProgramiv(shaderprogram, GL.GL_LINK_STATUS,intBuffer);
      if (intBuffer.get(0)!=1){
         gl.glGetProgramiv(shaderprogram, GL.GL_INFO_LOG_LENGTH,intBuffer);
         int size = intBuffer.get(0);
         System.err.println("Program link error: ");
         if (size>0){
            ByteBuffer byteBuffer = ByteBuffer.allocate(size);
            gl.glGetProgramInfoLog(shaderprogram, size, intBuffer, byteBuffer);
            for (byte b:byteBuffer.array()){
               System.err.print((char)b);
            }
         } else {
            System.out.println("Unknown");
         }
         System.exit(1);
      }
      gl.glUseProgram(shaderprogram);
   }

   public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
        GL gl = drawable.getGL();
        GLU glu = new GLU();

        if (height <= 0) { // avoid a divide by zero error!

            height = 1;
        }
        final float h = (float) width / (float) height;
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        glu.gluOrtho2D(0, 1, 0, 1);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
    }



    public void display(GLAutoDrawable drawable) {
        GL gl = drawable.getGL();

      if (updateUniformVars){
         updateUniformVars(gl);
      }

        // Reset the current matrix to the "identity"
        gl.glLoadIdentity();

        // Draw A Quad
        gl.glBegin(GL.GL_QUADS);
      {
         gl.glTexCoord2f(0.0f, 1.0f);
         gl.glVertex3f(0.0f, 1.0f, 1.0f);  // Top Left
         gl.glTexCoord2f(1.0f, 1.0f);
         gl.glVertex3f(1.0f, 1.0f, 1.0f);   // Top Right
         gl.glTexCoord2f(1.0f, 0.0f);
         gl.glVertex3f(1.0f, 0.0f, 1.0f);  // Bottom Right
         gl.glTexCoord2f(0.0f, 0.0f);
         gl.glVertex3f(0.0f, 0.0f, 1.0f); // Bottom Left
      }
      // Done Drawing The Quad
        gl.glEnd();

        // Flush all drawing operations to the graphics card
        gl.glFlush();
    }

   private void updateUniformVars(GL gl) {
      int mandel_x = gl.glGetUniformLocation(shaderprogram, "mandel_x");
      int mandel_y = gl.glGetUniformLocation(shaderprogram, "mandel_y");
      int mandel_width = gl.glGetUniformLocation(shaderprogram, "mandel_width");
      int mandel_height = gl.glGetUniformLocation(shaderprogram, "mandel_height");
      int mandel_iterations = gl.glGetUniformLocation(shaderprogram, "mandel_iterations");
      assert(mandel_x!=-1);
      assert(mandel_y!=-1);
      assert(mandel_width!=-1);
      assert(mandel_height!=-1);
      assert(mandel_iterations!=-1);

      gl.glUniform1f(mandel_x, x);
      gl.glUniform1f(mandel_y, y);
      gl.glUniform1f(mandel_width, width);
      gl.glUniform1f(mandel_height, height);
      gl.glUniform1f(mandel_iterations, iterations);

   }

   public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
    }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            JFrame jf = new JFrame();
            jf.setSize(800,800);
            jf.getContentPane().setLayout(new BorderLayout());
            jf.getContentPane().add(new SimpleShaderExample(), BorderLayout.CENTER);
            jf.setVisible(true);
         }
      });

   }
}
Offline kaffiene
« Reply #2 - Posted 2010-02-08 23:35:14 »

Thank you so much!  Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline mortennobel

Junior Newbie





« Reply #3 - Posted 2010-02-13 19:15:18 »

I have now finished my blog post (or at least the first part) - with a few improvements in the GLSL code. You can find it here:

http://blog.nobel-joergensen.com/2010/02/13/real-time-mandelbrot-in-java-part-1/

Kind regards,
Morten
Offline gouessej
« Reply #4 - Posted 2010-02-17 10:44:11 »

I have now finished my blog post (or at least the first part) - with a few improvements in the GLSL code. You can find it here:

http://blog.nobel-joergensen.com/2010/02/13/real-time-mandelbrot-in-java-part-1/

Kind regards,
Morten
Thanks. I advise you to have a look at JOCL/JOGL 2 examples that Mickael Bien wrote, some of them deal with Mandelbrot, it might be interesting for you.

Julien Gouesse | Personal blog | Website | Jogamp
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (582 views)
2019-09-04 15:33:17

hadezbladez (5517 views)
2018-11-16 13:46:03

hadezbladez (2403 views)
2018-11-16 13:41:33

hadezbladez (5779 views)
2018-11-16 13:35:35

hadezbladez (1226 views)
2018-11-16 13:32:03

EgonOlsen (4664 views)
2018-06-10 19:43:48

EgonOlsen (5684 views)
2018-06-10 19:43:44

EgonOlsen (3199 views)
2018-06-10 19:43:20

DesertCoockie (4096 views)
2018-05-13 18:23:11

nelsongames (5116 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
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!