Rotating the text on the face of cube

hi,

I am new to coding using OpenGL. I have drawn a cube and can rotate it successfully. I wanted to know how I can print text on the face of the cube (not using an image file)and rotate it so it looks like the text is printed on the face of the cube.

I tried using

gl.glRasterPos3f(-1.5f, 1f,1.0f);
glut.glutBitmapString(GLUT.BITMAP_TIMES_ROMAN_24, " My text");

but when the cube rotates…the text is all messed up.
Please help…

(not using an image file) -> well you should at least use a texture. It can be generated at runtime, glutBitmapString to an empty background, glCopyTexImage it to a texture, clear screen (without swapping buffers yet, so the first text will not be visible), then apply the texture to the cube.

Feel free to ask if you need more details, but searching for opengl texture tutorials should help enough.

Thanks Zbuffer,

I can now get to copy the text on to all sides of cube. I am putting the code in the init function. I am facing problem that everytime I run the code, I am getting different shades and different colors on my screen,even though I am clearing the screen before copying the text and also after copying the text.

the following is added to init :

gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
gl.glShadeModel(GL.GL_SMOOTH);

gl.glEnable(GL.GL_TEXTURE_2D);
texture = genTexture(gl);
gl.glBindTexture(GL.GL_TEXTURE_2D, texture);
glut.glutBitmapString(GLUT.BITMAP_TIMES_ROMAN_24, " heeeeeeelloooo");
gl.glCopyTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, 0, 0, 128, 128, 0);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

Do I need to clear the screen once program exits??

I am drawing 4 different cubes. I wanted the different text to be different cubes. How can I achieve that?

Thanks.

Also I wanted to know how to make the sides of the cube (i.e faces of cube) opaque so that one cannot see the face on the back side when viewing from the front.

glClearColor is just setting the color used to clear the color buffer.

if you want to clear the screen to black at the beginning of each frame, you could do like this:

glClearColor( 0, 0, 0, 0 );
glCLear( GL_COLOR_BUFFER_BIT );

if the back faces of your cube is visibale, I guess you do not enable the depth test :

glClearDepth( 1.0f );
glCLear( GL_DEPTH_BUFFER_BIT );
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LEQUAL );

I am having 5 cubes.I rotate view by 360 degrees so that I can see them from all side. Now at some positions they look like as if cube at the back is going into the cube in front of it. So how can I fix this? I have included this in my code as mentioned:

glClearDepth( 1.0f );
glCLear( GL_DEPTH_BUFFER_BIT );
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LEQUAL );

that sounds like the depth test( or buffer ) does not really work, or the transform of the cubes ( or the view ) is incorrect.

could you put more codes here( especially the initializing and rendering codes ), or some snapshots at the incorrect position?

the following is my init and drawcube function code:

public void init(GLAutoDrawable drawable)
{
GL gl = drawable.getGL();
glut = new GLUT();

  gl.glClearDepth( 1.0f );

gl.glClear( gl.GL_DEPTH_BUFFER_BIT );
gl.glEnable( gl.GL_DEPTH_TEST );
gl.glDepthFunc( gl.GL_LEQUAL );

gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
gl.glClear(gl.GL_COLOR_BUFFER_BIT);
gl.glShadeModel(GL.GL_SMOOTH);

gl.glEnable(GL.GL_TEXTURE_2D);
texture = genTexture(gl);
gl.glBindTexture(GL.GL_TEXTURE_2D, texture);

gl.glColor4f(1.0f,1.0f,1.0f,1.0f);
glut.glutBitmapString(GLUT.BITMAP_TIMES_ROMAN_24, "TEXT 1");
gl.glCopyTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, 0, 0, 128, 128, 0);
gl.glClearColor(0.0f, 1.0f, 1.0f, 0.0f);

gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);

}

public void Drawcube(GLAutoDrawable drawable){
GL gl = drawable.getGL();
gl.glBegin(GL.GL_QUADS);

gl.glColor4f(1.0f,0.0f,0.0f,1.0f);

gl.glVertex3f( 2.0f, 2.0f,-2.0f);    // Top Right Of The Quad (Top)
gl.glVertex3f(-2.0f, 2.0f,-2.0f);    // Top Left Of The Quad (Top)
gl.glVertex3f(-2.0f, 2.0f, 2.0f);    // Bottom Left Of The Quad (Top)
gl.glVertex3f( 2.0f, 2.0f, 2.0f);    // Bottom Right Of The Quad (Top)

// gl.glColor4f(0.0f,0.0f,1.0f,1.0f); // Color Blue
gl.glVertex3f( 2.0f,-2.0f, 2.0f); // Top Right Of The Quad (Bottom)
gl.glVertex3f(-2.0f,-2.0f, 2.0f); // Top Left Of The Quad (Bottom)
gl.glVertex3f(-2.0f,-2.0f,-2.0f); // Bottom Left Of The Quad (Bottom)
gl.glVertex3f( 2.0f,-2.0f,-2.0f); // Bottom Right Of The Quad (Bottom)

// gl.glColor4f(1.0f,1.0f,0.0f,1.0f); // Color Yellow
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f( 2.0f, 2.0f, 2.0f); // Top Right Of The Quad (Front)
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(-2.0f, 2.0f, 2.0f); // Top Left Of The Quad (Front)
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(-2.0f,-2.0f, 2.0f); // Bottom Left Of The Quad (Front)
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f( 2.0f,-2.0f, 2.0f); // Bottom Right Of The Quad (Front)

// gl.glColor4f(0.0f,1.0f,0.0f,1.0f); // Color Green
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f( 2.0f,-2.0f,-2.0f); // Top Right Of The Quad (Back)
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(-2.0f,-2.0f,-2.0f); // Top Left Of The Quad (Back)
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(-2.0f, 2.0f,-2.0f); // Bottom Left Of The Quad (Back)
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f( 2.0f, 2.0f,-2.0f); // Bottom Right Of The Quad (Back)

// gl.glColor4f(0.0f,0.0f,1.0f,1.0f); // Color Blue
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f(-2.0f, 2.0f, 2.0f); // Top Right Of The Quad (Left)
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f(-2.0f, 2.0f,-2.0f); // Top Left Of The Quad (Left)
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f(-2.0f,-2.0f,-2.0f); // Bottom Left Of The Quad (Left)
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f(-2.0f,-2.0f, 2.0f); // Bottom Right Of The Quad (Left)

// gl.glColor4f(1.0f,0.0f,0.0f,1.0f); // Color Red
gl.glTexCoord2f(1.0f, 1.0f);
gl.glVertex3f( 2.0f, 2.0f,-2.0f); // Top Right Of The Quad (Right)
gl.glTexCoord2f(0.0f, 1.0f);
gl.glVertex3f( 2.0f, 2.0f, 2.0f); // Top Left Of The Quad (Right)
gl.glTexCoord2f(0.0f, 0.0f);
gl.glVertex3f( 2.0f,-2.0f, 2.0f); // Bottom Left Of The Quad (Right)
gl.glTexCoord2f(1.0f, 0.0f);
gl.glVertex3f( 2.0f,-2.0f,-2.0f); // Bottom Right Of The Quad (Right)
gl.glEnd();

}

sonnik,

here is the snapshot by running your code on my application ( with some colors modified ):

and it is all right.

so I guess the problem has something to do with the way you draw the cubes, or the way you deal with the matrices, assuming that your depth buffer is valid and all your faces are drawn counter clockwise.

Hi,

I have a couple of queries/difficulties…

  1. I have been able to print text on the face of my cubes using textures and glCopyTexImage. But I want to be able to write text in different lines…for e.g. HELLO
    WORLD
    i want to split the text in different lines. How to do that?
    Currently my text is always displayed only at the bottom of the cube and the string gets clipped if its too long.
    Is there any way to print the long strings in separate lines and also be able to move them anywhere on the face?

  2. Can I make the display function independent of the other graphics code?

  3. Can I make the glTranslate and gl Rotate functions dynamic to take random co-ordinates during run time?

Below is my init() and display() methods:
public void init(GLAutoDrawable drawable)
{
GL gl = drawable.getGL();
glut = new GLUT();
gl.glShadeModel(GL.GL_SMOOTH);

gl.glClearColor(0.8f, 0.7f, 0.5f, 0.0f);
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT );
gl.glEnable(GL.GL_TEXTURE_2D);
texture = genTexture(gl);
gl.glBindTexture(GL.GL_TEXTURE_2D, texture);

// gl.glRasterPos2f
glut.glutBitmapString(GLUT.BITMAP_TIMES_ROMAN_24, “MY TEXT”);

