Adding button on an OPENGL Screen - android

I have created a rotating CUBE with the help of OPENGL in Android now i want to add a listener to handle the rotation of the cube i.e. When i click button then CUBE must start rotating and vice versa..How can i add a button on an OPENGL Screen?

There are two options here:
Create a layout that includes both your GLSurfaceView and whatever buttons/settings you want. Use the standard OnClickListener for the button and have the listener call a method in your renderer to do the rotations. This is pretty much the easiest way to get this working, but the style of the button will change based on OS version and vendor. If you want to have more control over the button, you can try the other option.
Draw the button using OpenGL. After you render the cube, switch the projection matrix to an orthographic projection (glOrtho) with the same dimensions as your screen and draw a textured rectangle with your button image (You can also scale your button image like Android does by using 9 rectangles to keep the corners and edges sharp and just scale the inner part of the image which is usually a gradient or solid color). You'll have to set your renderer up to receive touch input and do a simple containment check that the pressed point is within the bounds of the button. If it is then you can call the method that handles rotation.
If you want a full-blown UI library, I'm sure there are several out there that exist for OpenGL ES on Android, and most of them would probably be part of larger game engines, which would handle most of the work for you. If you just want the single button though, I would recommend the first option as it's the simplest.

You can draw a Button object (rectangle) which remains always perpendicular to the camera for the
visual effect of a button. Everytime camera gets transformed the button object should also be relatively transformed.
The click part needs to be handled seperately thorough picking, i.e. you need to cast a ray from the centre of the camera and see if it intersects your 3D button object
Also if you were doing this on desktop you would have searched for "overlay GUI" libraries like
CEGUI which is quite good
http://www.cegui.org.uk/wiki/index.php/Main_Page
or else you could also use Navilibrary and SDL
http://princeofcode.com/navilibrary.php
In case of android you can explore the above libraries code (open source) and try to get the ideas from there.

Related

how to make a rectangle sprite stick to one side in Unity?

