OpenGL newbie questions

Problems with Canvas, OpenGL, etc...

OpenGL newbie questions

Postby afail » Wed Oct 27, 2010 7:18 pm

Hi, this is like 10th topic I post here and hopefully will get response this time, other times did not have much luck :)

I am starting game development with OpenGL as Android developer. So far I've been C++ developer and I think I won't have any problems (not so far) in learning OpenGL wrapped within Android. I am having though some questions that I'd like to hear from professionals but none in my surroundings exist, nor I've found info online:

1. I have created simple button over GLSurfaceView. The button is not Android button (View) but a simple Rect drawn with OpenGL and with Texture on it. When the user clicks on it I need some operation to be performed. I can't find way to map the 'rect' over the GLSurfaceView so that I check whether the user has clicked over the rect where the 'button' is drawn (I can't map the button's coordinates within the surface view).

2. I don't get the whole GLSurfaceView with the change of views and renderer. I.e. when the user clicks "Start" I need another view to appear showing another drawn elements on it. I suppose I would need another View but I guess I don't need another Renderer. How do I tell the same renderer (for different views) to draw/behave differently? I.e. to render different objects?

Thanks in advance,
bashing and critics is welcomed. :)
afail
Developer
Developer
 
Posts: 31
Joined: Sat Oct 02, 2010 8:43 pm

Top

Re: OpenGL newbie questions

Postby Schermvlieger » Wed Oct 27, 2010 7:32 pm

Hi afail,

q1:
Rect and RectF have a method called .contains()
Would that not be usable?
Schermvlieger
Senior Developer
Senior Developer
 
Posts: 159
Joined: Fri Feb 26, 2010 1:37 pm

Re: OpenGL newbie questions

Postby afail » Wed Oct 27, 2010 7:38 pm

Schermvlieger wrote:Hi afail,

q1:
Rect and RectF have a method called .contains()
Would that not be usable?


That would be lovely and was hoping for similar method :) but I am not using the Rect class, I am simply creating vertices and connecting them with a indexes. When I said 'rect' I meant 'rectangle that I get by connecting 4 points in the coord.system.'.

thanks.
afail
Developer
Developer
 
Posts: 31
Joined: Sat Oct 02, 2010 8:43 pm

Re: OpenGL newbie questions

Postby seed » Thu Oct 28, 2010 7:12 pm

afail wrote:1. I have created simple button over GLSurfaceView. The button is not Android button (View) but a simple Rect drawn with OpenGL and with Texture on it. When the user clicks on it I need some operation to be performed. I can't find way to map the 'rect' over the GLSurfaceView so that I check whether the user has clicked over the rect where the 'button' is drawn (I can't map the button's coordinates within the surface view).


I use OnTouch. I convert the x,y coordinates that onTouch provides to x and y coordinates that are correct for my OpenGL world and then I just do if (x>blah) and (x<blah) statements. I suppose I should write a little button class for this. In the example below, my world is 4 units tall with a y from -2 to 2. getHeight() and getWidth() are my own methods that return the height and width of the screen in pixels.

Code: Select all
public boolean onTouchEvent(MotionEvent event) {
   float x = event.getX();
        float y = event.getY();
       
        if(event.getAction() == MotionEvent.ACTION_UP) {
             // Convert to world x,y coordinates
             float ratio = 4f/((float) getHeight());
             y = getHeight() - y;
             y = y - ((float)getHeight())/2f;
             y = y * ratio;
             x = x - ((float)getWidth())/2f;
             x = x * ratio;
              
             if (x<0.4f && x>-0.4f && y<0.4f && y>-0.4f)
             {
                // Do button action
             }
          }
}

2. I don't get the whole GLSurfaceView with the change of views and renderer. I.e. when the user clicks "Start" I need another view to appear showing another drawn elements on it. I suppose I would need another View but I guess I don't need another Renderer. How do I tell the same renderer (for different views) to draw/behave differently? I.e. to render different objects?
[/quote]

That sounds complicated. Instead, I have a state variable for what state the view is in. Like VIEW_STATE_MENU or VIEW_STATE_PLAYING. In OnDraw, I just have and if statement that calls my menu_draw() routine when I am in the menu state and the game_draw() routine when the user is playing. You need to do a similar thing in your onTouch().

Hope this helps.
Charlie
Visit Exit 4 Gaming - http://www.exit4games.com/
Home of LavaBall - http://exit4games.com/?page_id=3
Home of Rebound - http://exit4games.com/?page_id=138
Home of Tap Crazy - http://exit4games.com/?page_id=219
Download "Caveman Pool" From the Market Today!
seed
Senior Developer
Senior Developer
 
Posts: 103
Joined: Mon Mar 15, 2010 3:22 pm

Re: OpenGL newbie questions

Postby afail » Thu Oct 28, 2010 8:31 pm

I get it man. You're the greatest. :) Never thought of the proportions of the OpenGL world within the Android View. Also the drawing technique you described is applicable. Will try this tomorrow morning. Wish you the best, and thanks!
afail
Developer
Developer
 
Posts: 31
Joined: Sat Oct 02, 2010 8:43 pm

Top

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

Who is online

Users browsing this forum: No registered users and 1 guest