Downloadable
Controls:
Exit: ESC or click X
Pause/Resume: Space (hold)
Hide Mesh: Shift (hold)
Change Effect: Ctrl (hold)
Change Mesh: Enter (hold)
Move Object: Up/Down/Left/Right (hold)
Move Camera: W/A/S/D (hold)
Rotate Camera: Q/E (hold)
Key Points
Design & Load Binary File
The main process of loading binary files of effect is pretty much the same as that of loading binary files of mesh. We need a builder, we need our lua-format files, and we also need to load our binary files when initializing.
Considering Effect class needs render state, vertex shader, and fragment shader for initialization, here is an example of my human-readable effect file:
-- Standard effect
return{
render_state = {
alpha_transparency = false,
depth_testing = true,
depth_writing = true,
draw_both_triangle_sides = false,
},
vertex_shader = "Shaders/Vertex/standard.shader",
fragment_shader = "Shaders/Fragment/standard.shader",
}
The binary version of that file looks like this:
The first byte is RenderState, which is a uint8_t type integer. The readable parts of the file are paths of two shaders. I chose to add "data/" during build-time to save some time when loading binary files. Though the prefix increases file size, I think this is the better choice because we want to use paths from files directly when loading. The loaded file's memory is consistent, and we don't want to reload path strings to another place.
I added a '\0' at the end of the path string. One reason was that I could use it as a string without processing it when loading. The other reason was that, because I did not include string length in files, I needed it for the strlen() function, so the processor knows where the second string starts.
This is how I extract them:
const char* vertex_path = reinterpret_cast<char*>(currentOffset);
currentOffset += strlen(vertex_path) + 1;
const char* fragment_path = reinterpret_cast<char*>(currentOffset);
currentOffset += strlen(fragment_path);
Here’s a screenshot of the gameplay:
Opmerkingen