Add models_first_person_maze, object destruction error correction, Model.Draw()

This commit is contained in:
Rob Loach
2020-12-27 18:52:30 -05:00
parent 7e0f2f3804
commit afe8574a55
9 changed files with 203 additions and 6 deletions

View File

@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [vX.X.X] - Unreleased
### Added
- `Model.Draw()`
- `Model.DrawWires()`
- `models_first_person_maze.cpp` example
- Error correction when unloading images, materials, models and meshes
## [v3.5.0-alpha1] - 2020-12-26
### Changed
- Update to raylib 3.5.0

View File

@@ -0,0 +1,137 @@
/*******************************************************************************************
*
* raylib [models] example - first person maze
*
* This example has been created using raylib 2.5 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2019 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib-cpp.hpp"
#include <stdlib.h> // Required for: free()
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
raylib::Window window(screenWidth, screenHeight, "raylib [models] example - first person maze");
// Define the camera to look into our 3d world
raylib::Camera camera({ 0.2f, 0.4f, 0.2f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f);
raylib::Image imMap("resources/cubicmap.png"); // Load cubicmap image (RAM)
raylib::Texture cubicmap(imMap); // Convert image to texture to display (VRAM)
raylib::Mesh mesh = raylib::Mesh::Cubicmap(imMap, (Vector3){ 1.0f, 1.0f, 1.0f });
raylib::Model model(mesh);
// NOTE: By default each cube is mapped to one part of texture atlas
raylib::Texture texture("resources/cubicmap_atlas.png"); // Load map texture
model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Set map diffuse texture
// Get map image data to be used for collision detection
Color *mapPixels = imMap.LoadColors();
imMap.Unload(); // Unload image from RAM
raylib::Vector3 mapPosition(-16.0f, 0.0f, -8.0f); // Set model position
raylib::Vector3 playerPosition(camera.position); // Set player position
camera.SetMode(CAMERA_FIRST_PERSON); // Set camera mode
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!window.ShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
raylib::Vector3 oldCamPos(camera.position); // Store old camera position
camera.Update(); // Update camera
// Check player collision (we simplify to 2D collision detection)
raylib::Vector2 playerPos(camera.position.x, camera.position.z);
float playerRadius = 0.1f; // Collision radius (player is modelled as a cilinder for collision)
int playerCellX = static_cast<int>(playerPos.x - mapPosition.x + 0.5f);
int playerCellY = static_cast<int>(playerPos.y - mapPosition.z + 0.5f);
// Out-of-limits security check
if (playerCellX < 0) playerCellX = 0;
else if (playerCellX >= cubicmap.width) playerCellX = cubicmap.width - 1;
if (playerCellY < 0) playerCellY = 0;
else if (playerCellY >= cubicmap.height) playerCellY = cubicmap.height - 1;
// Check map collisions using image data and player position
// TODO: Improvement: Just check player surrounding cells for collision
for (int y = 0; y < cubicmap.height; y++)
{
for (int x = 0; x < cubicmap.width; x++)
{
if ((mapPixels[y*cubicmap.width + x].r == 255) && // Collision: white pixel, only check R channel
(playerPos.CheckCollisionCircleRec(playerRadius,
(Rectangle){ mapPosition.x - 0.5f + x*1.0f, mapPosition.z - 0.5f + y*1.0f, 1.0f, 1.0f })))
{
// Collision detected, reset camera position
camera.position = oldCamPos;
}
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
camera.BeginMode3D();
model.Draw(mapPosition); // Draw maze map
// DrawCubeV(playerPosition, (Vector3){ 0.2f, 0.4f, 0.2f }, RED); // Draw player
camera.EndMode3D();
cubicmap.Draw((Vector2){ GetScreenWidth() - cubicmap.width*4 - 20, 20 }, 0.0f, 4.0f, WHITE);
DrawRectangleLines(GetScreenWidth() - cubicmap.width*4 - 20, 20, cubicmap.width*4, cubicmap.height*4, GREEN);
// Draw player position radar
DrawRectangle(GetScreenWidth() - cubicmap.width*4 - 20 + playerCellX*4, 20 + playerCellY*4, 4, 4, RED);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
free(mapPixels); // Unload color array
/**
* TODO: Fix double freeing of Mesh, possibly from UnloadModel() unloading meshes in Model.hpp?
*
* INFO: TEXTURE: [ID 4] Unloaded texture data from VRAM (GPU)
* INFO: VAO: [ID 2] Unloaded vertex data from VRAM (GPU)
* INFO: MODEL: Unloaded model (and meshes) from RAM and VRAM
* double free or corruption (!prev)
*/
mesh.vboId = NULL; // This shouldn't be needed, but currently is.
//UnloadTexture(cubicmap); // Unload cubicmap texture
//UnloadTexture(texture); // Unload map texture
//UnloadModel(model); // Unload map model
//CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 164 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

