Skip to content

Creating a 3D scene

In this guide, you will create a 3D camera, add lighting, and render your first cube.

Before starting:

  • CC:Tweaked installed
  • DirectGPU installed
  • A DirectGPU display
local gpu = peripheral.find("directgpu")
local displayId = gpu.autoDetectAndCreateDisplay()

Before rendering 3D objects, initialize the camera using setupCamera.

gpu.setupCamera(
displayId,
60,
0.1,
1000
)

Move the camera:

gpu.setCameraPosition(
displayId,
0,
2,
5
)

Look at the scene:

gpu.lookAt(
displayId,
0,
0,
0
)

A scene without lights will appear dark.

Add ambient light:

gpu.addAmbientLight(
displayId,
50,
50,
50,
0.5
)

Add a directional light:

gpu.addDirectionalLight(
displayId,
0,
-1,
0,
255,
255,
255,
1
)

Now draw a cube:

gpu.drawCube(
displayId,
0,
0,
0,
2,
0,
45,
0,
255,
100,
100
)

Finally update:

gpu.updateDisplay(displayId)

You now have a basic 3D scene.