Added some more WebGL functions to gl_functions.js. Made the test_app render something more interesting. Added a bool return from App_UpdateAndRender which allows it to request that the application loop ends.

This commit is contained in:
2025-09-01 19:21:47 -07:00
parent 42cf6d9e9f
commit 81f5457b61
5 changed files with 150 additions and 29 deletions

View File

@@ -28,6 +28,15 @@ struct
static const char* VertexShaderCodeStr;
static const char* FragmentShaderCodeStr;
r32 OscillateBy(u64 timeSource, r32 min, r32 max, u64 periodMs, u64 offset)
{
r32 lerpValue = (sinf((((timeSource + offset) % periodMs) / (r32)periodMs) * 2*Pi32) + 1.0f) / 2.0f;
return min + (max - min) * lerpValue;
}
// +--------------------------------------------------------------+
// | Initialize |
// +--------------------------------------------------------------+
WASM_EXPORT(App_Initialize) void App_Initialize()
{
InitializeCWasm(Kilobytes(128));
@@ -113,14 +122,25 @@ WASM_EXPORT(App_Initialize) void App_Initialize()
app.projMatrixLocation = jsGlGetUniformLocation(app.testShader, (int)strlen(projMatrixName), projMatrixName);
}
r32 OscillateBy(u64 timeSource, r32 min, r32 max, u64 periodMs, u64 offset)
// +--------------------------------------------------------------+
// | Close |
// +--------------------------------------------------------------+
WASM_EXPORT(App_Close) void App_Close()
{
r32 lerpValue = (sinf((((timeSource + offset) % periodMs) / (r32)periodMs) * 2*Pi32) + 1.0f) / 2.0f;
return min + (max - min) * lerpValue;
//TODO: Delete vao
jsGlDeleteProgram(app.testShader);
jsGlDeleteShader(app.vertexShader);
jsGlDeleteShader(app.fragmentShader);
jsGlDeleteBuffer(app.positionBuffer);
jsGlDeleteBuffer(app.colorBuffer);
}
WASM_EXPORT(App_UpdateAndRender) void App_UpdateAndRender(r64 programTimeR64)
// +--------------------------------------------------------------+
// | Update and Render |
// +--------------------------------------------------------------+
WASM_EXPORT(App_UpdateAndRender) bool App_UpdateAndRender(r64 programTimeR64)
{
bool shouldContinue = true;
r64 timeScaleR64 = (programTimeR64 - app.prevProgramTimeR64) / (1000.0 / 60.0);
if (fabs(timeScaleR64 - 1.0) < 0.001) { timeScaleR64 = 1.0; }
if (timeScaleR64 > 4.0) { timeScaleR64 = 4.0; }
@@ -130,43 +150,53 @@ WASM_EXPORT(App_UpdateAndRender) void App_UpdateAndRender(r64 programTimeR64)
u64 programTime = (u64)programTimeR64;
u64 elapsedMs = programTime - (u64)app.prevProgramTimeR64;
// jsGlClearColor(OscillateBy(programTime, 0.0f, 1.0f, 3700, 1500), OscillateBy(programTime, 0.0f, 1.0f, 5300, 2000), OscillateBy(programTime, 0.0f, 1.0f, 2300, 500), 1.0f);
jsGlClearColor(32/255.0f, 32/255.0f, 32/255.0f, 1.0f);
jsGlClear(GL_COLOR_BUFFER_BIT);
jsGlBindVertexArray(app.vao); // our vertex array object
jsGlUseProgram(app.testShader); // our shader program
r32 identityMatrix[] = {
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
};
r32 worldMatrix[] = {
OscillateBy(programTime, 0.9f, 1.1f, 3000, 0), 0.0f, 0.0f, OscillateBy(programTime, -0.1f, 0.1f, 3000, 0),
0.0f, OscillateBy(programTime, 0.9f, 1.1f, 3000, 1500), 0.0f, OscillateBy(programTime, -0.1f, 0.1f, 3000, 750),
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
};
jsGlClearColor(OscillateBy(programTime, 0.0f, 1.0f, 3700, 1500), OscillateBy(programTime, 0.0f, 1.0f, 5300, 2000), OscillateBy(programTime, 0.0f, 1.0f, 2300, 500), 1.0f);
jsGlClear(GL_COLOR_BUFFER_BIT);
jsGlBindVertexArray(app.vao); // our vertex array object
jsGlUseProgram(app.testShader); // our shader program
r32 uniformValues[] = {
OscillateBy(programTime, 0.0f, 1.0f, 2000, 0),
OscillateBy(programTime, 0.0f, 1.0f, 2000, 1200),
OscillateBy(programTime, 0.0f, 1.0f, 2000, 700),
};
jsGlUniform1fv(app.testUniformLocation, ArrayCount(uniformValues), &uniformValues[0]);
jsGlUniformMatrix4fv(app.worldMatrixLocation, &worldMatrix[0]);
jsGlUniformMatrix4fv(app.viewMatrixLocation, &identityMatrix[0]);
jsGlUniformMatrix4fv(app.projMatrixLocation, &identityMatrix[0]);
jsGlDrawArrays(
GL_TRIANGLES, // drawing mode
0, // index of the first vertex to draw
3 // number of vertices to draw
);
const u64 numTris = 75;
for (u64 tIndex = 0; tIndex < numTris; tIndex++)
{
r32 uniformValues[] = {
OscillateBy(programTime, 0.0f, 1.0f, 2000, 0 + tIndex*(2000/numTris)),
OscillateBy(programTime, 0.0f, 1.0f, 2000, 1200 + tIndex*(2000/numTris)),
OscillateBy(programTime, 0.0f, 1.0f, 2000, 700 + tIndex*(2000/numTris)),
};
jsGlUniform1fv(app.testUniformLocation, ArrayCount(uniformValues), &uniformValues[0]);
r32 offsetX = OscillateBy(programTime, -0.1f, 0.1f, 3000, 0 + tIndex*(3000/numTris));
r32 offsetY = OscillateBy(programTime, -0.1f, 0.1f, 3000, 750 + tIndex*(3000/numTris));
r32 scaleX = OscillateBy(programTime, 0.9f - tIndex*(0.9f/numTris), 1.1f - tIndex*(0.9f/numTris), 3000, 0 + tIndex*(3000/numTris));
r32 scaleY = OscillateBy(programTime, 0.9f - tIndex*(0.9f/numTris), 1.1f - tIndex*(0.9f/numTris), 3000, 1500 + tIndex*(3000/numTris));
r32 worldMatrix[] = {
scaleX, 0.0f, 0.0f, offsetX,
0.0f, scaleY, 0.0f, offsetY,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
};
jsGlUniformMatrix4fv(app.worldMatrixLocation, &worldMatrix[0]);
jsGlDrawArrays(GL_TRIANGLES, 0, 3);
}
app.frameIndex++;
app.prevProgramTimeR64 = programTimeR64;
return shouldContinue;
}
// +--------------------------------------------------------------+
// | Shaders |
// +--------------------------------------------------------------+
static const char* VertexShaderCodeStr = "#version 300 es\n"
"// ^^^\n"
"// the version definition has to be the first line in\n"