android使用NDK实现纹理贴图Texture

Home / Android MrLee 2015-3-4 5729

20150304145007


首先需要配置纹理 在GL2JNIView.java中修改Renderer类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.android.gl2jni;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.opengl.GLUtils;
import android.util.Log;
class GL2JNIView extends GLSurfaceView {
    public GL2JNIView(Context context) {
        super(context);
        init(false, 0, 0);
    }
    public GL2JNIView(Context context, boolean translucent, int depth,
            int stencil) {
        super(context);
        init(translucent, depth, stencil);
    }
    private void init(boolean translucent, int depth, int stencil) {
        if (translucent) {
            this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
        }
        setEGLContextFactory(new ContextFactory());
        setEGLContextClientVersion(2);
        setRenderer(new Renderer());
    }
    private static class ContextFactory implements
            GLSurfaceView.EGLContextFactory {
        private static int EGL_CONTEXT_CLIENT_VERSION = 0x3098;
        public EGLContext createContext(EGL10 egl, EGLDisplay display,
                EGLConfig eglConfig) {
            int[] attrib_list = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE };
            EGLContext context = egl.eglCreateContext(display, eglConfig,
                    EGL10.EGL_NO_CONTEXT, attrib_list);
            return context;
        }
        public void destroyContext(EGL10 egl, EGLDisplay display,
                EGLContext context) {
            egl.eglDestroyContext(display, context);
        }
    }
    private class Renderer implements GLSurfaceView.Renderer {
        public void onDrawFrame(GL10 gl) {
            GL2JNILib.step();
        }
        public void onSurfaceChanged(GL10 gl, int width, int height) {
            GL2JNILib.init(width, height);
        }
        private Context mContext;
        int textureId;
        private int[] TextureString = new int[1];
        public void onSurfaceCreated(GL10 gl, EGLConfig config) {
            mContext = GL2JNIView.this.getContext();
            // Bitmap bitmap = getBitmap(mContext,R.drawable.bac);
            Bitmap bitmap = getBitmap(mContext, R.drawable.bac);
            if (bitmap != null) {
                Log.e("step", "bing the texture succeed!");
                gl.glEnable(GLES20.GL_TEXTURE_2D);
                gl.glGenTextures(1, TextureString, 0);
                textureId = TextureString[0];
                Log.e("textureId", String.valueOf(textureId));
                gl.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
                gl.glTexParameterf(GLES20.GL_TEXTURE_2D,
                        GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
                gl.glTexParameterf(GLES20.GL_TEXTURE_2D,
                        GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
                gl.glTexParameterf(GLES20.GL_TEXTURE_2D,
                        GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
                gl.glTexParameterf(GLES20.GL_TEXTURE_2D,
                        GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
                GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
                GL2JNILib.setTextures(TextureString);
                // GL2JNILib.setTextures(textureId);
                bitmap.recycle();
            }
        }
        private Bitmap getBitmap(Context context, int resId) {
            // getBitmap by decodeResources()
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inScaled = false;
            return BitmapFactory.decodeResource(context.getResources(), resId,
                    options);
        }
    }
}

GL2JNILib.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.gl2jni;
// Wrapper for native library
public class GL2JNILib {
     static {
         System.loadLibrary("gl2jni");
     }
    /**
     * @param width the current view width
     * @param height the current view height
     */
     public static native void init(int width, int height);
     public static native void step();
     public static native void setTextures(int[] textures);
}

CPP代码 GL2JNIActivity.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <jni.h>
#include <android log.h="">
#include <gles2 gl2.h="">
#include <gles2 gl2ext.h="">
#include <math.h>
GLuint *mTexture;
unsigned int m_texture;
GLuint gProgram;
GLuint gvPositionHandle;
GLuint gvTexCoorHandle;
const GLfloat gTriangleVertices[] = { 0.0f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f };
const GLfloat gTexCoor[] = { 0.5f, 0, 0, 1, 1, 1 };
static const char gVertexShader[] = "attribute vec4 vPosition;\n"
        "attribute vec2 vTexCoords;\n"
        "varying vec2 colorVarying;\n"
        "void main() {\n"
        "  gl_Position = vPosition;\n"
        "  colorVarying = vTexCoords;\n"
        "}\n";
static const char gFragmentShader[] = "precision mediump float;\n"
        "varying vec2 colorVarying;\n"
        "uniform sampler2D sampler;\n"
        "void main() {\n"
        "  //gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
        "gl_FragColor = texture2D(sampler,colorVarying);\n"
        "}\n";
GLuint loadShader(GLenum shaderType, const char* pSource) {
    GLuint shader = glCreateShader(shaderType);
    if (shader) {
        glShaderSource(shader, 1, &pSource, NULL);
        glCompileShader(shader);
        GLint compiled = 0;
        glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
        if (!compiled) {
            GLint infoLen = 0;
            glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
            if (infoLen) {
                char* buf = new char[infoLen];
                if (buf) {
                    glGetShaderInfoLog(shader, infoLen, NULL, buf);
                    delete buf;
                }
                glDeleteShader(shader);
                shader = 0;
            }
        }
    }
    return shader;
}
GLuint createProgram(const char* pVertexSource, const char* pFragmentSource) {
    GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource);
    if (!vertexShader) {
        return 0;
    }
    GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource);
    if (!pixelShader) {
        return 0;
    }
    GLuint program = glCreateProgram();
    if (program) {
        glAttachShader(program, vertexShader);
        glAttachShader(program, pixelShader);
        glLinkProgram(program);
        GLint linkStatus = GL_FALSE;
        glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
        if (linkStatus != GL_TRUE) {
            GLint bufLength = 0;
            glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
            if (bufLength) {
                char* buf = new char[bufLength];
                if (buf) {
                    glGetProgramInfoLog(program, bufLength, NULL, buf);
                    delete buf;
                }
            }
            glDeleteProgram(program);
            program = 0;
        }
    }
    return program;
}
bool setupGraphics(int w, int h) {
    gProgram = createProgram(gVertexShader, gFragmentShader);
    if (!gProgram) {
        return false;
    }
    gvPositionHandle = glGetAttribLocation(gProgram, "vPosition");
    gvTexCoorHandle = glGetAttribLocation(gProgram, "vTexCoords");
    glViewport(0, 0, w, h);
    return true;
}
void renderFrame() {
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    //glClear( GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    glUseProgram(gProgram);
    glVertexAttribPointer(gvPositionHandle, 2, GL_FLOAT, GL_FALSE, 0,
            gTriangleVertices);
    glVertexAttribPointer(gvTexCoorHandle, 2, GL_FLOAT, GL_FALSE, 0, gTexCoor);
    glEnableVertexAttribArray(gvPositionHandle);
    glEnableVertexAttribArray(gvTexCoorHandle);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, mTexture[0]);
    glDrawArrays(GL_TRIANGLES, 0, 3);
}
extern "C" JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_init(
        JNIEnv * env, jobject obj, jint width, jint height) {
    setupGraphics(width, height);
}
extern "C" JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_step(
        JNIEnv * env, jobject obj) {
    renderFrame();
}
extern "C" JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_setTextures(
        JNIEnv * env, jobject obj, jintArray texture) {
    mTexture = (GLuint *) env->GetIntArrayElements(texture, 0);
}
</math.h></gles2></gles2></android></jni.h>

工程下载:GL2JNIActivity

本文链接:https://it72.com/1225.htm

推荐阅读
最新回复 (0)
返回