add basic lighting

This commit is contained in:
Shylie 2023-04-11 19:54:15 -04:00
parent 73ca255664
commit 346719824e
2 changed files with 49 additions and 24 deletions

View File

@ -59,9 +59,10 @@ static DVLB_s* vshaderDVLB;
static shaderProgram_s program; static shaderProgram_s program;
static s8 spheresUniformLocation; static s8 spheresUniformLocation;
static s8 sphereColorsUniformLocation; 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_H = 10 * VERTEX_COUNT_W / 6;
static constexpr unsigned int VERTEX_COUNT = VERTEX_COUNT_W * VERTEX_COUNT_H; 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]; vertex& v = vertexList[x + y * VERTEX_COUNT_W];
const float s = static_cast<float>(x + rand01()) / VERTEX_COUNT_W; const float s = static_cast<float>(x) / VERTEX_COUNT_W;
const float t = static_cast<float>(y + rand01()) / VERTEX_COUNT_H; const float t = static_cast<float>(y) / VERTEX_COUNT_H;
// swapped due to 3DS screen orientation // swapped due to 3DS screen orientation
v.st[0] = t; v.st[0] = t + rand01() / VERTEX_COUNT_W;
v.st[1] = s; v.st[1] = s + rand01() / VERTEX_COUNT_H;
v.coords[0] = 2.0f * s - 1.0f; v.coords[0] = 2.0f * s - 1.0f;
v.coords[1] = 2.0f * t - 1.0f; v.coords[1] = 2.0f * t - 1.0f;
@ -128,9 +129,12 @@ static void setupVertices()
} }
memcpy(vboData, vertexList, sizeof(vertexList)); memcpy(vboData, vertexList, sizeof(vertexList));
}
constexpr int NUM_RAND = 4; static void setupRandom()
C3D_FVec* randPtr = C3D_FVUnifWritePtr(GPU_VERTEX_SHADER, randLocation, NUM_RAND); {
constexpr int NUM_RAND = 10;
C3D_FVec* randPtr = C3D_FVUnifWritePtr(GPU_VERTEX_SHADER, randUniformLocation, NUM_RAND);
for (int i = 0; i < NUM_RAND; i++) for (int i = 0; i < NUM_RAND; i++)
{ {
randPtr[i] = randvec(); randPtr[i] = randvec();
@ -162,7 +166,8 @@ static void sceneInit()
spheresUniformLocation = shaderInstanceGetUniformLocation(program.vertexShader, "spheres"); spheresUniformLocation = shaderInstanceGetUniformLocation(program.vertexShader, "spheres");
sphereColorsUniformLocation = shaderInstanceGetUniformLocation(program.vertexShader, "sphereColors"); 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(); C3D_AttrInfo* attrInfo = C3D_GetAttrInfo();
AttrInfo_Init(attrInfo); AttrInfo_Init(attrInfo);
@ -187,16 +192,24 @@ static void sceneInit()
C3D_TexEnvFunc(env, C3D_RGB, GPU_INTERPOLATE); C3D_TexEnvFunc(env, C3D_RGB, GPU_INTERPOLATE);
C3D_TexEnvFunc(env, C3D_Alpha, GPU_REPLACE); 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); C3D_FVec* spheres = C3D_FVUnifWritePtr(GPU_VERTEX_SHADER, spheresUniformLocation, NUM_SPHERES);
spheres[0] = FVec4_New(0.0f, -100.5f, -1.0f, 100.0f); 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[1] = FVec4_New(0.6f, 0.0f, -1.0f, 0.5f);
spheres[2] = 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); C3D_FVec* sphereColors = C3D_FVUnifWritePtr(GPU_VERTEX_SHADER, sphereColorsUniformLocation, NUM_SPHERES);
sphereColors[0] = FVec3_New(0.9f, 0.3f, 0.3f); sphereColors[0] = FVec3_New(0.9f, 0.3f, 0.3f);
sphereColors[1] = FVec3_New(0.3f, 0.9f, 0.3f); sphereColors[1] = FVec3_New(0.3f, 0.9f, 0.3f);
sphereColors[2] = FVec3_New(0.3f, 0.3f, 0.9f); 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() static void sceneRender()
@ -323,6 +336,7 @@ int main(int argc, char* argv[])
} }
setupVertices(); setupVertices();
setupRandom();
C3D_TexEnvColor(C3D_GetTexEnv(0), getFrameNumColor(frameNum)); C3D_TexEnvColor(C3D_GetTexEnv(0), getFrameNumColor(frameNum));

