Skip to content

Commit

Permalink
[Android] Ported Offscreen C99 example to Android.
Browse files Browse the repository at this point in the history
- Write output PNG image to "/storage/emulated/0/Documents/" folder.
  This doesn't work in the emulator for some reason even though the folder exists,
  but it does work on an actual Android device (tested on Galaxy Tab A8).
- Added "300 es" profiles when creating shaders on Android.
- Added precision qualifiers for Offscreen ESSL shaders.
  • Loading branch information
LukasBanana committed Sep 2, 2024
1 parent 8c30210 commit ffadc60
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
28 changes: 21 additions & 7 deletions examples/C99/Offscreen/Offscreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ The rendered result will be written to a PNG file called "Offscreen.Results.png"
This image should look identical to the "Offscreen.png" image.
*/

// Include STBI library to write PNG files - expected in <LLGL-ROOT>/external/stb/
// Include this first to use regular fopen() for writing output,
// since ExampleBase.h will override this to use the AAssetManager for file reading.
#include <stb/stb_image_write.h> // stbi_write_png()

#include <LLGL-C/LLGL.h>
#include <ExampleBase.h>
#include <stdio.h> // printf()
#include <stdlib.h> // malloc()/free()
#include <math.h> // sinf()/cosf()

#include "../../../external/stb/stb_image_write.h" // stbi_write_png()

#define FRAME_WIDTH 512
#define FRAME_HEIGHT 512
#define ENABLE_MULTISAMPLING 1
Expand Down Expand Up @@ -57,6 +60,9 @@ float LerpColorWheel(float t, int component)

int ExampleInit()
{
// Register standard output as log callback
llglRegisterLogCallbackStd();

// Load render system module
LLGLReport report = {};
if (llglLoadRenderSystemExt(&(g_config.rendererDesc), report) == 0)
Expand Down Expand Up @@ -161,14 +167,20 @@ int ExampleInit()
.type = LLGLShaderTypeVertex,
.source = "Offscreen.vert",
.sourceType = LLGLShaderSourceTypeCodeFile,
.flags = LLGLShaderCompilePatchClippingOrigin
.flags = LLGLShaderCompilePatchClippingOrigin,
#if LLGLEXAMPLE_MOBILE
.profile = "300 es",
#endif
};
LLGLShaderDescriptor fragShaderDesc =
{
.debugName = "FragmentShader",
.type = LLGLShaderTypeFragment,
.source = "Offscreen.frag",
.sourceType = LLGLShaderSourceTypeCodeFile
.sourceType = LLGLShaderSourceTypeCodeFile,
#if LLGLEXAMPLE_MOBILE
.profile = "300 es",
#endif
};

// Specify vertex attributes for vertex shader
Expand Down Expand Up @@ -313,12 +325,14 @@ int ExampleInit()
llglReadTexture(texture, &dstRegion, &dstImageView);

// Save results to disk
#if defined(ANDROID) || defined(__ANDROID__)
const char* outputFilename = "/storage/emulated/0/Documents/Offscreen.Results.png";
#else
const char* outputFilename = "Offscreen.Results.png";
#endif
llglLogPrintf("Writing result to PNG output: %s\n", outputFilename);
if (stbi_write_png(outputFilename, FRAME_WIDTH, FRAME_HEIGHT, 4, imageData, (int)imageRowStride) == 0)
{
llglLogErrorf("Failed to save image to disk: %s\n", outputFilename);
return 1;
}

// Free temporary resources
free(imageData);
Expand Down
4 changes: 4 additions & 0 deletions examples/C99/Offscreen/Offscreen.frag
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// GLSL shader version 3.30 (for OpenGL 3.3)
#version 330

#ifdef GL_ES
precision mediump float;
#endif

in vec4 vColor;

out vec4 outColor;
Expand Down
4 changes: 4 additions & 0 deletions examples/C99/Offscreen/Offscreen.vert
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// GLSL shader version 3.30 (for OpenGL 3.3)
#version 330

#ifdef GL_ES
precision mediump float;
#endif

in vec2 position;
in vec3 color;

Expand Down

0 comments on commit ffadc60

Please sign in to comment.