I’m trying to get an Opengl es app working and I’m currently working on the shaders. To do that I need to open tow text files situated in src/shaders/ Here is my code:
private static int LoadShader(String filepath, int type){ File file = new File(filepath); StringBuilder codebuilder = new StringBuilder(); try{ BufferedReader reader = new BufferedReader(new FileReader(file)); String line; while((line = reader.readLine())!=null){ codebuilder.append(line); codebuilder.append("n"); } reader.close(); }catch(IOException e){ System.err.println("could not read" + filepath); e.printStackTrace(); System.exit(-1); } //And some other code }
The inputs are String a = "src/shaders/vertexshader.txt", int GLES20.GL_VERTEX_SHADER
and String b = "src/shaders/fragmentshader.txt", int GLES20.GL_FRAGMENT_SHADER
.
The app builds correctly, but it Instantly crashes because of the System.exit(-1);
(When I remove it it doesn’t crash anymore).
The file path isn’t the problem, I checked it multiple times. The worse is that I don’t have access to any form of backdoor or log communication with my program, because of the only compiler that I can use (java N-IDE for android). Please help, this project is driving me crazy.
//Edit
I am new to java and programing in general and I come more from a c++ background, so try catch is relatively new to me. So I don’t fully understand the exception handling, which was copied from a tutorial on opengl.
The System.err.println("could not read" + filepath);
And e.printStackTrace();
Actually aren’t working for me and I just didn’t bother to remove them.
Also here’s the implementation of all this:
//Constructor of abstract class //Loadshader is in the same class public ShaderBase(String vertexfile, String fragmentfile){ vertexshaderID = LoadShader(vertexfile,GLES20.GL_VERTEX_SHADER); fragmentshaderID = LoadShader(fragmentfile,GLES20.GL_FRAGMENT_SHADER); //And other code }
//Implementation of abstract class private static final String VERT_FILE = "src/shaders/vertexshader.txt"; private static final String FRAG_FILE = "src/shaders/fragmentshader.txt"; public StaticShader(){ super(VERT_FILE,FRAG_FILE); } //And other code }
Also, here’s my file system: My file system
The code is mainly a mismatch of tutorials on opengl (https://youtu.be/VS8wlS9hF8E), opengl es (https://youtu.be/QcmJQ2_kpks) and some working examples I found online (https://www.learnopengles.com/android-lesson-one-getting-started/) So if you have some good tutorial suggestions, please tell me.
Advertisement
Answer
I have found the answer: the file system before compiling and after compiling is not the same. By doing MainActivity.getAssets().open("something.txt");
, I was able to open files situated in the apps/apps/src/main/assets folder.