Is there any lib that provides you with using shaders? What i meant is that I am making a game in java, pure java no libs but i want shaders in my game. So is it posible that you just add it to my game and render it?
Why would you need a library for that?
You can implement shaders in less than 50 lines of code.
The process for a shader: Generate an id for the shader, attach source, compile, check if successfully compiled(, if not log the shader’s infolog).
For a shader program: Generate an id, attach shaders, link, check if successfully linked(, if not print the program’s infolog), detach and delete shaders, optionally validate the program.
It might seem scary at first written out like that but it is actually pretty simple.
Sorry for not providong code examples but I’m currently on mobile.
Edit: Oh, you mean pure Java as Java2D? In that case you can’t use shaders since those are part of OpenGL.
Here’s an amazing tutorial on what shaders do:
And for loading shaders into java objects, I don’t mind you cheating off my implementation (It’s not finished, but its working):
https://github.com/ecumene-software/LibEcu/blob/master/source/ecumene/opengl/Shader.java
Some example code on how to load/use them from my object:
ambient = new Shader();
//Attaching source files:
ambient.attachShaderSource(new File("./assets/shaders/ambient.fs"), GL20.GL_FRAGMENT_SHADER);
ambient.attachShaderSource(new File("./assets/shaders/ambient.vs"), GL20.GL_VERTEX_SHADER);
//Using them with VAOs:
ambient.bindAttribute(0, "in_Position");
ambient.bindAttribute(1, "in_TextureUV");
ambient.bindAttribute(2, "in_Color");
ambient.link();
For rendering something with the shader:
ambient.bind();
//Uniforms can only be set after the shader is being used!!!
//Setting uniform matrices
ambient.setUniformM4("projMatrix", true, camera.getProjection());
ambient.setUniformM4("viewMatrix", true, camera.getView());
//For setting "samplers" with "texture units (multi-texture)" use glUniformi NOT glUniformf
ambient.setUniformi("diffuse", 0);
//For setting a uniform vec4 (vector 4 data being used as a color)
ambient.setUniformf("ambientLight", 1f, 1f, 1f, 1f);
renderMyObjects();
ambient.unbind();
EDIT: And if you have any small questions, just PM me, I’m fine with that.
I think he meant to use shaders with Java2D. It is IMPOSSIBLE.
Waiting for lib to transpile GLSL to BufferedImageOps. :
Not that it would be thoroughly useless or anything…
Your code goes a lot less farther than the usual shader code / program / state abstraction implemented in some engines and in JogAmp.
Shaders might be used with hardware accelerated implementations of Java2D based on OpenGL but not the build-in one. However, it defeats the purpose of something “pure Java, no libs”.
I meant i dont have any libs !!
That doesn’t make any sense at all.
If you want to make a game with pure Java, you have to use Java2D, which does not provide shaders.
You can’t have “shaders” with pure Java. Its impossible.
You will need to use LWJGL/JogAmp or (even better) libGDX.
These give you the ability to use shaders.
Have a nice day.
- Longor1996
It’s not that it’s impossible, it’s that OP has no clue what shaders are in the first place, as it doesn’t make sense to ask for shaders at the level of abstraction Java2D offers.
You can create your own GLSL parser for the files, and then draw everything (through a “fragment shader”) to a fullscreen BufferedImage. This isn’t hardware accelerated though, which is why you would use shaders in the first place.
Also, it would be pointless to do something like this, when you have perfectly good, hardware accelerated shaders provided by Opengl that require minimal effort to get working.