View File

@ -7,21 +7,24 @@
.alias far myconst.wwww .alias far myconst.wwww
.alias noHit myconst2.yyyy .alias noHit myconst2.yyyy
.consti bounceLoopParams(3, 0, 1, 0) .consti bounceLoopParams(9, 0, 1, 0)
.consti calcSphereLoopParams(2, 0, 1, 0) .consti calcSphereLoopParams(3, 0, 1, 0)
.setb b0 true .setb b0 true
.alias true b0 .alias true b0
; xyz center (in world space) ; xyz center (in world space)
; w radius (in world space) ; w radius (in world space)
.fvec spheres[3] .fvec spheres[4]
; material albedo ; material albedo
.fvec sphereColors[3] .fvec sphereColors[4]
; material emitted light
.fvec sphereLights[4]
; random numbers ; random numbers
.fvec rand[4] .fvec rand[10]
.in inOrigin v0 .in inOrigin v0
.in inLowerLeftCorner v1 .in inLowerLeftCorner v1
@ -51,10 +54,12 @@
mad r2.xyz, inHorizontal, r3.x, r2.xyz mad r2.xyz, inHorizontal, r3.x, r2.xyz
mad r2.xyz, inVertical, r3.y, r2.xyz mad r2.xyz, inVertical, r3.y, r2.xyz
; set initial color to (1, 1, 1) ; set initial color multiplier to (1, 1, 1)
; as lights are not implemented yet
mov r4.xyz, ones mov r4.xyz, ones
; set initial color to (0, 0, 0)
mov r13.xyz, zeros
; calculate light bounces ; calculate light bounces
for bounceLoopParams for bounceLoopParams
; setup random numbers for this iteration ; setup random numbers for this iteration
@ -156,20 +161,24 @@
; set albedo ; set albedo
mov r10.xyz, sphereColors[aL].xyz mov r10.xyz, sphereColors[aL].xyz
; set light emitted
mov r12.xyz, sphereLights[aL].xyz
; early exit label ; early exit label
calcSphereExit: calcSphereExit:
nop nop
; done with calculation ; done with calculation
.end .end
; check if noHit > albedo ; check if noHit < albedo
cmp noHit.xyz, le, le, r10.xyz cmp noHit.xyz, lt, lt, r10.xyz
breakc cmp.x breakc cmp.x
jmpc cmp.x, noHitLabel
; multiply color by albedo ; multiply color by albedo
mul r4.xyz, r4.xyz, r10.xyz mul r4.xyz, r4.xyz, r10.xyz
mad r13.xyz, r4.xyz, r12.xyz, r13.xyz
; set r1 to new ray origin ; set r1 to new ray origin
mov r1.xyz, r5.xyz mov r1.xyz, r5.xyz
@ -177,10 +186,10 @@
add r2.xyz, r7.xyz, r11.xyz add r2.xyz, r7.xyz, r11.xyz
.end .end
noHitLabel:
; copy final color to output ; 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 ; set alpha to 1
mov outColor.w, ones mov outColor.w, ones
@ -197,12 +206,14 @@
; r3.xyz: sphere origin ; r3.xyz: sphere origin
; r3.w: sphere radius ; r3.w: sphere radius
; r4.w: min distance ; r4.w: min distance
; r11.xyzw: random numbers ; r11.xyz: random unit vector
; r11.w: random number
; ;
; Outputs ; Outputs
; ------- ; -------
; r4.w: new min distance ; r4.w: new min distance
; r10.xyz: albedo ; r10.xyz: albedo
; r12.xyz: light emitted
; ;
; Temporaries ; Temporaries
; ----------- ; -----------