Unity端调用
[DllImport ("RenderingPlugin")]
private static extern void SetTextureFromUnity(System.IntPtr texture);
[DllImport("RenderingPlugin")]
private static extern IntPtr GetRenderEventFunc();
SetTextureFromUnity(System.IntPtr texture);
GL.IssuePluginEvent(GetRenderEventFunc(), 1);
C++端
// 设置贴图
static void* g_StereoScreen = NULL;
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API SetTextureFromUnity(void* texturePtr)
{
g_StereoScreen = texturePtr;
}
// OnRenderEvent
static void UNITY_INTERFACE_API OnRenderEvent(int eventID)
{
if (s_DeviceType == kUnityGfxRendererNull)
return;
float worldMatrix[16] = {
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1,
};
float identityMatrix[16] = {
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1,
};
float projectionMatrix[16] = {
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1,
};
// Actual functions defined below
SetDefaultGraphicsState ();
DoRendering (identityMatrix, identityMatrix, projectionMatrix);
}
// --------------------------------------------------------------------------
// GetRenderEventFunc, an example function we export which is used to get a rendering event callback function.
extern "C" UnityRenderingEvent UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API GetRenderEventFunc()
{
return OnRenderEvent;
}
if (s_DeviceType == kUnityGfxRendererOpenGLES20 ||
s_DeviceType == kUnityGfxRendererOpenGLES30)
{
// Tweak the projection matrix a bit to make it match what identity projection would do in D3D case.
projectionMatrix[0] = 1.12f;
projectionMatrix[5] = 2.0f;
//projectionMatrix[10] = 2.0f;
projectionMatrix[14] = -1.0f;
glUseProgram(g_Program);
glUniformMatrix4fv(g_WorldMatrixUniformIndex, 1, GL_FALSE, worldMatrix);
glUniformMatrix4fv(g_ProjMatrixUniformIndex, 1, GL_FALSE, projectionMatrix);
glClearColor(0.0f, 0.0f, 0.0f, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// 纹理贴图
GLuint gltexture = 0;
if (g_StereoScreen)
{
glEnable(GL_TEXTURE_2D);
gltexture = (GLuint)(size_t)(g_StereoScreen);
glBindTexture(GL_TEXTURE_2D, gltexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //线性滤波
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //线性滤波
}
GLuint VertexVBOID = 0;
glGenBuffers(1, &VertexVBOID);
glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
glBufferData(GL_ARRAY_BUFFER, sizeof(MyVertex) * 2 * kMeshWidth * kMeshHeight, &g_Verts[0].x, GL_STATIC_DRAW);
GLuint IndexVBOID = 0;
glGenBuffers(1, &IndexVBOID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * 2 * (kMeshWidth - 1) * (kMeshHeight - 1) * 6, g_indices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, VertexVBOID);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(MyVertex), BUFFER_OFFSET(0));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(MyVertex), BUFFER_OFFSET(12));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(MyVertex), BUFFER_OFFSET(20));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexVBOID);
glDrawElements(GL_TRIANGLES, 2 * (kMeshWidth - 1) * (kMeshHeight - 1) * 6, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glDeleteBuffers(1, &VertexVBOID);
glDeleteBuffers(1, &IndexVBOID);
}