[OpenGL] Creating a 20 tile wide grid [continued]

Problems with Canvas, OpenGL, etc...

[OpenGL] Creating a 20 tile wide grid [continued]

Postby Quipeace » Wed Jun 30, 2010 8:43 pm

Hello all,

After developing a number of "ordinary" applications I thought it would be a nice challenge to try and make a game. Because I'm used to using tiles as a platform I decided that that's the method I want to use.
For the record, it's basically a mix between mario and the 2d/3d approach of LittleBigPlanet.

Now here's the "problem", because I'm using these tiles I'd like that lets say 20 tiles exactly fit a fit the length of a screen (in landscape mode). So far i've been trying to get the correct Z coordinate by trial and error, but there must be an easier way to do this right?
(because I'm completely new to openGL I'm building off THIS guide)

Thank you very much ;)
Last edited by Quipeace on Thu Jul 01, 2010 5:41 pm, edited 1 time in total.
Quipeace
Developer
Developer
 
Posts: 27
Joined: Tue Feb 23, 2010 4:00 pm

Top

Re: [OpenGL] Creating a 20 tile wide grid

Postby impi » Thu Jul 01, 2010 12:33 am

You normally do this with an in projection mode, not adjusting the Z-value so that its fitting the screen.

Projection mode uses the Z-value only for sorting, and not for the actual depth in the scene. So ideal for 2D drawing.

You can easily switch between the modes from gl, and mix 2d with 3d graphics.

http://wiki.delphigl.com/index.php/Tutorial_2D

here is an very good tutorial which describes the modes, it's normal opengl for desktop, so there are statements that doesn't exist in opengl es, which android does use, but you can understand whats going on there.

Code: Select all
   @Override
   public void onSurfaceChanged(GL10 gl, int width, int height) {
      gl.glViewport(0, 0, width, height);
      gl.glMatrixMode(GL10.GL_PROJECTION);
      gl.glLoadIdentity();
      gl.glOrthof(0, width, height, 0, -1, 1);
      gl.glMatrixMode(GL10.GL_MODELVIEW);
      gl.glLoadIdentity();
   }


This is some initialization code for the projection matrix using the native screen resolution (480x320 on the G1)

If you want that your game looks the same on every device you need to scale manually, or set glViewport to an fixed resolution and gl scales for you. (ignoring the aspect ratio)

For more code you can look at the andengine, or simply use it.

Edit: aaargh i now see that the tutorial is in german, forgot this, sorry :). Well i you are german than it's probably better for you, if not, well there are a million tutorials on the net :)
impi
Developer
Developer
 
Posts: 29
Joined: Wed Apr 28, 2010 11:17 pm

Re: [OpenGL] Creating a 20 tile wide grid

Postby Quipeace » Thu Jul 01, 2010 7:38 am

Wew, Im obviously missing quite a part of basic openGL. I suppose I'll start from the beginning again, because I really want to make use of some 3D effects (tiles moving back and forth as you walk on them etc).

Sadly I'm not german, but I think I somewhat understood what they were trying to say.

EDIT: Alright, I ran into a problem.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         @Override
  2.         public void onSurfaceChanged(GL10 gl, int w, int h)
  3.         {
  4.             gl.glMatrixMode(GL10.GL_PROJECTION);
  5.             float ratio = w/h;
  6.             gl.glOrthof(-3, 3, -3/ratio, 3/ratio, 0.01f, 100.0f);
  7.             gl.glViewport(0, 0, w, h);
  8.             gl.glMatrixMode(GL10.GL_MODELVIEW);
  9.             gl.glEnable(GL10.GL_DEPTH_TEST);
  10.         }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

That's my current onSurfaceChanged, I'm calculating the aspect ratio from the width and height of the screen, however, the height is not set properly (the object is compressed on the vertical axis).
Manually calculating the aspect ratio (800/480) and multiplying my value with the result -does- work.

Am I missing something?

EDIT: Well, hard to believe, but it looks like the supplied width and height are infact right, but the calculated ratio is not!
I/Renderer( 2107): RATIO w:800 h:480 r:1.0
Quipeace
Developer
Developer
 
Posts: 27
Joined: Tue Feb 23, 2010 4:00 pm

Re: [OpenGL] Creating a 20 tile wide grid [continued]

Postby impi » Sat Jul 03, 2010 12:58 pm

just use gl.glOrthof(0, w, h, 0, -1, 1); for 0,0 at top-right

if you use the ratio method, you have an - in my opinion - very unpractiable positioning system, because you are in the range -3 to 3

edit: and for the ratio:

Code: Select all
float ratio = (float)w / float(h);
should do it.
impi
Developer
Developer
 
Posts: 29
Joined: Wed Apr 28, 2010 11:17 pm

Re: [OpenGL] Creating a 20 tile wide grid [continued]

Postby Quipeace » Sat Jul 03, 2010 1:55 pm

Thanks, that worked! Can't believe I missed that.
I'm using the ratio approach so that I only have to worry about screen resolutions once and then just use my own grid.
Quipeace
Developer
Developer
 
Posts: 27
Joined: Tue Feb 23, 2010 4:00 pm

Top

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

Who is online

Users browsing this forum: No registered users and 4 guests