Slick Forums

Discuss the Slick 2D Library
It is currently Tue May 21, 2013 8:44 pm

All times are UTC




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: Slick Shader
PostPosted: Sun Jul 29, 2007 7:43 pm 
Offline
Regular

Joined: Tue Jun 19, 2007 7:35 am
Posts: 233
Location: Germany
yeahaaaa
i got this nasty shader stuff to run. i've done just a simple vertex shader, which transforms the vertices (basic fixed pipeline functionality) and a simple fragment (pixel) shader which renders the given texture with a simple color addition (rgba: +0.5).

ScreenShot with shader source


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 30, 2007 7:56 am 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
Nice!

Nothing to do with Slick tho is it?

Kev


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 30, 2007 8:24 am 
Offline
Regular

Joined: Tue Jun 19, 2007 7:35 am
Posts: 233
Location: Germany
i use slick for everything :D (rendering, logical updates, input, etc) the shader class is a simple "extension", well currently a dirty little hack, for slick.
the shader class wrapps the rendering function from slicks image class. shader stuff (loading / activation) is done via lwjgl, so no extra lib is required for this.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 30, 2007 10:01 am 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
K, I've just been trying to think of a tidy way to add shader support without breaking anyone without shaders. Some sort of effects system as mentioned before is what's needed, but I'm looking for a clean solution.

Kev


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 30, 2007 11:36 am 
Offline
Regular

Joined: Tue Jun 19, 2007 7:35 am
Posts: 233
Location: Germany
that shouldn't be a problem. there are some OpenGL / lwjgl functions which can check the hardware configuration for supported shader functionality, e.g. ARBShaderObjects.glSupportedBlaSomething(GL_SOME_MIGHTY_VARIABLE).

another solution would be a shader class, which handles shader rendering. if anyone want use shaders, he / she can use this special way of rendering. (my approach)

i can give you some source code, for a little view inside my shader stuff, if you want.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 30, 2007 11:46 am 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
The source would be great.

However, the issue for me isn't how to detect whether there is shader support - seems pretty trivial. It's how to provide a common api that can do lots of different effects, with different implementations for different hardware and to provide each of these.

I'm not keen on someone using an effect that only works on some systems. Has to be at least a similar effect for low end machines.

Kev


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 30, 2007 12:00 pm 
Offline
Regular

Joined: Tue Jun 19, 2007 7:35 am
Posts: 233
Location: Germany
Quote:
However, the issue for me isn't how to detect whether there is shader support - seems pretty trivial. It's how to provide a common api that can do lots of different effects, with different implementations for different hardware and to provide each of these.

sry, my fault :D
check your pm for the source code.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 02, 2007 5:25 pm 
Offline
User avatar

Joined: Tue Sep 11, 2007 5:52 pm
Posts: 2
Looks awesome, been looking into this myself. But decided to get more into slickset as a whole first.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 13, 2007 6:50 pm 
Offline

Joined: Mon Jul 23, 2007 10:37 am
Posts: 5
This would be an awesome feature, any progress been made on this since?

Not sure how this would work out in an automated fashion btw (same effect->different targets), then you'd need some sort of DSL & compiler for different backends? :s


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 09, 2008 6:20 pm 
Offline

Joined: Sun Feb 11, 2007 9:25 pm
Posts: 92
Whatever happened to this?

I am curious to how he put this together, as I am having problems getting shaders working.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 09, 2008 7:47 pm 
Offline
Regular

Joined: Tue Jun 19, 2007 7:35 am
Posts: 233
Location: Germany
hi,
what happend to this? ask my company... i've a lot of work to do there (args) :(

i can give you the source from this little sample, maybe this can be a starting point for you.

Program.java
Code:
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class Program extends BasicGame {

   
   private Image image = null;
   private Shader shader;

   public Program(String title) {
      super(title);
   }

   public void init(GameContainer container) throws SlickException {
      try {
         image = new Image("data/test.png");
      } catch (SlickException e) {
         e.printStackTrace();
      }
      
      shader = new Shader("data/shader/test");
   }

   public void update(GameContainer container, int delta) throws SlickException {
      
   }

   public void render(GameContainer container, Graphics g) throws SlickException {
      
      g.drawString("Fixed pipeline:", 10f, 216f);
      g.drawString("Shader:", 148f, 216f);
      
      g.drawImage(image, 10f, 236.0f);
      //g.drawImage(image, 148f, 236.0f);
      shader.render(image, 148f, 236f);
   }
   
   
   public static void main(String[] args) {
      try {
         AppGameContainer container = new AppGameContainer(new Program("Shader Test"));
         container.setDisplayMode(800, 600, false);
         container.start();
      } catch (SlickException e) {
         e.printStackTrace();
      }
   }
}


and...

Shader.java
Code:
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.ARBFragmentShader;
import org.lwjgl.opengl.ARBShaderObjects;
import org.lwjgl.opengl.ARBVertexShader;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Image;

public class Shader {

   protected int shaderProgramId = -1;
   protected int vertexShaderId = -1;
   protected int fragmentShaderId = -1;
   
   protected int locationBaseTexture = -1;
   
   /** Basic Shader object */
   public Shader (String filename) {
      
      vertexShaderId = ARBShaderObjects.glCreateShaderObjectARB(ARBVertexShader.GL_VERTEX_SHADER_ARB);
      ARBShaderObjects.glShaderSourceARB(vertexShaderId, getProgramCode(filename + ".vert"));
      ARBShaderObjects.glCompileShaderARB(vertexShaderId);
      
      fragmentShaderId = ARBShaderObjects.glCreateShaderObjectARB(ARBFragmentShader.GL_FRAGMENT_SHADER_ARB);
      ARBShaderObjects.glShaderSourceARB(fragmentShaderId, getProgramCode(filename + ".frag"));
      ARBShaderObjects.glCompileShaderARB(fragmentShaderId);
      
      shaderProgramId = ARBShaderObjects.glCreateProgramObjectARB();
      ARBShaderObjects.glAttachObjectARB(shaderProgramId, vertexShaderId);
      ARBShaderObjects.glAttachObjectARB(shaderProgramId, fragmentShaderId);
      
      ARBShaderObjects.glLinkProgramARB(shaderProgramId);
      
      ARBShaderObjects.glUseProgramObjectARB(shaderProgramId);
      locationBaseTexture = ARBShaderObjects.glGetUniformLocationARB(shaderProgramId, toByteString("baseTexture"));
      ARBShaderObjects.glUseProgramObjectARB(0);
   }
   
   public void render(Image image, float x, float y) {
      ARBShaderObjects.glUseProgramObjectARB(shaderProgramId);
      GL11.glBegin(GL11.GL_QUADS);
         image.drawEmbedded(x, y, image.getWidth(), image.getHeight());
      GL11.glEnd();
      ARBShaderObjects.glUseProgramObjectARB(0);
   }
   
   // helper function from: http://lwjgl.org/wiki/doku.php/lwjgl/tutorials/opengl/basicshaders
   private ByteBuffer getProgramCode(String filename) {
      
      InputStream fileInputStream = null;
      byte[] shaderCode = null;

      try
      {
         if (fileInputStream == null)
               fileInputStream = new FileInputStream(filename);
         DataInputStream dataStream = new DataInputStream(fileInputStream);
         dataStream.readFully(shaderCode = new byte[ fileInputStream.available() ]);
         fileInputStream.close();
         dataStream.close();
      }
      catch (Exception e)
      {
         System.out.println(e.getMessage());
      }
      ByteBuffer shaderPro = BufferUtils.createByteBuffer(shaderCode.length);

      shaderPro.put(shaderCode);
      shaderPro.flip();

      return shaderPro;
   }
   
   
   // see above
   private ByteBuffer toByteString(String str, boolean isNullTerminated) {
      int length = str.length();
      if (isNullTerminated)
         length++;
      ByteBuffer buff = BufferUtils.createByteBuffer(length);
      buff.put( str.getBytes() );

      if (isNullTerminated)
         buff.put( (byte)0 );

      buff.flip();
      return buff;
   }
   
   private ByteBuffer toByteString(String str) {
      return toByteString(str, true);
   }
   
}

_________________
Embero Games | Astroiz | ConK | From Outa Space


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 10, 2008 4:58 pm 
Offline

Joined: Sun Feb 11, 2007 9:25 pm
Posts: 92
Thanks a bunch!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group