Textured models with quaternion rotation
In this guide, you will load a textured 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
- A Base64 encoded texture image
Loading the model and texture
Section titled “Loading the model and texture”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 textured triangle model:
local modelData = [[v -0.5 -0.5 0v 0.5 -0.5 0v 0 0.5 0vt 0 1vt 1 1vt 0.5 0f 1/1 2/2 3/3]]Load the model and its texture:
local textureData = "base64_png_here"
local modelId = gpu.load3DModel(modelData)local textureId = gpu.loadTexture(textureData)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 that rotates over time:
local angle = os.clock()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 textured model passing the quaternion after the color args:
gpu.clear(displayId, 0, 0, 0)gpu.clearZBuffer(displayId)gpu.draw3DModelTextured(displayId, modelId, textureId, 0, 0, 0, 0, 0, 0, 1, 255, 255, 255, qx, qy, qz, qw)
if gpu.updateDisplay then gpu.updateDisplay(displayId)endThe textured model is now drawn rotating over time using the quaternion.