0

I am trying to build a game engine from scratch by following various tutorials using Java, LWJGL, and OpenGL. My current plan, is to follow each tutorial, generating the code they teach, until I have working code. Step 2 is to re-organize, rename, document, and otherwise move stuff around until it matches my specific coding style.

Each new subject, is then packaged up nice and neat in its own project, which is then turned into a library .jar file.

That way I can create a new program, add a few specific library .jar files for "2D Text" or "particle effects" to add new features to my game as needed without a ton of copy and pasting between projects, or importing a super large library into a program that isn't going to use all aspects of it.

Now, here is the part I can't figure out. Most OpenGL rendering with the programmable pipeline uses shaders. If I locate the shaders within my new program, everything is fine. I reference the shaders using the "src/Shaders/shaderEntity.vert" file path.

However, If I want everything nice and neat, and reusable, I should locate those shaders within the library .jar file with the rest of the classes for that feature.

How do I reference a shader file, within another .jar file? The library .jar file itself is located in "dist/lib/myJarFile.jar" and would be in the "/Shaders/shaderEntity.vert" location within the .jar file.

Or is this a bad idea and I should continue locating my shaders within the new programs?

Zaanzabar
  • 28
  • 1
  • 1
  • 10

1 Answers1

1

A shader doesn't have to be read from a file. OpenGL, or more specifically glLoadShader takes the source code of your shader as a string.

I'm don't know much about .jar files (and have limited experience with the rest of Java) but the goal is to store it in the .jar in some way that you can read it out as a string to pass to OpenGL. That might mean changing your class to take in the source code string and loading that string from the .jar elsewhere, or taking in 2 paths (or one string with a divider, e.g. dist/lib/myJarFile.jar:/Shaders/shaderEntity.vert) that provide the exact path to the file, and the class will then open up the file within the .jar and read from it.

Robert Rouhani
  • 14,512
  • 6
  • 44
  • 59
  • So, you are saying I should be able to reference my shader just with "/Shaders/shaderEntity.vert" only? .... Even if that doesn't work, I think you may have given me the idea I needed. If the shader isn't likely to change, I could hardcode the shader into a string within the program. (maybe a bad idea, but would work) – Zaanzabar Dec 01 '15 at 04:20
  • If you already know the path to the jar, then yes. A quick Google search brought this up for reading files from inside a jar, you might find it useful: `InputStream input = getClass().getResourceAsStream("/classpath/to/my/file");`. From there you would read the stream out as a string in the same way this question asks: http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string and then call `glLoadShader` with that string. Hardcoding the shader in the program also works, though harder to edit (especially if you have an editor with GLSL syntax highlighting) – Robert Rouhani Dec 01 '15 at 04:50
  • Problem is, I have tried everything I can think of for the "/classpath/to/my/file" path and nothing works. That is the core of the problem, what string do I use for the path? – Zaanzabar Dec 01 '15 at 17:34
  • I tried many different things, until finally I got it to work. Functionally it functions, but it is not ... distributable. By using `MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();` I was able to get the absolute path of the class file the code is run within. Then add on the path to the shader, and it works. HOWEVER, that gives the path to the Library Project version, Not the one in the library.jar file (which is the one that would be distributed with the program). So, working, but not ... solved. – Zaanzabar Dec 01 '15 at 17:36