Android 上的 OpenGL ES 1.x 入门 - 基本原语

发布于 2024-12-20 05:41:37 字数 2002 浏览 0 评论 0原文

我已经使用 Android 一段时间了,在使用基于 OpenGL ES 的 Android 库之后,我决定冒险尝试自己学习。这似乎是一个相当陡峭的学习曲线,但我决心并且总是愿意学习。然而,在阅读了我能找到的任何初学者教程后,我正在努力将我的第一个应用程序串起来。我只是想在屏幕上显示一个三角形,但到目前为止我最终得到的只是黑屏。

如果有人能够指出我错在哪里(如果可能的话解释原因),我将非常感激!谢谢! (下面是渲染器,我认为也没有必要包含活动)。

public class OpenGLRenderer implements Renderer {

private OpenGLActivity mContext;

public OpenGLRenderer(OpenGLActivity pContext) {
    mContext = pContext;
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);

    gl.glClearColor(0.0f, 0.0f, 0.0f, 1f);
    gl.glShadeModel(GL10.GL_FLAT);

    gl.glDisable(GL10.GL_DEPTH_TEST);
    gl.glDisable(GL10.GL_DITHER);
    gl.glDisable(GL10.GL_LIGHTING);
    gl.glDisable(GL10.GL_MULTISAMPLE);

    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glEnable(GL10.GL_BLEND);
    gl.glEnable(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glEnable(GL10.GL_VERTEX_ARRAY);
    gl.glEnable(GL10.GL_CULL_FACE);

    gl.glFrontFace(GL10.GL_CCW);
    gl.glCullFace(GL10.GL_BACK);     

    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

    gl.glViewport(0, 0, 480,  800);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glOrthof(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);
}

@Override
public void onDrawFrame(GL10 gl) {
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    gl.glLoadIdentity();

    gl.glDisable(GL10.GL_TEXTURE_2D);
    gl.glColor4f(0f, 1f, 0f, 1f);

    float[] vertices = new float[ ]{1,0, 0,1, -1,0};
    FloatBuffer buffer = ByteBuffer.allocateDirect(24).order(ByteOrder.nativeOrder()).asFloatBuffer();

    buffer.put(vertices);

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

    gl.glVertexPointer(2, GL10.GL_FLOAT, 0, buffer);

    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);

    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    gl.glViewport(0, 0, 480,  800);
    gl.glLoadIdentity();
}
}

I've been on Android for a while, after using Android libraries based on OpenGL ES, I've decided to take the plunge and learn it myself. It seems to be a pretty steep learning curve but I'm committed and always willing to learn. However, after reading whatever beginner tutorials I could find, I'm struggling to string together my first application. I'm simply trying to display a triangle on the screen, but so far all I end up with is a black screen.

If anyone is able to point out where I'm going wrong (and if possible explain why), I'd be really appreciative! Thanks! (Below is the Renderer, I saw no point in including the Activity as well).

public class OpenGLRenderer implements Renderer {

private OpenGLActivity mContext;

public OpenGLRenderer(OpenGLActivity pContext) {
    mContext = pContext;
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);

    gl.glClearColor(0.0f, 0.0f, 0.0f, 1f);
    gl.glShadeModel(GL10.GL_FLAT);

    gl.glDisable(GL10.GL_DEPTH_TEST);
    gl.glDisable(GL10.GL_DITHER);
    gl.glDisable(GL10.GL_LIGHTING);
    gl.glDisable(GL10.GL_MULTISAMPLE);

    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glEnable(GL10.GL_BLEND);
    gl.glEnable(GL10.GL_TEXTURE_COORD_ARRAY);
    gl.glEnable(GL10.GL_VERTEX_ARRAY);
    gl.glEnable(GL10.GL_CULL_FACE);

    gl.glFrontFace(GL10.GL_CCW);
    gl.glCullFace(GL10.GL_BACK);     

    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

    gl.glViewport(0, 0, 480,  800);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glOrthof(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);
}

@Override
public void onDrawFrame(GL10 gl) {
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    gl.glLoadIdentity();

    gl.glDisable(GL10.GL_TEXTURE_2D);
    gl.glColor4f(0f, 1f, 0f, 1f);

    float[] vertices = new float[ ]{1,0, 0,1, -1,0};
    FloatBuffer buffer = ByteBuffer.allocateDirect(24).order(ByteOrder.nativeOrder()).asFloatBuffer();

    buffer.put(vertices);

    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

    gl.glVertexPointer(2, GL10.GL_FLOAT, 0, buffer);

    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);

    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    gl.glViewport(0, 0, 480,  800);
    gl.glLoadIdentity();
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等待我真够勒 2024-12-27 05:41:37

这是很多仅用于显示三角形的代码,可能是从某处复制的。您应该尝试在 onSurfaceCreated 方法中逐行删除并查看是否有变化。您还应该在这里发布您的活动。

以下是渲染三角形的示例

http://blog.jayway.com/2009/12/03/opengl-es-tutorial-for-android-part-i/

http://code .google.com/p/android-gamedev/source/browse/trunk/src/com/badlogic/gamedev/samples/TriangleSample.java

既然你正在学习 OpenGL,你应该看看 Jayway教程。他们帮助了我很多

This is a lot of code for just displaying a triangle, probably copied from somewhere. You should try to remove line after line in the onSurfaceCreated method and look if somethings changing. You should also post your activity here.

Here are to examples of rendering a triangle

http://blog.jayway.com/2009/12/03/opengl-es-tutorial-for-android-part-i/

http://code.google.com/p/android-gamedev/source/browse/trunk/src/com/badlogic/gamedev/samples/TriangleSample.java

And since you're learning OpenGL you should have a look at the Jayway tutorials. They helped me a lot

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文