Where to store shader code in an Android application

I start with OpenGL ES2.0 on Android (5.0.1), a level 19 API. Where to store shader code? The first example encodes a shader directly as a string.

I would like to have shader code in a separate file for better usability. What is the best practice for storing and loading vertex and fragment shaders?

+6
source share
1 answer

There are two main options:

  • Store them as text files in the assets folder of your project. To load a shader:

    • Get the AssetManager using the getAssets() method of the context.
    • Call open() in the AssetManager , passing the name of the shader file. This gives you an InputStream .
    • Read the shader code from InputStream and save it in String .
    • Call close() on an InputStream .
  • Save them in the res/raw folder of your project. To load a shader:

    • Get Resources using the getResources() method of the context.
    • Call openRawResource() on Resources , passing the resource identifier ( R.raw.<name> ). This gives you an InputStream .
    • (as mentioned above)
    • (as mentioned above)

I do not believe that there is a big reason to prefer one over the other. The main difference is that you use the file name to access the assets, while you use the automatically assigned resource identifier for the resources. This is a preference question that you like best.

+9
source

Source: https://habr.com/ru/post/979961/


All Articles