Rework random numbers

This commit is contained in:
Shylie 2024-02-18 19:12:44 -05:00
parent 2b32584f15
commit 803b663b84
2 changed files with 34 additions and 13 deletions

View File

@ -18,6 +18,7 @@ struct vertex
float st[2];
float coords[3];
float uv[2];
float rand[4];
};
static DVLB_s* vshaderDVLB;
@ -27,12 +28,13 @@ static s8 sphereColorsUniformLocation;
static s8 sphereLightsUniformLocation;
static s8 randUniformLocation;
static constexpr unsigned int VERTEX_COUNT_W = 120;
static constexpr unsigned int VERTEX_COUNT_W = 150;
static constexpr unsigned int VERTEX_COUNT_H = 10 * VERTEX_COUNT_W / 6;
static constexpr unsigned int VERTEX_COUNT = VERTEX_COUNT_W * VERTEX_COUNT_H;
static constexpr unsigned int INDICES_COUNT = (VERTEX_COUNT * 2 + VERTEX_COUNT_H * 2);
static vertex vertexList[VERTEX_COUNT];
static u16 vertexIndices[VERTEX_COUNT * 2 + VERTEX_COUNT_H * 2];
static u16 vertexIndices[INDICES_COUNT];
static void* vboData;
static void* iboData;
@ -108,8 +110,6 @@ static C3D_FVec randvec()
while (FVec3_Magnitude(out) > 1.0f);
return out;
return FVec4_New(0, 0, 0, 0);
}
static void setupVertices()
@ -141,12 +141,20 @@ static void setupVertices()
static void setupRandom()
{
constexpr int NUM_RAND = 10;
constexpr int NUM_RAND = 20;
C3D_FVec* randPtr = C3D_FVUnifWritePtr(GPU_VERTEX_SHADER, randUniformLocation, NUM_RAND);
for (int i = 0; i < NUM_RAND; i++)
{
randPtr[i] = randvec();
}
for (int i = 0; i < VERTEX_COUNT; i++)
{
vertex& v = static_cast<vertex*>(vboData)[i];
const C3D_FVec r = randvec();
memcpy(v.rand, r.c, sizeof(r.c));
}
}
static void sceneInit()
@ -186,13 +194,14 @@ static void sceneInit()
AttrInfo_AddLoader(attrInfo, 4, GPU_FLOAT, 2);
AttrInfo_AddLoader(attrInfo, 5, GPU_FLOAT, 3);
AttrInfo_AddLoader(attrInfo, 6, GPU_FLOAT, 2);
AttrInfo_AddLoader(attrInfo, 7, GPU_FLOAT, 4);
iboData = linearAlloc(sizeof(vertexIndices));
memcpy(iboData, vertexIndices, sizeof(vertexIndices));
C3D_BufInfo* bufInfo = C3D_GetBufInfo();
BufInfo_Init(bufInfo);
BufInfo_Add(bufInfo, vboData, sizeof(vertex), 3, 0x654);
BufInfo_Add(bufInfo, vboData, sizeof(vertex), 4, 0x7654);
C3D_TexEnv* env = C3D_GetTexEnv(0);
C3D_TexEnvInit(env);
@ -222,7 +231,7 @@ static void sceneInit()
static void sceneRender()
{
C3D_DrawElements(GPU_TRIANGLE_STRIP, VERTEX_COUNT * 2 + VERTEX_COUNT_H * 2, C3D_UNSIGNED_SHORT, iboData);
C3D_DrawElements(GPU_TRIANGLE_STRIP, INDICES_COUNT, C3D_UNSIGNED_SHORT, iboData);
}
static void sceneExit()

View File

@ -1,13 +1,14 @@
.constf myconst(0.0, 1.0, 0.001, 1000.0)
.constf myconst2(0.5, 999.0, 1.1, 0.0)
.constf myconst2(0.5, 999.0, 1.1, 2.0)
.alias zeros myconst.xxxx
.alias halfs myconst2.xxxx
.alias ones myconst.yyyy
.alias twos myconst2.wwww
.alias tooclose myconst.zzzz
.alias far myconst.wwww
.alias noHit myconst2.yyyy
.consti bounceLoopParams(9, 0, 1, 0)
.consti bounceLoopParams(19, 0, 1, 0)
.consti calcSphereLoopParams(3, 0, 1, 0)
.setb b0 true
@ -24,7 +25,7 @@
.fvec sphereLights[4]
; random numbers
.fvec rand[10]
.fvec rand[20]
.in inOrigin v0
.in inLowerLeftCorner v1
@ -33,6 +34,7 @@
.in inST v4
.in inPos v5
.in inUV v6
.in inRand v7
.out outPos position
.out outUV texcoord0
@ -57,7 +59,10 @@
; calculate light bounces
for bounceLoopParams
; setup random numbers for this iteration
mov r11, rand[aL]
mul r11, rand[aL], inRand
dp3 r6.x, r11, r11
rsq r6.x, r6.x
mul r11.xyz, r6.xxx, r11.xyz
; reset max ray distance
mov r4.w, far
@ -94,7 +99,7 @@
mov r1.xyz, r5.xyz
; set r2 to new ray direction
call diffuse
call metallic
.end
labl:
@ -133,7 +138,6 @@
; Temporaries
; -----------
; r5.xyz: new origin
; r6.xyz: new direction
; r7.xyz: hit normal
; r8.xyzw: used for calculations
; r9.xyzw: used for calculations
@ -231,4 +235,12 @@
.proc diffuse
add r2.xyz, r7.xyz, r11.xyz
.end
.proc metallic
dp3 r6.xyz, r2, r7
mul r6.xyz, twos, r6.xyz
mad r2.xyz, -r6.xyz, r7.xyz, r2.xyz
; add a bit of random "fuzziness" to the metal?
mad r2.xyz, r11.xyz, r11.w, r2.xyz
.end