Added a bunch of WebGL functions related to shader uniforms to gl_functions.js

This commit is contained in:
2025-09-01 18:50:14 -07:00
parent b548b7bb26
commit 42cf6d9e9f
7 changed files with 315 additions and 39 deletions

View File

@@ -16,8 +16,13 @@ struct
GlId vertexShader;
GlId fragmentShader;
GlId testShader;
GlId testUniformLocation;
GlId worldMatrixLocation;
GlId viewMatrixLocation;
GlId projMatrixLocation;
u32 frameIndex;
r64 prevProgramTimeR64;
} app;
static const char* VertexShaderCodeStr;
@@ -27,12 +32,21 @@ WASM_EXPORT(App_Initialize) void App_Initialize()
{
InitializeCWasm(Kilobytes(128));
#if 0
Write_D("Hello\nWorld!");
PrintLine_I(" Fuzzy %u Bunnies!\n%s", 31415926, "What");
WriteLine_D("");
WriteLine_W("When");
Write_D("\n");
WriteLine_E("Where");
#endif
memset(&app, 0x00, sizeof(app));
r32 positions[] = {
-0.9, -0.79, // left bottom corner
1.0, -1.0, // right bottom corner
0.0, 1.0 // center top corner
-0.7, -0.7, // left bottom corner
0.7, -0.7, // right bottom corner
0.0, 0.7 // center top corner
};
app.positionBuffer = jsGlCreateBuffer();
jsGlBindBuffer(GL_ARRAY_BUFFER, app.positionBuffer);
@@ -88,15 +102,61 @@ WASM_EXPORT(App_Initialize) void App_Initialize()
jsGlLinkProgram(app.testShader);
// also debug the program status
if (!jsGlGetProgramParameterBool(app.testShader, GL_LINK_STATUS)) { WriteLine_E("Failed to link shader program!"); } //TODO: jsGlGetProgramInfoLog
const char* uniformName = "TestUniform";
app.testUniformLocation = jsGlGetUniformLocation(app.testShader, (int)strlen(uniformName), uniformName);
const char* worldMatrixName = "WorldMatrix";
app.worldMatrixLocation = jsGlGetUniformLocation(app.testShader, (int)strlen(worldMatrixName), worldMatrixName);
const char* viewMatrixName = "ViewMatrix";
app.viewMatrixLocation = jsGlGetUniformLocation(app.testShader, (int)strlen(viewMatrixName), viewMatrixName);
const char* projMatrixName = "ProjMatrix";
app.projMatrixLocation = jsGlGetUniformLocation(app.testShader, (int)strlen(projMatrixName), projMatrixName);
}
WASM_EXPORT(App_UpdateAndRender) void App_UpdateAndRender()
r32 OscillateBy(u64 timeSource, r32 min, r32 max, u64 periodMs, u64 offset)
{
r32 time = app.frameIndex * (1000.0f/60.0f);
jsGlClearColor(sinf(time * 0.0005f + 1.5f)/2 + 0.5f, sinf(time * 0.0013f + 2.3f)/2 + 0.5f, sinf(time * 0.0007f + 3.7f)/2 + 0.5f, 1.0f);
r32 lerpValue = (sinf((((timeSource + offset) % periodMs) / (r32)periodMs) * 2*Pi32) + 1.0f) / 2.0f;
return min + (max - min) * lerpValue;
}
WASM_EXPORT(App_UpdateAndRender) void App_UpdateAndRender(r64 programTimeR64)
{
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; }
if (timeScaleR64 < 0.0) { timeScaleR64 = 0.0; }
r32 timeScale = (r32)timeScaleR64;
r32 programTimef = (r32)programTimeR64;
u64 programTime = (u64)programTimeR64;
u64 elapsedMs = programTime - (u64)app.prevProgramTimeR64;
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
@@ -104,6 +164,7 @@ WASM_EXPORT(App_UpdateAndRender) void App_UpdateAndRender()
);
app.frameIndex++;
app.prevProgramTimeR64 = programTimeR64;
}
static const char* VertexShaderCodeStr = "#version 300 es\n"
@@ -115,6 +176,10 @@ static const char* VertexShaderCodeStr = "#version 300 es\n"
"// data types\n"
"precision highp float;\n"
"\n"
"uniform mat4 WorldMatrix;\n"
"uniform mat4 ViewMatrix;\n"
"uniform mat4 ProjMatrix;\n"
"\n"
"// this is the vertex attribute at index 0\n"
"// which we defined in the vertex array object.\n"
"// we can use any name for this in the glsl code\n"
@@ -133,12 +198,14 @@ static const char* VertexShaderCodeStr = "#version 300 es\n"
" \n"
" // vertex position for the shader program\n"
" // always a vec4 value\n"
" gl_Position = vec4(aPos, 0.0, 1.0);\n"
" gl_Position = ((vec4(aPos, 0.0, 1.0) * WorldMatrix) * ViewMatrix) * ProjMatrix;\n"
"}\n";
static const char* FragmentShaderCodeStr = "#version 300 es\n"
"precision highp float;\n"
"\n"
"uniform float TestUniform[3];\n"
"\n"
"in vec3 vCol; // the data from vertex shader\n"
"\n"
"// fragment output value\n"
@@ -146,5 +213,5 @@ static const char* FragmentShaderCodeStr = "#version 300 es\n"
"out vec4 outCol;\n"
"\n"
"void main(){\n"
" outCol = vec4(vCol, 1.0);\n"
" outCol = vec4(vec3(vCol.r * TestUniform[0], vCol.g * TestUniform[1], vCol.b * TestUniform[2]), 1.0);\n"
"}\n";