2D and 3D together

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

2D and 3D together

Postby shredder » Fri Apr 11, 2008 4:22 am

Hi,

I have this problem of rendering 2D objects on GLSurfaceView.

We have already known, View are not openGL-able on m5. A tragedy for me as I could not figure out a way to use raw 2D painting methods. Trying to force Android painting causes unusual flickering.

My current method is to create a 2D array of int as a backbuffer like this:


[font=Lucida Console]w = screen width
h = screen height[/font]
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 m_backBufferW = NextPow2( w);
  2.  
  3.                 m_backBufferH = NextPow2( h);
  4.  
  5.                 m_backBuffer = new int[m_backBufferW*m_backBufferH];
  6.  
  7.  
  8.  
  9.                 m_texBackBuffer = CGlobal.GenerateTexture(); // New texture ID
  10.  
  11.                
  12.  
  13.                 float[] screenVerts = { 0.0f, 0.0f, -1.0f,
  14.  
  15.                                                                 0.0f, (float)h, -1.0f,
  16.  
  17.                                                                 (float)w, (float)h, -1.0f,
  18.  
  19.                                                                 (float)w, 0.0f, -1.0f };
  20.  
  21.                 float[] screenUVs = { 0.0f, (float)h/m_backBufferH,
  22.  
  23.                                                           0.0f, 0.0f,
  24.  
  25.                                                           (float)w/m_backBufferW, 0.0f,
  26.  
  27.                                                           (float)w/m_backBufferW, (float)h/m_backBufferH };
  28.  
  29.                                                          
  30.  
  31.                 short[] screenIndices = { 0, 1, 3, 1, 2, 3 };
  32.  
  33.                
  34.  
  35.                 m_screenVertices = CGlobal.Array2Buffer( screenVerts );
  36.  
  37.                 m_screenUV = CGlobal.Array2Buffer( screenUVs );
  38.  
  39.                 m_screenIndices = CGlobal.Array2Buffer( screenIndices );
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


When I need to flush 2D graphics, I push all pixel data into the texture, and draw a quad:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                CGlobal.EnableTexture();
  2.  
  3.                
  4.  
  5.                 ByteBuffer buf = ByteBuffer.allocateDirect( m_backBufferW * m_backBufferH * 4 );
  6.  
  7.                 buf.order( ByteOrder.BIG_ENDIAN );
  8.  
  9.                 IntBuffer bb = buf.asIntBuffer();
  10.  
  11.                 bb.put( m_backBuffer.m_data );
  12.  
  13.                 bb.position( 0 );
  14.  
  15.                 buf.position( 0 );
  16.  
  17.                
  18.  
  19.                 CGlobal.PushTextureData( m_texBackBuffer, m_backBufferW, m_backBufferH, buf );
  20.  
  21.                
  22.  
  23.                 CGlobal.OrthoProjection();
  24.  
  25.                 CGlobal.ResetWorld();
  26.  
  27.                 CGlobal.DisableDepth();
  28.  
  29.                
  30.  
  31.  
  32.  
  33.                 CGlobal.SetVerticesPointer( m_screenUV, m_screenVertices );        
  34.  
  35.                 CGlobal.DrawTriangles( m_screenIndices );        
  36.  
  37.                 CGlobal.Flush();
  38.  
  39.                
  40.  
  41.                 CGlobal.EnableDepth();
  42.  
  43.                 CGlobal.ResetProjection();     
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


It works, but I am afraid it's slow.

I really appreciate any solution superior to this.
shredder
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon Mar 03, 2008 2:34 pm

Top

Postby shredder » Fri Apr 11, 2008 9:54 am

Also, this was how I tried:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                                 glc.waitNative();              
  3.  
  4.                                 Paint_3D_Stuffs();
  5.  
  6.                                
  7.  
  8.                                 // Once we're done with GL, we need to call post()                               
  9.  
  10.                                 glc.post();
  11.  
  12.                                
  13.  
  14.                                 glc.waitGL();
  15.  
  16.                                 Canvas c = holder.lockCanvas();
  17.  
  18.                                 Paint_2D_Sutffs();
  19.  
  20.                                 holder.unlockCanvasAndPost( c );       
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


But, in vain.
shredder
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon Mar 03, 2008 2:34 pm

Postby Hungable » Sat Apr 12, 2008 12:48 am

hi, what do you mean by a view is not opengl-able? I have been forever using a surfaceview for my opengl view! my application is very slow though, could this be a reason? displaying above 300 triangles per frame makes my app virtually unusable, <1 FPS :(

Any ideas? Even if I load the vertex/texture buffers only once each frame and call DrawElements once!!, it's still fking slow...

to answer yr question a bit. do you want to use 2D for a HUD-like display? you should put your opengl view in a framelayout from xml, and then you can put other UI stuff on top or next to it or whatever you want to achieve.
Hungable
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Apr 11, 2008 10:40 pm

Postby shredder » Sat Apr 12, 2008 3:06 am

I am sorry if I was too panic to be clear, View means plainly "View", not "SurfaceView".

I have tried using "View", and it errors "Not supported"
shredder
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon Mar 03, 2008 2:34 pm

Postby shredder » Sun May 11, 2008 4:48 am

Hi again,

Finally I have done the 2D by using 3D quads, and 3D quads only. It was terrible moment to do it from a mixed 2D-3D game, but the result worths it.

Unfortunately, it is forbidden to post company's code here. If anyone interests, please read "Focus On 2D in Direct3D (Premier Press)". It covers much of the point, though it is DirectX.
shredder
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon Mar 03, 2008 2:34 pm

You can use an overlay!

Postby teacake » Fri May 16, 2008 8:15 pm

Hi shredder.
In m3 series SDK's you could use a View for OpenGL rendering. As an added bonus, you could use the onDraw method to draw 2d objects. In m5 series this is no longer supported so you have to use a SurfaceView to render OpenGL and you can use a View "overlayed" on top of the surface. That way you can draw 2D objects on top of OpenGL objects. For more details you can take a look at my blog. This is the approach I took for my own project monolithandroid.
teacake
Junior Developer
Junior Developer
 
Posts: 14
Joined: Fri Jan 11, 2008 1:04 am
Location: Kalamata, Greece

Top

Postby shredder » Sat May 17, 2008 4:17 am

I am truly appreciated by your hints and all. However, this is how I used to do it:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         c = holder.lockCanvas();               
  3.  
  4.         CGlobal.s_canvas = c;
  5.  
  6.                                                                                        
  7.  
  8.         CGlobal.ClearDepth(); // 3D                            
  9.  
  10.  
  11.  
  12.         // game logic and painting go here, also use CGlobal.s_canvas to draw 2Ds
  13.  
  14.         s_cGameInstance.Run();
  15.  
  16.                                
  17.  
  18.         holder.unlockCanvasAndPost( c );
  19.  
  20.         glc.post();
  21.  
  22.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Simple, isn't it :lol:

But it's slow and unrecommended even with 3D-accelerated. Just put it here if somebody interests.
shredder
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon Mar 03, 2008 2:34 pm

Top

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest