OpenGL, how to a screen size independent object.

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

OpenGL, how to a screen size independent object.

Postby Hachaso » Wed Feb 03, 2010 11:25 am

Hi!

How can I create for instance a plane that is screen size independent. I would like to have a rectangle with a texture, that shows a photo. I would like it to be fullscreen always. The goal is that is should take the whole screen no matter the screen size of the phone. Is this possible to do?
If so, how?

Please help me find a possible solution to this problem I'm having.

Thanks
Hachaso
Developer
Developer
 
Posts: 43
Joined: Mon Jan 26, 2009 11:44 am

Top

Postby MichaelEGR » Wed Feb 03, 2010 3:19 pm

There are couple ways. One is orthographic projection which I'll provide code and the other is to align by the projection/camera a textured quad (triangle strip, etc.). This can be done with free form rotation translation (you can animate the image rotating / flipping etc) or even by moving / orienting a camera differently. Also look up billboarding (plenty of OpenGL tutorials online) as there is a way to align textures to the eye space such that the textured quad always faces the direction the camera is facing.

check out orthographic projection as essentially you'll be working in 2D space though this may not be what you want.. Sounds like you want the 3D case.

-------------------

The following Typhon4Android framework code sets ortho mode binds a texture and draws a triangle strip that repeats. The x2 texture coordinate being 3.75 because 128 * 3.75 is 480. The texture size is 128x128.

glUtil.setOrtho(480, 320);
backgroundTexture.setTexParameters();
Texture.drawTexturedQuad(0, 0, 480, 320, 0, 0, 3.75f, 2.5f);
glUtil.resetOrtho();

-------------------------

full code in methods still..

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.    public void setOrtho(int width, int height)
  3.  
  4.    {
  5.  
  6.       gl.glMatrixMode(GL_PROJECTION);                                      // Select Projection
  7.  
  8.       gl.glPushMatrix();                                                                 // Push The Matrix
  9.  
  10.       gl.glLoadIdentity();                                                          // Reset The Matrix
  11.  
  12.       gl.glOrthof( 0, width, height, 0, -1, 1 );                // Select Ortho Mode
  13.  
  14.       gl.glMatrixMode(GL_MODELVIEW);                                       // Select Modelview Matrix
  15.  
  16.       gl.glPushMatrix();                                                                 // Push The Matrix
  17.  
  18.       gl.glLoadIdentity();                                                          // Reset The Matrix
  19.  
  20.    }
  21.  
  22.  
  23.  
  24.    public void resetOrtho()
  25.  
  26.    {
  27.  
  28.       gl.glMatrixMode(GL_PROJECTION);                 // Select Projection
  29.  
  30.       gl.glPopMatrix();                                                             // Pop The Matrix
  31.  
  32.       gl.glMatrixMode(GL_MODELVIEW);                                       // Select Modelview
  33.  
  34.       gl.glPopMatrix();                                                             // Pop The Matrix
  35.  
  36.    }
  37.  
  38.  
  39.  
  40. //NOTE this is a method of a larger Texture class/object in Typhon. It presumes you have a texture ID and the wrap / //min/mag filter settings, etc.
  41.  
  42.  
  43.  
  44.    public void setTexParameters()
  45.  
  46.    {
  47.  
  48.       gl.glBindTexture(GL_TEXTURE_2D, id);
  49.  
  50.       gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS);
  51.  
  52.       gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT);
  53.  
  54.       gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  55.  
  56.       gl.glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
  57.  
  58.       gl.glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, mode);
  59.  
  60.    }
  61.  
  62.  
  63.  
  64. // NOTE - vertexBuffer and textureBuffer are VectorBuffer3f and VectorBuffer2f additional NIO buffer wrappers from the //Typhon framework. These are backed by underlying NIO FloatBuffers (so you could create a FloatBuffer and fill it //sequentially)
  65.  
  66.  
  67.  
  68.    public static void drawTexturedQuad(float x, float y, float width, float height, float tx, float ty, float tw,
  69.  
  70.     float th)
  71.  
  72.    {
  73.  
  74.       vertexBuffer.put(x, y, 0);
  75.  
  76.       vertexBuffer.put(x + width, y, 0);
  77.  
  78.       vertexBuffer.put(x, y + height, 0);
  79.  
  80.       vertexBuffer.put(x + width, y + height, 0);
  81.  
  82.       vertexBuffer.rewindWrite();
  83.  
  84.  
  85.  
  86.       textureBuffer.put(tx, ty);
  87.  
  88.       textureBuffer.put(tx + tw, ty);
  89.  
  90.       textureBuffer.put(tx, ty + th);
  91.  
  92.       textureBuffer.put(tx + tw, ty + th);
  93.  
  94.       textureBuffer.rewindWrite();
  95.  
  96.  
  97.  
  98.       gl.glVertexPointer(3, 0, vertexBuffer.getReadBuffer());
  99.  
  100.       gl.glTexCoordPointer(2, 0, textureBuffer.getReadBuffer());
  101.  
  102.       gl.glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  103.  
  104.    }
  105.  
  106.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org
