Skip to content

Commit

Permalink
it appears to actually be working; still quite a hack though!
Browse files Browse the repository at this point in the history
  • Loading branch information
ixchow committed Nov 16, 2023
1 parent 0f706df commit 1036d27
Show file tree
Hide file tree
Showing 13 changed files with 237 additions and 19 deletions.
12 changes: 12 additions & 0 deletions ColorProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ ColorProgram::ColorProgram() {
//Compile vertex and fragment shaders using the convenient 'gl_compile_program' helper function:
program = gl_compile_program(
//vertex shader:
#ifdef __ANDROID__
"#version 320 es\n"
#else
"#version 330\n"
#endif
"uniform mat4 OBJECT_TO_CLIP;\n"
"in vec4 Position;\n"
"in vec4 Color;\n"
Expand All @@ -20,12 +24,20 @@ ColorProgram::ColorProgram() {
"}\n"
,
//fragment shader:
#ifdef __ANDROID__
"#version 320 es\n"
"precision highp int;\n" //overkill but saves re-writing the shader below
"precision highp float;\n" //similar
#else
"#version 330\n"
#endif
"in vec4 color;\n"
"out vec4 fragColor;\n"
"void main() {\n"
" fragColor = color;\n"
"}\n"
,
"ColorProgram"
);
//As you can see above, adjacent strings in C/C++ are concatenated.
// this is very useful for writing long shader programs inline.
Expand Down
14 changes: 14 additions & 0 deletions LitColorTextureProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ LitColorTextureProgram::LitColorTextureProgram() {
//Compile vertex and fragment shaders using the convenient 'gl_compile_program' helper function:
program = gl_compile_program(
//vertex shader:
#ifdef __ANDROID__
"#version 320 es\n"
#else
"#version 330\n"
#endif
"#line " STR(__LINE__) "\n"
"uniform mat4 OBJECT_TO_CLIP;\n"
"uniform mat4x3 OBJECT_TO_LIGHT;\n"
"uniform mat3 NORMAL_TO_LIGHT;\n"
Expand All @@ -68,7 +73,14 @@ LitColorTextureProgram::LitColorTextureProgram() {
"}\n"
,
//fragment shader:
#ifdef __ANDROID__
"#version 320 es\n"
"precision highp int;\n" //overkill but saves re-writing the shader below
"precision highp float;\n" //similar
#else
"#version 330\n"
#endif
"#line " STR(__LINE__) "\n"
"uniform sampler2D TEX;\n"
"uniform int LIGHT_TYPE;\n"
"uniform vec3 LIGHT_LOCATION;\n"
Expand Down Expand Up @@ -105,6 +117,8 @@ LitColorTextureProgram::LitColorTextureProgram() {
" vec4 albedo = texture(TEX, texCoord) * color;\n"
" fragColor = vec4(e*albedo.rgb, albedo.a);\n"
"}\n"
,
"LitColorTextureProgram"
);
//As you can see above, adjacent strings in C/C++ are concatenated.
// this is very useful for writing long shader programs inline.
Expand Down
10 changes: 9 additions & 1 deletion Maekfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const game_sources = [

const common_sources = [
'data_path.cpp',
'asset_stream.cpp',
'PathFont.cpp',
'PathFont-font.cpp',
'DrawLines.cpp',
Expand Down Expand Up @@ -216,7 +217,13 @@ const ovr_openxr_loader_so = maek.COPY('../ovr-openxr-sdk/OpenXR/Libs/Android/ar
const libcpp_shared_so = maek.COPY(`${NDK}/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/libc++_shared.so`, 'objs/android/apk/lib/arm64-v8a/libc++_shared.so');


//assets that are just stored in the apk as-is (no processing):
const android_assets = [
maek.COPY('dist/hexapod.scene', 'objs/android/apk/assets/hexapod.scene'),
maek.COPY('dist/hexapod.pnct', 'objs/android/apk/assets/hexapod.pnct'),
];

//icons which are processed before being stored into the apk:
const android_icons = [ ];
for (const dpi of ['mdpi', 'hdpi', 'xhdpi', 'xxhdpi', 'xxxhdpi']) {
const inFile = `android/res/mipmap-${dpi}/gp-icon.png`;
Expand Down Expand Up @@ -259,6 +266,7 @@ const package_apk_task = async () => {
AAPT2, 'link',
'-o', packaged_apk,
'-I', `${ANDROID_PLATFORM}/android.jar`,
'-A', 'objs/android/apk/assets',
'--manifest', 'android/AndroidManifest.xml',
...android_icons,
'-v' //verbose output
Expand Down Expand Up @@ -297,7 +305,7 @@ const package_apk_task = async () => {


};
package_apk_task.depends = [android_game_so, ovr_openxr_loader_so, libcpp_shared_so, ...android_icons];
package_apk_task.depends = [android_game_so, ovr_openxr_loader_so, libcpp_shared_so, ...android_icons, ...android_assets];
package_apk_task.label = `PACKAGE ${android_apk}`;
maek.tasks[android_apk] = package_apk_task;

Expand Down
4 changes: 3 additions & 1 deletion Mesh.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Mesh.hpp"
#include "asset_stream.hpp"
#include "read_write_chunk.hpp"

#include <glm/glm.hpp>
Expand All @@ -14,7 +15,8 @@
MeshBuffer::MeshBuffer(std::string const &filename) {
glGenBuffers(1, &buffer);

std::ifstream file(filename, std::ios::binary);
std::unique_ptr< std::istream > file_str = asset_stream(filename);
std::istream &file = *file_str;

GLuint total = 0;

Expand Down
11 changes: 9 additions & 2 deletions PlayMode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,6 @@ void PlayMode::update(float elapsed) {
}

void PlayMode::draw(glm::uvec2 const &drawable_size) {
//update window's camera aspect ratio for drawable:
camera->aspect = float(drawable_size.x) / float(drawable_size.y);

//set up light type and position for lit_color_texture_program:
// TODO: consider using the Light(s) in the scene to do this
Expand All @@ -184,6 +182,12 @@ void PlayMode::draw(glm::uvec2 const &drawable_size) {
glUniform3fv(lit_color_texture_program->LIGHT_ENERGY_vec3, 1, glm::value_ptr(glm::vec3(1.0f, 1.0f, 0.95f)));
glUseProgram(0);

//NOTE: on android, only render to swapchain images (below), not to the main window:
#ifndef __ANDROID__

//update window's camera aspect ratio for drawable:
camera->aspect = float(drawable_size.x) / float(drawable_size.y);

//main scene drawing into the window:
draw_helper(camera->make_projection() * glm::mat4(camera->transform->make_world_to_local()));

Expand All @@ -209,6 +213,8 @@ void PlayMode::draw(glm::uvec2 const &drawable_size) {
glm::u8vec4(0xff, 0xff, 0xff, 0x00));
}

#endif //__ANDROID__

//----------------------------------------------

if (xr && xr->next_frame.should_render) {
Expand Down Expand Up @@ -251,6 +257,7 @@ void PlayMode::draw(glm::uvec2 const &drawable_size) {
}
}

GL_ERRORS();

}
void PlayMode::draw_helper(glm::mat4 const &world_to_clip) {
Expand Down
5 changes: 3 additions & 2 deletions Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

#include "gl_errors.hpp"
#include "read_write_chunk.hpp"
#include "asset_stream.hpp"

#include <glm/gtc/type_ptr.hpp>

#include <fstream>

//-------------------------

Expand Down Expand Up @@ -169,7 +169,8 @@ void Scene::draw(glm::mat4 const &world_to_clip, glm::mat4x3 const &world_to_lig
void Scene::load(std::string const &filename,
std::function< void(Scene &, Transform *, std::string const &) > const &on_drawable) {

std::ifstream file(filename, std::ios::binary);
std::unique_ptr< std::istream > file_ptr = asset_stream(filename);
auto &file = *file_ptr;

std::vector< char > names;
read_chunk(file, "str0", &names);
Expand Down
6 changes: 4 additions & 2 deletions XR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ XR::XR(
//allocate framebuffer:
glGenFramebuffers(1, &view.framebuffers[i].fb);
glBindFramebuffer(GL_FRAMEBUFFER, view.framebuffers[i].fb);
//glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, view.framebuffers[i].depth_rb);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, view.framebuffers[i].depth_rb);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, view.framebuffers[i].color_tex, 0);

GL_ERRORS();
Expand Down Expand Up @@ -729,7 +729,9 @@ void XR::end_frame() {
}

XrCompositionLayerProjection layer{XR_TYPE_COMPOSITION_LAYER_PROJECTION};
layer.layerFlags = XR_COMPOSITION_LAYER_CORRECT_CHROMATIC_ABERRATION_BIT; //note: "planned for deprecation"
layer.layerFlags = XR_COMPOSITION_LAYER_CORRECT_CHROMATIC_ABERRATION_BIT //note: "planned for deprecation"
//| XR_COMPOSITION_LAYER_BLEND_TEXTURE_SOURCE_ALPHA_BIT //probably not needed for OPAQUE mode?
;
layer.space = stage;
layer.viewCount = projection_views.size();
layer.views = projection_views.data();
Expand Down
41 changes: 41 additions & 0 deletions asset_stream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "asset_stream.hpp"

//Note, based in part on tchow Rainbow's "GameData.cpp"

#if __ANDROID__
#include <sstream>
#include <cassert>

#include <android/native_activity.h>
extern ANativeActivity *activity;
#else
#include <fstream>
#endif

std::unique_ptr< std::istream > asset_stream(std::string const &filename) {
#ifdef __ANDROID__
assert(activity); //DEBUG

AAsset *asset = AAssetManager_open(activity->assetManager, filename.c_str(), AASSET_MODE_STREAMING);

if (asset == NULL) {
throw std::runtime_error("Can't open asset '" + filename + "'.");
}

off64_t size = AAsset_getLength64(asset);
void const *buffer = AAsset_getBuffer(asset);

if (buffer == NULL) {
throw std::runtime_error("Failed to get pointer to entire contents of asset '" + filename + "'.");
}

//copy buffer into a string wrapped in a string stream:
std::unique_ptr< std::istringstream > ret(new std::istringstream(std::string(reinterpret_cast< const char * >(buffer), size)));

AAsset_close(asset);

return ret;
#else //__ANDROID__
return std::make_unique< std::ifstream >(filename, std::ios::binary);
#endif
}
7 changes: 7 additions & 0 deletions asset_stream.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <istream>

//work-around for asset loading on android where assets don't actually have filenames!

std::unique_ptr< std::istream > asset_stream(std::string const &filename);
7 changes: 7 additions & 0 deletions data_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#endif //WINDOWS


#ifndef __ANDROID__
//This function gets the path to the current executable in various os-specific ways:
static std::string get_exe_path() {
#if defined(_WIN32)
Expand Down Expand Up @@ -56,10 +57,16 @@ static std::string get_exe_path() {
#error "No idea what the OS is."
#endif
}
#endif

std::string data_path(std::string const &suffix) {
#ifdef __ANDROID__
std::cout << "Reading from " << suffix << std::endl; //DEBUG
return suffix; //since assets are handled by asset manager and don't have filenames per se
#else
static std::string path = get_exe_path(); //cache result of get_exe_path()
return path + "/" + suffix;
#endif
}

/* From Rktcr; to be used eventually!
Expand Down
13 changes: 7 additions & 6 deletions gl_compile_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <stdexcept>
#include <iostream>

static GLuint gl_compile_shader(GLenum type, std::string const &source) {
static GLuint gl_compile_shader(GLenum type, std::string const &source, std::string const &DEBUG_name) {
GLuint shader = glCreateShader(type);
GLchar const *str = source.c_str();
GLint str_length = GLint(source.size());
Expand All @@ -14,26 +14,27 @@ static GLuint gl_compile_shader(GLenum type, std::string const &source) {
GLint compile_status = GL_FALSE;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_status);
if (compile_status != GL_TRUE) {
std::cerr << "Failed to compile shader." << std::endl;
std::cerr << "Failed to compile shader [" << DEBUG_name << "]." << std::endl;
GLint info_log_length = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_log_length);
std::vector< GLchar > info_log(info_log_length, 0);
GLsizei length = 0;
glGetShaderInfoLog(shader, GLint(info_log.size()), &length, &info_log[0]);
std::cerr << "Info log: " << std::string(info_log.begin(), info_log.begin() + length);
glDeleteShader(shader);
throw std::runtime_error("Failed to compile shader.");
throw std::runtime_error("Failed to compile shader [" + DEBUG_name + "].");
}
return shader;
}

GLuint gl_compile_program(
std::string const &vertex_shader_source,
std::string const &fragment_shader_source
std::string const &fragment_shader_source,
std::string const &DEBUG_program_name
) {

GLuint vertex_shader = gl_compile_shader(GL_VERTEX_SHADER, vertex_shader_source);
GLuint fragment_shader = gl_compile_shader(GL_FRAGMENT_SHADER, fragment_shader_source);
GLuint vertex_shader = gl_compile_shader(GL_VERTEX_SHADER, vertex_shader_source, DEBUG_program_name + ".vertex");
GLuint fragment_shader = gl_compile_shader(GL_FRAGMENT_SHADER, fragment_shader_source, DEBUG_program_name + ".fragment");

GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader);
Expand Down
4 changes: 3 additions & 1 deletion gl_compile_program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
// throws on compilation error.
GLuint gl_compile_program(
std::string const &vertex_shader_source,
std::string const &fragment_shader_source);
std::string const &fragment_shader_source,
std::string const &DEBUG_program_name //program name, used for debug messages
);
Loading

0 comments on commit 1036d27

Please sign in to comment.