Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Robert-Castle · Feb 13, 2012 at 03:22 PM · iostexturepluginopengl

Issue with glDrawArrays in plugin

I am trying to write a plugin for Unity that does the following on iOS using OpenGL ES 2.0:

  1. Read camera image

  2. Draw the camera image into an FBO

  3. FBO is rendered to a Unity texture.

  4. Do stuff with texture in Unity.

The plugin is called in the update function of a script attached to the main camera.

The sticking point comes when I try and draw the textured quad in the offscreen FBO with glDrawArrays(GL_TRIANGLE_STRIP, 0, 4). If I comment out this line the app runs, my 3d scene is drawn correctly and runs at full framerate. All that is missing is the texture. If I enable the line, nothing draws and everything runs at about 3fps. No errors are reported.

This is the complete draw function:

 - (void) drawVideoFrame
 {
   GLint nCurrentProg;
   glGetIntegerv(GL_CURRENT_PROGRAM, &nCurrentProg);
 
   GLint nCurrentFrameBuffer;
   GLint nCurrentRenderBuffer;
   glGetIntegerv(GL_FRAMEBUFFER_BINDING, &nCurrentFrameBuffer );
   glGetIntegerv(GL_RENDERBUFFER_BINDING, &nCurrentRenderBuffer );
 
   glBindFramebuffer(GL_FRAMEBUFFER, nFBOBuffer);
   glBindRenderbuffer(GL_RENDERBUFFER, nFBORender);
 
   glViewport(0, 0, videoWidth, videoHeight);
   
   static const GLfloat squareVertices[] = {
      -1.0f, -1.0f,
       1.0f, -1.0f,
       -1.0f,  1.0f,
       1.0f,  1.0f,
     };
     
     static const GLfloat textureVertices[] = {
       0.0f, 1.0f,
       1.0f, 1.0f,
       0.0f, 0.0f,
       1.0f, 0.0f,
     };
 
   // Use shader program.
   glUseProgram(passThroughProgram);
   
   glBindTexture(GL_TEXTURE_2D, nTexID);
   
   // Update attribute values.
   glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, squareVertices);
   glEnableVertexAttribArray(ATTRIB_VERTEX);
   glVertexAttribPointer(ATTRIB_TEXTUREPOSITON, 2, GL_FLOAT, 0, 0, textureVertices);
   glEnableVertexAttribArray(ATTRIB_TEXTUREPOSITON);
 
   glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
 
   glUseProgram(nCurrentProg);
   glBindTexture(GL_TEXTURE_2D, 0);
   glBindFramebuffer(GL_FRAMEBUFFER, nCurrentFrameBuffer);
   glBindRenderbuffer(GL_RENDERBUFFER, nCurrentRenderBuffer);
 }

The fragment and vertex programs are just simple pass throughs:

Fragment

 varying highp vec2 coordinate;
 uniform sampler2D videoframe;
 
 void main()
 {
     gl_FragColor = texture2D(videoframe, coordinate);
 }

Vertex

 attribute vec4 position;
 attribute mediump vec4 textureCoordinate;
 varying mediump vec2 coordinate;
 
 void main()
 {
     gl_Position = position;
     coordinate = textureCoordinate.xy;
 }


I took my plugin code and implemented it as a stand alone app, doing the same thing, and everything worked.

Note:

  • I am aware that this example is slightly pointless, but it is a minimal example

  • I can read the camera image and upload it directly to a unity owned texture, but that is not what I want to do here.

Does anyone have any idea what is wrong?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hohohmm · Mar 06, 2012 at 04:39 AM 0
Share

Trying to do the same thing and want to know the answer as well. No one from Unity saw this?

avatar image s0phist · May 16, 2013 at 02:02 AM 0
Share

Also looking for an answer here! Any luck?

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Robert-Castle · Sep 14, 2013 at 06:11 PM

Finally, a solution to this question: https://github.com/robertcastle/UnityFBO

I am not sure what I was doing wrong in this example. I think it was a combination of not quite doing enough storing and reseting of the OpenGL state, and not calling GL.InvalidateState() after every plugin call in Unity.

This demo project I have put together works, and provides a starting block for anyone who wants to use an FBO with Unity. I hope it is helpful.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by thorbrian · Aug 07, 2012 at 04:47 PM

I think there are 2 things going on that are making that function not work.

First is that Unity uses VBO's on android, and it's leaving a VBO bound when you are calling your function. So when you are making the "glVertexAttribPointer" call, it's interpreting your "textureVertices" pointer as an offset into the bound VBO. See the glVertexAttribPointer docs here for more info on that: http://www.khronos.org/opengles/sdk/docs/man/xhtml/glVertexAttribPointer.xml

So you need to unbind Unity's VBO with a call to glBindBuffer with a buffer handle of 0, so like this:

 glBindBuffer(GL_ARRAY_BUFFER_BINDING, 0);

The other problem I think you are having is that you are trying to use the existing program handle when rendering the quad. I don't know what code you did in Unity land to try and set the current program before calling your native function, but when I've played around with this stuff, I was using material.SetPass(0), and that function was not setting the current program :(

The only way I could get stuff to render in a native plugin for android was to use a program created in the plugin itself, as Unity wasn't "glUseProgram"ing for me :P

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by s0phist · May 23, 2013 at 04:07 PM

I found upgrading from Unity3 to Unity4 solved a similar issue for me. I am now successfully setting of a FBO to copy from an arbitrary texture ID into a unity managed texture ID.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Extremely slow texture upload in plugin when a model is being drawn 1 Answer

Saving Unity Texture2D updated with OpenGL FBO via custom plugin 2 Answers

renderplugin example not working on ios 1 Answer

A node in a childnode? 1 Answer

Regarding Uwebkit and Zoomdata 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges