Rotating 3D models with quaternions
In this guide, you will load an OBJ model and render it with quaternion rotation.
Requirements
Section titled “Requirements”Before starting, make sure you have:
- CC:Tweaked installed
- DirectGPU installed
- A DirectGPU display
Loading a model
Section titled “Loading a model”Get the DirectGPU peripheral and create a display:
local gpu = peripheral.wrap("directgpu_1") or peripheral.find("directgpu")local displayId = gpu.autoDetectAndCreateDisplay(1)Define a simple triangle model:
local modelData = [[v -0.5 -0.5 0v 0.5 -0.5 0v 0 0.5 0f 1 2 3]]
local modelId = gpu.load3DModel(modelData)Setting up the camera
Section titled “Setting up the camera”Configure the camera to view the model:
gpu.setupCamera(displayId, 50, 0.05, 100)gpu.setCameraPosition(displayId, 0, 0, -4)gpu.lookAt(displayId, 0, 0, 0)Building a quaternion
Section titled “Building a quaternion”Create a quaternion rotating around the Y axis:
local angle = math.rad(90)local half = angle / 2
local qx = 0local qy = math.sin(half)local qz = 0local qw = math.cos(half)Rendering the model
Section titled “Rendering the model”Clear the framebuffer, clear the depth buffer, and draw the model passing the quaternion after the color args:
gpu.clear(displayId, 0, 0, 0)gpu.clearZBuffer(displayId)gpu.draw3DModel(displayId, modelId, 0, 0, 0, 0, 0, 0, 1, 255, 255, 255, qx, qy, qz, qw)
if gpu.updateDisplay then gpu.updateDisplay(displayId)endThe model is now drawn rotated using the quaternion.