http://www.egrsoftware.com
User avatar
MichaelEGR
Senior Developer
Senior Developer
 
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

Postby Hachaso » Wed Feb 03, 2010 9:33 pm

Thanks a lot.

Yes I would like to have a 3D solution for my problem.
If I understand it correctly I can't use glOrtho with 3D?
I would like to apply some kind of animation to my plane maybe moving it in Z direction later on...but the Start position should always be fullscreen facing the camera.

Is it possible to maybe user glOrtho for startup but then changing the projection on runtime ?

I'm really new at this.

Thanks for all the help
Hachaso
Developer
Developer
 
Posts: 43
Joined: Mon Jan 26, 2009 11:44 am

Postby MichaelEGR » Wed Feb 03, 2010 11:11 pm

"I'm really new at this."

No worries. GL dev takes a while to wrap ones head around it. And as things go to do things right at this moment you need to learn OpenGL ES 1.x which is totally different that 2.x which is imminently accessible via Android.

You can switch projection modes at will and mix 3D and ortho content. Usually with 2D text rendering for instance you render the 3D scene switch to ortho projection render text essentially on top each frame (then switch back from ortho projection).

Yeah. For 3D you'll need a camera object and / or setup the projection manually once.. For manipulating an image in 3D essentially you create a FloatBuffer and store relative points around 0. Say (-100, -100, 100, 100) in a triangle strip compatible way then using vertex arrays or vertex buffer objects call glDrawArrays after using appropriate glTranslatef and glRotatef with in modelview mode. Don't forget to push/pop the modelview matrix before translate/rotate of each image.

As things go I am imminently releasing a comprehensive framework for Android that handles a lot of this and more.
Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org
http://www.egrsoftware.com
User avatar
MichaelEGR
Senior Developer
Senior Developer
 
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

Postby Hachaso » Thu Feb 04, 2010 7:56 pm

Thanks a lot for all the help.

Don't know if this is true but I read in some forum that OpenGL handles the screen size independence automatically.
For instance if I create a Cube, place it to cover the whole screen on a phone... If I runt the same application with the cube on another phone with smaller or bigger screen, it will cover the whole screen as well.
Is this really correct?
Hachaso
Developer
Developer
 
Posts: 43
Joined: Mon Jan 26, 2009 11:44 am

Postby MichaelEGR » Fri Feb 05, 2010 12:53 am

>"Thanks a lot for all the help."

[s]So switching back to the modelview matrix and drawing the triangle gave the correct results right?[/s]
oops that was a reply for a different thread. :)

>"Don't know if this is true but I read in some forum that OpenGL handles the screen size independence automatically."

This is true when dealing with 3D perspective/projection. In ortho mode you will be dealing with the particular resolution of the screen.

That really is the power and one of the freebies when using OpenGL over say the Android 2D API working in screen coordinates because of the projection aspect is essentially mapping a 3D space onto a 2D screen and it takes into account behind the scenes the viewport / screen size in doing this so you get comparable results on different screen sizes.
Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org
http://www.egrsoftware.com
User avatar
MichaelEGR
Senior Developer
Senior Developer
 
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

Top

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

Who is online

Users browsing this forum: No registered users and 3 guests