View File

@@ -163,7 +163,10 @@ class Image : public ::Image {
}
inline void Unload() {
::UnloadImage(*this);
if (data != NULL) {
::UnloadImage(*this);
data = NULL;
}
}
/**

View File

@@ -76,7 +76,10 @@ class Material : public ::Material {
* Unload material from memory
*/
inline void Unload() {
::UnloadMaterial(*this);
if (maps != NULL) {
::UnloadMaterial(*this);
maps = NULL;
}
}
/**

View File

@@ -27,6 +27,7 @@
#include <string>
#include <vector>
#include <iostream>
#include "./raylib.hpp"
#include "./raylib-cpp-utils.hpp"
@@ -161,7 +162,10 @@ class Mesh : public ::Mesh {
}
inline void Unload() {
::UnloadMesh(*this);
if (vboId != NULL) {
::UnloadMesh(*this);
vboId = NULL;
}
}
inline raylib::BoundingBox BoundingBox() const {

View File

@@ -53,7 +53,12 @@ class Model : public ::Model {
GETTERSETTER(::Matrix, Transform, transform)
GETTERSETTER(int, MeshCount, meshCount)
GETTERSETTER(int, MaterialCount, materialCount)
GETTERSETTER(::Mesh *, Meshes, meshes)
GETTERSETTER(::Material *, Materials, materials)
GETTERSETTER(int *, MeshMaterial, meshMaterial)
GETTERSETTER(int, BoneCount, boneCount)
GETTERSETTER(::BoneInfo *, Bones, bones)
GETTERSETTER(::Transform *, BindPoe, bindPose)
Model& operator=(const ::Model& model) {
set(model);
@@ -69,7 +74,11 @@ class Model : public ::Model {
* Unload model (including meshes) from memory (RAM and/or VRAM)
*/
inline void Unload() {
::UnloadModel(*this);
if (meshes != NULL || materials != NULL) {
::UnloadModel(*this);
meshes = NULL;
materials = NULL;
}
}
/**
@@ -110,6 +119,38 @@ class Model : public ::Model {
return ::IsModelAnimationValid(*this, anim);
}
/**
* Draw a model (with texture if set)
*/
inline Model& Draw(::Vector3 position, float scale = 1.0f, ::Color tint = WHITE) {
::DrawModel(*this, position, scale, tint);
return *this;
}
/**
* Draw a model with extended parameters
*/
inline Model& Draw(::Vector3 position, ::Vector3 rotationAxis, float rotationAngle = 0.0f, ::Vector3 scale = {1.0f,1.0f,1.0f}, ::Color tint = WHITE) {
::DrawModelEx(*this, position, rotationAxis, rotationAngle, scale, tint);
return *this;
}
/**
* Draw a model wires (with texture if set)
*/
inline Model& DrawWires(::Vector3 position, float scale = 1.0f, ::Color tint = WHITE) {
::DrawModelWires(*this, position, scale, tint);
return *this;
}
/**
* Draw a model wires (with texture if set) with extended parameters
*/
inline Model& DrawWires(::Vector3 position, ::Vector3 rotationAxis, float rotationAngle = 0.0f, ::Vector3 scale = {1.0f,1.0f,1.0f}, ::Color tint = WHITE) {
::DrawModelWiresEx(*this, position, rotationAxis, rotationAngle, scale, tint);
return *this;
}
protected:
inline void set(::Model model) {
transform = model.transform;

View File

@@ -51,9 +51,11 @@ class Music : public ::Music {
Unload();
}
GETTERSETTER(int, CtxType, ctxType)
GETTERSETTER(bool, Looping, looping)
GETTERSETTER(::AudioStream, Stream, stream)
GETTERSETTER(unsigned int, SampleCount, sampleCount)
GETTERSETTER(bool, Looping, looping)
GETTERSETTER(int, CtxType, ctxType)
GETTERSETTER(void*, CtxData, ctxData)
Music& operator=(const ::Music& music) {
set(music);