Added some simple background textures from OpenGameArt.org and rendered them with a little parallax effect

This commit is contained in:
2025-09-02 12:20:11 -07:00
parent 3aee6123c6
commit f45c2d88e0
10 changed files with 46 additions and 14 deletions

View File

@@ -6,6 +6,7 @@ Description:
** When no other application is present, this serves as a simple test case for the CWasm layer
*/
#define CWASM_DEBUG 1
#include "cwasm.c"
struct
@@ -25,6 +26,8 @@ struct
GlId viewMatrixLocation;
GlId projMatrixLocation;
GlId backgroundTextures[5];
u32 frameIndex;
r64 prevProgramTimeR64;
} app;
@@ -45,16 +48,25 @@ WASM_EXPORT(App_GetResourcePath) const char* App_GetResourcePath(int resourceInd
{
switch (resourceIndex)
{
case 0: return "icon_64.png";
case 0: return "parallax-mountain-bg.png";
case 1: return "parallax-mountain-montain-far.png";
case 2: return "parallax-mountain-mountains.png";
case 3: return "parallax-mountain-trees.png";
case 4: return "parallax-mountain-foreground-trees.png";
default: return nullptr;
}
}
WASM_EXPORT(App_ResourceLoaded) void App_ResourceLoaded(int resourceIndex, int fileSize, u8* fileBytes)
{
switch (resourceIndex)
{
case 0:
case 1:
case 2:
case 3:
case 4:
{
PrintLine_D("Got resource %d bytes: %p %02X %02X %02X %02X", fileSize, fileBytes, fileBytes[0], fileBytes[1], fileBytes[2], fileBytes[3]);
ScratchBegin(scratch); //NOTE: stbi_load_from_memory implicitly allocates from the first scratch arena
@@ -95,10 +107,10 @@ WASM_EXPORT(App_ResourceLoaded) void App_ResourceLoaded(int resourceIndex, int f
}
#endif
if (app.testTexture != 0) { jsGlDeleteTexture(app.testTexture); }
app.testTexture = jsGlCreateTexture();
// if (app.testTexture != 0) { jsGlDeleteTexture(app.testTexture); }
app.backgroundTextures[resourceIndex] = jsGlCreateTexture();
jsGlActiveTexture(GL_TEXTURE0);
jsGlBindTexture(GL_TEXTURE_2D, app.testTexture);
jsGlBindTexture(GL_TEXTURE_2D, app.backgroundTextures[resourceIndex]);
// jsGlPixelStorei(GL_UNPACK_PREMULTIPLY_ALPHA_WEBGL, 1);
jsGlTexImage2D(
GL_TEXTURE_2D, //bound texture type
@@ -112,8 +124,8 @@ WASM_EXPORT(App_ResourceLoaded) void App_ResourceLoaded(int resourceIndex, int f
imageWidth*imageHeight*sizeof(u32), //dataLength
textureBytes //dataPntr
);
jsGlTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
jsGlTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
jsGlTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
jsGlTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
jsGlTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
jsGlTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
jsGlGenerateMipmap(GL_TEXTURE_2D);
@@ -346,8 +358,6 @@ WASM_EXPORT(App_UpdateAndRender) bool App_UpdateAndRender(r64 programTimeR64)
jsGlClear(GL_COLOR_BUFFER_BIT);
jsGlBindVertexArray(app.vao); // our vertex array object
jsGlUseProgram(app.testShader); // our shader program
jsGlActiveTexture(GL_TEXTURE0);
jsGlBindTexture(GL_TEXTURE_2D, app.testTexture);
mat4 identityMatrix = Mat4_Identity;
jsGlUniformMatrix4fv(app.viewMatrixLocation, &identityMatrix);
@@ -379,6 +389,19 @@ WASM_EXPORT(App_UpdateAndRender) bool App_UpdateAndRender(r64 programTimeR64)
}
#endif
for (int bIndex = 0; bIndex < ArrayCount(app.backgroundTextures); bIndex++)
{
mat4 worldMatrix = Mat4_Identity_Const;
worldMatrix = MulMat4(MakeScaleMat4(3.4f, 2.0f, 1.0f), worldMatrix);
r32 parrallax = ((r32)bIndex / (r32)ArrayCount(app.backgroundTextures));
worldMatrix = MulMat4(MakeTranslateMat4(-1.7f + OscillateBy(programTime, -0.2f*parrallax, 0.2f*parrallax, 15000, 0), -1.0f, 0.0f), worldMatrix);
jsGlUniformMatrix4fv(app.worldMatrixLocation, &worldMatrix);
jsGlActiveTexture(GL_TEXTURE0);
jsGlBindTexture(GL_TEXTURE_2D, app.backgroundTextures[bIndex]);
jsGlDrawArrays(GL_TRIANGLES, 0, 6);
}
#if 1
{
r32 offsetX = -0.4f;
r32 offsetY = -0.4f;
@@ -397,8 +420,11 @@ WASM_EXPORT(App_UpdateAndRender) bool App_UpdateAndRender(r64 programTimeR64)
);
#endif
jsGlUniformMatrix4fv(app.worldMatrixLocation, &worldMatrix);
jsGlActiveTexture(GL_TEXTURE0);
jsGlBindTexture(GL_TEXTURE_2D, app.testTexture);
jsGlDrawArrays(GL_TRIANGLES, 0, 6);
}
#endif
// if (programTime > 5000) { shouldContinue = false; }