Sign in to follow this  
Kevin Koh Jen Sen

Lesson 17: Is it possible to add text onto a 3D surface instead of 2D

Recommended Posts

I want to use the same technique of adding text like in tutorial 17 but instead paste it onto the 3D image and not onto the 2D image..For example paste the text onto the moving object in the centre. Is it possible to do it?? I cant seem to figure out how to do it.The printing function and the drawing function is as below...Is it possible to map the text like mapping a texture on a 3D surface.. I really hope someone can help me with this.. thanks GLvoid glPrint(GLint x, GLint y, char *string, int set) { if (set>1) { set=1; } glBindTexture(GL_TEXTURE_2D, texture[0]); glDisable(GL_DEPTH_TEST); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrtho(0,640,0,480,-1,1); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glTranslated(x,y,0); glListBase(base-32+(128*set)); glCallLists(strlen(string),GL_UNSIGNED_BYTE,string); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); glEnable(GL_DEPTH_TEST); } int DrawGLScene(GLvoid) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glBindTexture(GL_TEXTURE_2D, texture[1]); glTranslatef(0.0f,0.0f,-5.0f); glRotatef(45.0f,0.0f,0.0f,1.0f); glRotatef(cnt1*30.0f,1.0f,1.0f,0.0f); glDisable(GL_BLEND); glColor3f(1.0f,1.0f,1.0f); glBegin(GL_QUADS); glTexCoord2d(0.0f,0.0f); glVertex2f(-1.0f, 1.0f); glTexCoord2d(1.0f,0.0f); glVertex2f( 1.0f, 1.0f); glTexCoord2d(1.0f,1.0f); glVertex2f( 1.0f,-1.0f); glTexCoord2d(0.0f,1.0f); glVertex2f(-1.0f,-1.0f); glEnd(); glRotatef(90.0f,1.0f,1.0f,0.0f); glBegin(GL_QUADS); glTexCoord2d(0.0f,0.0f); glVertex2f(-1.0f, 1.0f); glTexCoord2d(1.0f,0.0f); glVertex2f( 1.0f, 1.0f); glTexCoord2d(1.0f,1.0f); glVertex2f( 1.0f,-1.0f); glTexCoord2d(0.0f,1.0f); glVertex2f(-1.0f,-1.0f); glEnd(); glEnable(GL_BLEND); glLoadIdentity(); // Pulsing Colors Based On Text Position glColor3f(1.0f*float(cos(cnt1)),1.0f*float(sin(cnt2)),1.0f-0.5f*float(cos(cnt1+cnt2))); glPrint(int((280+250*cos(cnt1))),int(235+200*sin(cnt2)),"NeHe",0); cnt1+=0.01f; cnt2+=0.0081f; return TRUE; }

Share this post


Link to post
Share on other sites
It is possible, but it isn't easy, basicly you have to first render the text to the screen, then make a texture out of that and finaly you could either project the texture on an object using fragment shaders, or use regular uv mapping and render the texture on the object

Share this post


Link to post
Share on other sites
what you really are trying to do is make the text track one of your 3D objects, correct? Since right now your drawing function accepts coordinates in screen space, and you want to draw it at the place where a 3D object intersects that space, an easy way to understand and implement would be setting up matrix math, and then multiplying the coordinates of the 3D object through the modelview and projection matrixes , (and clip and perspective divide) to get to screen space coordinates. then, send those coordinates to your render text function.

Another way of doing essentially the same thing is by using gluUnproject(). It does those calculations for you, except it might take a bit of monkeying to make sure that the coordinate system ends up aligned right. NeHe has an "article" about this.

Another method of making text do this is first setting a raster offset in 3D by doing glRasterPos3f, but I am not sure that that will work for textured quads, maybe only for bitmaps. To see what I mean, see nehe's bitmap font tutorial and try applying it to a 3D tutorial.

There is another method too, that involves good use of perspective, and modelview matrixes in combination with an ortho projection, to do sort of a weird perspective-resizing billboard on the 3D object, but it would be tricky to pull off. If you could do it though, it would probably be the fastest.


I don't know if I have helped at all, but good luck on your project: personally I think that fragment shaders and pbuffers are way overkill for something like this, heck UnrealTournament and Quake 2 didnt have access to those and they managed 3D tracking text just fine! but I dont mean to rain on anyone else or anything, so if my ideas dont work for you or you feel more comfortable with pbuffers then go for offscreen rendering by all means.

Share this post


Link to post
Share on other sites
Dear Steve132,

I've just registered to this forum due to the introduction of a friend, where I find this forum incredibly fascinating and people here know dam alot about OpenGL.

I'm very interested in the way that you've explained texture mapped fonts on an object steve. I understand what you've said conceptually, but I can't figure out how it is I can actually imply it on OpenGL.

Can anyone help me here by writing an example or a simple program that could paste Texture mapped fonts from tutorial 17 onto an object on screen? Thank you, your help would be very very much appreciated. Think I'll be more active in this FORUM yet!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this