I am using a square sprite in Unity for making Android game. I want this rectangle to be always placed at the bottom of the screen. This strip acts as a ground and other objects can fall on it. I want the lower side of this rectangle to stick to the lower side of the screen. How to do it?.If i try to place it there manually then after I change the screen resolution the placement gets disturbed?Also the camera is not moving, so I only want to fix the position of this strip with respect to the camera once. (i think so). What should i do?
We need a little more information. Sprites at the lower side of the screen, usually intend to be in the UI. Is this the case? If it is, the way to do it is very different from a normal sprite in the world.
(Since I am not allowed to comment, I will try to answer for both cases, however I am not at a computer capable of running Unity, so I can't really provide a concrete answer (aka: Code))
UI:
Add an Image to your Canvas. Go to your anchor preset (inside the Rect Transform) and set it to the appropriate position. Then, in case you want the image to not be stretched, go to the Image component and Check on the "Maintain Aspect Ratio" (Or something like that) option. Add the sprite to the image and you are all set.
World:
Here, the situation is way more complicated. You first need to get the screen dimensions, then calculate the size of the object according to the aspect ratio, then have some Camera.ScreenToWorldPoint conversion and finally use the LookAt method in the Update() in order to have the sprite face the camera at all times. Or use the UI layer as described above, let's be honest that is probably what you need.

how to attach overlay to detected object using android and OpenCV

I'm writing an android app using OpenCV for my masters that will be something like a game. The main goal is to a detect a car in selected area. The "prize" will be triggered randomly while detecting cars. When the user will hit the proper car I want to display a 3D object overlay on the screen and attach it to the middle of the car and keep it there so when the user will change the angle of his view on the car, the object will also be seen from diffrent angle.
at the moment I have EVERYTHING beside attaching the object. I've created detection, I'm drawing the 3D overlay, I've created functions that allow me to rotate the camera etc. BUT I do not have any clue how can I attach the overlay to the specific point. Cause I don't have this I have no point to recalculate the renderer to change the overlay perspective.
Please, I really need some help, even a small idea will be fine:
How can I attach the overlay to the specific in real world
(Sorry, I couldn't comment. Need at least 50 points to do that ... :P )
I assume your image of the car is coming from a camera feed and you are drawing 3d car in opengl. If so, then you can try this:
You set the pixel format of the opengl layer as RGBA_8888, so that you can set the background of the opengl camera as a transparent color.
You take a relative layout as layout of your activity.
first you add the opencv camera layout to it as full height and width.
then you add opengl layer as full height and width.
you get the position of the real car from opencv layer as pixel value or something you did.
then scale it to your opengl parameters so that you can draw it on the right spot.
it worked for me. hope it works for you too.

Android OpenGL ES - ingame scoreboard

I am currently working on an Android OpenGL ES 2.0 2D game and I need to implement scrolling scoreboard, something like this:
but when there are so many players they overflow from the specified region (white). When I implement scrolling (using Matrix translation), the same problem happens on top of the list. Anyone can help me?
One approach is to use the scissor test to limit where drawing occurs. Set the scissor with glScissor(), enable it with glEnable(GL_SCISSOR_TEST), draw the text, and disable it with glDisable(GL_SCISSOR_TEST).
Note the scissor is specified in window coordinates.
Another approach would be to arrange the drawing such that the blue border is drawn on top of the text, either by setting the depth or drawing it last. (This assumes you're not drawing it with glClear().)

Gestures and OpenGL

I'm using OpenGL ES on android 2.3.3 at the minute to render a simple 3D game. I'd like to extend it using the built in gestures library, but I can't find a way to recognise the gestures from a GLSurfaceView as opposed to an android view(I don't have an XML layout is what I'm saying)
Is there any way to either implement an XML layout on top of what I have already, or preferably to implement the gestures library on top of the GLSurfaceview instead.
Thanks.
You can attach a normal onTouchListener to GLSurfaceView, so long as you have an instance of GLSurfaceView (which it sounds like is what you have). This is only really useful if you just want to know the raw x,y coordinates on screen where the user has pressed (e.g to rotate around the y if the user moves their finger left/right across the screen)
For gesture library, which I haven't used myself, you should be able to just place your GLSurfaceView inside a frame layout and then place another view (e.g. linear layout set to match_parent) in the same frame layout so that it completely covers the GLSurfaceView and is on top of it. Attach the gesture library to this view (and obviously make sure it is transparent so people can still see the GLSurfaceView below).
The gesture library has a way of 'stealing' events from the view. See here for details.
Here, here, and here are some examples that should make it clearer.

Android: how to have an animation on top of a background image

What I'm trying to do is have a background image, for sake of simplicity, lets say it's a picture of the front of a house. Then, I want to have a red ball move from window to window.
**I want to have a background picture, and a picture on top of it.
**I then want to be able to tell the top picture EXACTLY where to go.
How can I do this?
I'm just beginning to learn about animations in Android, and have not yet run across any way to do this.
There are two routes to animation in android: Canvas and OpenGL ES.
I would recommend OpenGL for anything requiring smoothness and speed, like a moving ball.
You should create a view using the helper class GLSurfaceView
http://android-developers.blogspot.com/2009/04/introducing-glsurfaceview.html, and implement a Renderer.
I assume you have the images saved in your res/drawable folders, in a format like png and the ball file contains an alpha channel.
You can see many tutorials online, but basically you need to load your background image and your ball resource at onSurfaceCreated and store it in a texture using GLUtils.texImage2D.
In the onDrawFrame method, you should set up a 2D projection such as glOrtho2D, then draw the background.
Then just before you draw the ball texture, you can use the glTranslate(x,y,0) function to move the ball over the house. Use an alpha blend for the ball:
glBlendFunc(GL_SRC_ALPHA, GL_SRC_ONE_MINUS_ALPHA);
glEnable(GL_BLEND);
Unfortunately writing in OpenGL isn't as straightforward as you might hope. Everything is done with 3D coordinates, despite the fact you want only a 2D image. But hopefully this gives you enough info to google for good exmaples, which are abundant!

Categories

Resources