Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions examples/00_quad/main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <libdragon.h>

#include <t3d/t3d.h>
#include <t3d/t3dmath.h>

/**
* Simple example with a spinning quad.
Expand All @@ -18,25 +17,25 @@ int main()

t3d_init((T3DInitParams){}); // Init library itself, use empty params for default settings

T3DMat4 modelMat; // matrix for our model, this is a "normal" float matrix
t3d_mat4_identity(&modelMat);
fm_mat4_t modelMat; // matrix for our model, this is a "normal" float matrix
fm_mat4_identity(&modelMat);
// Now allocate a fixed-point matrix, this is what t3d uses internally.
T3DMat4FP* modelMatFP = malloc_uncached(sizeof(T3DMat4FP));

const T3DVec3 camPos = {{0,0,-18}};
const T3DVec3 camTarget = {{0,0,0}};
const fm_vec3_t camPos = {{0,0,-18}};
const fm_vec3_t camTarget = {{0,0,0}};

uint8_t colorAmbient[4] = {50, 50, 50, 0xFF};
uint8_t colorDir[4] = {0xFF, 0xFF, 0xFF, 0xFF};

T3DVec3 lightDirVec = {{0.0f, 0.0f, 1.0f}};
t3d_vec3_norm(&lightDirVec);
fm_vec3_t lightDirVec = {{0.0f, 0.0f, 1.0f}};
fm_vec3_norm(&lightDirVec, &lightDirVec);

// Allocate vertices (make sure to have an uncached pointer before passing it to the API!)
// For performance reasons, 'T3DVertPacked' contains two vertices at once in one struct.
T3DVertPacked* vertices = malloc_uncached(sizeof(T3DVertPacked) * 2);

uint16_t norm = t3d_vert_pack_normal(&(T3DVec3){{ 0, 0, 1}}); // normals are packed in a 5.6.5 format
uint16_t norm = t3d_vert_pack_normal(&(fm_vec3_t){{ 0, 0, 1}}); // normals are packed in a 5.6.5 format
vertices[0] = (T3DVertPacked){
.posA = {-16, -16, 0}, .rgbaA = 0xFF0000'FF, .normA = norm,
.posB = { 16, -16, 0}, .rgbaB = 0x00FF00'FF, .normB = norm,
Expand All @@ -47,8 +46,8 @@ int main()
};

float rotAngle = 0.0f;
T3DVec3 rotAxis = {{-1.0f, 2.5f, 0.25f}};
t3d_vec3_norm(&rotAxis);
fm_vec3_t rotAxis = {{-1.0f, 2.5f, 0.25f}};
fm_vec3_norm(&rotAxis, &rotAxis);

// create a viewport, this defines the section to draw to (by default the whole screen)
// and contains the projection & view (camera) matrices
Expand All @@ -63,12 +62,12 @@ int main()

// we can set up our viewport settings beforehand here
t3d_viewport_set_projection(&viewport, T3D_DEG_TO_RAD(85.0f), 10.0f, 100.0f);
t3d_viewport_look_at(&viewport, &camPos, &camTarget, &(T3DVec3){{0,1,0}});
t3d_viewport_look_at(&viewport, &camPos, &camTarget, &(fm_vec3_t){{0,1,0}});

// Model-Matrix, t3d offers some basic matrix functions
t3d_mat4_identity(&modelMat);
t3d_mat4_rotate(&modelMat, &rotAxis, rotAngle);
t3d_mat4_scale(&modelMat, 0.4f, 0.4f, 0.4f);
fm_mat4_identity(&modelMat);
fm_mat4_from_axis_angle(&modelMat, &rotAxis, rotAngle);
fm_mat4_scale(&modelMat, &(fm_vec3_t){{0.4f, 0.4f, 0.4f}});
t3d_mat4_to_fixed(modelMatFP, &modelMat);

// ======== Draw (3D) ======== //
Expand Down
11 changes: 5 additions & 6 deletions examples/01_model/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <libdragon.h>
#include <t3d/t3d.h>
#include <t3d/t3dmath.h>
#include <t3d/t3dmodel.h>

#define FB_COUNT 3
Expand Down Expand Up @@ -39,14 +38,14 @@ int main()
// In an actual game make sure to free this viewport via 't3d_viewport_destroy' if no longer needed.
T3DViewport viewport = t3d_viewport_create_buffered(FB_COUNT);

const T3DVec3 camPos = {{0,10.0f,40.0f}};
const T3DVec3 camTarget = {{0,0,0}};
const fm_vec3_t camPos = {{0,10.0f,40.0f}};
const fm_vec3_t camTarget = {{0,0,0}};

uint8_t colorAmbient[4] = {80, 80, 100, 0xFF};
uint8_t colorDir[4] = {0xEE, 0xAA, 0xAA, 0xFF};

T3DVec3 lightDirVec = {{-1.0f, 1.0f, 1.0f}};
t3d_vec3_norm(&lightDirVec);
fm_vec3_t lightDirVec = {{-1.0f, 1.0f, 1.0f}};
fm_vec3_norm(&lightDirVec, &lightDirVec);

// Load a model-file, this contains the geometry and some metadata
T3DModel *model = t3d_model_load("rom:/model.t3dm");
Expand All @@ -65,7 +64,7 @@ int main()
float modelScale = 0.1f;

t3d_viewport_set_projection(&viewport, T3D_DEG_TO_RAD(85.0f), 10.0f, 150.0f);
t3d_viewport_look_at(&viewport, &camPos, &camTarget, &(T3DVec3){{0,1,0}});
t3d_viewport_look_at(&viewport, &camPos, &camTarget, &(fm_vec3_t){{0,1,0}});

// slowly rotate model, for more information on matrices and how to draw objects
// see the example: "03_objects"
Expand Down
13 changes: 6 additions & 7 deletions examples/02_lighting/main.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#include <libdragon.h>
#include <t3d/t3d.h>
#include <t3d/t3dmath.h>
#include <t3d/t3dmodel.h>

#define FB_COUNT 3

typedef struct {
color_t color;
T3DVec3 dir;
fm_vec3_t dir;
} DirLight;

/**
Expand Down Expand Up @@ -37,8 +36,8 @@ int main()
T3DMat4FP* modelMatFP = malloc_uncached(sizeof(T3DMat4FP) * FB_COUNT);
T3DMat4FP* lightMatFP = malloc_uncached(sizeof(T3DMat4FP) * 4 * FB_COUNT);

T3DVec3 camPos = {{40.0f,15.0f,0}};
const T3DVec3 camTarget = {{0,0,0}};
fm_vec3_t camPos = {{40.0f,15.0f,0}};
const fm_vec3_t camTarget = {{0,0,0}};

DirLight dirLights[4] = {
{.color = {0xFF, 0x00, 0x00, 0xFF}, .dir = {{ 1.0f, 1.0f, 0.0f}}},
Expand All @@ -48,8 +47,8 @@ int main()
};
uint8_t colorAmbient[4] = {0, 0, 0, 0xFF};

T3DVec3 lightDirVec = {{1.0f, 1.0f, 0.0f}};
t3d_vec3_norm(&lightDirVec);
fm_vec3_t lightDirVec = {{1.0f, 1.0f, 0.0f}};
fm_vec3_norm(&lightDirVec, &lightDirVec);

T3DModel *model = t3d_model_load("rom:/model.t3dm");
T3DModel *modelLight = t3d_model_load("rom:/light.t3dm");
Expand Down Expand Up @@ -79,7 +78,7 @@ int main()
lightCountTimer += 0.003f;

t3d_viewport_set_projection(&viewport, T3D_DEG_TO_RAD(65.0f), 10.0f, 250.0f);
t3d_viewport_look_at(&viewport, &camPos, &camTarget, &(T3DVec3){{0,1,0}});
t3d_viewport_look_at(&viewport, &camPos, &camTarget, &(fm_vec3_t){{0,1,0}});

// Model Matrix
t3d_mat4fp_from_srt_euler(&modelMatFP[frameIdx],
Expand Down
10 changes: 5 additions & 5 deletions examples/03_objects/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ int main()
actors[i] = actor_create(i, dpls[i*3 % 2]);
}

const T3DVec3 camPos = {{100.0f,25.0f,0}};
const T3DVec3 camTarget = {{0,0,0}};
const fm_vec3_t camPos = {{100.0f,25.0f,0}};
const fm_vec3_t camTarget = {{0,0,0}};

uint8_t colorAmbient[4] = {80, 50, 50, 0xFF};
T3DVec3 lightDirVec = {{1.0f, 1.0f, 0.0f}};
fm_vec3_t lightDirVec = {{1.0f, 1.0f, 0.0f}};
uint8_t lightDirColor[4] = {120, 120, 120, 0xFF};
t3d_vec3_norm(&lightDirVec);
fm_vec3_norm(&lightDirVec, &lightDirVec);

int actorCount = 50;

Expand Down Expand Up @@ -149,7 +149,7 @@ int main()
timeUpdate = get_time_ms() - timeUpdate;

t3d_viewport_set_projection(&viewport, T3D_DEG_TO_RAD(65.0f), 10.0f, 100.0f);
t3d_viewport_look_at(&viewport, &camPos, &camTarget, &(T3DVec3){{0,1,0}});
t3d_viewport_look_at(&viewport, &camPos, &camTarget, &(fm_vec3_t){{0,1,0}});

// ======== Draw (3D) ======== //
rdpq_attach(display_get(), display_get_zbuf());
Expand Down
7 changes: 3 additions & 4 deletions examples/04_dynamic/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <libdragon.h>
#include <t3d/t3d.h>
#include <t3d/t3dmath.h>
#include <t3d/t3dmodel.h>

#define FB_COUNT 3
Expand Down Expand Up @@ -46,7 +45,7 @@ int main()
(float[3]){0,0,0}
);

const T3DVec3 camTarget = {{0,0,0}};
const fm_vec3_t camTarget = {{0,0,0}};
float camAngle = 0.0f;

uint8_t colorAmbient[4] = {0xF0, 0xF0, 0xF0, 0xFF};
Expand Down Expand Up @@ -104,10 +103,10 @@ int main()
camAngle -= 0.005f;
tileOffset += 0.1f;

T3DVec3 camPos = {{sinf(camAngle) * 45.0f, 35.0f, cosf(camAngle) * 60.0f}};
fm_vec3_t camPos = {{sinf(camAngle) * 45.0f, 35.0f, cosf(camAngle) * 60.0f}};

t3d_viewport_set_projection(&viewport, T3D_DEG_TO_RAD(85.0f), 10.0f, 150.0f);
t3d_viewport_look_at(&viewport, &camPos, &camTarget, &(T3DVec3){{0,1,0}});
t3d_viewport_look_at(&viewport, &camPos, &camTarget, &(fm_vec3_t){{0,1,0}});

// transform mesh
if(transformEnabled)
Expand Down
4 changes: 2 additions & 2 deletions examples/05_splitscreen/collision.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static AABB mapColl[MAP_COLL_AABB_COUNT] = {
{{ 500-AABB_BORDER, -270-AABB_BORDER}, {1020+AABB_BORDER, 270+AABB_BORDER}}
};

static void resolve_aabb_collision(AABB *aabb, T3DVec3 *pos) {
static void resolve_aabb_collision(AABB *aabb, fm_vec3_t *pos) {
// Check if the point is inside the AABB
if(pos->v[0] >= aabb->min[0] && pos->v[0] <= aabb->max[0]
&& pos->v[2] >= aabb->min[1] && pos->v[2] <= aabb->max[1])
Expand All @@ -40,7 +40,7 @@ static void resolve_aabb_collision(AABB *aabb, T3DVec3 *pos) {
}
}

static void check_map_collision(T3DVec3 *pos) {
static void check_map_collision(fm_vec3_t *pos) {
// in a real-world scenario, you would use a more sophisticated collision logic
// for example, by having a broad-phase to pre-filter AABBs
for(int i=0; i<MAP_COLL_AABB_COUNT; ++i) {
Expand Down
23 changes: 11 additions & 12 deletions examples/05_splitscreen/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include <libdragon.h>
#include <t3d/t3d.h>
#include <t3d/t3dmath.h>
#include <t3d/t3dmodel.h>

#include "collision.h"
Expand All @@ -14,7 +13,7 @@
#define PLAYER_COUNT 3

typedef struct {
T3DVec3 position;
fm_vec3_t position;
float rot;
T3DMat4FP* mat;
color_t color;
Expand All @@ -40,10 +39,10 @@ int main()
joypad_init();
t3d_init((T3DInitParams){});

T3DMat4 tmpMatrix;
fm_mat4_t tmpMatrix;

T3DMat4FP* modelMatFP = malloc_uncached(sizeof(T3DMat4FP));
t3d_mat4_identity(&tmpMatrix);
fm_mat4_identity(&tmpMatrix);
t3d_mat4_to_fixed(modelMatFP, &tmpMatrix);

int sizeX = display_get_width();
Expand Down Expand Up @@ -105,12 +104,12 @@ int main()

// Player movement
players[selPlayer].rot += joypad.stick_x * 0.06f * deltaTime;
T3DVec3 moveDir = {{
fm_vec3_t moveDir = {{
fm_cosf(players[selPlayer].rot) * (joypad.stick_y * 6.0f * deltaTime), 0.0f,
fm_sinf(players[selPlayer].rot) * (joypad.stick_y * 6.0f * deltaTime)
}};

t3d_vec3_add(&players[selPlayer].position, &players[selPlayer].position, &moveDir);
fm_vec3_add(&players[selPlayer].position, &players[selPlayer].position, &moveDir);
check_map_collision(&players[selPlayer].position);

for(int p=0; p<PLAYER_COUNT; ++p) {
Expand Down Expand Up @@ -140,12 +139,12 @@ int main()
T3DViewport *vp = &viewports[v];
float fov = v == 2 ? T3D_DEG_TO_RAD(50.0f) : T3D_DEG_TO_RAD(75.0f);

T3DVec3 camTarget = {{
fm_vec3_t camTarget = {{
players[v].position.v[0] + fm_cosf(players[v].rot) * 10.0f,
players[v].position.v[1] + 90.0f,
players[v].position.v[2] + fm_sinf(players[v].rot) * 10.0f
}};
T3DVec3 camPos = {{
fm_vec3_t camPos = {{
players[v].position.v[0],
players[v].position.v[1] + 90.0f,
players[v].position.v[2]
Expand All @@ -154,7 +153,7 @@ int main()
// after that apply the viewport and draw your scene
// Since each of the viewport-structs has its own matrices, no conflicts will occur
t3d_viewport_set_projection(vp, fov, 20.0f, 2000.0f);
t3d_viewport_look_at(vp, &camPos, &camTarget, &(T3DVec3){{0,1,0}});
t3d_viewport_look_at(vp, &camPos, &camTarget, &(fm_vec3_t){{0,1,0}});
t3d_viewport_attach(vp);

// if you need directional light, re-apply it here after a new viewport has been attached
Expand Down Expand Up @@ -206,9 +205,9 @@ int main()
// draw icons in 3d view of player pos
for(int i=0; i<PLAYER_COUNT; ++i) {
if(selPlayer == i)continue;
T3DVec3 posView;
T3DVec3 posCenter;
t3d_vec3_add(&posCenter, &players[selPlayer].position, &(T3DVec3){{0, 60.0f, 0}});
fm_vec3_t posView;
fm_vec3_t posCenter;
fm_vec3_add(&posCenter, &players[selPlayer].position, &(fm_vec3_t){{0, 60.0f, 0}});
t3d_viewport_calc_viewspace_pos(&viewports[i], &posView, &posCenter);
rdpq_set_mode_fill(RGBA32(0x00, 0x00, 0x00, 0xFF));
posView.v[0] -= 2;
Expand Down
11 changes: 5 additions & 6 deletions examples/06_offscreen/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <libdragon.h>
#include <t3d/t3d.h>
#include <t3d/t3dmath.h>
#include <t3d/t3dmodel.h>
#include <t3d/t3ddebug.h>

Expand Down Expand Up @@ -67,8 +66,8 @@ int main()
(float[3]){0,0,0}, (float[3]){0,-1,0}
);

T3DVec3 lightDirVec = {{1.0f, 1.0f, 1.0f}};
t3d_vec3_norm(&lightDirVec);
fm_vec3_t lightDirVec = {{1.0f, 1.0f, 1.0f}};
fm_vec3_norm(&lightDirVec, &lightDirVec);

T3DModel *modelBox = t3d_model_load("rom:/box.t3dm");
T3DModel *modelCRT = t3d_model_load("rom:/target.t3dm");
Expand Down Expand Up @@ -125,13 +124,13 @@ int main()

// zoom-in / out
camDist = fmaxf(14.0f, fminf(30.0f, camDist + (float)joypad.stick_y * -deltaTime * 0.6f));
T3DVec3 camPos = {{sinf(rotAngle) * camDist, 1.5f, cosf(rotAngle) * camDist}};
fm_vec3_t camPos = {{sinf(rotAngle) * camDist, 1.5f, cosf(rotAngle) * camDist}};

t3d_viewport_set_projection(&viewport, T3D_DEG_TO_RAD(85.0f), 1.5f, 100.0f);
t3d_viewport_look_at(&viewport, &camPos, &(T3DVec3){{0,0,0}}, &(T3DVec3){{0,1,0}});
t3d_viewport_look_at(&viewport, &camPos, &(fm_vec3_t){{0,0,0}}, &(fm_vec3_t){{0,1,0}});

t3d_viewport_set_projection(&viewportOffscreen, T3D_DEG_TO_RAD(85.0f), 5.0f, 150.0f);
t3d_viewport_look_at(&viewportOffscreen, &(T3DVec3){{0,5.0f,40.0f}}, &(T3DVec3){{0,0,0}}, &(T3DVec3){{0,1,0}});
t3d_viewport_look_at(&viewportOffscreen, &(fm_vec3_t){{0,5.0f,40.0f}}, &(fm_vec3_t){{0,0,0}}, &(fm_vec3_t){{0,1,0}});

t3d_mat4fp_from_srt_euler(&matrixBox[frameIdx],
(float[3]){0.2f, 0.2f, 0.2f},
Expand Down
Loading
Loading