GLU.gluOrtho2D, frustum culling and "picking"(?).

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

GLU.gluOrtho2D, frustum culling and "picking"(?).

Postby Skywhy » Fri Nov 06, 2009 7:17 pm

Hey guys!

I've been busy working on my game for Android but there are few things I'm still a bit feeling "wonky" over. Can I get the android screen pixels to match the world coordinates? I'm guessing not, never thought of this really.

But my main question is, since I'm doing a 2D game, how should I perform frustum culling for objects in my 2D game? Obviously I don't want to to render objects that are WAY off the screen :)

Also, what about "picking"? Since I'm going to do a game where place stuff into the game world by pressing the touch screen I guess I need to do some sort of conversion from screen coordinates to the actual world coordinates that OpenGL ES uses. How is this done?

Basically what I'm after I want to slam an object in the world on to the exact spot the user just pressed on :) Also if it matters at all I'm using the Glu.gluOrtho2D() method to produce my orthogonal view of the game world.

Cheers!


EDIT #1:
or does calling GLU.gluOrtho2D() already match the world coordinates to screen coordinates if I give it the screen W and H? If so, I can then easily just use screen coordinates to say that "place this item here", right? But if no then I need to do the screen space to world space conversion.

So easiest for me probably would be to able to match screen coordinates to openGL world coordinates. Any ideas?

// SkyWhy
Skywhy
Developer
Developer
 
Posts: 35
Joined: Tue Oct 20, 2009 7:55 pm

Top

Postby Skywhy » Sat Nov 07, 2009 12:57 pm

Now that was easy. After banging my head against the wall last night I gave up and this morning it just hit me.

Code: Select all
gl.glMatrixMode(GL10.GL_PROJECTION);
GLU.gluOrtho2D(gl, 0, _screenWidth, _screenHeight, 0);


There you go. Pixel "perfect" match to your screen size. Now the picking will be as easy as it is in 2D and the same thing with "is the object in the screen". Hurr.

Of course you can use what ever units you want instead of screen height and width but if you want to match to pixels then that's what you should do. :)

Cheers.


// SkyWhy
Skywhy
Developer
Developer
 
Posts: 35
Joined: Tue Oct 20, 2009 7:55 pm

Postby Kyros » Sat Nov 07, 2009 4:42 pm

This didn't work for me, I must be missing something.
Do you have a simple but complete example of this working?
Like maybe drawing something on the screen where you touch?
Kyros
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed Oct 21, 2009 11:49 am

Postby Kyros » Sat Nov 07, 2009 6:03 pm

Im using the draw texture extension and figured out what my problem was.
Using your method 0,0 for my is at the bottom left not top left so I had to flip the y value so it would draw at top left.
Kyros
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed Oct 21, 2009 11:49 am

Postby Skywhy » Sun Nov 08, 2009 2:11 pm

That's strange, probably something else wrong with your program since it works just fine for me. I don't have a complete example for you but here's something.

Declaration of buffers:
Code: Select all
   ByteBuffer vbb = ByteBuffer.allocateDirect(3* 2 * 4);
        vbb.order(ByteOrder.nativeOrder());
       
        mFVertexBuffer = vbb.asFloatBuffer();
        ByteBuffer ibb = ByteBuffer.allocateDirect(3* 2);
        ibb.order(ByteOrder.nativeOrder());
       
        mIndexBuffer = ibb.asShortBuffer();
        float[] coords = {  0.0f,  0.0f, // (x1,y1)
                          0.0f,  20.0f,
                          20.0f,  20.0f };

      for (int i = 0; i < 3; i++)
            for(int j = 0; j < 2; j++)
                mFVertexBuffer.put(coords[i*2+j]);
           
       
        short[] myIndecesArray = {0, 1, 2};
       
        for (int i=0;i<myIndecesArray.length;i++)
           mIndexBuffer.put(myIndecesArray[i]);
       
        mFVertexBuffer.position(0);
        mIndexBuffer.position(0);

And there, a triangle is defined to start from 0.0 and is 20 pixels "tall" and "wide". Nothing special there really.

Rendering the triangle:
Code: Select all
gl.glColor4f(1.0f, 0, 0, 0.5f);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, mFVertexBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, VERTS, GL10.GL_UNSIGNED_SHORT, mIndexBuffer);


The triangle is rendered in the top left corner.

EDIT #1: fixed simple bug from indice list ;)
Skywhy
Developer
Developer
 
Posts: 35
Joined: Tue Oct 20, 2009 7:55 pm

Top

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

Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests