Draw 2d(controls) over 3d scene

Problems with Canvas, OpenGL, etc...

Draw 2d(controls) over 3d scene

Postby randomcracker » Mon Sep 27, 2010 4:32 pm

I'm sure this has been asked a thousand times, even though I have yet to find the answer, but I have searched for a week now and I just can't get the hang of it. I'm trying to draw a bitmap(in 2d space directly to the screen) over an opengl 3d scene. From what I understand you have to load the bitmap as a texture and then there's a lot of stuff about matrices that I don't understand. The main question I have is why is there not a simple way like canvas.drawBitmap(bitmap, x, y) instead of all that other stuff, I honestly don't see the point. Mostly I get frustrated because I don't understand matrices, is there anywhere a description for someone that's a little slow to learn this thing? Please can someone give me a simple example, like a cube in 3d space, and then arrows in 2d space that control rotation of the cube? Just basically how to draw bitmaps over a 3d scene.
Freedom in Death <3
randomcracker
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Feb 20, 2010 11:03 am

Top

Re: Draw 2d(controls) over 3d scene

Postby tttcowan » Tue Oct 05, 2010 1:16 pm

There is, I've been struggling on this for the last few days, use

((GL11Ext) gl).glDrawTexfOES

That function draws a 2d texture directly to the screen... You've also got to crop the texture before drawing it as well but I don't have access to source code at the moment. Have a look at replicaisland source code, everything in there is drawn using glDrawTexfOES. Theres barely any documentation on the web about it.
tttcowan
Developer
Developer
 
Posts: 46
Joined: Mon Jul 19, 2010 11:08 pm

Re: Draw 2d(controls) over 3d scene

Postby moblcade.com » Tue Oct 05, 2010 4:10 pm

Yes, glDrawTex is the easiest, so long as it's supported by the device. It's not guaranteed. The next way is to, after having drawn your entire 3D scene, is to clear the Z buffer, set up an orthogonal projection, then render textured quads to the screen. This is, IMO the /better/ way to do it because glDrawTex* does not support rotation of textures, only scaling. Building your 2D functionality in using geometry instead of direct texture rendering will be more flexible. The problem is that in Android pushing vertices is far slower than glDrawTex. This could be due to an known bug in Android's implementation of FloatBuffer, but since this bug lives on hundreds of thousands of phones that are already out there and not going away any time soon, you haven't much of a choice but to work around it.

And yes, you'll have to set up the texture crop parameters before you call glDrawTex.

The relevant code for rendering a quad with glDrawTex:
Code: Select all
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
int[] crop = new int[] {
    sourceTextureX, sourceTextureY,  // Coordinates to use from the SOURCE texture. NOT where it will be drawn to the screen
    sourceTextureWidth, -sourceTextureHeight  // use a negative height to flip the image upside down
}

gl.glEnable(GL10.GL_TEXTURE_2D);
((GL11) gl).glTexParameteriv(GL10.GL_TEXTURE_2D, GL11Ext.GL_TEXTURE_CROP_RECT_OES, crop, 0);
((GL11) gl).glDrawTexiOES(destinationX, destinationY, destinationZ /* typically 0 */, sourceTextureWidth, sourceTextureHeight);
moblcade.com
Junior Developer
Junior Developer
 
Posts: 19
Joined: Tue Sep 07, 2010 6:31 pm

Re: Draw 2d(controls) over 3d scene

Postby randomcracker » Tue Oct 05, 2010 5:25 pm

Thanks guys, although it doesn't work with a negative sourceTextureHeight. Had to use the posetive although then it got upsidedown of course...
Freedom in Death <3
randomcracker
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Feb 20, 2010 11:03 am

Re: Draw 2d(controls) over 3d scene

Postby moblcade.com » Tue Oct 05, 2010 5:38 pm

randomcracker wrote:Thanks guys, although it doesn't work with a negative sourceTextureHeight. Had to use the posetive although then it got upsidedown of course...


That's why you supply the negative source texture height. It flips the image. Try drawing at destinationY + height and see what happens when you supply negative sourceTextureHeight.
moblcade.com
Junior Developer
Junior Developer
 
Posts: 19
Joined: Tue Sep 07, 2010 6:31 pm

Re: Draw 2d(controls) over 3d scene

Postby randomcracker » Tue Oct 05, 2010 5:41 pm

moblcade.com wrote:
randomcracker wrote:Thanks guys, although it doesn't work with a negative sourceTextureHeight. Had to use the posetive although then it got upsidedown of course...


That's why you supply the negative source texture height. It flips the image. Try drawing at destinationY + height and see what happens when you supply negative sourceTextureHeight.


Doesn't work, it doesn't draw it correctly.
Freedom in Death <3
randomcracker
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Feb 20, 2010 11:03 am

Top

Re: Draw 2d(controls) over 3d scene

Postby moblcade.com » Tue Oct 05, 2010 6:09 pm

randomcracker wrote:
moblcade.com wrote:
randomcracker wrote:Thanks guys, although it doesn't work with a negative sourceTextureHeight. Had to use the posetive although then it got upsidedown of course...


That's why you supply the negative source texture height. It flips the image. Try drawing at destinationY + height and see what happens when you supply negative sourceTextureHeight.


Doesn't work, it doesn't draw it correctly.


Strange... this code is copied from my working application.
moblcade.com
Junior Developer
Junior Developer
 
Posts: 19
Joined: Tue Sep 07, 2010 6:31 pm

Re: Draw 2d(controls) over 3d scene

Postby randomcracker » Tue Oct 05, 2010 6:13 pm

Yeah no, I got it to work just now, changed the sourceTextureY to include height. Maybe that's what you ment? Anyhow GREAT! Finally an easy way to do this, thanks a lot!! Now I only have to get the 3d stuff to work. I'm trying to implement the modelloader.jar for android but I'm not having much luck with it. I can't understand why it works to draw a model in one place but as soon as I send the gl10 object to another class to draw using the exact same code it just doesn't draw anything. Wierd stuff this is.
Freedom in Death <3
randomcracker
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Feb 20, 2010 11:03 am

Re: Draw 2d(controls) over 3d scene

Postby moblcade.com » Tue Oct 05, 2010 6:21 pm

randomcracker wrote:Yeah no, I got it to work just now, changed the sourceTextureY to include height. Maybe that's what you ment? Anyhow GREAT! Finally an easy way to do this, thanks a lot!! Now I only have to get the 3d stuff to work. I'm trying to implement the modelloader.jar for android but I'm not having much luck with it. I can't understand why it works to draw a model in one place but as soon as I send the gl10 object to another class to draw using the exact same code it just doesn't draw anything. Wierd stuff this is.


A lot of times it is because you haven't set up OpenGL properly. My issues usually are one of these:
* Forget to use a native buffer
* Forget to enable the correct gl state
* Incorrect modelview or projection matrix.
* Forget to check for GL errors when debugging.
moblcade.com
Junior Developer
Junior Developer
 
Posts: 19
Joined: Tue Sep 07, 2010 6:31 pm

Re: Draw 2d(controls) over 3d scene

Postby randomcracker » Tue Oct 05, 2010 6:35 pm

Well, I don't see what can go wrong, I'm simply moving the code to a diffrent function in a diffrent class and calling it from where the code originally was.
Freedom in Death <3
randomcracker
Junior Developer
Junior Developer
 
Posts: 19
Joined: Sat Feb 20, 2010 11:03 am

Re: Draw 2d(controls) over 3d scene

Postby lukasz.iwanski » Mon Nov 08, 2010 2:26 pm

Hi Guys,

This solution is great! But I need help.
I have used your code to draw 2D sprite over 3D and now it gets complicated:
on my Desire phone it looks great!
but Eclipse shows black square.

What I want to do is to create simple 2D animation - you may remember doom and guys face.. starts bleeding when life is below some value.. and now is my question:

is there any simple way of doing that so Eclipse will display that properly or do I have to check that on my phone?

Kind Regards,
Luke
Deep Beta - That's the way the cookie crumbles
User avatar
lukasz.iwanski
Freshman
Freshman
 
Posts: 2
Joined: Fri Nov 05, 2010 8:56 pm
Location: Edinburgh

Top

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

Who is online

Users browsing this forum: No registered users and 4 guests