【问题标题】:drawing lines on Android with OpenGL ES 1.0使用 OpenGL ES 1.0 在 Android 上绘制线条
【发布时间】:2011-12-31 03:06:43
【问题描述】:

以下代码尝试绘制 GLSurfaceView 的框架和一条对角线,但结果却是一个大黑屏。我尝试设置正交投影,将视口大小与视图大小对齐,用 glDrawArrays() 绘制框架线,用 glDrawElements() 绘制对角线。我找不到问题的根源。

public class MapView extends GLSurfaceView implements Renderer {

private int w, h;

private FloatBuffer frameVertices;

private ByteBuffer diagIndices;

public MapView(Context ctx) { this(ctx, null); }

public MapView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setRenderer(this);
}

@Override
public void onDrawFrame(GL10 gl) {
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glVertexPointer(2, GL10.GL_FLOAT, 0, frameVertices);
    gl.glColor4f(0f, 0f, 1f, 0.5f);
    gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, 4);
    gl.glDrawElements(GL10.GL_LINES, 1, GL10.GL_UNSIGNED_BYTE, diagIndices);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    // TODO Auto-generated method stub

}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    w = getWidth(); h =  getHeight();
    TRC.debug("w = " + w + ", h = " + h);
    gl.glClearColor(0, 0, 0, 1);
    gl.glViewport(0, 0, w, h);
    gl.glDepthRangex(1, -1);    // TODO remove
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glOrthox(0, w, 0, h, 1, -1);

    float[] frame = {
        0, 0,
        w-1, 0,
        w-1, h-1,
        0, h-1 };
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(frame.length * 4);
    byteBuffer.order(ByteOrder.nativeOrder());
    frameVertices = byteBuffer.asFloatBuffer();
    frameVertices.put(frame);
    frameVertices.flip();
    frameVertices.position(0);
    gl.glLineWidthx(10);
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glDisable(GL10.GL_DEPTH_TEST);
    diagIndices = ByteBuffer.allocateDirect(2);
    diagIndices.order(ByteOrder.nativeOrder());
    diagIndices.put(new byte[] {0, 2});
    diagIndices.flip();
}

}

怎么回事?

【问题讨论】:

    标签: android opengl-es


    【解决方案1】:

    我发现了问题所在。我使用了glOrthox(),我认为x 只是表示整数值。但这并不完全是,它在中间用一个二进制点处理。 16bit.16bit。我的顶点超出了屏幕范围...我的解决方案是将glOrthox() 更改为glOrthof()。感谢您的帮助!

    【讨论】:

      【解决方案2】:

      您不需要gl.glDepthRangex() 方法调用,您的近剪裁平面和远剪裁平面在gl.glOrthox() 中切换。除此之外,我看不出有什么不妥。尝试使用gl.glClearColor() 设置不同的透明颜色,但我认为不会有太大的不同。

      再想一想,近剪裁平面最好设为 0,试试这个:gl.glOrthox(0, w, 0, h, 0, 1);

      【讨论】:

      • 您好,感谢您的浏览。我尝试了不同的透明颜色,效果很好。我将剪裁平面更改为 0、1 并没有发现任何差异。整个视图都充满了清晰的颜色(很好,至少我知道 GL 的某些部分正在工作)。
      • 首先尝试将顶点硬编码更改为朝向屏幕中间的随机点。如果您没有看到任何内容,请尝试在您的 draw 方法末尾使用 int error = gl.glGetError(); if (error != GL10.GL_NO_ERROR) { Log.e(error); } 以查看 OpenGL 是否抛出了特定错误。您的错误将是一个 int,您可以查找含义 here 和错误代码 here 的解释。在这里评论错误,我会进一步调查。
      • glGetError() 在 onDrawFrame() 方法中的每次 GL 调用后返回 0。现在我尝试在下载的示例源中将基于透视的投影更改为正交。修改后的示例正在运行,尽管我的应用程序没有,我仍然无法弄清楚这两个应用程序之间的棘手区别是什么。
      • 在我的代码中,我从不翻转顶点的顺序,你也从不调用diagIndices.position(0);除此之外,我会尝试将顶点从屏幕的边缘移动到某个地方在中间,以便更容易注意到它是否正在渲染。尝试尽可能地反映该示例,现在很高兴看到您的代码的更新版本。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多