Skip to content
Tags

60. 3D – Writing on the screen

May 22, 2013

It may surprise you, but if you write text on the screen the normal way, when you’re drawing a 3D scene, you’ll see nothing.

To write something you can see, you need to insert this line of code before writing anything:

    ortho() --restores default orthographic projection
    viewMatrix(matrix()) 

So what I’m going to do is take the code we’ve been working with, and write the current drawing speed and the x,y,z position, at top left of screen.

Drawing speed

A commonly accepted measure of drawing speed, in frames per second, or FPS, is

FPS = FPS * 0.9 + 0.1/DeltaTime

If Codea draws at 60 frames a second, then DeltaTime, the time between redraws, will be 1/60, and so 1/DeltaTime will equal 60. If this is the speed, why bother with multiplying by 0.1, 0.9, etc?

The reason is that DeltaTime will bounce around a lot, making the speed hard to read, so the formula above smoothes it out by adding the latest figure in and reducing the value of the previous figure.

Writing the text

So we’ll put FPS=60 in the setup routine to get it started, and then at the end of the draw function, we’ll write our text, like this:

    ortho() --restores default orthographic projection
    viewMatrix(matrix()) 
    FPS=FPS*.9+.1/DeltaTime
    pushStyle()  
    fill(0)  --set the font etc
    fontSize(18)
    strokeWidth(3)
    textMode(CORNER)
    text('FPS: '..string.format('%d',FPS),50,HEIGHT-50)
    -- formatting as '%  d' makes Codea include leading spaces
    text('x='..string.format('%  d',posX),50,HEIGHT-70)
    text('y='..string.format('%  d',Viewpoint),50,HEIGHT-90)
    text('z='..string.format('%  d',posZ),50,HEIGHT-110)
    popStyle()

Write the text last!

Make sure you write the text after doing all your 3D drawing!

Here is the full code.

From → 3D

4 Comments
  1. Saurabh permalink

    Awesome so we can combine 2d physics with 3d backround with this or is it only drawing and writing things?

  2. Sorry, no physics.

    2D is like a glass window you’re looking through, onto the 3D world. You can draw on the 2D window, and it looks as though it is in the 3D world, but it’s actually completely separate.

  3. but don’t be too concerned. We can do some amazing things in 3D. Just wait and see!

  4. Jesse Baca permalink

    I was totally lost a LONG time ago, now I’m just copying the code. :/

Leave a comment