【发布时间】:2025-12-24 21:20:08
【问题描述】:
我在更改 GLSurfaceView 中的渲染网格时遇到了麻烦,更准确地说是在 Renderer 中,我猜在 Renderer 类的工作流程中有些东西我不理解。
所以,让我们考虑一下:
public class MyRenderer extends GLSurfaceView.Renderer{
private Mesh aMesh;
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config){
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
aMesh = new Mesh("TestMesh1");
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
...Creating viewport...
}
@Override
public void onDrawFrame(GL10 gl) {
GLES20.glClear(gl.GL_COLOR_BUFFER_BIT);
...all Maths to create mvpMatrix...
aMesh.draw(mvpMatrix);
}
}
所以这是完美的工作,aMesh 以所有良好的设置显示在屏幕上。现在,我想在用户按下按钮时更改此网格,并刷新我的渲染器,我在渲染器中创建了一个特定的方法,如下所示:
public class MyRenderer extends GLSurfaceView.Renderer{
private Mesh aMesh;
public void changeMesh(String newMeshName){
GLES20.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);
....
aMesh=new Mesh(newMeshName);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config){
....
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
...
}
@Override
public void onDrawFrame(GL10 gl) {
aMesh.draw(mvpMatrix);
}
}
所以在我看来,调用 changeMesh 会使 onDrawFrame 方法用新模型重绘 aMesh,对吧? 我的问题是结果是一个空的渲染器。它甚至没有崩溃。当我调用 changeMesh() 时,先前显示良好的网格消失(如预期的那样),但新的网格未绘制。
所以我想知道,是否需要创建一个新的渲染器(看起来有点重)?有没有办法手动询问 OnSurfaceCreated 的通过?我对这个错误有点迷茫,因为它有点出乎意料。
【问题讨论】:
-
您所描述的代码应该可以工作。在您更改网格并检查正在绘制的内容后,我将在调试器中逐步执行绘图调用。
标签: android rendering opengl-es-2.0 surfaceview renderer