我正在尝试在Android(java)中为Unity3D制作一个插件,以使用OpenGL纹理进行渲染,获取 native 指针并在Unity中映射Quad。到目前为止,我的 Unity 代码很简单:
// Use this for initialization
void Start () {
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
context = jc.GetStatic<AndroidJavaObject>("currentActivity");
surface2Unity = new AndroidJavaObject("com.everywoah.surface2unity.Surface2Unity");
int i = surface2Unity.Call<int> ("getTextureID");
t =Texture2D.CreateExternalTexture (1280, 720, TextureFormat.ARGB32, false,false, new IntPtr(i));
t.filterMode = FilterMode.Bilinear;
t.wrapMode = TextureWrapMode.Repeat;
debug.text = "" + i;
GetComponent<MeshRenderer> ().material.mainTexture = t;
}
// Update is called once per frame
void Update () {
//transform.Rotate(1f,1f,1f);
surface2Unity.Call ("draw");
}
在 Java 中我有:
public MyRenderer(){
initGL();
initFBO();
}
private void initGL(){
GLstatics.checkGlError("initGL_S");
String vertexShader = "attribute vec4 aPosition;\n" +
"attribute vec4 aTex;\n" +
"varying vec2 tex;\n" +
"void main() {\n" +
" gl_Position = aPosition;\n" +
"tex=aTex.xy;\n" +
"}";
String fragmentShader = "precision mediump float;\n" +
"\n" +
"void main(){\n" +
" gl_FragColor = vec4(1.0,1.0,0.0,1.0);\n" +
"\n" +
"}";
mProgram = GLstatics.createProgram(vertexShader, fragmentShader);
maPositionHandle = GLES30.glGetAttribLocation(mProgram, "aPosition");
maTexHandle = GLES30.glGetAttribLocation(mProgram, "aTex");
GLstatics.checkGlError("initGL_E");
}
private void initFBO(){
GLstatics.checkGlError("initFBO_S");
GLES30.glGenFramebuffers(1, mFboId, 0);
GLES30.glGenRenderbuffers(1, mRboId, 0);
GLES30.glGenTextures(1, mTexId, 0);
GLES30.glGenBuffers(1,buffer, 0);
GLES30.glBindRenderbuffer(GLES30.GL_RENDERBUFFER, mRboId[0]);
GLES30.glRenderbufferStorage(GLES30.GL_RENDERBUFFER, GLES30.GL_DEPTH_COMPONENT16, 1280,
720);
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFboId[0]);
GLES30.glFramebufferRenderbuffer(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_ATTACHMENT,
GLES30.GL_RENDERBUFFER, mRboId[0]);
GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mTexId[0]);
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_LINEAR);
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR);
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE);
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_CLAMP_TO_EDGE);
GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, 1280, 720, 0,
GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null);
GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0,
GLES30.GL_TEXTURE_2D, mTexId[0], 0);
GLstatics.checkGlError("initFBO_E ");
}
public void draw() {
GLstatics.checkGlError("draw_S");
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFboId[0]);
GLES30.glClearColor(0.0f,0.0f,1.0f,1.0f);
GLES30.glViewport(0, 0, 1280, 720);
GLES30.glClear(GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
GLES30.glUseProgram(mProgram);
mVtxBuf.position(0);
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, buffer[0]);
GLES30.glVertexAttribPointer(maPositionHandle,
3, GLES30.GL_FLOAT, false, 4*(3+2), mVtxBuf);
GLES30.glEnableVertexAttribArray(maPositionHandle);
mVtxBuf.position(3);
GLES30.glVertexAttribPointer(maTexHandle, 2, GLES30.GL_FLOAT, false, 4 * (3 + 2), mVtxBuf);
GLES30.glEnableVertexAttribArray(maTexHandle);
GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
GLstatics.checkGlError("draw_E ");
}
这应该是一个非常简单的代码,只需在蓝色背景中绘制一个黄色矩形,我就可以让它在 Activity 中工作。这里的问题是我的应用程序看起来像这样:
从那里我认为我弄乱了已经绑定(bind)的openGL缓冲区,因为我的矩形的顶点被绘制了,但里面有一个奇怪的东西。我尝试保存它们并在绘制函数之后绑定(bind)它们,但我得到了相同的结果。有什么想法我做错了吗?如果可能的话,我真的很想用 Java 解决这个问题,我使用 NDK 的经验最多为 0。
最佳答案
好吧,问题是我必须自己控制 EGLContex,我无法在与 Unity 相同的上下文中进行渲染,所以我需要这个类:
public class SurfaceManager {
public final static String TAG = "TextureManager";
private static final int EGL_RECORDABLE_ANDROID = 0x3142;
// Contexto donde dibujar
private EGLContext mEGLContext = null;
// Contexto compartido entre hilos. Para poder pasar el FBO de un hilo a otro
private EGLContext mEGLSharedContext = null;
private EGLSurface mEGLSurface = null;
private EGLDisplay mEGLDisplay = null;
// La surface donde se va a dibujar
private Surface mSurface;
/**
* Creates an EGL context and an EGL surface.
*/
public SurfaceManager(Surface surface) {
EGLContext shared = EGL14.eglGetCurrentContext();
if (surface == null) {
throw new NullPointerException();
}
mSurface = surface;
mEGLSharedContext = shared;
Log.d("Surface2UnityDebug", "vamos al setup");
eglSetup();
}
// Hace que la surface actual sea esta
public void makeCurrent() {
if (!EGL14.eglMakeCurrent(mEGLDisplay, mEGLSurface, mEGLSurface, mEGLContext))
throw new RuntimeException("eglMakeCurrent failed");
}
// Cambia el buffer donde se está pintando por el de la surface. es decir, guarda lo que se haya pintado.
public void swapBuffers() {
EGL14.eglSwapBuffers(mEGLDisplay, mEGLSurface);
}
/**
* Sends the presentation time stamp to EGL. Time is expressed in nanoseconds.
*/
public void setPresentationTime(long nsecs) {
EGLExt.eglPresentationTimeANDROID(mEGLDisplay, mEGLSurface, nsecs);
checkEglError("eglPresentationTimeANDROID");
}
/**
* Prepares EGL. We want a GLES 2.0 context and a surface that supports recording.
*/
private void eglSetup() {
mEGLDisplay = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY);
if (mEGLDisplay == EGL14.EGL_NO_DISPLAY) {
Log.d("Surface2UnityDebug", "unable to get EGL14 display");
throw new RuntimeException("unable to get EGL14 display");
}
int[] version = new int[2];
if (!EGL14.eglInitialize(mEGLDisplay, version, 0, version, 1)) {
Log.d("Surface2UnityDebug", "unable to initialize EGL14");
throw new RuntimeException("unable to initialize EGL14");
}
// Configure EGL for recording and OpenGL ES 2.0.
int[] attribList;
attribList = new int[]{
EGL14.EGL_RED_SIZE, 8,
EGL14.EGL_GREEN_SIZE, 8,
EGL14.EGL_BLUE_SIZE, 8,
EGL14.EGL_RENDERABLE_TYPE, EGL14.EGL_OPENGL_ES2_BIT,
EGL_RECORDABLE_ANDROID, 1,
EGL14.EGL_NONE
};
EGLConfig[] configs = new EGLConfig[1];
int[] numConfigs = new int[1];
EGL14.eglChooseConfig(mEGLDisplay, attribList, 0, configs, 0, configs.length,
numConfigs, 0);
checkEglError("eglCreateContext RGB888+recordable ES2");
// Configure context for OpenGL ES 2.0.
int[] attrib_list = {
EGL14.EGL_CONTEXT_CLIENT_VERSION, 3,
EGL14.EGL_NONE
};
mEGLContext = EGL14.eglCreateContext(mEGLDisplay, configs[0], mEGLSharedContext, attrib_list, 0);
checkEglError("eglCreateContext");
// Create a window surface, and attach it to the Surface we received.
int[] surfaceAttribs = {
EGL14.EGL_NONE
};
mEGLSurface = EGL14.eglGetCurrentSurface(EGL14.EGL_DRAW);
checkEglError("eglCreateWindowSurface");
}
/**
* Discards all resources held by this class, notably the EGL context. Also releases the
* Surface that was passed to our constructor.
*/
public void release() {
if (mEGLDisplay != EGL14.EGL_NO_DISPLAY) {
EGL14.eglMakeCurrent(mEGLDisplay, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE,
EGL14.EGL_NO_CONTEXT);
EGL14.eglDestroySurface(mEGLDisplay, mEGLSurface);
EGL14.eglDestroyContext(mEGLDisplay, mEGLContext);
EGL14.eglReleaseThread();
EGL14.eglTerminate(mEGLDisplay);
}
mEGLDisplay = EGL14.EGL_NO_DISPLAY;
mEGLContext = EGL14.EGL_NO_CONTEXT;
mEGLSurface = EGL14.EGL_NO_SURFACE;
mSurface.release();
}
/**
* Checks for EGL errors. Throws an exception if one is found.
*/
private void checkEglError(String msg) {
int error;
if ((error = EGL14.eglGetError()) != EGL14.EGL_SUCCESS) {
Log.d("Surface2UnityDebug", msg + ": EGL error: 0x" + Integer.toHexString(error));
throw new RuntimeException(msg + ": EGL error: 0x" + Integer.toHexString(error));
}
}}
和方法:
// Guarda el estado actual
public void saveRenderState() {
mSavedEglDisplay = EGL14.eglGetCurrentDisplay();
mSavedEglDrawSurface = EGL14.eglGetCurrentSurface(EGL14.EGL_DRAW);
mSavedEglReadSurface = EGL14.eglGetCurrentSurface(EGL14.EGL_READ);
mSavedEglContext = EGL14.eglGetCurrentContext();
}
// Carga el estado guardado
public void restoreRenderState() {
if (!EGL14.eglMakeCurrent(
mSavedEglDisplay,
mSavedEglDrawSurface,
mSavedEglReadSurface,
mSavedEglContext)) {
throw new RuntimeException("eglMakeCurrent failed");
}
}
这样我就可以调用 saveRenderState()、makeCurrent()、draw() 和 RestoreRenderState(),现在一切正常。
关于java - Unity3d在android中将openGL FBO渲染为纹理(java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40787339/