当形状不再出现在屏幕上时,我想将计数增加 1。问题是我需要知道我的形状的顶点在我的 Android 设备屏幕上的位置。我该如何计算这个?我发现的是绘制多边形时顶点的位置。
In glrenderer class
int count = 0;
int h, w;
public MyGLRenderer() {
h = MyGLSurfaceView.height;
w = MyGLSurfaceView.width;
}
public void onScreen(int a) {
for (int i = 0; i < star[a].vertices.length; i+=3) {
Log.i("vert", String.valueOf(star[a].vertices[i]));//i found where vertices are in relation to other vertices
float starW = (star[a].vertices[i] * (w/2)) + (w/2) * GL10.GL_PROJECTION * GL10.GL_MODELVIEW;//i need to know where vertices are on thhe screen
float starH = (star[a].vertices[i+1] * (h/2)) + (h/2) * GL10.GL_PROJECTION * GL10.GL_MODELVIEW;
if ( starW >= w ||
starW <= 0 ||
starH >= h ||
starH <= 0){
count++;
// Log.i("count", String.valueOf(count));
}
}
}
在我的 glsurfaceview 类中,我有
public static int height, width;
public MyGLSurfaceView(Context context){
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Point size = new Point();
display.getSize(size);
height = size.y;
width = size.x;
}
最佳答案
这不会起作用:
float starW = (star[a].vertices[i] * (w/2)) + (w/2) * GL10.GL_PROJECTION * GL10.GL_MODELVIEW;
float starH = (star[a].vertices[i+1] * (h/2)) + (h/2) * GL10.GL_PROJECTION * GL10.GL_MODELVIEW;
GL_PROJECTION
和 GL_MODELVIEW
是枚举值,它们是 glMatrixMode()
的可能参数值。它们绝对不是矩阵。
您在这里尝试做的是在您自己的代码中应用投影和模型 View 矩阵。获得这些矩阵的当前值的最简单方法是将它们保留在您自己的代码中。毕竟,您确实指定了它们,所以您应该知道它们是什么。
如果您确实想查询这些矩阵的当前值,正确的方法是:
float[] mvMat = new float[16];
GL11.glGetFloatv(GL11.GL_MODELVIEW_MATRIX, mvMat);
float[] projMat = new float[16];
GL11.glGetFloatv(GL11.GL_PROJECTION_MATRIX, projMat);
一旦获得这些矩阵,您就可以开始将它们与顶点坐标相乘。您可以为此编写自己的代码,也可以使用 android.opengl.Matrix类(class)。 multiplyMM()
和 multiplyMV()
是该类中的方法,可以在这项工作中派上用场。
除此之外,当我看到人们使用 ES 1.x 时,不可避免地会有这样的评论:在我看来,目前这必须被视为过时的 API。除非你有充分的理由使用它,否则我强烈建议使用 ES 2.0 或更高版本。
关于java - 返回android屏幕上opengl形状的像素坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29112060/