From 346719824e582010c4e5ce867105ce9e7117f856 Mon Sep 17 00:00:00 2001 From: Shylie Date: Tue, 11 Apr 2023 19:54:15 -0400 Subject: [PATCH] add basic lighting --- source/main.cpp | 34 ++++++++++++++++++++++++---------- source/vshader.v.pica | 39 +++++++++++++++++++++++++-------------- 2 files changed, 49 insertions(+), 24 deletions(-) diff --git a/source/main.cpp b/source/main.cpp index 12e3378..4e7b7f5 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -59,9 +59,10 @@ static DVLB_s* vshaderDVLB; static shaderProgram_s program; static s8 spheresUniformLocation; static s8 sphereColorsUniformLocation; -static s8 randLocation; +static s8 sphereLightsUniformLocation; +static s8 randUniformLocation; -static constexpr unsigned int VERTEX_COUNT_W = 150; +static constexpr unsigned int VERTEX_COUNT_W = 210; static constexpr unsigned int VERTEX_COUNT_H = 10 * VERTEX_COUNT_W / 6; static constexpr unsigned int VERTEX_COUNT = VERTEX_COUNT_W * VERTEX_COUNT_H; @@ -111,12 +112,12 @@ static void setupVertices() { vertex& v = vertexList[x + y * VERTEX_COUNT_W]; - const float s = static_cast(x + rand01()) / VERTEX_COUNT_W; - const float t = static_cast(y + rand01()) / VERTEX_COUNT_H; + const float s = static_cast(x) / VERTEX_COUNT_W; + const float t = static_cast(y) / VERTEX_COUNT_H; // swapped due to 3DS screen orientation - v.st[0] = t; - v.st[1] = s; + v.st[0] = t + rand01() / VERTEX_COUNT_W; + v.st[1] = s + rand01() / VERTEX_COUNT_H; v.coords[0] = 2.0f * s - 1.0f; v.coords[1] = 2.0f * t - 1.0f; @@ -128,9 +129,12 @@ static void setupVertices() } memcpy(vboData, vertexList, sizeof(vertexList)); +} - constexpr int NUM_RAND = 4; - C3D_FVec* randPtr = C3D_FVUnifWritePtr(GPU_VERTEX_SHADER, randLocation, NUM_RAND); +static void setupRandom() +{ + constexpr int NUM_RAND = 10; + C3D_FVec* randPtr = C3D_FVUnifWritePtr(GPU_VERTEX_SHADER, randUniformLocation, NUM_RAND); for (int i = 0; i < NUM_RAND; i++) { randPtr[i] = randvec(); @@ -162,7 +166,8 @@ static void sceneInit() spheresUniformLocation = shaderInstanceGetUniformLocation(program.vertexShader, "spheres"); sphereColorsUniformLocation = shaderInstanceGetUniformLocation(program.vertexShader, "sphereColors"); - randLocation = shaderInstanceGetUniformLocation(program.vertexShader, "rand"); + sphereLightsUniformLocation = shaderInstanceGetUniformLocation(program.vertexShader, "sphereLights"); + randUniformLocation = shaderInstanceGetUniformLocation(program.vertexShader, "rand"); C3D_AttrInfo* attrInfo = C3D_GetAttrInfo(); AttrInfo_Init(attrInfo); @@ -187,16 +192,24 @@ static void sceneInit() C3D_TexEnvFunc(env, C3D_RGB, GPU_INTERPOLATE); C3D_TexEnvFunc(env, C3D_Alpha, GPU_REPLACE); - constexpr u16 NUM_SPHERES = 3; + constexpr u16 NUM_SPHERES = 4; C3D_FVec* spheres = C3D_FVUnifWritePtr(GPU_VERTEX_SHADER, spheresUniformLocation, NUM_SPHERES); spheres[0] = FVec4_New(0.0f, -100.5f, -1.0f, 100.0f); spheres[1] = FVec4_New(0.6f, 0.0f, -1.0f, 0.5f); spheres[2] = FVec4_New(-0.6f, 0.0f, -1.0f, 0.5f); + spheres[3] = FVec4_New(0.0f, 1.75f, -1.0f, 1.0f); C3D_FVec* sphereColors = C3D_FVUnifWritePtr(GPU_VERTEX_SHADER, sphereColorsUniformLocation, NUM_SPHERES); sphereColors[0] = FVec3_New(0.9f, 0.3f, 0.3f); sphereColors[1] = FVec3_New(0.3f, 0.9f, 0.3f); sphereColors[2] = FVec3_New(0.3f, 0.3f, 0.9f); + sphereColors[3] = FVec3_New(1.0f, 1.0f, 1.0f); + + C3D_FVec* sphereLights = C3D_FVUnifWritePtr(GPU_VERTEX_SHADER, sphereLightsUniformLocation, NUM_SPHERES); + sphereLights[0] = FVec3_New(0.0f, 0.0f, 0.0f); + sphereLights[1] = FVec3_New(0.0f, 0.0f, 0.0f); + sphereLights[2] = FVec3_New(0.0f, 0.0f, 0.0f); + sphereLights[3] = FVec3_New(1.0f, 1.0f, 1.0f); } static void sceneRender() @@ -323,6 +336,7 @@ int main(int argc, char* argv[]) } setupVertices(); + setupRandom(); C3D_TexEnvColor(C3D_GetTexEnv(0), getFrameNumColor(frameNum)); diff --git a/source/vshader.v.pica b/source/vshader.v.pica index 3e75b36..8888815 100644 --- a/source/vshader.v.pica +++ b/source/vshader.v.pica @@ -7,21 +7,24 @@ .alias far myconst.wwww .alias noHit myconst2.yyyy -.consti bounceLoopParams(3, 0, 1, 0) -.consti calcSphereLoopParams(2, 0, 1, 0) +.consti bounceLoopParams(9, 0, 1, 0) +.consti calcSphereLoopParams(3, 0, 1, 0) .setb b0 true .alias true b0 ; xyz center (in world space) ; w radius (in world space) -.fvec spheres[3] +.fvec spheres[4] ; material albedo -.fvec sphereColors[3] +.fvec sphereColors[4] + +; material emitted light +.fvec sphereLights[4] ; random numbers -.fvec rand[4] +.fvec rand[10] .in inOrigin v0 .in inLowerLeftCorner v1 @@ -51,10 +54,12 @@ mad r2.xyz, inHorizontal, r3.x, r2.xyz mad r2.xyz, inVertical, r3.y, r2.xyz - ; set initial color to (1, 1, 1) - ; as lights are not implemented yet + ; set initial color multiplier to (1, 1, 1) mov r4.xyz, ones + ; set initial color to (0, 0, 0) + mov r13.xyz, zeros + ; calculate light bounces for bounceLoopParams ; setup random numbers for this iteration @@ -156,20 +161,24 @@ ; set albedo mov r10.xyz, sphereColors[aL].xyz + ; set light emitted + mov r12.xyz, sphereLights[aL].xyz + ; early exit label calcSphereExit: nop ; done with calculation .end - ; check if noHit > albedo - cmp noHit.xyz, le, le, r10.xyz + ; check if noHit < albedo + cmp noHit.xyz, lt, lt, r10.xyz breakc cmp.x - jmpc cmp.x, noHitLabel ; multiply color by albedo mul r4.xyz, r4.xyz, r10.xyz + mad r13.xyz, r4.xyz, r12.xyz, r13.xyz + ; set r1 to new ray origin mov r1.xyz, r5.xyz @@ -177,10 +186,10 @@ add r2.xyz, r7.xyz, r11.xyz .end - noHitLabel: - ; copy final color to output - mov outColor.xyz, r4.xyz + ; min r13.xyz, ones, r13.xyz + mov outColor.xyz, r13.xyz + ; set alpha to 1 mov outColor.w, ones @@ -197,12 +206,14 @@ ; r3.xyz: sphere origin ; r3.w: sphere radius ; r4.w: min distance -; r11.xyzw: random numbers +; r11.xyz: random unit vector +; r11.w: random number ; ; Outputs ; ------- ; r4.w: new min distance ; r10.xyz: albedo +; r12.xyz: light emitted ; ; Temporaries ; -----------