如何从 Renderer 类 Opengles (android, java) 访问文件 [英] How to access files form the Renderer class Opengles (android, java)
问题描述
我正在为 android 制作一个 opengles 应用程序,我需要访问应用程序中的文件.我最近了解到我可以使用 Activity.getAssets().open(); 打开 assets 文件夹中的文件;方法.但是,此方法通过执行 MainActivity.getAssets().open(); 需要 mainActivity(扩展 Activity).
I am making an opengles application for android and I need to access files in the app. I recently learned that I could open files in the assets folder with the Activity.getAssets().open(); method. However this method requirers the mainActivity (extending Activity) by doing MainActivity.getAssets().open();.
到目前为止一切正常.但是,为了让 Opengles 工作,我需要提供 MainActivity.setRenderer();使用 GLSurfaceView.Renderer(我称之为 Renderer)的实现,它将容纳主循环.
Up to this point it was fine. However, in order for Opengles to work, I need to feed MainActivity.setRenderer(); with an implementation of GLSurfaceView.Renderer (that I call Renderer) which will house the main loop.
我的问题是,每当我尝试在任何在 Renderer 方法中直接或间接声明的方法中使用 MainActivity 执行某些操作时,它似乎都不起作用.如果有人可以帮我找到解决方案,或者知道如何访问 Renderer 类中的文件而不预先加载它们,请帮助我!
My problem is, whenever I try to do something with MainActivity in any method that is directly or indirectly declared in a method of Renderer, it seems not to work. If anyone can help me find a solution for this or knows a way to access files within the Renderer class without loading them all beforehand please help me!
附言当我在普通班级中提供 MainActivity 时它起作用
P.S. it works when I feed MainActivity in a normal class
示例:
//MainActivity
public class MainActivity extends Activity {
private renderer surface;
private GLSurfaceView surfaceView;
private int STORAGE_PERMISSION_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
surfaceView = new GLSurfaceView(this);
surfaceView.setEGLContextClientVersion(2);
surface = new Renderer(this);
surfaceView.setRenderer(surface);
setContentView(surfaceView);
NormalClass class = new NormalClass(this); //this works
}
//Renderer class
public class renderer implements GLSurfaceView.Renderer{
renderer(MainActivity main){
}
public void onSurfaceCreated(GL10 gl, EGLConfig config){
NormalClass class = new NormalClass(main); //this doesn't work
}
public void onSurfaceChanged(GL10 gl, int width, int height){
}
public void onDrawFrame(GL10 gl){
}
private void render(Object object){
}
}
//NormalClass
public class NormalClass{
NormalClass(MainActivity main){
InpuStream in = main.getAssets().open("someFile.txt"); //this just doesn't work when called from Renderer
Toast.makeTest(main.getApplicationContext(),"test",Toast.LENGTH_SHORT).show(); //this actually crashes the app when called from Renderer
}
}
经过进一步研究,似乎渲染器自动在单独的线程上运行.我认为这就是问题所在.我不知道如何处理线程思想,有人可以帮助我吗?
edit: Upon futher resaerches it seems like Renderer is automatically run on a seperate thread. I think this is the issue. I don't know how to handle threads thought, could someone help me?
推荐答案
事实证明这里根本没有问题!请参阅此处了解新问题:GLES20.glShaderSource 什么都不做
Turns out there is no problem here at all! See here for the new problem: GLES20.glShaderSource does nothing
这篇关于如何从 Renderer 类 Opengles (android, java) 访问文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!