【问题标题】:Android - 3D Cube with OPENGL + TexturesAndroid - 带有 OPENGL + 纹理的 3D 立方体
【发布时间】:2012-09-23 20:38:09
【问题描述】:

我已经在 Android 上使用 OPENGL 库完成了一个 3d 立方体,立方体工作得很好,但是要在立方体上打印的图像没有显示...

这是我的渲染代码(java 类):

public class GLRenderEx implements Renderer {

    private GLCube cube;
    Context c;

    public GLRenderEx(Context c) {
        cube = new GLCube();
        this.c = c;
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        gl.glDisable(GL10.GL_DITHER);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
        GLU.gluLookAt(gl, 0, 0, -3, 0, 0, 0, 0, 2, 0);

        long time = SystemClock.uptimeMillis() % 4000L;
        float angle = .090f * ((int) time);
        gl.glRotatef(angle, 2, 4, 3);

        cube.draw(gl);

    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        // TODO Auto-generated method stub
        gl.glViewport(0, 0, width, height);
        float ratio = (float) width / height;
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glFrustumf(-ratio, ratio, -1, 1, 1, 25);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig egl) {
        gl.glDisable(GL10.GL_DITHER);
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
        gl.glClearColor(0f, 0f, 0f, 1);
        gl.glClearDepthf(1f);
    }

}

这是我的立方体的代码:

    public class GLCube {

    /** The buffer holding the vertices */
    private FloatBuffer vertexBuffer;
    /** The buffer holding the texture coordinates */
    private FloatBuffer textureBuffer;
    /** The buffer holding the indices */
    private ByteBuffer indexBuffer;

    /** Our texture pointer */
    private int[] textures = new int[1];

    /**
     * The initial vertex definition
     * 
     * Note that each face is defined, even if indices are available, because of
     * the texturing we want to achieve
     */
    private float vertices[] = {
            // Vertices according to faces
            -1.0f,
            -1.0f,
            1.0f, // Vertex 0
            1.0f,
            -1.0f,
            1.0f, // v1
            -1.0f,
            1.0f,
            1.0f, // v2
            1.0f,
            1.0f,
            1.0f, // v3

            1.0f,
            -1.0f,
            1.0f, // ...
            1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f,

            1.0f, -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f, -1.0f, -1.0f,
            1.0f, -1.0f,

            -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f,
            1.0f, 1.0f,

            -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, 1.0f,
            -1.0f, 1.0f,

            -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f,
            1.0f, -1.0f, };

    /** The initial texture coordinates (u, v) */
    private float texture[] = {
            // Mapping coordinates for the vertices
            0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,

            0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,

            0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,

            0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,

            0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,

            0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,

    };

    /** The initial indices definition */
    private byte indices[] = {
            // Faces definition
            0, 1, 3, 0, 3,
            2, // Face front
            4, 5, 7, 4, 7,
            6, // Face right
            8, 9, 11, 8, 11,
            10, // ...
            12, 13, 15, 12, 15, 14, 16, 17, 19, 16, 19, 18, 20, 21, 23, 20, 23,
            22, };

    /**
     * The Cube constructor.
     * 
     * Initiate the buffers.
     */
    public GLCube() {
        //
        ByteBuffer byteBuf = ByteBuffer.allocateDirect(vertices.length * 4);
        byteBuf.order(ByteOrder.nativeOrder());
        vertexBuffer = byteBuf.asFloatBuffer();
        vertexBuffer.put(vertices);
        vertexBuffer.position(0);

        //
        byteBuf = ByteBuffer.allocateDirect(texture.length * 4);
        byteBuf.order(ByteOrder.nativeOrder());
        textureBuffer = byteBuf.asFloatBuffer();
        textureBuffer.put(texture);
        textureBuffer.position(0);

        //
        indexBuffer = ByteBuffer.allocateDirect(indices.length);
        indexBuffer.put(indices);
        indexBuffer.position(0);
    }

    /**
     * The object own drawing function. Called from the renderer to redraw this
     * instance with possible changes in values.
     * 
     * @param gl
     *            - The GL Context
     */
    public void draw(GL10 gl) {
        // Bind our only previously generated texture in this case
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
        gl.glEnable(GL10.GL_TEXTURE_2D);
        // Point to our buffers
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

        // Set the face rotation
        gl.glFrontFace(GL10.GL_CCW);

        // Enable the vertex and texture state
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

        // Draw the vertices as triangles, based on the Index Buffer information
        gl.glDrawElements(GL10.GL_TRIANGLES, indices.length,
                GL10.GL_UNSIGNED_BYTE, indexBuffer);

        // Disable the client state before leaving
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
    }

    /**
     * Load the textures
     * 
     * @param gl
     *            - The GL Context
     * @param context
     *            - The Activity context
     */
    public void loadGLTexture(GL10 gl, Context context) {
        // Get the texture from the Android resource directory
        InputStream is = context.getResources().openRawResource(
                R.drawable.ic_launcher);
        Bitmap bitmap = null;
        try {
            // BitmapFactory is an Android graphics utility for images
            bitmap = BitmapFactory.decodeStream(is);

        } finally {
            // Always clear and close
            try {
                is.close();
                is = null;
            } catch (IOException e) {
            }
        }

        // Generate one texture pointer...
        gl.glGenTextures(1, textures, 0);
        // ...and bind it to our array
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

        // Create Nearest Filtered Texture
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
                GL10.GL_NEAREST);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
                GL10.GL_LINEAR);

        // Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
                GL10.GL_CLAMP_TO_EDGE);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
                GL10.GL_CLAMP_TO_EDGE);

        // Use the Android GLUtils to specify a two-dimensional texture image
        // from our bitmap
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

        // Clean up
        bitmap.recycle();
    }
}

为什么我的代码上的位图没有显示在立方体中??!!

谢谢。

【问题讨论】:

  • 您似乎在设置中忘记了glActiveTexture( GL_TEXTURE0 )

标签: java android opengl-es bitmap cube


【解决方案1】:

我查看了您的代码,但没有找到您拨打loadGLTexture() 的地方。当我实现您的代码来制作纹理时,我只是在onDrawFrame() 方法中的cube.draw(gl); 上方添加了cube.loadGLTexture(gl, c);。希望对您有所帮助。

【讨论】:

    【解决方案2】:

    如果你想为你的对象添加纹理,你必须使用glEnable(GL_TEXTURE_2D); 启用纹理

    我在你的代码中没有看到这个。

    【讨论】:

    • 我已经更新了代码并在渲染类上启用了它,但它似乎不起作用......
    • 你的英文不是特别清楚,到底是什么不行?你的结果是什么样的,你可以链接到图片吗? @MarcOrtiz
    • 我的立方体正在工作(移动 x、y、z),但纹理不在立方体上(图像)
    • 立方体上有什么?白色,黑色?你的纹理是二次幂吗? IIRC 使用 GL_REPEAT 仅适用于 Po2 纹理。 @MarcOrtiz
    • 我的立方体是全白的...我必须在渲染类或立方体类上启用 GL_REPEAT 吗?
    【解决方案3】:

    我和你一样遇到了一些问题。

    导致我出现问题的原因是纹理与立方体不完全匹配。

    仔细检查您的代码,您可能会发现一些不匹配的地方。

    【讨论】:

    • 只是体验。您已经检查了可能导致问题的所有内容。但是基础匹配可能有问题,我只是说“可能”,希望对您有所帮助。
    猜你喜欢
    • 2013-02-10
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多