Textured cube. OpenGL code sample.

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

Postby kasas » Thu Jul 03, 2008 4:44 pm

I don't kwow why this example doesn't work... it is something wrong with GL10 because it always hangs when using some opengl functions...
Posts: 5
Joined: Thu May 22, 2008 6:39 pm
Location: Barcelona


Postby pravin » Wed Nov 12, 2008 11:41 am

How do i get the OpenGLContext class it is not present in android.jar?
Please reply
I am unable to run the application
Junior Developer
Junior Developer
Posts: 13
Joined: Wed Nov 12, 2008 11:26 am
Location: India

Postby MrSnowflake » Wed Nov 12, 2008 12:24 pm

This tut was build on a previous sdk. Take a look in the samples dir of the SDK, there's an OpenGL example.
User avatar
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby maczoman » Fri Nov 21, 2008 12:27 am

eugenk in your code you use TEX_SIZE const... Can you fix it to for example TEX_WIDTH and TEX_HEIGHT. Coz if you have rectangle shaped texture it wont work...

I have 256x64 texture and I have now problem :) I guess I could split it into 4 * 64x64 or merge this pic into bigger square with other textures. But still it would be nice to know which TEX_SIZE in your code is width and which height of texture.

thx in advance.
Posts: 4
Joined: Sun Nov 09, 2008 10:38 am

SDK--Most recent

Postby cj » Sat Jan 03, 2009 6:53 pm

Summarily: You can create a textured cube using some of google's API Demo classes & Eugen's TCube inner class, with a few modifications.

For what's worth, you can get a textured cube by doing a little hacking on the classes that are available in the API Demos, and creating a separate class from the inner TCube class with Eugen's posted code. The classes within the API Demos you can use are GLSurfaceView and CubeRenderer. Create a new project using the eclipse plugin. Within the Activity class (make similiar to Cube.java of the API Demo code--graphics package for all these files), create a GLSurfaceView instance. Set the renderer to the CubeRenderer. I created a private Context property for the CubeRenderer, and then added a new constructor to the CubeRenderer that took an additional parameter, which is the context. The context is passed from the GLSurfaceView instance in the Activity by calling it's respective getContext method.

Within the CubeRenderer's surfaceCreated implementation, I added the below code after the translucent background code. (The comments in the surfaceCreated method of the Renderer interface in GLSurfaceView specifically calls out doing the texturing in this methods implementation. When you initailize TCube, you are doing texturing.)


tCube = new TCube(gl,this.mContext); //<--This is declared private TCube tCube; in CubeRenderer.

Then, in CubeRenderer comment out calls to "mCube", and add the following three lines in the drawFrame implementation after the ...GL_COLOR_ARRAY statement:


These are the important changes. Summarily, I'm just saying that with a little hacking of the aforementioned classes, you can create a decent textured cube requiring very little change to Eugen's TCube implementation. (I think the only change I made was cut-and-pasting the TCube code into it's own class, and dropping a couple of import statements.)
Once Poster
Once Poster
Posts: 1
Joined: Sat Jan 03, 2009 6:32 pm

Re: SDK--Most recent

Postby Ciryon » Thu Feb 19, 2009 12:25 am

With the help of this great tutorial, I managed to create an application with textured rotating cubes, which works perfectly.
However, when I changed one of the textures to a transparent png (thereby having a non-trivia alpha channel), it did not turn out as expected (the corresponding face of the cube became semi-transparent (as intended) but gray, thus not showing any hint of the original texture, which is still present on the png).

I have no deep knowledge of openGL-functions and started playing around with several of the parameters, obviously without having any success (other than creating a totally blank screen, of course ;) ).

Does anyone have an idea what to do about that?

Thanks in advance,
Once Poster
Once Poster
Posts: 1
Joined: Thu Feb 19, 2009 12:17 am


Re: Textured cube. OpenGL code sample.

Postby whatever12684 » Wed Mar 04, 2009 11:26 am

[quote="eugenk"]This is a simple OpenGL texturing example. It based on Colored 3D cube example (http://www.anddev.org/colored_3d_cube-t4.html), and I assume that you are familiar with it, before to try my code. Textured cube example shows two cubes. One - colored 3D cube. And other - textured. This code help you to understand how works texturing feature in Android OpenGL. Enjoy !

P.S plusminus, sorry. but can you increase attachment limit ? 256Kb is too small, to upload projects with some artworks. Regards, Eugene.

P.P.S. The second question. How to upload picture to this forum ? I've try it, but all inserting buttons requires URL. I have not my own website, so cannot apply weblink. So I need to upload my picture here directly. How can I do it ?[/quote]

Thanks a lot! I needed the load texture help! my code is cleaner now, thanks.
Posts: 4
Joined: Sun Mar 01, 2009 10:07 am

Postby rafa_perfeito » Thu May 21, 2009 1:13 pm

cj, i also tried do follow that line of thought: use new rendering and view examples from the android dev website and TCube class from this tutorial. But i wasn't successful...

The TCube constructor uses a GL10 gl object to generate textures (gl.glGenTextures(6, texBuf); ). The problem is that i can't do the equivalent to 'gl = (GL10)(GLContext.getGL());' in the new SDK because GLContext...well, seems to be gone! I've tried EGLContext but i can't instantiate it...

How did you solve this? It would be cool if you shared your code...

Posts: 3
Joined: Thu May 14, 2009 5:36 pm

how to detect the face of cube where touch event happened

Postby srispis » Fri Jan 22, 2010 5:40 am

Hi -
I am trying to use the TCube with texture and code works like gem.
All 6 faces, I used different types of images.

Now, I wanted to extend this to detect the touch event and map it the face of cube where it happened.
I am not able to succeed on translating screen coordinates to open gl coordinates and also, not able to find a good example to translate this to the face.

I appreciate, if someone could help with this.
Posts: 2
Joined: Thu Jan 21, 2010 3:24 pm


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

Who is online

Users browsing this forum: No registered users and 3 guests