gl.glCopyTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, -1, 0, 128, 128, 0);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);

}

public void display(GLAutoDrawable drawable)
{
GL gl = drawable.getGL();
GLU glu = new GLU();
GLUT glut = new GLUT();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glEnable(GL.GL_CULL_FACE);
gl.glLoadIdentity();

gl.glTranslatef(0.0f, -3.0f, -19.0f);    // Translate Into The Screen along z axis 12.0 Units
gl.glRotatef(rotqube/5,0.0f,1.0f,0.0f);    // Rotate The cube around the Y axis
//gl.glRotatef(180, 0.0f,1.0f,0.0f);

gl.glScalef(1.0f, 1.0f, 1.0f); // CHANGE TO SEE THE DIAGRAM PROPER !!!

Drawcube(drawable); // Lower left class – derived class1
DrawDivideLines(drawable);

gl.glBegin(GL.GL_LINES);
//gl.glColor4f(1.0f,1.0f,1.0f,1);
gl.glVertex3f(0,2,0); // line connecting inherited class
gl.glVertex3f(0,9,0);
gl.glEnd();

gl.glBegin(GL.GL_TRIANGLES);
gl.glColor4f(0.0f,0.0f,1.0f,1);
gl.glVertex3f(0.3f,9,0);
gl.glVertex3f(-0.3f,9,0);
gl.glVertex3f(0,10,0);
gl.glEnd();
gl.glTranslatef(0.0f,12.0f,0.0f);

Drawcube(drawable); // Upper Left class – super class
DrawDivideLines(drawable);

gl.glTranslatef(-2.36f,0.0f,0.0f); // translate outside the cube to draw diamond shape
gl.glRotatef(45,0.0f,0.0f,1.0f); // rotate to make the cube look like a diamond

Drawsmallcube(drawable); // draw diamond

gl.glRotatef(-45,0.0f,0.0f,1.0f); // rotate to come back to original position
gl.glTranslatef(-0.35f,0.0f,0.0f); // come to the lower tip of the diamond

gl.glBegin(GL.GL_LINES);
//gl.glColor3f(1.0f,1.0f,1.0f);
gl.glVertex3f(0,0,0);
gl.glVertex3f(-3,0,0);
gl.glEnd();

gl.glTranslatef(-5.0f,0.0f,0.0f);

Drawcube(drawable); // Upper right class
DrawDivideLines(drawable);

gl.glTranslatef(0.0f,-12.0f,0.0f);

Drawcube(drawable); // Lower right class
DrawDivideLines(drawable);

gl.glEnable (GL.GL_LINE_STIPPLE);
gl.glLineStipple(1,(short) 170);
gl.glBegin(GL.GL_LINES);
//gl.glColor3f(1.0f,1.0f,1.0f);
gl.glVertex3f(0,2,0); // vertical line from lower right cube(inheritance)
gl.glVertex3f(0,5,0);
gl.glEnd();

gl.glBegin(GL.GL_LINES);
//gl.glColor3f(1.0f,1.0f,1.0f);
gl.glVertex3f(0,5,0); // horizontal line from lower right cube (inheritance)
gl.glVertex3f(6.5f,5,0);
gl.glEnd();

gl.glBegin(GL.GL_LINES);
//gl.glColor3f(1.0f,1.0f,1.0f);
gl.glVertex3f(6.5f,5,0); // horizontal line from lower right cube (inheritance)
gl.glVertex3f(6.5f,9,0);
gl.glEnd();
gl.glDisable(GL.GL_LINE_STIPPLE);

gl.glTranslatef(6.5f,0.0f,0.0f);

gl.glBegin(GL.GL_TRIANGLES);
gl.glColor3f(0.0f,0.0f,1.0f);
gl.glVertex3f(0.3f,9,0);
gl.glVertex3f(-0.3f,9,0);
gl.glVertex3f(0,10,0);
gl.glEnd();

gl.glTranslatef(-6.5f,12.0f,8.0f);
Drawcube(drawable);
DrawDivideLines(drawable);

gl.glBegin(GL.GL_LINES);
//gl.glColor3f(1.0f,1.0f,1.0f);
gl.glVertex3f(0,0,-2);
gl.glVertex3f(0,0,-6);
gl.glEnd();

rotqube++;
if (rotqube/30 >= 360.f)
    rotqube = 0.0f;

}

PLEASE HELP!!