【发布时间】:2023-03-08 13:11:01
【问题描述】:
经过两天的猛烈抨击并试图弄清楚这些东西后,我可悲地没有成功,希望有人能指出我正确的方向。
我正在尝试在 GLES 2.0 中制作基于图块的游戏,但我无法以我想要的方式显示任何内容。基本上,我有一个顶点数组,它们组成成对的三角形,形成一个方形网格。我想使用GLES20.glDrawArrays() 一次绘制这个网格的子部分。
我已经想出了如何使用Matrix.orthoM() 和Matrix.setLookAtM() 的组合从不同的角度“查看”,但是对于我的生活,我可以弄清楚如何让我的三角形不填满整个屏幕。
我真的需要一些关于设置投影的指导,以便如果三角形定义为 (0,0,0) (0,20,0) (20,0,0) 它在屏幕上显示为 20像素宽和 20 像素高,由我当前的视图翻译。
这是我目前拥有的,但它只是用绿色填充了我的整个屏幕。如果有人可以向我展示操纵场景的正确方法,以使其充满相机,或者相机只显示 20 个三角形宽 x 10 个三角形高,那将是我的一周。
当表面发生变化时:
GLES20.glViewport(0, 0, ScreenX, ScreenY);
float ratio = ScreenX / ScreenY;
Matrix.orthoM(_ProjMatrix, 0,
-ratio,
ratio,
-1, 1,
3, 7);
Matrix.setLookAtM(_VMatrix, 0,
60, 60, 7,
60, 60, 0,
0, 1, 0);
开始绘图:
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
Matrix.multiplyMM(_MVPMatrix, 0, _ProjMatrix, 0, _VMatrix, 0);
if (_activeMap != null)
_activeMap.draw(0, 0, (int)ScreenX, (int)ScreenY, _MVPMatrix);
绘制函数:
public void draw(int x, int y, int width, int height, float[] MVPMatrix)
{
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
GLES20.glUseProgram(_pHandle);
GLES20.glUniformMatrix4fv(_uMVPMatrixHandle, 1, false, MVPMatrix, 0);
int minRow, minCol, maxRow, maxCol;
minRow = (int) (y / Engine.TileSize);
minCol = (int) (x / Engine.TileSize);
maxRow = (int) (minRow + (height / Engine.TileSize));
maxCol = (int) (minCol + (width / Engine.TileSize));
minRow = (minRow < 0) ? 0 : minRow;
minCol = (minCol < 0) ? 0 : minCol;
maxRow = (maxRow > _rows) ? (int)_rows : maxRow;
maxCol = (maxCol > _cols) ? (int)_cols : maxCol;
for (int r = minRow; r < maxRow - 1; r++)
for (int d = 0; d < _vBuffers.length; d++)
{
_vBuffers[d].position(0);
GLES20.glVertexAttribPointer(_vAttHandle, 3, GLES20.GL_FLOAT,
false,
0, _vBuffers[d]);
GLES20.glEnableVertexAttribArray(_vAttHandle);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES,
(int) (r * 6 * _cols),
(maxCol - minCol) * 6);
}
}
着色器脚本:
private static final String _VERT_SHADER =
"uniform mat4 uMVPMatrix; \n"
+ "attribute vec4 vPosition; \n"
+ "void main() \n"
+ "{ \n"
+ " gl_Position = uMVPMatrix * vPosition; \n"
+ "} \n";
private static final String _FRAG_SHADER =
"precision mediump float; \n"
+ "void main() \n"
+ "{ \n"
+ " gl_FragColor = vec4 (0.63671875, 0.76953125, 0.22265625, 1.0); \n"
+ "} \n";
【问题讨论】:
标签: android opengl-es opengl-es-2.0