
首先需要配置纹理 在GL2JNIView.java中修改Renderer类
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
/*
* 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
#include#include #include #include #include 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); }
工程下载:GL2JNIActivity
收藏的用户(0) X
正在加载信息~
推荐阅读
最新回复 (0)
站点信息
- 文章2313
- 用户1336
- 访客11757557
每日一句
Life is short; Live it!
人生苦短,活出精彩。
人生苦短,活出精彩。
信鸽推送报错NSObject checkTargetOtherLinkFlagForObjc
简单利用Clover四叶草安装U盘安装黑苹果
学习使用Java注解
OllyDbg中如何找出B模块中所有调用了A模块的C方法的地方
解决SSH客户端中文乱码
10年后,Android应用程序仍然比iOS应用程序差
C++11特性里面的thread
XPosed微信自动生成二维码
解决android studio "found an invalid color"的问题
T9社区注册方法【勼适様鲃女尔懟死】
Thinkpad x1 Extreme黑苹果10.14.5安装完成
基于大白主题增加图片本地化的功能
Linux系统查看CPU使用率的几个命令
新会员