1) Load your bitmap into a power-of-two texture larger or roughly the
same size as it. 512x512 works or 256x512 if you don't mind losing a
bit of resolution. I won't post the code for that here. It's ugly
and there's plenty of examples.
2) Generate a quad's worth of geometry (it's simple, something like
this, or you can do it with floats if you want, just take out the
shift, make one 1.0f and use the gl "f" methods instead of "x")
int widthx = viewportWidth << 16;
int heightx = viewportHeight << 16;
verts = createDirectIntBuffer(new int[] {
// top left
0, 0, 0,
// top right
widthx, 0, 0,
// bottom right
widthx, heightx, 0,
// bottom left
0, heightx, 0 });
int one = 65536;
tex = createDirectIntBuffer(new int[] {
// top left
0, 0,
// top right
one, 0,
// bottom right
one, one,
// bottom left
0, one });
2) On draw:
// configure the projection
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glPushMatrix();
gl.glLoadIdentity();
GLU.gluOrtho2D(gl, 0, viewportWidth, viewportHeight, 0);
// switch to the model view
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glPushMatrix();
gl.glLoadIdentity();
// turn off depth-test for 2D
gl.glDisable(GL10.GL_DEPTH_TEST);
// draw the quad
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
gl.glFrontFace(GL10.GL_CW);
gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, tex);
gl.glVertexPointer(3, GL10.GL_FIXED, 0, verts);
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 4);
gl.glPopMatrix();
// turn back on depth-test for 3D
gl.glEnable(GL10.GL_DEPTH_TEST);
// put the projection back
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glPopMatrix();
gl.glMatrixMode(GL10.GL_MODELVIEW);
I use this for the buffer:
public static IntBuffer createDirectIntBuffer(int[] values) {
ByteBuffer vbb = ByteBuffer.allocateDirect(values.length * 4);
vbb.order(ByteOrder.nativeOrder());
IntBuffer intBuffer = vbb.asIntBuffer();
intBuffer.put(values);
intBuffer.position(0);
return intBuffer;
}
On Apr 28, 9:20 am, CMF <[email protected]> wrote:
> Thank String and Robert,I can create the quad but I would like to know
> how to make the quad with texture(a 320x480 bitmap) best fits with the
> screen?
>
> On Apr 28, 5:17 pm, Robert Green <[email protected]> wrote:
>
>
>
>
>
> > String is correct on using the matrix stack. Follow those
> > instructions and also consider just creating a textured quad and
> > rendering it in an orthographic projection. That's a common practice
> > for a nice, fast, easy background on a 3D scene. If you want it to
> > look far away just blur and darken it in your favorite 2d graphic
> > editor.
>
> > If you don't know how to set the projection to orthographic or create
> > a quad or set texture coordinates, etc, consider getting the red or
> > blue book on OpenGL. It's an excellent investment for your first GL
> > project.
>
> > On Apr 28, 2:32 am, String <[email protected]> wrote:
>
> > > On Apr 26, 7:24 am, CMF <[email protected]> wrote:
>
> > > > Hi all, I would like to ask how to add a static background to the kube
> > > > from the ApiDemo
>
> > > There are two ways that immediately occur to me. First, make it
> > > translucent and put it on top of a view containing your background
> > > image. See
> > > com.example.android.apis.graphics.TranslucentGLSurfaceViewActivity for
> > > an example.
>
> > > > I have created a sqaure behind the Kube, but it will rotate as the
> > > > kube does
>
> > > And this is the other approach. To make the cube rotate independently
> > > of your background, you should be able to restore the unrotated model-
> > > view matrix before your background-rendering code. Something like
> > > this:
>
> > > gl.glMatrixMode(GL10.GL_MODELVIEW);
> > > gl.glLoadIdentity();
> > > gl.glTranslatef(0, 0, -3.0f);
> > > gl.glScalef(0.5f, 0.5f, 0.5f);
>
> > > gl.glPushMatrix(); // This saves the unrotated matrix
> > > gl.glRotatef(mAngle, 0, 1, 0);
> > > gl.glRotatef(mAngle*0.25f, 1, 0, 0);
> > > mWorld.draw(gl);
> > > gl.glPopMatrix(); // This restores the unrotated matrix
>
> > > // Put your background-drawing code here
>
> > > I haven't tested the above code (it was written on the fly for this
> > > response) but that's the basic idea. I use that technique in a couple
> > > of places in various OpenGL apps.
>
> > > String
>
> > > --
> > > You received this message because you are subscribed to the Google
> > > Groups "Android Developers" group.
> > > To post to this group, send email to [email protected]
> > > To unsubscribe from this group, send email to
> > > [email protected]
> > > For more options, visit this group
> > > athttp://groups.google.com/group/android-developers?hl=en
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to [email protected]
> > To unsubscribe from this group, send email to
> > [email protected]
> > For more options, visit this group
> > athttp://groups.google.com/group/android-developers?hl=en
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group
> athttp://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en