This commit is contained in:
Tyler Bezera 2020-02-28 17:23:42 -08:00
commit 7b4c13d22f
5 changed files with 783 additions and 372 deletions

View file

@ -12,7 +12,10 @@
#include "raylib.h"
// NOTE: Storage positions must start with 0, directly related to file memory layout
typedef enum { STORAGE_SCORE = 0, STORAGE_HISCORE } StorageData;
typedef enum {
STORAGE_POSITION_SCORE = 0,
STORAGE_POSITION_HISCORE = 1
} StorageData;
int main(void)
{
@ -43,14 +46,14 @@ int main(void)
if (IsKeyPressed(KEY_ENTER))
{
StorageSaveValue(STORAGE_SCORE, score);
StorageSaveValue(STORAGE_HISCORE, hiscore);
SaveStorageValue(STORAGE_POSITION_SCORE, score);
SaveStorageValue(STORAGE_POSITION_HISCORE, hiscore);
}
else if (IsKeyPressed(KEY_SPACE))
{
// NOTE: If requested position could not be found, value 0 is returned
score = StorageLoadValue(STORAGE_SCORE);
hiscore = StorageLoadValue(STORAGE_HISCORE);
score = LoadStorageValue(STORAGE_POSITION_SCORE);
hiscore = LoadStorageValue(STORAGE_POSITION_HISCORE);
}
framesCounter++;

View file

@ -0,0 +1,105 @@
/*******************************************************************************************
*
* raylib [models] example - Load 3d model with animations and play them
*
* This example has been created using raylib 3.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Example contributed by Tyler Bezera (@gamerfiend) and reviewed by Ramon Santamaria (@raysan5)
*
* Copyright (c) 2019 Tyler Bezera (@gamerfiend) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include <stdlib.h>
#include "raylib.h"
#define RGLTFANIMATION_IMPLEMENTATION
#include "rgltfanim.h"
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - model animation gltf");
// Define the camera to look into our 3d world
Camera camera = { 0 };
camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
camera.fovy = 45.0f; // Camera field-of-view Y
camera.type = CAMERA_PERSPECTIVE; // Camera mode type
// Load the animated model mesh and basic data
Model model = LoadModel("resources/models/RiggedFigure.glb");
Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
// Load animation data
// int animsCount = 0;
ModelAnimationsGLTF anims = LoadModelGLTFAnimations("resources/models/RiggedFigure.glb");
// int animFrameCounter = 0;
SetCameraMode(camera, CAMERA_FREE); // Set free camera mode
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(&camera);
// Play animation when spacebar is held down
if (IsKeyDown(KEY_SPACE))
{
// animFrameCounter++;
UpdateModelAnimationGLTF(model, 0, 0.5);
// if (animFrameCounter >= anims[0].frameCount) animFrameCounter = 0;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(camera);
DrawModelEx(model, position, (Vector3){ 1.0f, 0.0f, 0.0f }, -90.0f, (Vector3){ 1.0f, 1.0f, 1.0f }, WHITE);
for (int i = 0; i < model.boneCount; i++)
{
//DrawCube(anims[0].framePoses[animFrameCounter][i].translation, 0.2f, 0.2f, 0.2f, RED);
}
DrawGrid(10, 1.0f); // Draw a grid
EndMode3D();
DrawText("PRESS SPACE to PLAY MODEL ANIMATION", 10, 10, 20, MAROON);
DrawText("(c) Guy IQM 3D model by @culacant", screenWidth - 200, screenHeight - 20, 10, GRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// Unload model animations data
// for (int i = 0; i < animsCount; i++) UnloadModelAnimation(anims[i]);
// RL_FREE(anims);
UnloadModel(model); // Unload model
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

Binary file not shown.

257
examples/models/rgltfanim.h Normal file
View file

@ -0,0 +1,257 @@
/**********************************************************************************************
*
* raylib.gltfanimation - Simple module to have animation support with GLTF models, based on the
* implementation work for Google's Filament engine.
*
* CONFIGURATION:
*
* #define RGLTFANIMATION_IMPLEMENTATION
* Generates the implementation of the library into the included file.
* If not defined, the library is in header only mode and can be included in other headers
* or source files without problems. But only ONE file should hold the implementation.
*
* LICENSE: zlib/libpng
*
* Copyright (c) 2020 Tyler Bezera and Ramon Santamaria
*
* This software is provided "as-is", without any express or implied warranty. In no event
* will the authors be held liable for any damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose, including commercial
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not claim that you
* wrote the original software. If you use this software in a product, an acknowledgment
* in the product documentation would be appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
* as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*
**********************************************************************************************/
#ifndef RGLTFANIM_H
#define RGLTFANIM_H
#include "raylib.h"
#include "../../src/external/cgltf.h"
typedef enum
{
TYPE_LINEAR,
TYPE_STEP,
TYPE_CUBICSPLINE
} AnimationGLTFInterpolationType;
typedef enum
{
TYPE_TRANSLATION,
TYPE_ROTATION,
TYPE_SCALE,
TYPE_WEIGHTS
} AnimationGLTFPathType;
typedef struct ModelGLTFAnimationSampler
{
AnimationGLTFInterpolationType interpolationType;
float *sourceValues;
int sourceValuesCount;
} ModelGLTFAnimationSampler;
typedef struct ModelGLTFAnimationChannel
{
AnimationGLTFPathType pathType;
ModelGLTFAnimationSampler *sourceData;
Model targetModel;
} ModelGLTFAnimationChannel;
typedef struct ModelAnimationGLTF
{
char animationName[50];
ModelGLTFAnimationSampler *samplers;
int samplersCount;
ModelGLTFAnimationChannel *channels;
int channelsCount;
float duration;
float start;
float end;
} ModelAnimationGLTF;
typedef struct ModelAnimationsGLTF
{
ModelAnimationGLTF *animations;
int animationsCount;
Matrix *boneMatrices;
int boneMatricesCount;
} ModelAnimationsGLTF;
#ifdef __cplusplus
extern "C"
{ // Prevents name mangling of functions
#endif
//----------------------------------------------------------------------------------
// Module Functions Declaration
//----------------------------------------------------------------------------------
ModelAnimationsGLTF LoadModelGLTFAnimations(const char *filename);
void UpdateModelAnimationGLTF(Model model, int animationIndex, float time);
void createSampler(cgltf_animation_sampler *src, ModelGLTFAnimationSampler *dst);
#ifdef __cplusplus
}
#endif
#endif //RGLTFANIM_H
/***********************************************************************************
*
* RGLTFANIM IMPLEMENTATION
*
************************************************************************************/
#if defined(RGLTFANIMATION_IMPLEMENTATION)
#include "raylib.h"
#include <stdio.h>
//#define CGLTF_IMPLEMENTATION
#include "../../src/external/cgltf.h"
void createSampler(cgltf_animation_sampler *src, ModelGLTFAnimationSampler *dst) {
//uint8_t *tileLineBlob = (uint8_t*)src->input->buffer_view->buffer->data;
//float *tileLineFloats = (float*)(tileLineBlob + src->input->offset + src->input->buffer_view->offset);
for (int i = 0, len = src->input->count; i < len; ++i) {
//TODO: Need to support map
//dst->times
switch (src->output->type) {
case cgltf_type_scalar:
dst->sourceValues = RL_CALLOC(src->output->count, sizeof(float));
cgltf_accessor_unpack_floats(src->output, &dst->sourceValues[0], src->output->count);
break;
case cgltf_type_vec3:
dst->sourceValues = RL_CALLOC(src->output->count * 3, sizeof(float));
cgltf_accessor_unpack_floats(src->output, &dst->sourceValues[0], src->output->count * 3);
break;
case cgltf_type_vec4:
dst->sourceValues = RL_CALLOC(src->output->count * 3, sizeof(float));
cgltf_accessor_unpack_floats(src->output, &dst->sourceValues[0], src->output->count * 3);
break;
default:
break;
}
switch (src->interpolation) {
case cgltf_interpolation_type_linear:
dst->interpolationType = TYPE_LINEAR;
break;
case cgltf_interpolation_type_step:
dst->interpolationType = TYPE_STEP;
break;
case cgltf_interpolation_type_cubic_spline:
dst->interpolationType = TYPE_CUBICSPLINE;
break;
}
}
}
void setTransformType(cgltf_animation_channel *src, ModelGLTFAnimationChannel *dst) {
switch (src->target_path) {
case cgltf_animation_path_type_translation:
dst->pathType = TYPE_TRANSLATION;
break;
case cgltf_animation_path_type_rotation:
dst->pathType = TYPE_ROTATION;
break;
case cgltf_animation_path_type_scale:
dst->pathType = TYPE_SCALE;
break;
case cgltf_animation_path_type_weights:
dst->pathType = TYPE_WEIGHTS;
break;
}
}
ModelAnimationsGLTF LoadModelGLTFAnimations(const char *fileName) {
ModelAnimationsGLTF animationsGLTF = { 0 };
// glTF file loading
FILE *gltfFile = fopen(fileName, "rb");
if (gltfFile == NULL)
{
TraceLog(LOG_WARNING, "[%s] glTF file could not be opened", fileName);
return animationsGLTF;
}
fseek(gltfFile, 0, SEEK_END);
int size = ftell(gltfFile);
fseek(gltfFile, 0, SEEK_SET);
void *buffer = RL_MALLOC(size);
fread(buffer, size, 1, gltfFile);
fclose(gltfFile);
// glTF data loading
cgltf_options options = { 0 };
cgltf_data *data = NULL;
cgltf_result result = cgltf_parse(&options, buffer, size, &data);
if (result == cgltf_result_success)
{
// Read data buffers
result = cgltf_load_buffers(&options, data, fileName);
if (result != cgltf_result_success) TraceLog(LOG_INFO, "[%s][%s] Error loading mesh/material buffers", fileName, (data->file_type == 2)? "glb" : "gltf");
//Animation count
int animationsCount = data->animations_count;
//Initialize our animations array
animationsGLTF.animationsCount = animationsCount;
animationsGLTF.animations = RL_CALLOC(animationsCount, sizeof(ModelAnimationGLTF));
//Loop through each Animation
for (int i = 0; i < animationsCount; i++) {
//Copy Animation name
if(data->animations[i].name) TextCopy(animationsGLTF.animations[i].animationName, data->animations[i].name);
int samplerCount = data->animations[i].samplers_count;
animationsGLTF.animations[i].samplersCount = samplerCount;
animationsGLTF.animations[i].samplers = RL_CALLOC(samplerCount, sizeof(ModelGLTFAnimationSampler));
for (int j = 0; j < samplerCount; j++) {
createSampler(&data->animations[i].samplers[j], &animationsGLTF.animations[i].samplers[j]);
//TODO handle map times
}
int channelCount = data->animations[i].channels_count;
animationsGLTF.animations[i].channelsCount = channelCount;
animationsGLTF.animations[i].channels = RL_CALLOC(channelCount, sizeof(ModelGLTFAnimationChannel));
for (int j = 0; j < channelCount; j++) {
//TODO Model stuff? Also... strange pointer arthmetic? data->animations[i].channels[j].sampler - data->animations[i].samplers??
animationsGLTF.animations[i].channels[j].sourceData = &animationsGLTF.animations[i].samplers[data->animations[i].channels[j].sampler - data->animations[i].samplers];
setTransformType(&data->animations[i].channels[j], &animationsGLTF.animations[i].channels[j]);
}
}
}
return animationsGLTF;
}
void UpdateModelAnimationGLTF(Model model, int animationIndex, float time) {
}
#endif

View file

@ -54,6 +54,8 @@
#include <GLFW/glfw3.h> // Windows/Context and inputs management
#include <stdio.h> // Required for: printf()
#define RED (Color){ 230, 41, 55, 255 } // Red
#define RAYWHITE (Color){ 245, 245, 245, 255 } // My own White (raylib logo)
#define DARKGRAY (Color){ 80, 80, 80, 255 } // Dark Gray
@ -61,8 +63,8 @@
//----------------------------------------------------------------------------------
// Module specific Functions Declaration
//----------------------------------------------------------------------------------
static void ErrorCallback(int error, const char* description);
static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
static void ErrorCallback(int error, const char *description);
static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods);
// Drawing functions (uses rlgl functionality)
static void DrawGrid(int slices, float spacing);
@ -86,10 +88,10 @@ int main(void)
if (!glfwInit())
{
TraceLog(LOG_WARNING, "GLFW3: Can not initialize GLFW");
printf("GLFW3: Can not initialize GLFW\n");
return 1;
}
else TraceLog(LOG_INFO, "GLFW3: GLFW initialized successfully");
else printf("GLFW3: GLFW initialized successfully\n");
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_DEPTH_BITS, 16);
@ -105,7 +107,7 @@ int main(void)
glfwTerminate();
return 2;
}
else TraceLog(LOG_INFO, "GLFW3: Window created successfully");
else printf("GLFW3: Window created successfully\n");
glfwSetWindowPos(window, 200, 200);
@ -215,13 +217,13 @@ int main(void)
//----------------------------------------------------------------------------------
// GLFW3: Error callback
static void ErrorCallback(int error, const char* description)
static void ErrorCallback(int error, const char *description)
{
TraceLog(LOG_ERROR, description);
fprintf(stderr, description);
}
// GLFW3: Keyboard callback
static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
{