gpt4 book ai didi

android - OpenGL ES 2 投影切换前景和背景

转载 作者:行者123 更新时间:2023-12-04 10:19:49 24 4
gpt4 key购买 nike

我有一个包含地形图顶点高度的数组。
当我第一次绘制地形时,它看起来不错:

enter image description here

但是当我在 z 轴上旋转它时,形状的一部分似乎转换到背面的顶点后面:

90 度旋转(z 轴):

90 degreee rotation

~180 度旋转(z 轴):

enter image description here

除了我对 map 的实现之外,我的代码相当简单:

顶点着色器:

attribute vec4 position;
attribute vec4 color;

uniform mat4 matrix;
varying vec4 interpolated_color;

void main() {
gl_Position = matrix * position;
interpolated_color = color;
}

fragment 着色器:
precision mediump float;

varying vec4 interpolated_color;

void main(){
gl_FragColor = interpolated_color;
}

渲染器:
public class MapRenderer implements GLSurfaceView.Renderer {

...

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
GLES20.glClearColor(1.0f, 0.0f,0.0f, 1.0f);
map = mapGen.composeMap(); //gets array with vertices heights
mapView = new MapView(context, map, mapGen);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
GLES20.glViewport(0, 0, width, height);
float aspect_ratio = (float) width/height;
Matrix.perspectiveM(projectionMatrix, 0, 45, aspect_ratio, 1f, 10f);
}


@Override
public void onDrawFrame(GL10 gl) {
float[] scratch = new float[16];

GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

Matrix.setIdentityM(modelMatrix, 0);

Matrix.translateM(modelMatrix, 0, 0, 0, -4);
Matrix.rotateM(modelMatrix, 0, -cameraAngle, 1, 0, 0); //cameraAngle initialized at 0 changes with user input
Matrix.rotateM(modelMatrix, 0, mapAngle, 0, 0, 1); //mapAngle initialized at 0 changes with user input

Matrix.multiplyMM(scratch, 0, projectionMatrix, 0, modelMatrix, 0);

mapView.draw(scratch);
}
}

map View 类:
    public void draw(float[] mvpMatrix){
int matrix = GLES20.glGetUniformLocation(program, "matrix");

GLES20.glUniformMatrix4fv(matrix, 1, false, mvpMatrix, 0);

//nFaces and facesBuffer are class variables
GLES20.glDrawElements(GLES20.GL_TRIANGLES, nFaces*3, GLES20.GL_UNSIGNED_SHORT, facesBuffer);
}

我尝试打开和关闭面部剔除以查看是否发生了任何差异,但没有发生。
除了改变错误开始发生的角度之外,改变投影矩阵似乎也没有任何影响。当使用 Matrix.perspectiveM 时,它似乎发生在 ~90 度和高达 ~270 度。使用 Matrix.orthoM 时正好在 90 和 270 .

我还通过 glGetErrors() 检查了 OpenGL 是否返回了任何错误。方法并没有得到任何东西。

我的顶点在缓冲区中按顺序从位于 (-1, 1, 0) 的顶点到位于 (1, -1, 0) 的最后一个顶点排序。我不知道这是否会导致这个问题,或者即使是这种情况,我也不知道如何在 OpenGL ES 2 中解决这个问题以支持跨 z 轴的旋转。

最佳答案

需要启用深度测试,以便 OpenGL 考虑距离。

在渲染器上:

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
GLES20.glClearColor(1.0f, 0.0f,0.0f, 1.0f);

GLES20.glEnable(GLES20.GL_DEPTH_TEST);

map = mapGen.composeMap(); //gets array with vertices heights
mapView = new MapView(context, map, mapGen);
}

关于android - OpenGL ES 2 投影切换前景和背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60908504/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com