convert submodules to normal folders for now
111
raylib/examples/models/models_animation.c
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Load 3d model with animations and play them
|
||||
*
|
||||
* Example originally created with raylib 2.5, last time updated with raylib 3.5
|
||||
*
|
||||
* Example contributed by Culacant (@culacant) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2019-2024 Culacant (@culacant) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************
|
||||
*
|
||||
* NOTE: To export a model from blender, make sure it is not posed, the vertices need to be
|
||||
* in the same position as they would be in edit mode and the scale of your models is
|
||||
* set to 0. Scaling can be done from the export menu.
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [models] example - model animation");
|
||||
|
||||
// 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.projection = CAMERA_PERSPECTIVE; // Camera mode type
|
||||
|
||||
Model model = LoadModel("resources/models/iqm/guy.iqm"); // Load the animated model mesh and basic data
|
||||
Texture2D texture = LoadTexture("resources/models/iqm/guytex.png"); // Load model texture and set material
|
||||
SetMaterialTexture(&model.materials[0], MATERIAL_MAP_DIFFUSE, texture); // Set model material map texture
|
||||
|
||||
Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
|
||||
|
||||
// Load animation data
|
||||
int animsCount = 0;
|
||||
ModelAnimation *anims = LoadModelAnimations("resources/models/iqm/guyanim.iqm", &animsCount);
|
||||
int animFrameCounter = 0;
|
||||
|
||||
DisableCursor(); // Catch cursor
|
||||
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, CAMERA_FIRST_PERSON);
|
||||
|
||||
// Play animation when spacebar is held down
|
||||
if (IsKeyDown(KEY_SPACE))
|
||||
{
|
||||
animFrameCounter++;
|
||||
UpdateModelAnimation(model, anims[0], animFrameCounter);
|
||||
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();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadTexture(texture); // Unload texture
|
||||
UnloadModelAnimations(anims, animsCount); // Unload model animations data
|
||||
UnloadModel(model); // Unload model
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
raylib/examples/models/models_animation.png
Normal file
|
After Width: | Height: | Size: 65 KiB |
113
raylib/examples/models/models_billboard.c
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Drawing billboards
|
||||
*
|
||||
* Example originally created with raylib 1.3, last time updated with raylib 3.5
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2015-2024 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
#include "raymath.h"
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [models] example - drawing billboards");
|
||||
|
||||
// Define the camera to look into our 3d world
|
||||
Camera camera = { 0 };
|
||||
camera.position = (Vector3){ 5.0f, 4.0f, 5.0f }; // Camera position
|
||||
camera.target = (Vector3){ 0.0f, 2.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.projection = CAMERA_PERSPECTIVE; // Camera projection type
|
||||
|
||||
Texture2D bill = LoadTexture("resources/billboard.png"); // Our billboard texture
|
||||
Vector3 billPositionStatic = { 0.0f, 2.0f, 0.0f }; // Position of static billboard
|
||||
Vector3 billPositionRotating = { 1.0f, 2.0f, 1.0f }; // Position of rotating billboard
|
||||
|
||||
// Entire billboard texture, source is used to take a segment from a larger texture.
|
||||
Rectangle source = { 0.0f, 0.0f, (float)bill.width, (float)bill.height };
|
||||
|
||||
// NOTE: Billboard locked on axis-Y
|
||||
Vector3 billUp = { 0.0f, 1.0f, 0.0f };
|
||||
|
||||
// Set the height of the rotating billboard to 1.0 with the aspect ratio fixed
|
||||
Vector2 size = { source.width/source.height, 1.0f };
|
||||
|
||||
// Rotate around origin
|
||||
// Here we choose to rotate around the image center
|
||||
Vector2 origin = Vector2Scale(size, 0.5f);
|
||||
|
||||
// Distance is needed for the correct billboard draw order
|
||||
// Larger distance (further away from the camera) should be drawn prior to smaller distance.
|
||||
float distanceStatic;
|
||||
float distanceRotating;
|
||||
float rotation = 0.0f;
|
||||
|
||||
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, CAMERA_ORBITAL);
|
||||
|
||||
rotation += 0.4f;
|
||||
distanceStatic = Vector3Distance(camera.position, billPositionStatic);
|
||||
distanceRotating = Vector3Distance(camera.position, billPositionRotating);
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
BeginMode3D(camera);
|
||||
|
||||
DrawGrid(10, 1.0f); // Draw a grid
|
||||
|
||||
// Draw order matters!
|
||||
if (distanceStatic > distanceRotating)
|
||||
{
|
||||
DrawBillboard(camera, bill, billPositionStatic, 2.0f, WHITE);
|
||||
DrawBillboardPro(camera, bill, source, billPositionRotating, billUp, size, origin, rotation, WHITE);
|
||||
}
|
||||
else
|
||||
{
|
||||
DrawBillboardPro(camera, bill, source, billPositionRotating, billUp, size, origin, rotation, WHITE);
|
||||
DrawBillboard(camera, bill, billPositionStatic, 2.0f, WHITE);
|
||||
}
|
||||
|
||||
EndMode3D();
|
||||
|
||||
DrawFPS(10, 10);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadTexture(bill); // Unload texture
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
raylib/examples/models/models_billboard.png
Normal file
|
After Width: | Height: | Size: 54 KiB |
177
raylib/examples/models/models_bone_socket.c
Normal file
|
|
@ -0,0 +1,177 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [core] example - Using bones as socket for calculating the positioning of something
|
||||
*
|
||||
* Example originally created with raylib 4.5, last time updated with raylib 4.5
|
||||
*
|
||||
* Example contributed by iP (@ipzaur) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2024 iP (@ipzaur)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#include "raymath.h"
|
||||
|
||||
#define BONE_SOCKETS 3
|
||||
#define BONE_SOCKET_HAT 0
|
||||
#define BONE_SOCKET_HAND_R 1
|
||||
#define BONE_SOCKET_HAND_L 2
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [models] example - bone socket");
|
||||
|
||||
// Define the camera to look into our 3d world
|
||||
Camera camera = { 0 };
|
||||
camera.position = (Vector3){ 5.0f, 5.0f, 5.0f }; // Camera position
|
||||
camera.target = (Vector3){ 0.0f, 2.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.projection = CAMERA_PERSPECTIVE; // Camera projection type
|
||||
|
||||
// Load gltf model
|
||||
Model characterModel = LoadModel("resources/models/gltf/greenman.glb"); // Load character model
|
||||
Model equipModel[BONE_SOCKETS] = {
|
||||
LoadModel("resources/models/gltf/greenman_hat.glb"), // Index for the hat model is the same as BONE_SOCKET_HAT
|
||||
LoadModel("resources/models/gltf/greenman_sword.glb"), // Index for the sword model is the same as BONE_SOCKET_HAND_R
|
||||
LoadModel("resources/models/gltf/greenman_shield.glb") // Index for the shield model is the same as BONE_SOCKET_HAND_L
|
||||
};
|
||||
|
||||
bool showEquip[3] = { true, true, true }; // Toggle on/off equip
|
||||
|
||||
// Load gltf model animations
|
||||
int animsCount = 0;
|
||||
unsigned int animIndex = 0;
|
||||
unsigned int animCurrentFrame = 0;
|
||||
ModelAnimation *modelAnimations = LoadModelAnimations("resources/models/gltf/greenman.glb", &animsCount);
|
||||
|
||||
// indices of bones for sockets
|
||||
int boneSocketIndex[BONE_SOCKETS] = { -1, -1, -1 };
|
||||
|
||||
// search bones for sockets
|
||||
for (int i = 0; i < characterModel.boneCount; i++)
|
||||
{
|
||||
if (TextIsEqual(characterModel.bones[i].name, "socket_hat"))
|
||||
{
|
||||
boneSocketIndex[BONE_SOCKET_HAT] = i;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (TextIsEqual(characterModel.bones[i].name, "socket_hand_R"))
|
||||
{
|
||||
boneSocketIndex[BONE_SOCKET_HAND_R] = i;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (TextIsEqual(characterModel.bones[i].name, "socket_hand_L"))
|
||||
{
|
||||
boneSocketIndex[BONE_SOCKET_HAND_L] = i;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
|
||||
unsigned short angle = 0; // Set angle for rotate character
|
||||
|
||||
DisableCursor(); // Limit cursor to relative movement inside the window
|
||||
|
||||
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, CAMERA_THIRD_PERSON);
|
||||
|
||||
// Rotate character
|
||||
if (IsKeyDown(KEY_F)) angle = (angle + 1)%360;
|
||||
else if (IsKeyDown(KEY_H)) angle = (360 + angle - 1)%360;
|
||||
|
||||
// Select current animation
|
||||
if (IsKeyPressed(KEY_T)) animIndex = (animIndex + 1)%animsCount;
|
||||
else if (IsKeyPressed(KEY_G)) animIndex = (animIndex + animsCount - 1)%animsCount;
|
||||
|
||||
// Toggle shown of equip
|
||||
if (IsKeyPressed(KEY_ONE)) showEquip[BONE_SOCKET_HAT] = !showEquip[BONE_SOCKET_HAT];
|
||||
if (IsKeyPressed(KEY_TWO)) showEquip[BONE_SOCKET_HAND_R] = !showEquip[BONE_SOCKET_HAND_R];
|
||||
if (IsKeyPressed(KEY_THREE)) showEquip[BONE_SOCKET_HAND_L] = !showEquip[BONE_SOCKET_HAND_L];
|
||||
|
||||
// Update model animation
|
||||
ModelAnimation anim = modelAnimations[animIndex];
|
||||
animCurrentFrame = (animCurrentFrame + 1)%anim.frameCount;
|
||||
UpdateModelAnimation(characterModel, anim, animCurrentFrame);
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
BeginMode3D(camera);
|
||||
// Draw character
|
||||
Quaternion characterRotate = QuaternionFromAxisAngle((Vector3){ 0.0f, 1.0f, 0.0f }, angle*DEG2RAD);
|
||||
characterModel.transform = MatrixMultiply(QuaternionToMatrix(characterRotate), MatrixTranslate(position.x, position.y, position.z));
|
||||
UpdateModelAnimation(characterModel, anim, animCurrentFrame);
|
||||
DrawMesh(characterModel.meshes[0], characterModel.materials[1], characterModel.transform);
|
||||
|
||||
// Draw equipments (hat, sword, shield)
|
||||
for (int i = 0; i < BONE_SOCKETS; i++)
|
||||
{
|
||||
if (!showEquip[i]) continue;
|
||||
|
||||
Transform *transform = &anim.framePoses[animCurrentFrame][boneSocketIndex[i]];
|
||||
Quaternion inRotation = characterModel.bindPose[boneSocketIndex[i]].rotation;
|
||||
Quaternion outRotation = transform->rotation;
|
||||
|
||||
// Calculate socket rotation (angle between bone in initial pose and same bone in current animation frame)
|
||||
Quaternion rotate = QuaternionMultiply(outRotation, QuaternionInvert(inRotation));
|
||||
Matrix matrixTransform = QuaternionToMatrix(rotate);
|
||||
// Translate socket to its position in the current animation
|
||||
matrixTransform = MatrixMultiply(matrixTransform, MatrixTranslate(transform->translation.x, transform->translation.y, transform->translation.z));
|
||||
// Transform the socket using the transform of the character (angle and translate)
|
||||
matrixTransform = MatrixMultiply(matrixTransform, characterModel.transform);
|
||||
|
||||
// Draw mesh at socket position with socket angle rotation
|
||||
DrawMesh(equipModel[i].meshes[0], equipModel[i].materials[1], matrixTransform);
|
||||
}
|
||||
|
||||
DrawGrid(10, 1.0f);
|
||||
EndMode3D();
|
||||
|
||||
DrawText("Use the T/G to switch animation", 10, 10, 20, GRAY);
|
||||
DrawText("Use the F/H to rotate character left/right", 10, 35, 20, GRAY);
|
||||
DrawText("Use the 1,2,3 to toggle shown of hat, sword and shield", 10, 60, 20, GRAY);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadModelAnimations(modelAnimations, animsCount);
|
||||
UnloadModel(characterModel); // Unload character model and meshes/material
|
||||
|
||||
// Unload equipment model and meshes/material
|
||||
for (int i = 0; i < BONE_SOCKETS; i++) UnloadModel(equipModel[i]);
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
raylib/examples/models/models_bone_socket.png
Normal file
|
After Width: | Height: | Size: 40 KiB |
126
raylib/examples/models/models_box_collisions.c
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Detect basic 3d collisions (box vs sphere vs box)
|
||||
*
|
||||
* Example originally created with raylib 1.3, last time updated with raylib 3.5
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2015-2024 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [models] example - box collisions");
|
||||
|
||||
// Define the camera to look into our 3d world
|
||||
Camera camera = { { 0.0f, 10.0f, 10.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
|
||||
|
||||
Vector3 playerPosition = { 0.0f, 1.0f, 2.0f };
|
||||
Vector3 playerSize = { 1.0f, 2.0f, 1.0f };
|
||||
Color playerColor = GREEN;
|
||||
|
||||
Vector3 enemyBoxPos = { -4.0f, 1.0f, 0.0f };
|
||||
Vector3 enemyBoxSize = { 2.0f, 2.0f, 2.0f };
|
||||
|
||||
Vector3 enemySpherePos = { 4.0f, 0.0f, 0.0f };
|
||||
float enemySphereSize = 1.5f;
|
||||
|
||||
bool collision = false;
|
||||
|
||||
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
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Move player
|
||||
if (IsKeyDown(KEY_RIGHT)) playerPosition.x += 0.2f;
|
||||
else if (IsKeyDown(KEY_LEFT)) playerPosition.x -= 0.2f;
|
||||
else if (IsKeyDown(KEY_DOWN)) playerPosition.z += 0.2f;
|
||||
else if (IsKeyDown(KEY_UP)) playerPosition.z -= 0.2f;
|
||||
|
||||
collision = false;
|
||||
|
||||
// Check collisions player vs enemy-box
|
||||
if (CheckCollisionBoxes(
|
||||
(BoundingBox){(Vector3){ playerPosition.x - playerSize.x/2,
|
||||
playerPosition.y - playerSize.y/2,
|
||||
playerPosition.z - playerSize.z/2 },
|
||||
(Vector3){ playerPosition.x + playerSize.x/2,
|
||||
playerPosition.y + playerSize.y/2,
|
||||
playerPosition.z + playerSize.z/2 }},
|
||||
(BoundingBox){(Vector3){ enemyBoxPos.x - enemyBoxSize.x/2,
|
||||
enemyBoxPos.y - enemyBoxSize.y/2,
|
||||
enemyBoxPos.z - enemyBoxSize.z/2 },
|
||||
(Vector3){ enemyBoxPos.x + enemyBoxSize.x/2,
|
||||
enemyBoxPos.y + enemyBoxSize.y/2,
|
||||
enemyBoxPos.z + enemyBoxSize.z/2 }})) collision = true;
|
||||
|
||||
// Check collisions player vs enemy-sphere
|
||||
if (CheckCollisionBoxSphere(
|
||||
(BoundingBox){(Vector3){ playerPosition.x - playerSize.x/2,
|
||||
playerPosition.y - playerSize.y/2,
|
||||
playerPosition.z - playerSize.z/2 },
|
||||
(Vector3){ playerPosition.x + playerSize.x/2,
|
||||
playerPosition.y + playerSize.y/2,
|
||||
playerPosition.z + playerSize.z/2 }},
|
||||
enemySpherePos, enemySphereSize)) collision = true;
|
||||
|
||||
if (collision) playerColor = RED;
|
||||
else playerColor = GREEN;
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
BeginMode3D(camera);
|
||||
|
||||
// Draw enemy-box
|
||||
DrawCube(enemyBoxPos, enemyBoxSize.x, enemyBoxSize.y, enemyBoxSize.z, GRAY);
|
||||
DrawCubeWires(enemyBoxPos, enemyBoxSize.x, enemyBoxSize.y, enemyBoxSize.z, DARKGRAY);
|
||||
|
||||
// Draw enemy-sphere
|
||||
DrawSphere(enemySpherePos, enemySphereSize, GRAY);
|
||||
DrawSphereWires(enemySpherePos, enemySphereSize, 16, 16, DARKGRAY);
|
||||
|
||||
// Draw player
|
||||
DrawCubeV(playerPosition, playerSize, playerColor);
|
||||
|
||||
DrawGrid(10, 1.0f); // Draw a grid
|
||||
|
||||
EndMode3D();
|
||||
|
||||
DrawText("Move player with arrow keys to collide", 220, 40, 20, GRAY);
|
||||
|
||||
DrawFPS(10, 10);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
raylib/examples/models/models_box_collisions.png
Normal file
|
After Width: | Height: | Size: 22 KiB |
99
raylib/examples/models/models_cubicmap.c
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Cubicmap loading and drawing
|
||||
*
|
||||
* Example originally created with raylib 1.8, last time updated with raylib 3.5
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2015-2024 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [models] example - cubesmap loading and drawing");
|
||||
|
||||
// Define the camera to look into our 3d world
|
||||
Camera camera = { 0 };
|
||||
camera.position = (Vector3){ 16.0f, 14.0f, 16.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.projection = CAMERA_PERSPECTIVE; // Camera projection type
|
||||
|
||||
Image image = LoadImage("resources/cubicmap.png"); // Load cubicmap image (RAM)
|
||||
Texture2D cubicmap = LoadTextureFromImage(image); // Convert image to texture to display (VRAM)
|
||||
|
||||
Mesh mesh = GenMeshCubicmap(image, (Vector3){ 1.0f, 1.0f, 1.0f });
|
||||
Model model = LoadModelFromMesh(mesh);
|
||||
|
||||
// NOTE: By default each cube is mapped to one part of texture atlas
|
||||
Texture2D texture = LoadTexture("resources/cubicmap_atlas.png"); // Load map texture
|
||||
model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture
|
||||
|
||||
Vector3 mapPosition = { -16.0f, 0.0f, -8.0f }; // Set model position
|
||||
|
||||
UnloadImage(image); // Unload cubesmap image from RAM, already uploaded to VRAM
|
||||
|
||||
bool pause = false; // Pause camera orbital rotation (and zoom)
|
||||
|
||||
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
|
||||
//----------------------------------------------------------------------------------
|
||||
if (IsKeyPressed(KEY_P)) pause = !pause;
|
||||
|
||||
if (!pause) UpdateCamera(&camera, CAMERA_ORBITAL);
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
BeginMode3D(camera);
|
||||
|
||||
DrawModel(model, mapPosition, 1.0f, WHITE);
|
||||
|
||||
EndMode3D();
|
||||
|
||||
DrawTextureEx(cubicmap, (Vector2){ screenWidth - cubicmap.width*4.0f - 20, 20.0f }, 0.0f, 4.0f, WHITE);
|
||||
DrawRectangleLines(screenWidth - cubicmap.width*4 - 20, 20, cubicmap.width*4, cubicmap.height*4, GREEN);
|
||||
|
||||
DrawText("cubicmap image used to", 658, 90, 10, GRAY);
|
||||
DrawText("generate map 3d model", 658, 104, 10, GRAY);
|
||||
|
||||
DrawFPS(10, 10);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadTexture(cubicmap); // Unload cubicmap texture
|
||||
UnloadTexture(texture); // Unload map texture
|
||||
UnloadModel(model); // Unload map model
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
raylib/examples/models/models_cubicmap.png
Normal file
|
After Width: | Height: | Size: 403 KiB |
245
raylib/examples/models/models_draw_cube_texture.c
Normal file
|
|
@ -0,0 +1,245 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Draw textured cube
|
||||
*
|
||||
* Example originally created with raylib 4.5, last time updated with raylib 4.5
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2022-2024 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#include "rlgl.h" // Required to define vertex data (immediate-mode style)
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Custom Functions Declaration
|
||||
//------------------------------------------------------------------------------------
|
||||
void DrawCubeTexture(Texture2D texture, Vector3 position, float width, float height, float length, Color color); // Draw cube textured
|
||||
void DrawCubeTextureRec(Texture2D texture, Rectangle source, Vector3 position, float width, float height, float length, Color color); // Draw cube with a region of a texture
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [models] example - draw cube texture");
|
||||
|
||||
// Define the camera to look into our 3d world
|
||||
Camera camera = { 0 };
|
||||
camera.position = (Vector3){ 0.0f, 10.0f, 10.0f };
|
||||
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
|
||||
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
|
||||
camera.fovy = 45.0f;
|
||||
camera.projection = CAMERA_PERSPECTIVE;
|
||||
|
||||
// Load texture to be applied to the cubes sides
|
||||
Texture2D texture = LoadTexture("resources/cubicmap_atlas.png");
|
||||
|
||||
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
|
||||
//----------------------------------------------------------------------------------
|
||||
// TODO: Update your variables here
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
BeginMode3D(camera);
|
||||
|
||||
// Draw cube with an applied texture
|
||||
DrawCubeTexture(texture, (Vector3){ -2.0f, 2.0f, 0.0f }, 2.0f, 4.0f, 2.0f, WHITE);
|
||||
|
||||
// Draw cube with an applied texture, but only a defined rectangle piece of the texture
|
||||
DrawCubeTextureRec(texture, (Rectangle){ 0.0f, texture.height/2.0f, texture.width/2.0f, texture.height/2.0f },
|
||||
(Vector3){ 2.0f, 1.0f, 0.0f }, 2.0f, 2.0f, 2.0f, WHITE);
|
||||
|
||||
DrawGrid(10, 1.0f); // Draw a grid
|
||||
|
||||
EndMode3D();
|
||||
|
||||
DrawFPS(10, 10);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadTexture(texture); // Unload texture
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Custom Functions Definition
|
||||
//------------------------------------------------------------------------------------
|
||||
// Draw cube textured
|
||||
// NOTE: Cube position is the center position
|
||||
void DrawCubeTexture(Texture2D texture, Vector3 position, float width, float height, float length, Color color)
|
||||
{
|
||||
float x = position.x;
|
||||
float y = position.y;
|
||||
float z = position.z;
|
||||
|
||||
// Set desired texture to be enabled while drawing following vertex data
|
||||
rlSetTexture(texture.id);
|
||||
|
||||
// Vertex data transformation can be defined with the commented lines,
|
||||
// but in this example we calculate the transformed vertex data directly when calling rlVertex3f()
|
||||
//rlPushMatrix();
|
||||
// NOTE: Transformation is applied in inverse order (scale -> rotate -> translate)
|
||||
//rlTranslatef(2.0f, 0.0f, 0.0f);
|
||||
//rlRotatef(45, 0, 1, 0);
|
||||
//rlScalef(2.0f, 2.0f, 2.0f);
|
||||
|
||||
rlBegin(RL_QUADS);
|
||||
rlColor4ub(color.r, color.g, color.b, color.a);
|
||||
// Front Face
|
||||
rlNormal3f(0.0f, 0.0f, 1.0f); // Normal Pointing Towards Viewer
|
||||
rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x - width/2, y - height/2, z + length/2); // Bottom Left Of The Texture and Quad
|
||||
rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x + width/2, y - height/2, z + length/2); // Bottom Right Of The Texture and Quad
|
||||
rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x + width/2, y + height/2, z + length/2); // Top Right Of The Texture and Quad
|
||||
rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x - width/2, y + height/2, z + length/2); // Top Left Of The Texture and Quad
|
||||
// Back Face
|
||||
rlNormal3f(0.0f, 0.0f, - 1.0f); // Normal Pointing Away From Viewer
|
||||
rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x - width/2, y - height/2, z - length/2); // Bottom Right Of The Texture and Quad
|
||||
rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x - width/2, y + height/2, z - length/2); // Top Right Of The Texture and Quad
|
||||
rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x + width/2, y + height/2, z - length/2); // Top Left Of The Texture and Quad
|
||||
rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x + width/2, y - height/2, z - length/2); // Bottom Left Of The Texture and Quad
|
||||
// Top Face
|
||||
rlNormal3f(0.0f, 1.0f, 0.0f); // Normal Pointing Up
|
||||
rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x - width/2, y + height/2, z - length/2); // Top Left Of The Texture and Quad
|
||||
rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x - width/2, y + height/2, z + length/2); // Bottom Left Of The Texture and Quad
|
||||
rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x + width/2, y + height/2, z + length/2); // Bottom Right Of The Texture and Quad
|
||||
rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x + width/2, y + height/2, z - length/2); // Top Right Of The Texture and Quad
|
||||
// Bottom Face
|
||||
rlNormal3f(0.0f, - 1.0f, 0.0f); // Normal Pointing Down
|
||||
rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x - width/2, y - height/2, z - length/2); // Top Right Of The Texture and Quad
|
||||
rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x + width/2, y - height/2, z - length/2); // Top Left Of The Texture and Quad
|
||||
rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x + width/2, y - height/2, z + length/2); // Bottom Left Of The Texture and Quad
|
||||
rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x - width/2, y - height/2, z + length/2); // Bottom Right Of The Texture and Quad
|
||||
// Right face
|
||||
rlNormal3f(1.0f, 0.0f, 0.0f); // Normal Pointing Right
|
||||
rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x + width/2, y - height/2, z - length/2); // Bottom Right Of The Texture and Quad
|
||||
rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x + width/2, y + height/2, z - length/2); // Top Right Of The Texture and Quad
|
||||
rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x + width/2, y + height/2, z + length/2); // Top Left Of The Texture and Quad
|
||||
rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x + width/2, y - height/2, z + length/2); // Bottom Left Of The Texture and Quad
|
||||
// Left Face
|
||||
rlNormal3f( - 1.0f, 0.0f, 0.0f); // Normal Pointing Left
|
||||
rlTexCoord2f(0.0f, 0.0f); rlVertex3f(x - width/2, y - height/2, z - length/2); // Bottom Left Of The Texture and Quad
|
||||
rlTexCoord2f(1.0f, 0.0f); rlVertex3f(x - width/2, y - height/2, z + length/2); // Bottom Right Of The Texture and Quad
|
||||
rlTexCoord2f(1.0f, 1.0f); rlVertex3f(x - width/2, y + height/2, z + length/2); // Top Right Of The Texture and Quad
|
||||
rlTexCoord2f(0.0f, 1.0f); rlVertex3f(x - width/2, y + height/2, z - length/2); // Top Left Of The Texture and Quad
|
||||
rlEnd();
|
||||
//rlPopMatrix();
|
||||
|
||||
rlSetTexture(0);
|
||||
}
|
||||
|
||||
// Draw cube with texture piece applied to all faces
|
||||
void DrawCubeTextureRec(Texture2D texture, Rectangle source, Vector3 position, float width, float height, float length, Color color)
|
||||
{
|
||||
float x = position.x;
|
||||
float y = position.y;
|
||||
float z = position.z;
|
||||
float texWidth = (float)texture.width;
|
||||
float texHeight = (float)texture.height;
|
||||
|
||||
// Set desired texture to be enabled while drawing following vertex data
|
||||
rlSetTexture(texture.id);
|
||||
|
||||
// We calculate the normalized texture coordinates for the desired texture-source-rectangle
|
||||
// It means converting from (tex.width, tex.height) coordinates to [0.0f, 1.0f] equivalent
|
||||
rlBegin(RL_QUADS);
|
||||
rlColor4ub(color.r, color.g, color.b, color.a);
|
||||
|
||||
// Front face
|
||||
rlNormal3f(0.0f, 0.0f, 1.0f);
|
||||
rlTexCoord2f(source.x/texWidth, (source.y + source.height)/texHeight);
|
||||
rlVertex3f(x - width/2, y - height/2, z + length/2);
|
||||
rlTexCoord2f((source.x + source.width)/texWidth, (source.y + source.height)/texHeight);
|
||||
rlVertex3f(x + width/2, y - height/2, z + length/2);
|
||||
rlTexCoord2f((source.x + source.width)/texWidth, source.y/texHeight);
|
||||
rlVertex3f(x + width/2, y + height/2, z + length/2);
|
||||
rlTexCoord2f(source.x/texWidth, source.y/texHeight);
|
||||
rlVertex3f(x - width/2, y + height/2, z + length/2);
|
||||
|
||||
// Back face
|
||||
rlNormal3f(0.0f, 0.0f, - 1.0f);
|
||||
rlTexCoord2f((source.x + source.width)/texWidth, (source.y + source.height)/texHeight);
|
||||
rlVertex3f(x - width/2, y - height/2, z - length/2);
|
||||
rlTexCoord2f((source.x + source.width)/texWidth, source.y/texHeight);
|
||||
rlVertex3f(x - width/2, y + height/2, z - length/2);
|
||||
rlTexCoord2f(source.x/texWidth, source.y/texHeight);
|
||||
rlVertex3f(x + width/2, y + height/2, z - length/2);
|
||||
rlTexCoord2f(source.x/texWidth, (source.y + source.height)/texHeight);
|
||||
rlVertex3f(x + width/2, y - height/2, z - length/2);
|
||||
|
||||
// Top face
|
||||
rlNormal3f(0.0f, 1.0f, 0.0f);
|
||||
rlTexCoord2f(source.x/texWidth, source.y/texHeight);
|
||||
rlVertex3f(x - width/2, y + height/2, z - length/2);
|
||||
rlTexCoord2f(source.x/texWidth, (source.y + source.height)/texHeight);
|
||||
rlVertex3f(x - width/2, y + height/2, z + length/2);
|
||||
rlTexCoord2f((source.x + source.width)/texWidth, (source.y + source.height)/texHeight);
|
||||
rlVertex3f(x + width/2, y + height/2, z + length/2);
|
||||
rlTexCoord2f((source.x + source.width)/texWidth, source.y/texHeight);
|
||||
rlVertex3f(x + width/2, y + height/2, z - length/2);
|
||||
|
||||
// Bottom face
|
||||
rlNormal3f(0.0f, - 1.0f, 0.0f);
|
||||
rlTexCoord2f((source.x + source.width)/texWidth, source.y/texHeight);
|
||||
rlVertex3f(x - width/2, y - height/2, z - length/2);
|
||||
rlTexCoord2f(source.x/texWidth, source.y/texHeight);
|
||||
rlVertex3f(x + width/2, y - height/2, z - length/2);
|
||||
rlTexCoord2f(source.x/texWidth, (source.y + source.height)/texHeight);
|
||||
rlVertex3f(x + width/2, y - height/2, z + length/2);
|
||||
rlTexCoord2f((source.x + source.width)/texWidth, (source.y + source.height)/texHeight);
|
||||
rlVertex3f(x - width/2, y - height/2, z + length/2);
|
||||
|
||||
// Right face
|
||||
rlNormal3f(1.0f, 0.0f, 0.0f);
|
||||
rlTexCoord2f((source.x + source.width)/texWidth, (source.y + source.height)/texHeight);
|
||||
rlVertex3f(x + width/2, y - height/2, z - length/2);
|
||||
rlTexCoord2f((source.x + source.width)/texWidth, source.y/texHeight);
|
||||
rlVertex3f(x + width/2, y + height/2, z - length/2);
|
||||
rlTexCoord2f(source.x/texWidth, source.y/texHeight);
|
||||
rlVertex3f(x + width/2, y + height/2, z + length/2);
|
||||
rlTexCoord2f(source.x/texWidth, (source.y + source.height)/texHeight);
|
||||
rlVertex3f(x + width/2, y - height/2, z + length/2);
|
||||
|
||||
// Left face
|
||||
rlNormal3f( - 1.0f, 0.0f, 0.0f);
|
||||
rlTexCoord2f(source.x/texWidth, (source.y + source.height)/texHeight);
|
||||
rlVertex3f(x - width/2, y - height/2, z - length/2);
|
||||
rlTexCoord2f((source.x + source.width)/texWidth, (source.y + source.height)/texHeight);
|
||||
rlVertex3f(x - width/2, y - height/2, z + length/2);
|
||||
rlTexCoord2f((source.x + source.width)/texWidth, source.y/texHeight);
|
||||
rlVertex3f(x - width/2, y + height/2, z + length/2);
|
||||
rlTexCoord2f(source.x/texWidth, source.y/texHeight);
|
||||
rlVertex3f(x - width/2, y + height/2, z - length/2);
|
||||
|
||||
rlEnd();
|
||||
|
||||
rlSetTexture(0);
|
||||
}
|
||||
BIN
raylib/examples/models/models_draw_cube_texture.png
Normal file
|
After Width: | Height: | Size: 62 KiB |
132
raylib/examples/models/models_first_person_maze.c
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - first person maze
|
||||
*
|
||||
* Example originally created with raylib 2.5, last time updated with raylib 3.5
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2019-2024 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#include <stdlib.h> // Required for: free()
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [models] example - first person maze");
|
||||
|
||||
// Define the camera to look into our 3d world
|
||||
Camera camera = { 0 };
|
||||
camera.position = (Vector3){ 0.2f, 0.4f, 0.2f }; // Camera position
|
||||
camera.target = (Vector3){ 0.185f, 0.4f, 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.projection = CAMERA_PERSPECTIVE; // Camera projection type
|
||||
|
||||
Image imMap = LoadImage("resources/cubicmap.png"); // Load cubicmap image (RAM)
|
||||
Texture2D cubicmap = LoadTextureFromImage(imMap); // Convert image to texture to display (VRAM)
|
||||
Mesh mesh = GenMeshCubicmap(imMap, (Vector3){ 1.0f, 1.0f, 1.0f });
|
||||
Model model = LoadModelFromMesh(mesh);
|
||||
|
||||
// NOTE: By default each cube is mapped to one part of texture atlas
|
||||
Texture2D texture = LoadTexture("resources/cubicmap_atlas.png"); // Load map texture
|
||||
model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture
|
||||
|
||||
// Get map image data to be used for collision detection
|
||||
Color *mapPixels = LoadImageColors(imMap);
|
||||
UnloadImage(imMap); // Unload image from RAM
|
||||
|
||||
Vector3 mapPosition = { -16.0f, 0.0f, -8.0f }; // Set model position
|
||||
|
||||
DisableCursor(); // Limit cursor to relative movement inside the window
|
||||
|
||||
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
|
||||
//----------------------------------------------------------------------------------
|
||||
Vector3 oldCamPos = camera.position; // Store old camera position
|
||||
|
||||
UpdateCamera(&camera, CAMERA_FIRST_PERSON);
|
||||
|
||||
// Check player collision (we simplify to 2D collision detection)
|
||||
Vector2 playerPos = { camera.position.x, camera.position.z };
|
||||
float playerRadius = 0.1f; // Collision radius (player is modelled as a cilinder for collision)
|
||||
|
||||
int playerCellX = (int)(playerPos.x - mapPosition.x + 0.5f);
|
||||
int playerCellY = (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
|
||||
(CheckCollisionCircleRec(playerPos, 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);
|
||||
|
||||
BeginMode3D(camera);
|
||||
DrawModel(model, mapPosition, 1.0f, WHITE); // Draw maze map
|
||||
EndMode3D();
|
||||
|
||||
DrawTextureEx(cubicmap, (Vector2){ GetScreenWidth() - cubicmap.width*4.0f - 20, 20.0f }, 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
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadImageColors(mapPixels); // Unload color array
|
||||
|
||||
UnloadTexture(cubicmap); // Unload cubicmap texture
|
||||
UnloadTexture(texture); // Unload map texture
|
||||
UnloadModel(model); // Unload map model
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
raylib/examples/models/models_first_person_maze.png
Normal file
|
After Width: | Height: | Size: 320 KiB |
88
raylib/examples/models/models_geometric_shapes.c
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Draw some basic geometric shapes (cube, sphere, cylinder...)
|
||||
*
|
||||
* Example originally created with raylib 1.0, last time updated with raylib 3.5
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2014-2024 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [models] example - geometric shapes");
|
||||
|
||||
// Define the camera to look into our 3d world
|
||||
Camera camera = { 0 };
|
||||
camera.position = (Vector3){ 0.0f, 10.0f, 10.0f };
|
||||
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
|
||||
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
|
||||
camera.fovy = 45.0f;
|
||||
camera.projection = CAMERA_PERSPECTIVE;
|
||||
|
||||
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
|
||||
//----------------------------------------------------------------------------------
|
||||
// TODO: Update your variables here
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
BeginMode3D(camera);
|
||||
|
||||
DrawCube((Vector3){-4.0f, 0.0f, 2.0f}, 2.0f, 5.0f, 2.0f, RED);
|
||||
DrawCubeWires((Vector3){-4.0f, 0.0f, 2.0f}, 2.0f, 5.0f, 2.0f, GOLD);
|
||||
DrawCubeWires((Vector3){-4.0f, 0.0f, -2.0f}, 3.0f, 6.0f, 2.0f, MAROON);
|
||||
|
||||
DrawSphere((Vector3){-1.0f, 0.0f, -2.0f}, 1.0f, GREEN);
|
||||
DrawSphereWires((Vector3){1.0f, 0.0f, 2.0f}, 2.0f, 16, 16, LIME);
|
||||
|
||||
DrawCylinder((Vector3){4.0f, 0.0f, -2.0f}, 1.0f, 2.0f, 3.0f, 4, SKYBLUE);
|
||||
DrawCylinderWires((Vector3){4.0f, 0.0f, -2.0f}, 1.0f, 2.0f, 3.0f, 4, DARKBLUE);
|
||||
DrawCylinderWires((Vector3){4.5f, -1.0f, 2.0f}, 1.0f, 1.0f, 2.0f, 6, BROWN);
|
||||
|
||||
DrawCylinder((Vector3){1.0f, 0.0f, -4.0f}, 0.0f, 1.5f, 3.0f, 8, GOLD);
|
||||
DrawCylinderWires((Vector3){1.0f, 0.0f, -4.0f}, 0.0f, 1.5f, 3.0f, 8, PINK);
|
||||
|
||||
DrawCapsule ((Vector3){-3.0f, 1.5f, -4.0f}, (Vector3){-4.0f, -1.0f, -4.0f}, 1.2f, 8, 8, VIOLET);
|
||||
DrawCapsuleWires((Vector3){-3.0f, 1.5f, -4.0f}, (Vector3){-4.0f, -1.0f, -4.0f}, 1.2f, 8, 8, PURPLE);
|
||||
|
||||
DrawGrid(10, 1.0f); // Draw a grid
|
||||
|
||||
EndMode3D();
|
||||
|
||||
DrawFPS(10, 10);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
raylib/examples/models/models_geometric_shapes.png
Normal file
|
After Width: | Height: | Size: 22 KiB |
119
raylib/examples/models/models_gpu_skinning.c
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [core] example - Doing skinning on the gpu using a vertex shader
|
||||
*
|
||||
* Example originally created with raylib 4.5, last time updated with raylib 4.5
|
||||
*
|
||||
* Example contributed by Daniel Holden (@orangeduck) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2024 Daniel Holden (@orangeduck)
|
||||
*
|
||||
* Note: Due to limitations in the Apple OpenGL driver, this feature does not work on MacOS
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#include "raymath.h"
|
||||
|
||||
#if defined(PLATFORM_DESKTOP)
|
||||
#define GLSL_VERSION 330
|
||||
#else // PLATFORM_ANDROID, PLATFORM_WEB
|
||||
#define GLSL_VERSION 100
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [models] example - GPU skinning");
|
||||
|
||||
// Define the camera to look into our 3d world
|
||||
Camera camera = { 0 };
|
||||
camera.position = (Vector3){ 5.0f, 5.0f, 5.0f }; // Camera position
|
||||
camera.target = (Vector3){ 0.0f, 2.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.projection = CAMERA_PERSPECTIVE; // Camera projection type
|
||||
|
||||
// Load gltf model
|
||||
Model characterModel = LoadModel("resources/models/gltf/greenman.glb"); // Load character model
|
||||
|
||||
// Load skinning shader
|
||||
Shader skinningShader = LoadShader(TextFormat("resources/shaders/glsl%i/skinning.vs", GLSL_VERSION),
|
||||
TextFormat("resources/shaders/glsl%i/skinning.fs", GLSL_VERSION));
|
||||
|
||||
characterModel.materials[1].shader = skinningShader;
|
||||
|
||||
// Load gltf model animations
|
||||
int animsCount = 0;
|
||||
unsigned int animIndex = 0;
|
||||
unsigned int animCurrentFrame = 0;
|
||||
ModelAnimation *modelAnimations = LoadModelAnimations("resources/models/gltf/greenman.glb", &animsCount);
|
||||
|
||||
Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
|
||||
|
||||
DisableCursor(); // Limit cursor to relative movement inside the window
|
||||
|
||||
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, CAMERA_THIRD_PERSON);
|
||||
|
||||
// Select current animation
|
||||
if (IsKeyPressed(KEY_T)) animIndex = (animIndex + 1)%animsCount;
|
||||
else if (IsKeyPressed(KEY_G)) animIndex = (animIndex + animsCount - 1)%animsCount;
|
||||
|
||||
// Update model animation
|
||||
ModelAnimation anim = modelAnimations[animIndex];
|
||||
animCurrentFrame = (animCurrentFrame + 1)%anim.frameCount;
|
||||
characterModel.transform = MatrixTranslate(position.x, position.y, position.z);
|
||||
UpdateModelAnimationBones(characterModel, anim, animCurrentFrame);
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
BeginMode3D(camera);
|
||||
|
||||
// Draw character mesh, pose calculation is done in shader (GPU skinning)
|
||||
DrawMesh(characterModel.meshes[0], characterModel.materials[1], characterModel.transform);
|
||||
|
||||
DrawGrid(10, 1.0f);
|
||||
|
||||
EndMode3D();
|
||||
|
||||
DrawText("Use the T/G to switch animation", 10, 10, 20, GRAY);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadModelAnimations(modelAnimations, animsCount); // Unload model animation
|
||||
UnloadModel(characterModel); // Unload model and meshes/material
|
||||
UnloadShader(skinningShader); // Unload GPU skinning shader
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
90
raylib/examples/models/models_heightmap.c
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Heightmap loading and drawing
|
||||
*
|
||||
* Example originally created with raylib 1.8, last time updated with raylib 3.5
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2015-2024 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [models] example - heightmap loading and drawing");
|
||||
|
||||
// Define our custom camera to look into our 3d world
|
||||
Camera camera = { 0 };
|
||||
camera.position = (Vector3){ 18.0f, 21.0f, 18.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.projection = CAMERA_PERSPECTIVE; // Camera projection type
|
||||
|
||||
Image image = LoadImage("resources/heightmap.png"); // Load heightmap image (RAM)
|
||||
Texture2D texture = LoadTextureFromImage(image); // Convert image to texture (VRAM)
|
||||
|
||||
Mesh mesh = GenMeshHeightmap(image, (Vector3){ 16, 8, 16 }); // Generate heightmap mesh (RAM and VRAM)
|
||||
Model model = LoadModelFromMesh(mesh); // Load model from generated mesh
|
||||
|
||||
model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture
|
||||
Vector3 mapPosition = { -8.0f, 0.0f, -8.0f }; // Define model position
|
||||
|
||||
UnloadImage(image); // Unload heightmap image from RAM, already uploaded to VRAM
|
||||
|
||||
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, CAMERA_ORBITAL);
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
BeginMode3D(camera);
|
||||
|
||||
DrawModel(model, mapPosition, 1.0f, RED);
|
||||
|
||||
DrawGrid(20, 1.0f);
|
||||
|
||||
EndMode3D();
|
||||
|
||||
DrawTexture(texture, screenWidth - texture.width - 20, 20, WHITE);
|
||||
DrawRectangleLines(screenWidth - texture.width - 20, 20, texture.width, texture.height, GREEN);
|
||||
|
||||
DrawFPS(10, 10);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadTexture(texture); // Unload texture
|
||||
UnloadModel(model); // Unload model
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
raylib/examples/models/models_heightmap.png
Normal file
|
After Width: | Height: | Size: 95 KiB |
153
raylib/examples/models/models_loading.c
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Models loading
|
||||
*
|
||||
* NOTE: raylib supports multiple models file formats:
|
||||
*
|
||||
* - OBJ > Text file format. Must include vertex position-texcoords-normals information,
|
||||
* if files references some .mtl materials file, it will be loaded (or try to).
|
||||
* - GLTF > Text/binary file format. Includes lot of information and it could
|
||||
* also reference external files, raylib will try loading mesh and materials data.
|
||||
* - IQM > Binary file format. Includes mesh vertex data but also animation data,
|
||||
* raylib can load .iqm animations.
|
||||
* - VOX > Binary file format. MagikaVoxel mesh format:
|
||||
* https://github.com/ephtracy/voxel-model/blob/master/MagicaVoxel-file-format-vox.txt
|
||||
* - M3D > Binary file format. Model 3D format:
|
||||
* https://bztsrc.gitlab.io/model3d
|
||||
*
|
||||
* Example originally created with raylib 2.0, last time updated with raylib 4.2
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2014-2024 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [models] example - models loading");
|
||||
|
||||
// Define the camera to look into our 3d world
|
||||
Camera camera = { 0 };
|
||||
camera.position = (Vector3){ 50.0f, 50.0f, 50.0f }; // Camera position
|
||||
camera.target = (Vector3){ 0.0f, 10.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.projection = CAMERA_PERSPECTIVE; // Camera mode type
|
||||
|
||||
Model model = LoadModel("resources/models/obj/castle.obj"); // Load model
|
||||
Texture2D texture = LoadTexture("resources/models/obj/castle_diffuse.png"); // Load model texture
|
||||
model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture
|
||||
|
||||
Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
|
||||
|
||||
BoundingBox bounds = GetMeshBoundingBox(model.meshes[0]); // Set model bounds
|
||||
|
||||
// NOTE: bounds are calculated from the original size of the model,
|
||||
// if model is scaled on drawing, bounds must be also scaled
|
||||
|
||||
bool selected = false; // Selected object flag
|
||||
|
||||
DisableCursor(); // Limit cursor to relative movement inside the window
|
||||
|
||||
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, CAMERA_FIRST_PERSON);
|
||||
|
||||
// Load new models/textures on drag&drop
|
||||
if (IsFileDropped())
|
||||
{
|
||||
FilePathList droppedFiles = LoadDroppedFiles();
|
||||
|
||||
if (droppedFiles.count == 1) // Only support one file dropped
|
||||
{
|
||||
if (IsFileExtension(droppedFiles.paths[0], ".obj") ||
|
||||
IsFileExtension(droppedFiles.paths[0], ".gltf") ||
|
||||
IsFileExtension(droppedFiles.paths[0], ".glb") ||
|
||||
IsFileExtension(droppedFiles.paths[0], ".vox") ||
|
||||
IsFileExtension(droppedFiles.paths[0], ".iqm") ||
|
||||
IsFileExtension(droppedFiles.paths[0], ".m3d")) // Model file formats supported
|
||||
{
|
||||
UnloadModel(model); // Unload previous model
|
||||
model = LoadModel(droppedFiles.paths[0]); // Load new model
|
||||
model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set current map diffuse texture
|
||||
|
||||
bounds = GetMeshBoundingBox(model.meshes[0]);
|
||||
|
||||
// TODO: Move camera position from target enough distance to visualize model properly
|
||||
}
|
||||
else if (IsFileExtension(droppedFiles.paths[0], ".png")) // Texture file formats supported
|
||||
{
|
||||
// Unload current model texture and load new one
|
||||
UnloadTexture(texture);
|
||||
texture = LoadTexture(droppedFiles.paths[0]);
|
||||
model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
|
||||
}
|
||||
}
|
||||
|
||||
UnloadDroppedFiles(droppedFiles); // Unload filepaths from memory
|
||||
}
|
||||
|
||||
// Select model on mouse click
|
||||
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
||||
{
|
||||
// Check collision between ray and box
|
||||
if (GetRayCollisionBox(GetScreenToWorldRay(GetMousePosition(), camera), bounds).hit) selected = !selected;
|
||||
else selected = false;
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
BeginMode3D(camera);
|
||||
|
||||
DrawModel(model, position, 1.0f, WHITE); // Draw 3d model with texture
|
||||
|
||||
DrawGrid(20, 10.0f); // Draw a grid
|
||||
|
||||
if (selected) DrawBoundingBox(bounds, GREEN); // Draw selection box
|
||||
|
||||
EndMode3D();
|
||||
|
||||
DrawText("Drag & drop model to load mesh/texture.", 10, GetScreenHeight() - 20, 10, DARKGRAY);
|
||||
if (selected) DrawText("MODEL SELECTED", GetScreenWidth() - 110, 10, 10, GREEN);
|
||||
|
||||
DrawText("(c) Castle 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY);
|
||||
|
||||
DrawFPS(10, 10);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadTexture(texture); // Unload texture
|
||||
UnloadModel(model); // Unload model
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
raylib/examples/models/models_loading.png
Normal file
|
After Width: | Height: | Size: 217 KiB |
102
raylib/examples/models/models_loading_gltf.c
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - loading gltf with animations
|
||||
*
|
||||
* LIMITATIONS:
|
||||
* - Only supports 1 armature per file, and skips loading it if there are multiple armatures
|
||||
* - Only supports linear interpolation (default method in Blender when checked
|
||||
* "Always Sample Animations" when exporting a GLTF file)
|
||||
* - Only supports translation/rotation/scale animation channel.path,
|
||||
* weights not considered (i.e. morph targets)
|
||||
*
|
||||
* Example originally created with raylib 3.7, last time updated with raylib 4.2
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2020-2024 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [models] example - loading gltf animations");
|
||||
|
||||
// Define the camera to look into our 3d world
|
||||
Camera camera = { 0 };
|
||||
camera.position = (Vector3){ 6.0f, 6.0f, 6.0f }; // Camera position
|
||||
camera.target = (Vector3){ 0.0f, 2.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.projection = CAMERA_PERSPECTIVE; // Camera projection type
|
||||
|
||||
// Load gltf model
|
||||
Model model = LoadModel("resources/models/gltf/robot.glb");
|
||||
Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
|
||||
|
||||
// Load gltf model animations
|
||||
int animsCount = 0;
|
||||
unsigned int animIndex = 0;
|
||||
unsigned int animCurrentFrame = 0;
|
||||
ModelAnimation *modelAnimations = LoadModelAnimations("resources/models/gltf/robot.glb", &animsCount);
|
||||
|
||||
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, CAMERA_ORBITAL);
|
||||
|
||||
// Select current animation
|
||||
if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) animIndex = (animIndex + 1)%animsCount;
|
||||
else if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) animIndex = (animIndex + animsCount - 1)%animsCount;
|
||||
|
||||
// Update model animation
|
||||
ModelAnimation anim = modelAnimations[animIndex];
|
||||
animCurrentFrame = (animCurrentFrame + 1)%anim.frameCount;
|
||||
UpdateModelAnimation(model, anim, animCurrentFrame);
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
BeginMode3D(camera);
|
||||
DrawModel(model, position, 1.0f, WHITE); // Draw animated model
|
||||
DrawGrid(10, 1.0f);
|
||||
EndMode3D();
|
||||
|
||||
DrawText("Use the LEFT/RIGHT mouse buttons to switch animation", 10, 10, 20, GRAY);
|
||||
DrawText(TextFormat("Animation: %s", anim.name), 10, GetScreenHeight() - 20, 10, DARKGRAY);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadModel(model); // Unload model and meshes/material
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
BIN
raylib/examples/models/models_loading_gltf.png
Normal file
|
After Width: | Height: | Size: 26 KiB |
174
raylib/examples/models/models_loading_m3d.c
Normal file
|
|
@ -0,0 +1,174 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Load models M3D
|
||||
*
|
||||
* Example originally created with raylib 4.5, last time updated with raylib 4.5
|
||||
*
|
||||
* Example contributed by bzt (@bztsrc) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* NOTES:
|
||||
* - Model3D (M3D) fileformat specs: https://gitlab.com/bztsrc/model3d
|
||||
* - Bender M3D exported: https://gitlab.com/bztsrc/model3d/-/tree/master/blender
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2022-2024 bzt (@bztsrc)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [models] example - M3D model loading");
|
||||
|
||||
// Define the camera to look into our 3d world
|
||||
Camera camera = { 0 };
|
||||
camera.position = (Vector3){ 1.5f, 1.5f, 1.5f }; // Camera position
|
||||
camera.target = (Vector3){ 0.0f, 0.4f, 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.projection = CAMERA_PERSPECTIVE; // Camera projection type
|
||||
|
||||
Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
|
||||
|
||||
char modelFileName[128] = "resources/models/m3d/cesium_man.m3d";
|
||||
bool drawMesh = 1;
|
||||
bool drawSkeleton = 1;
|
||||
bool animPlaying = false; // Store anim state, what to draw
|
||||
|
||||
// Load model
|
||||
Model model = LoadModel(modelFileName); // Load the bind-pose model mesh and basic data
|
||||
|
||||
// Load animations
|
||||
int animsCount = 0;
|
||||
int animFrameCounter = 0, animId = 0;
|
||||
ModelAnimation *anims = LoadModelAnimations(modelFileName, &animsCount); // Load skeletal animation data
|
||||
|
||||
DisableCursor(); // Limit cursor to relative movement inside the window
|
||||
|
||||
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, CAMERA_FIRST_PERSON);
|
||||
|
||||
if (animsCount)
|
||||
{
|
||||
// Play animation when spacebar is held down (or step one frame with N)
|
||||
if (IsKeyDown(KEY_SPACE) || IsKeyPressed(KEY_N))
|
||||
{
|
||||
animFrameCounter++;
|
||||
|
||||
if (animFrameCounter >= anims[animId].frameCount) animFrameCounter = 0;
|
||||
|
||||
UpdateModelAnimation(model, anims[animId], animFrameCounter);
|
||||
animPlaying = true;
|
||||
}
|
||||
|
||||
// Select animation by pressing C
|
||||
if (IsKeyPressed(KEY_C))
|
||||
{
|
||||
animFrameCounter = 0;
|
||||
animId++;
|
||||
|
||||
if (animId >= (int)animsCount) animId = 0;
|
||||
UpdateModelAnimation(model, anims[animId], 0);
|
||||
animPlaying = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Toggle skeleton drawing
|
||||
if (IsKeyPressed(KEY_B)) drawSkeleton ^= 1;
|
||||
|
||||
// Toggle mesh drawing
|
||||
if (IsKeyPressed(KEY_M)) drawMesh ^= 1;
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
BeginMode3D(camera);
|
||||
|
||||
// Draw 3d model with texture
|
||||
if (drawMesh) DrawModel(model, position, 1.0f, WHITE);
|
||||
|
||||
// Draw the animated skeleton
|
||||
if (drawSkeleton)
|
||||
{
|
||||
// Loop to (boneCount - 1) because the last one is a special "no bone" bone,
|
||||
// needed to workaround buggy models
|
||||
// without a -1, we would always draw a cube at the origin
|
||||
for (int i = 0; i < model.boneCount - 1; i++)
|
||||
{
|
||||
// By default the model is loaded in bind-pose by LoadModel().
|
||||
// But if UpdateModelAnimation() has been called at least once
|
||||
// then the model is already in animation pose, so we need the animated skeleton
|
||||
if (!animPlaying || !animsCount)
|
||||
{
|
||||
// Display the bind-pose skeleton
|
||||
DrawCube(model.bindPose[i].translation, 0.04f, 0.04f, 0.04f, RED);
|
||||
|
||||
if (model.bones[i].parent >= 0)
|
||||
{
|
||||
DrawLine3D(model.bindPose[i].translation,
|
||||
model.bindPose[model.bones[i].parent].translation, RED);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Display the frame-pose skeleton
|
||||
DrawCube(anims[animId].framePoses[animFrameCounter][i].translation, 0.05f, 0.05f, 0.05f, RED);
|
||||
|
||||
if (anims[animId].bones[i].parent >= 0)
|
||||
{
|
||||
DrawLine3D(anims[animId].framePoses[animFrameCounter][i].translation,
|
||||
anims[animId].framePoses[animFrameCounter][anims[animId].bones[i].parent].translation, RED);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DrawGrid(10, 1.0f); // Draw a grid
|
||||
|
||||
EndMode3D();
|
||||
|
||||
DrawText("PRESS SPACE to PLAY MODEL ANIMATION", 10, GetScreenHeight() - 80, 10, MAROON);
|
||||
DrawText("PRESS N to STEP ONE ANIMATION FRAME", 10, GetScreenHeight() - 60, 10, DARKGRAY);
|
||||
DrawText("PRESS C to CYCLE THROUGH ANIMATIONS", 10, GetScreenHeight() - 40, 10, DARKGRAY);
|
||||
DrawText("PRESS M to toggle MESH, B to toggle SKELETON DRAWING", 10, GetScreenHeight() - 20, 10, DARKGRAY);
|
||||
DrawText("(c) CesiumMan model by KhronosGroup", GetScreenWidth() - 210, GetScreenHeight() - 20, 10, GRAY);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Unload model animations data
|
||||
UnloadModelAnimations(anims, animsCount);
|
||||
|
||||
UnloadModel(model); // Unload model
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
raylib/examples/models/models_loading_m3d.png
Normal file
|
After Width: | Height: | Size: 25 KiB |
205
raylib/examples/models/models_loading_vox.c
Normal file
|
|
@ -0,0 +1,205 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Load models vox (MagicaVoxel)
|
||||
*
|
||||
* Example originally created with raylib 4.0, last time updated with raylib 4.0
|
||||
*
|
||||
* Example contributed by Johann Nadalutti (@procfxgen) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2021-2024 Johann Nadalutti (@procfxgen) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#include "raymath.h" // Required for: MatrixTranslate()
|
||||
|
||||
#define MAX_VOX_FILES 4
|
||||
|
||||
#define RLIGHTS_IMPLEMENTATION
|
||||
#include "rlights.h"
|
||||
|
||||
#if defined(PLATFORM_DESKTOP)
|
||||
#define GLSL_VERSION 330
|
||||
#else // PLATFORM_ANDROID, PLATFORM_WEB
|
||||
#define GLSL_VERSION 100
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
const char* voxFileNames[] = {
|
||||
"resources/models/vox/chr_knight.vox",
|
||||
"resources/models/vox/chr_sword.vox",
|
||||
"resources/models/vox/monu9.vox",
|
||||
"resources/models/vox/fez.vox"
|
||||
};
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [models] example - magicavoxel loading");
|
||||
|
||||
// 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.projection = CAMERA_PERSPECTIVE; // Camera projection type
|
||||
|
||||
//--------------------------------------------------------------------------------------
|
||||
// Load MagicaVoxel files
|
||||
Model models[MAX_VOX_FILES] = { 0 };
|
||||
|
||||
for (int i = 0; i < MAX_VOX_FILES; i++)
|
||||
{
|
||||
// Load VOX file and measure time
|
||||
double t0 = GetTime() * 1000.0;
|
||||
models[i] = LoadModel(voxFileNames[i]);
|
||||
double t1 = GetTime() * 1000.0;
|
||||
|
||||
TraceLog(LOG_WARNING, TextFormat("[%s] File loaded in %.3f ms", voxFileNames[i], t1 - t0));
|
||||
|
||||
// Compute model translation matrix to center model on draw position (0, 0 , 0)
|
||||
BoundingBox bb = GetModelBoundingBox(models[i]);
|
||||
Vector3 center = { 0 };
|
||||
center.x = bb.min.x + (((bb.max.x - bb.min.x) / 2));
|
||||
center.z = bb.min.z + (((bb.max.z - bb.min.z) / 2));
|
||||
|
||||
Matrix matTranslate = MatrixTranslate(-center.x, 0, -center.z);
|
||||
models[i].transform = matTranslate;
|
||||
}
|
||||
|
||||
int currentModel = 0;
|
||||
|
||||
//--------------------------------------------------------------------------------------
|
||||
// Load voxel shader
|
||||
Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/voxel_lighting.vs", GLSL_VERSION),
|
||||
TextFormat("resources/shaders/glsl%i/voxel_lighting.fs", GLSL_VERSION));
|
||||
|
||||
// Get some required shader locations
|
||||
shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
|
||||
// NOTE: "matModel" location name is automatically assigned on shader loading,
|
||||
// no need to get the location again if using that uniform name
|
||||
//shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocation(shader, "matModel");
|
||||
|
||||
// Ambient light level (some basic lighting)
|
||||
int ambientLoc = GetShaderLocation(shader, "ambient");
|
||||
SetShaderValue(shader, ambientLoc, (float[4]) { 0.1f, 0.1f, 0.1f, 1.0f }, SHADER_UNIFORM_VEC4);
|
||||
|
||||
// Assign out lighting shader to model
|
||||
for (int i = 0; i < MAX_VOX_FILES; i++)
|
||||
{
|
||||
Model m = models[i];
|
||||
for (int j = 0; j < m.materialCount; j++)
|
||||
{
|
||||
m.materials[j].shader = shader;
|
||||
}
|
||||
}
|
||||
|
||||
// Create lights
|
||||
Light lights[MAX_LIGHTS] = { 0 };
|
||||
lights[0] = CreateLight(LIGHT_POINT, (Vector3) { -20, 20, -20 }, Vector3Zero(), GRAY, shader);
|
||||
lights[1] = CreateLight(LIGHT_POINT, (Vector3) { 20, -20, 20 }, Vector3Zero(), GRAY, shader);
|
||||
lights[2] = CreateLight(LIGHT_POINT, (Vector3) { -20, 20, 20 }, Vector3Zero(), GRAY, shader);
|
||||
lights[3] = CreateLight(LIGHT_POINT, (Vector3) { 20, -20, -20 }, Vector3Zero(), GRAY, shader);
|
||||
|
||||
|
||||
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
|
||||
|
||||
//--------------------------------------------------------------------------------------
|
||||
Vector3 modelpos = { 0 };
|
||||
Vector3 camerarot = { 0 };
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
if (IsMouseButtonDown(MOUSE_BUTTON_MIDDLE))
|
||||
{
|
||||
const Vector2 mouseDelta = GetMouseDelta();
|
||||
camerarot.x = mouseDelta.x * 0.05f;
|
||||
camerarot.y = mouseDelta.y * 0.05f;
|
||||
}
|
||||
else
|
||||
{
|
||||
camerarot.x = 0;
|
||||
camerarot.y = 0;
|
||||
}
|
||||
|
||||
UpdateCameraPro(&camera,
|
||||
(Vector3) {
|
||||
(IsKeyDown(KEY_W) || IsKeyDown(KEY_UP)) * 0.1f - // Move forward-backward
|
||||
(IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN)) * 0.1f,
|
||||
(IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT)) * 0.1f - // Move right-left
|
||||
(IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT)) * 0.1f,
|
||||
0.0f // Move up-down
|
||||
},
|
||||
camerarot,
|
||||
GetMouseWheelMove() * -2.0f); // Move to target (zoom)
|
||||
|
||||
// Cycle between models on mouse click
|
||||
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) currentModel = (currentModel + 1) % MAX_VOX_FILES;
|
||||
|
||||
// Update the shader with the camera view vector (points towards { 0.0f, 0.0f, 0.0f })
|
||||
float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
|
||||
SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3);
|
||||
|
||||
// Update light values (actually, only enable/disable them)
|
||||
for (int i = 0; i < MAX_LIGHTS; i++) UpdateLightValues(shader, lights[i]);
|
||||
|
||||
//----------------------------------------------------------------------------------
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
// Draw 3D model
|
||||
BeginMode3D(camera);
|
||||
|
||||
DrawModel(models[currentModel], modelpos, 1.0f, WHITE);
|
||||
DrawGrid(10, 1.0);
|
||||
|
||||
// Draw spheres to show where the lights are
|
||||
for (int i = 0; i < MAX_LIGHTS; i++)
|
||||
{
|
||||
if (lights[i].enabled) DrawSphereEx(lights[i].position, 0.2f, 8, 8, lights[i].color);
|
||||
else DrawSphereWires(lights[i].position, 0.2f, 8, 8, ColorAlpha(lights[i].color, 0.3f));
|
||||
}
|
||||
|
||||
EndMode3D();
|
||||
|
||||
// Display info
|
||||
DrawRectangle(10, 400, 340, 60, Fade(SKYBLUE, 0.5f));
|
||||
DrawRectangleLines(10, 400, 340, 60, Fade(DARKBLUE, 0.5f));
|
||||
DrawText("MOUSE LEFT BUTTON to CYCLE VOX MODELS", 40, 410, 10, BLUE);
|
||||
DrawText("MOUSE MIDDLE BUTTON to ZOOM OR ROTATE CAMERA", 40, 420, 10, BLUE);
|
||||
DrawText("UP-DOWN-LEFT-RIGHT KEYS to MOVE CAMERA", 40, 430, 10, BLUE);
|
||||
DrawText(TextFormat("File: %s", GetFileName(voxFileNames[currentModel])), 10, 10, 20, GRAY);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
// Unload models data (GPU VRAM)
|
||||
for (int i = 0; i < MAX_VOX_FILES; i++) UnloadModel(models[i]);
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
BIN
raylib/examples/models/models_loading_vox.png
Normal file
|
After Width: | Height: | Size: 23 KiB |
190
raylib/examples/models/models_mesh_generation.c
Normal file
|
|
@ -0,0 +1,190 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib example - procedural mesh generation
|
||||
*
|
||||
* Example originally created with raylib 1.8, last time updated with raylib 4.0
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2017-2024 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#define NUM_MODELS 9 // Parametric 3d shapes to generate
|
||||
|
||||
static Mesh GenMeshCustom(void); // Generate a simple triangle mesh from code
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [models] example - mesh generation");
|
||||
|
||||
// We generate a checked image for texturing
|
||||
Image checked = GenImageChecked(2, 2, 1, 1, RED, GREEN);
|
||||
Texture2D texture = LoadTextureFromImage(checked);
|
||||
UnloadImage(checked);
|
||||
|
||||
Model models[NUM_MODELS] = { 0 };
|
||||
|
||||
models[0] = LoadModelFromMesh(GenMeshPlane(2, 2, 4, 3));
|
||||
models[1] = LoadModelFromMesh(GenMeshCube(2.0f, 1.0f, 2.0f));
|
||||
models[2] = LoadModelFromMesh(GenMeshSphere(2, 32, 32));
|
||||
models[3] = LoadModelFromMesh(GenMeshHemiSphere(2, 16, 16));
|
||||
models[4] = LoadModelFromMesh(GenMeshCylinder(1, 2, 16));
|
||||
models[5] = LoadModelFromMesh(GenMeshTorus(0.25f, 4.0f, 16, 32));
|
||||
models[6] = LoadModelFromMesh(GenMeshKnot(1.0f, 2.0f, 16, 128));
|
||||
models[7] = LoadModelFromMesh(GenMeshPoly(5, 2.0f));
|
||||
models[8] = LoadModelFromMesh(GenMeshCustom());
|
||||
|
||||
// Generated meshes could be exported as .obj files
|
||||
//ExportMesh(models[0].meshes[0], "plane.obj");
|
||||
//ExportMesh(models[1].meshes[0], "cube.obj");
|
||||
//ExportMesh(models[2].meshes[0], "sphere.obj");
|
||||
//ExportMesh(models[3].meshes[0], "hemisphere.obj");
|
||||
//ExportMesh(models[4].meshes[0], "cylinder.obj");
|
||||
//ExportMesh(models[5].meshes[0], "torus.obj");
|
||||
//ExportMesh(models[6].meshes[0], "knot.obj");
|
||||
//ExportMesh(models[7].meshes[0], "poly.obj");
|
||||
//ExportMesh(models[8].meshes[0], "custom.obj");
|
||||
|
||||
// Set checked texture as default diffuse component for all models material
|
||||
for (int i = 0; i < NUM_MODELS; i++) models[i].materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
|
||||
|
||||
// Define the camera to look into our 3d world
|
||||
Camera camera = { { 5.0f, 5.0f, 5.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
|
||||
|
||||
// Model drawing position
|
||||
Vector3 position = { 0.0f, 0.0f, 0.0f };
|
||||
|
||||
int currentModel = 0;
|
||||
|
||||
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, CAMERA_ORBITAL);
|
||||
|
||||
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
||||
{
|
||||
currentModel = (currentModel + 1)%NUM_MODELS; // Cycle between the textures
|
||||
}
|
||||
|
||||
if (IsKeyPressed(KEY_RIGHT))
|
||||
{
|
||||
currentModel++;
|
||||
if (currentModel >= NUM_MODELS) currentModel = 0;
|
||||
}
|
||||
else if (IsKeyPressed(KEY_LEFT))
|
||||
{
|
||||
currentModel--;
|
||||
if (currentModel < 0) currentModel = NUM_MODELS - 1;
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
BeginMode3D(camera);
|
||||
|
||||
DrawModel(models[currentModel], position, 1.0f, WHITE);
|
||||
DrawGrid(10, 1.0);
|
||||
|
||||
EndMode3D();
|
||||
|
||||
DrawRectangle(30, 400, 310, 30, Fade(SKYBLUE, 0.5f));
|
||||
DrawRectangleLines(30, 400, 310, 30, Fade(DARKBLUE, 0.5f));
|
||||
DrawText("MOUSE LEFT BUTTON to CYCLE PROCEDURAL MODELS", 40, 410, 10, BLUE);
|
||||
|
||||
switch(currentModel)
|
||||
{
|
||||
case 0: DrawText("PLANE", 680, 10, 20, DARKBLUE); break;
|
||||
case 1: DrawText("CUBE", 680, 10, 20, DARKBLUE); break;
|
||||
case 2: DrawText("SPHERE", 680, 10, 20, DARKBLUE); break;
|
||||
case 3: DrawText("HEMISPHERE", 640, 10, 20, DARKBLUE); break;
|
||||
case 4: DrawText("CYLINDER", 680, 10, 20, DARKBLUE); break;
|
||||
case 5: DrawText("TORUS", 680, 10, 20, DARKBLUE); break;
|
||||
case 6: DrawText("KNOT", 680, 10, 20, DARKBLUE); break;
|
||||
case 7: DrawText("POLY", 680, 10, 20, DARKBLUE); break;
|
||||
case 8: DrawText("Custom (triangle)", 580, 10, 20, DARKBLUE); break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadTexture(texture); // Unload texture
|
||||
|
||||
// Unload models data (GPU VRAM)
|
||||
for (int i = 0; i < NUM_MODELS; i++) UnloadModel(models[i]);
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Generate a simple triangle mesh from code
|
||||
static Mesh GenMeshCustom(void)
|
||||
{
|
||||
Mesh mesh = { 0 };
|
||||
mesh.triangleCount = 1;
|
||||
mesh.vertexCount = mesh.triangleCount*3;
|
||||
mesh.vertices = (float *)MemAlloc(mesh.vertexCount*3*sizeof(float)); // 3 vertices, 3 coordinates each (x, y, z)
|
||||
mesh.texcoords = (float *)MemAlloc(mesh.vertexCount*2*sizeof(float)); // 3 vertices, 2 coordinates each (x, y)
|
||||
mesh.normals = (float *)MemAlloc(mesh.vertexCount*3*sizeof(float)); // 3 vertices, 3 coordinates each (x, y, z)
|
||||
|
||||
// Vertex at (0, 0, 0)
|
||||
mesh.vertices[0] = 0;
|
||||
mesh.vertices[1] = 0;
|
||||
mesh.vertices[2] = 0;
|
||||
mesh.normals[0] = 0;
|
||||
mesh.normals[1] = 1;
|
||||
mesh.normals[2] = 0;
|
||||
mesh.texcoords[0] = 0;
|
||||
mesh.texcoords[1] = 0;
|
||||
|
||||
// Vertex at (1, 0, 2)
|
||||
mesh.vertices[3] = 1;
|
||||
mesh.vertices[4] = 0;
|
||||
mesh.vertices[5] = 2;
|
||||
mesh.normals[3] = 0;
|
||||
mesh.normals[4] = 1;
|
||||
mesh.normals[5] = 0;
|
||||
mesh.texcoords[2] = 0.5f;
|
||||
mesh.texcoords[3] = 1.0f;
|
||||
|
||||
// Vertex at (2, 0, 0)
|
||||
mesh.vertices[6] = 2;
|
||||
mesh.vertices[7] = 0;
|
||||
mesh.vertices[8] = 0;
|
||||
mesh.normals[6] = 0;
|
||||
mesh.normals[7] = 1;
|
||||
mesh.normals[8] = 0;
|
||||
mesh.texcoords[4] = 1;
|
||||
mesh.texcoords[5] =0;
|
||||
|
||||
// Upload mesh data from CPU (RAM) to GPU (VRAM) memory
|
||||
UploadMesh(&mesh, false);
|
||||
|
||||
return mesh;
|
||||
}
|
||||
BIN
raylib/examples/models/models_mesh_generation.png
Normal file
|
After Width: | Height: | Size: 27 KiB |
246
raylib/examples/models/models_mesh_picking.c
Normal file
|
|
@ -0,0 +1,246 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Mesh picking in 3d mode, ground plane, triangle, mesh
|
||||
*
|
||||
* Example originally created with raylib 1.7, last time updated with raylib 4.0
|
||||
*
|
||||
* Example contributed by Joel Davis (@joeld42) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2017-2024 Joel Davis (@joeld42) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
#include "raymath.h"
|
||||
|
||||
#define FLT_MAX 340282346638528859811704183484516925440.0f // Maximum value of a float, from bit pattern 01111111011111111111111111111111
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [models] example - mesh picking");
|
||||
|
||||
// Define the camera to look into our 3d world
|
||||
Camera camera = { 0 };
|
||||
camera.position = (Vector3){ 20.0f, 20.0f, 20.0f }; // Camera position
|
||||
camera.target = (Vector3){ 0.0f, 8.0f, 0.0f }; // Camera looking at point
|
||||
camera.up = (Vector3){ 0.0f, 1.6f, 0.0f }; // Camera up vector (rotation towards target)
|
||||
camera.fovy = 45.0f; // Camera field-of-view Y
|
||||
camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
|
||||
|
||||
Ray ray = { 0 }; // Picking ray
|
||||
|
||||
Model tower = LoadModel("resources/models/obj/turret.obj"); // Load OBJ model
|
||||
Texture2D texture = LoadTexture("resources/models/obj/turret_diffuse.png"); // Load model texture
|
||||
tower.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set model diffuse texture
|
||||
|
||||
Vector3 towerPos = { 0.0f, 0.0f, 0.0f }; // Set model position
|
||||
BoundingBox towerBBox = GetMeshBoundingBox(tower.meshes[0]); // Get mesh bounding box
|
||||
|
||||
// Ground quad
|
||||
Vector3 g0 = (Vector3){ -50.0f, 0.0f, -50.0f };
|
||||
Vector3 g1 = (Vector3){ -50.0f, 0.0f, 50.0f };
|
||||
Vector3 g2 = (Vector3){ 50.0f, 0.0f, 50.0f };
|
||||
Vector3 g3 = (Vector3){ 50.0f, 0.0f, -50.0f };
|
||||
|
||||
// Test triangle
|
||||
Vector3 ta = (Vector3){ -25.0f, 0.5f, 0.0f };
|
||||
Vector3 tb = (Vector3){ -4.0f, 2.5f, 1.0f };
|
||||
Vector3 tc = (Vector3){ -8.0f, 6.5f, 0.0f };
|
||||
|
||||
Vector3 bary = { 0.0f, 0.0f, 0.0f };
|
||||
|
||||
// Test sphere
|
||||
Vector3 sp = (Vector3){ -30.0f, 5.0f, 5.0f };
|
||||
float sr = 4.0f;
|
||||
|
||||
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
|
||||
//----------------------------------------------------------------------------------
|
||||
if (IsCursorHidden()) UpdateCamera(&camera, CAMERA_FIRST_PERSON); // Update camera
|
||||
|
||||
// Toggle camera controls
|
||||
if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT))
|
||||
{
|
||||
if (IsCursorHidden()) EnableCursor();
|
||||
else DisableCursor();
|
||||
}
|
||||
|
||||
// Display information about closest hit
|
||||
RayCollision collision = { 0 };
|
||||
char *hitObjectName = "None";
|
||||
collision.distance = FLT_MAX;
|
||||
collision.hit = false;
|
||||
Color cursorColor = WHITE;
|
||||
|
||||
// Get ray and test against objects
|
||||
ray = GetScreenToWorldRay(GetMousePosition(), camera);
|
||||
|
||||
// Check ray collision against ground quad
|
||||
RayCollision groundHitInfo = GetRayCollisionQuad(ray, g0, g1, g2, g3);
|
||||
|
||||
if ((groundHitInfo.hit) && (groundHitInfo.distance < collision.distance))
|
||||
{
|
||||
collision = groundHitInfo;
|
||||
cursorColor = GREEN;
|
||||
hitObjectName = "Ground";
|
||||
}
|
||||
|
||||
// Check ray collision against test triangle
|
||||
RayCollision triHitInfo = GetRayCollisionTriangle(ray, ta, tb, tc);
|
||||
|
||||
if ((triHitInfo.hit) && (triHitInfo.distance < collision.distance))
|
||||
{
|
||||
collision = triHitInfo;
|
||||
cursorColor = PURPLE;
|
||||
hitObjectName = "Triangle";
|
||||
|
||||
bary = Vector3Barycenter(collision.point, ta, tb, tc);
|
||||
}
|
||||
|
||||
// Check ray collision against test sphere
|
||||
RayCollision sphereHitInfo = GetRayCollisionSphere(ray, sp, sr);
|
||||
|
||||
if ((sphereHitInfo.hit) && (sphereHitInfo.distance < collision.distance))
|
||||
{
|
||||
collision = sphereHitInfo;
|
||||
cursorColor = ORANGE;
|
||||
hitObjectName = "Sphere";
|
||||
}
|
||||
|
||||
// Check ray collision against bounding box first, before trying the full ray-mesh test
|
||||
RayCollision boxHitInfo = GetRayCollisionBox(ray, towerBBox);
|
||||
|
||||
if ((boxHitInfo.hit) && (boxHitInfo.distance < collision.distance))
|
||||
{
|
||||
collision = boxHitInfo;
|
||||
cursorColor = ORANGE;
|
||||
hitObjectName = "Box";
|
||||
|
||||
// Check ray collision against model meshes
|
||||
RayCollision meshHitInfo = { 0 };
|
||||
for (int m = 0; m < tower.meshCount; m++)
|
||||
{
|
||||
// NOTE: We consider the model.transform for the collision check but
|
||||
// it can be checked against any transform Matrix, used when checking against same
|
||||
// model drawn multiple times with multiple transforms
|
||||
meshHitInfo = GetRayCollisionMesh(ray, tower.meshes[m], tower.transform);
|
||||
if (meshHitInfo.hit)
|
||||
{
|
||||
// Save the closest hit mesh
|
||||
if ((!collision.hit) || (collision.distance > meshHitInfo.distance)) collision = meshHitInfo;
|
||||
|
||||
break; // Stop once one mesh collision is detected, the colliding mesh is m
|
||||
}
|
||||
}
|
||||
|
||||
if (meshHitInfo.hit)
|
||||
{
|
||||
collision = meshHitInfo;
|
||||
cursorColor = ORANGE;
|
||||
hitObjectName = "Mesh";
|
||||
}
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
BeginMode3D(camera);
|
||||
|
||||
// Draw the tower
|
||||
// WARNING: If scale is different than 1.0f,
|
||||
// not considered by GetRayCollisionModel()
|
||||
DrawModel(tower, towerPos, 1.0f, WHITE);
|
||||
|
||||
// Draw the test triangle
|
||||
DrawLine3D(ta, tb, PURPLE);
|
||||
DrawLine3D(tb, tc, PURPLE);
|
||||
DrawLine3D(tc, ta, PURPLE);
|
||||
|
||||
// Draw the test sphere
|
||||
DrawSphereWires(sp, sr, 8, 8, PURPLE);
|
||||
|
||||
// Draw the mesh bbox if we hit it
|
||||
if (boxHitInfo.hit) DrawBoundingBox(towerBBox, LIME);
|
||||
|
||||
// If we hit something, draw the cursor at the hit point
|
||||
if (collision.hit)
|
||||
{
|
||||
DrawCube(collision.point, 0.3f, 0.3f, 0.3f, cursorColor);
|
||||
DrawCubeWires(collision.point, 0.3f, 0.3f, 0.3f, RED);
|
||||
|
||||
Vector3 normalEnd;
|
||||
normalEnd.x = collision.point.x + collision.normal.x;
|
||||
normalEnd.y = collision.point.y + collision.normal.y;
|
||||
normalEnd.z = collision.point.z + collision.normal.z;
|
||||
|
||||
DrawLine3D(collision.point, normalEnd, RED);
|
||||
}
|
||||
|
||||
DrawRay(ray, MAROON);
|
||||
|
||||
DrawGrid(10, 10.0f);
|
||||
|
||||
EndMode3D();
|
||||
|
||||
// Draw some debug GUI text
|
||||
DrawText(TextFormat("Hit Object: %s", hitObjectName), 10, 50, 10, BLACK);
|
||||
|
||||
if (collision.hit)
|
||||
{
|
||||
int ypos = 70;
|
||||
|
||||
DrawText(TextFormat("Distance: %3.2f", collision.distance), 10, ypos, 10, BLACK);
|
||||
|
||||
DrawText(TextFormat("Hit Pos: %3.2f %3.2f %3.2f",
|
||||
collision.point.x,
|
||||
collision.point.y,
|
||||
collision.point.z), 10, ypos + 15, 10, BLACK);
|
||||
|
||||
DrawText(TextFormat("Hit Norm: %3.2f %3.2f %3.2f",
|
||||
collision.normal.x,
|
||||
collision.normal.y,
|
||||
collision.normal.z), 10, ypos + 30, 10, BLACK);
|
||||
|
||||
if (triHitInfo.hit && TextIsEqual(hitObjectName, "Triangle"))
|
||||
DrawText(TextFormat("Barycenter: %3.2f %3.2f %3.2f", bary.x, bary.y, bary.z), 10, ypos + 45, 10, BLACK);
|
||||
}
|
||||
|
||||
DrawText("Right click mouse to toggle camera controls", 10, 430, 10, GRAY);
|
||||
|
||||
DrawText("(c) Turret 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY);
|
||||
|
||||
DrawFPS(10, 10);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadModel(tower); // Unload model
|
||||
UnloadTexture(texture); // Unload texture
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
raylib/examples/models/models_mesh_picking.png
Normal file
|
After Width: | Height: | Size: 136 KiB |
102
raylib/examples/models/models_orthographic_projection.c
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Show the difference between perspective and orthographic projection
|
||||
*
|
||||
* Example originally created with raylib 2.0, last time updated with raylib 3.7
|
||||
*
|
||||
* Example contributed by Max Danielsson (@autious) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2018-2024 Max Danielsson (@autious) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#define FOVY_PERSPECTIVE 45.0f
|
||||
#define WIDTH_ORTHOGRAPHIC 10.0f
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [models] example - geometric shapes");
|
||||
|
||||
// Define the camera to look into our 3d world
|
||||
Camera camera = { { 0.0f, 10.0f, 10.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, FOVY_PERSPECTIVE, CAMERA_PERSPECTIVE };
|
||||
|
||||
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
|
||||
//----------------------------------------------------------------------------------
|
||||
if (IsKeyPressed(KEY_SPACE))
|
||||
{
|
||||
if (camera.projection == CAMERA_PERSPECTIVE)
|
||||
{
|
||||
camera.fovy = WIDTH_ORTHOGRAPHIC;
|
||||
camera.projection = CAMERA_ORTHOGRAPHIC;
|
||||
}
|
||||
else
|
||||
{
|
||||
camera.fovy = FOVY_PERSPECTIVE;
|
||||
camera.projection = CAMERA_PERSPECTIVE;
|
||||
}
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
BeginMode3D(camera);
|
||||
|
||||
DrawCube((Vector3){-4.0f, 0.0f, 2.0f}, 2.0f, 5.0f, 2.0f, RED);
|
||||
DrawCubeWires((Vector3){-4.0f, 0.0f, 2.0f}, 2.0f, 5.0f, 2.0f, GOLD);
|
||||
DrawCubeWires((Vector3){-4.0f, 0.0f, -2.0f}, 3.0f, 6.0f, 2.0f, MAROON);
|
||||
|
||||
DrawSphere((Vector3){-1.0f, 0.0f, -2.0f}, 1.0f, GREEN);
|
||||
DrawSphereWires((Vector3){1.0f, 0.0f, 2.0f}, 2.0f, 16, 16, LIME);
|
||||
|
||||
DrawCylinder((Vector3){4.0f, 0.0f, -2.0f}, 1.0f, 2.0f, 3.0f, 4, SKYBLUE);
|
||||
DrawCylinderWires((Vector3){4.0f, 0.0f, -2.0f}, 1.0f, 2.0f, 3.0f, 4, DARKBLUE);
|
||||
DrawCylinderWires((Vector3){4.5f, -1.0f, 2.0f}, 1.0f, 1.0f, 2.0f, 6, BROWN);
|
||||
|
||||
DrawCylinder((Vector3){1.0f, 0.0f, -4.0f}, 0.0f, 1.5f, 3.0f, 8, GOLD);
|
||||
DrawCylinderWires((Vector3){1.0f, 0.0f, -4.0f}, 0.0f, 1.5f, 3.0f, 8, PINK);
|
||||
|
||||
DrawGrid(10, 1.0f); // Draw a grid
|
||||
|
||||
EndMode3D();
|
||||
|
||||
DrawText("Press Spacebar to switch camera type", 10, GetScreenHeight() - 30, 20, DARKGRAY);
|
||||
|
||||
if (camera.projection == CAMERA_ORTHOGRAPHIC) DrawText("ORTHOGRAPHIC", 10, 40, 20, BLACK);
|
||||
else if (camera.projection == CAMERA_PERSPECTIVE) DrawText("PERSPECTIVE", 10, 40, 20, BLACK);
|
||||
|
||||
DrawFPS(10, 10);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
raylib/examples/models/models_orthographic_projection.png
Normal file
|
After Width: | Height: | Size: 32 KiB |
182
raylib/examples/models/models_point_rendering.c
Normal file
|
|
@ -0,0 +1,182 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib example - point rendering
|
||||
*
|
||||
* Example originally created with raylib 5.0, last time updated with raylib 5.0
|
||||
*
|
||||
* Example contributed by Reese Gallagher (@satchelfrost) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2024 Reese Gallagher (@satchelfrost)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#include <stdlib.h> // Required for: rand()
|
||||
#include <math.h> // Required for: cos(), sin()
|
||||
|
||||
#define MAX_POINTS 10000000 // 10 million
|
||||
#define MIN_POINTS 1000 // 1 thousand
|
||||
|
||||
// Generate mesh using points
|
||||
Mesh GenMeshPoints(int numPoints);
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [models] example - point rendering");
|
||||
|
||||
Camera camera = {
|
||||
.position = { 3.0f, 3.0f, 3.0f },
|
||||
.target = { 0.0f, 0.0f, 0.0f },
|
||||
.up = { 0.0f, 1.0f, 0.0f },
|
||||
.fovy = 45.0f,
|
||||
.projection = CAMERA_PERSPECTIVE
|
||||
};
|
||||
|
||||
Vector3 position = { 0.0f, 0.0f, 0.0f };
|
||||
bool useDrawModelPoints = true;
|
||||
bool numPointsChanged = false;
|
||||
int numPoints = 1000;
|
||||
|
||||
Mesh mesh = GenMeshPoints(numPoints);
|
||||
Model model = LoadModelFromMesh(mesh);
|
||||
|
||||
//SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while(!WindowShouldClose())
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
UpdateCamera(&camera, CAMERA_ORBITAL);
|
||||
|
||||
if (IsKeyPressed(KEY_SPACE)) useDrawModelPoints = !useDrawModelPoints;
|
||||
if (IsKeyPressed(KEY_UP))
|
||||
{
|
||||
numPoints = (numPoints*10 > MAX_POINTS)? MAX_POINTS : numPoints*10;
|
||||
numPointsChanged = true;
|
||||
}
|
||||
if (IsKeyPressed(KEY_DOWN))
|
||||
{
|
||||
numPoints = (numPoints/10 < MIN_POINTS)? MIN_POINTS : numPoints/10;
|
||||
numPointsChanged = true;
|
||||
}
|
||||
|
||||
// Upload a different point cloud size
|
||||
if (numPointsChanged)
|
||||
{
|
||||
UnloadModel(model);
|
||||
mesh = GenMeshPoints(numPoints);
|
||||
model = LoadModelFromMesh(mesh);
|
||||
numPointsChanged = false;
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
ClearBackground(BLACK);
|
||||
|
||||
BeginMode3D(camera);
|
||||
|
||||
// The new method only uploads the points once to the GPU
|
||||
if (useDrawModelPoints)
|
||||
{
|
||||
DrawModelPoints(model, position, 1.0f, WHITE);
|
||||
}
|
||||
else
|
||||
{
|
||||
// The old method must continually draw the "points" (lines)
|
||||
for (int i = 0; i < numPoints; i++)
|
||||
{
|
||||
Vector3 pos = {
|
||||
.x = mesh.vertices[i*3 + 0],
|
||||
.y = mesh.vertices[i*3 + 1],
|
||||
.z = mesh.vertices[i*3 + 2],
|
||||
};
|
||||
Color color = {
|
||||
.r = mesh.colors[i*4 + 0],
|
||||
.g = mesh.colors[i*4 + 1],
|
||||
.b = mesh.colors[i*4 + 2],
|
||||
.a = mesh.colors[i*4 + 3],
|
||||
};
|
||||
|
||||
DrawPoint3D(pos, color);
|
||||
}
|
||||
}
|
||||
|
||||
// Draw a unit sphere for reference
|
||||
DrawSphereWires(position, 1.0f, 10, 10, YELLOW);
|
||||
|
||||
EndMode3D();
|
||||
|
||||
// Draw UI text
|
||||
DrawText(TextFormat("Point Count: %d", numPoints), 20, screenHeight - 50, 40, WHITE);
|
||||
DrawText("Up - increase points", 20, 70, 20, WHITE);
|
||||
DrawText("Down - decrease points", 20, 100, 20, WHITE);
|
||||
DrawText("Space - drawing function", 20, 130, 20, WHITE);
|
||||
|
||||
if (useDrawModelPoints) DrawText("Using: DrawModelPoints()", 20, 160, 20, GREEN);
|
||||
else DrawText("Using: DrawPoint3D()", 20, 160, 20, RED);
|
||||
|
||||
DrawFPS(10, 10);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadModel(model);
|
||||
|
||||
CloseWindow();
|
||||
//--------------------------------------------------------------------------------------
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Generate a spherical point cloud
|
||||
Mesh GenMeshPoints(int numPoints)
|
||||
{
|
||||
Mesh mesh = {
|
||||
.triangleCount = 1,
|
||||
.vertexCount = numPoints,
|
||||
.vertices = (float *)MemAlloc(numPoints*3*sizeof(float)),
|
||||
.colors = (unsigned char*)MemAlloc(numPoints*4*sizeof(unsigned char)),
|
||||
};
|
||||
|
||||
// https://en.wikipedia.org/wiki/Spherical_coordinate_system
|
||||
for (int i = 0; i < numPoints; i++)
|
||||
{
|
||||
float theta = PI*rand()/RAND_MAX;
|
||||
float phi = 2.0f*PI*rand()/RAND_MAX;
|
||||
float r = 10.0f*rand()/RAND_MAX;
|
||||
|
||||
mesh.vertices[i*3 + 0] = r*sin(theta)*cos(phi);
|
||||
mesh.vertices[i*3 + 1] = r*sin(theta)*sin(phi);
|
||||
mesh.vertices[i*3 + 2] = r*cos(theta);
|
||||
|
||||
Color color = ColorFromHSV(r*360.0f, 1.0f, 1.0f);
|
||||
|
||||
mesh.colors[i*4 + 0] = color.r;
|
||||
mesh.colors[i*4 + 1] = color.g;
|
||||
mesh.colors[i*4 + 2] = color.b;
|
||||
mesh.colors[i*4 + 3] = color.a;
|
||||
}
|
||||
|
||||
// Upload mesh data from CPU (RAM) to GPU (VRAM) memory
|
||||
UploadMesh(&mesh, false);
|
||||
|
||||
return mesh;
|
||||
}
|
||||
BIN
raylib/examples/models/models_point_rendering.png
Normal file
|
After Width: | Height: | Size: 127 KiB |
172
raylib/examples/models/models_rlgl_solar_system.c
Normal file
|
|
@ -0,0 +1,172 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - rlgl module usage with push/pop matrix transformations
|
||||
*
|
||||
* NOTE: This example uses [rlgl] module functionality (pseudo-OpenGL 1.1 style coding)
|
||||
*
|
||||
* Example originally created with raylib 2.5, last time updated with raylib 4.0
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2018-2024 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
#include "rlgl.h"
|
||||
|
||||
#include <math.h> // Required for: cosf(), sinf()
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Module Functions Declaration
|
||||
//------------------------------------------------------------------------------------
|
||||
void DrawSphereBasic(Color color); // Draw sphere without any matrix transformation
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
const float sunRadius = 4.0f;
|
||||
const float earthRadius = 0.6f;
|
||||
const float earthOrbitRadius = 8.0f;
|
||||
const float moonRadius = 0.16f;
|
||||
const float moonOrbitRadius = 1.5f;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [models] example - rlgl module usage with push/pop matrix transformations");
|
||||
|
||||
// Define the camera to look into our 3d world
|
||||
Camera camera = { 0 };
|
||||
camera.position = (Vector3){ 16.0f, 16.0f, 16.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.projection = CAMERA_PERSPECTIVE; // Camera projection type
|
||||
|
||||
float rotationSpeed = 0.2f; // General system rotation speed
|
||||
|
||||
float earthRotation = 0.0f; // Rotation of earth around itself (days) in degrees
|
||||
float earthOrbitRotation = 0.0f; // Rotation of earth around the Sun (years) in degrees
|
||||
float moonRotation = 0.0f; // Rotation of moon around itself
|
||||
float moonOrbitRotation = 0.0f; // Rotation of moon around earth in degrees
|
||||
|
||||
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, CAMERA_ORBITAL);
|
||||
|
||||
earthRotation += (5.0f*rotationSpeed);
|
||||
earthOrbitRotation += (365/360.0f*(5.0f*rotationSpeed)*rotationSpeed);
|
||||
moonRotation += (2.0f*rotationSpeed);
|
||||
moonOrbitRotation += (8.0f*rotationSpeed);
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
BeginMode3D(camera);
|
||||
|
||||
rlPushMatrix();
|
||||
rlScalef(sunRadius, sunRadius, sunRadius); // Scale Sun
|
||||
DrawSphereBasic(GOLD); // Draw the Sun
|
||||
rlPopMatrix();
|
||||
|
||||
rlPushMatrix();
|
||||
rlRotatef(earthOrbitRotation, 0.0f, 1.0f, 0.0f); // Rotation for Earth orbit around Sun
|
||||
rlTranslatef(earthOrbitRadius, 0.0f, 0.0f); // Translation for Earth orbit
|
||||
|
||||
rlPushMatrix();
|
||||
rlRotatef(earthRotation, 0.25, 1.0, 0.0); // Rotation for Earth itself
|
||||
rlScalef(earthRadius, earthRadius, earthRadius);// Scale Earth
|
||||
|
||||
DrawSphereBasic(BLUE); // Draw the Earth
|
||||
rlPopMatrix();
|
||||
|
||||
rlRotatef(moonOrbitRotation, 0.0f, 1.0f, 0.0f); // Rotation for Moon orbit around Earth
|
||||
rlTranslatef(moonOrbitRadius, 0.0f, 0.0f); // Translation for Moon orbit
|
||||
rlRotatef(moonRotation, 0.0f, 1.0f, 0.0f); // Rotation for Moon itself
|
||||
rlScalef(moonRadius, moonRadius, moonRadius); // Scale Moon
|
||||
|
||||
DrawSphereBasic(LIGHTGRAY); // Draw the Moon
|
||||
rlPopMatrix();
|
||||
|
||||
// Some reference elements (not affected by previous matrix transformations)
|
||||
DrawCircle3D((Vector3){ 0.0f, 0.0f, 0.0f }, earthOrbitRadius, (Vector3){ 1, 0, 0 }, 90.0f, Fade(RED, 0.5f));
|
||||
DrawGrid(20, 1.0f);
|
||||
|
||||
EndMode3D();
|
||||
|
||||
DrawText("EARTH ORBITING AROUND THE SUN!", 400, 10, 20, MAROON);
|
||||
DrawFPS(10, 10);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------
|
||||
// Module Functions Definitions (local)
|
||||
//--------------------------------------------------------------------------------------------
|
||||
|
||||
// Draw sphere without any matrix transformation
|
||||
// NOTE: Sphere is drawn in world position ( 0, 0, 0 ) with radius 1.0f
|
||||
void DrawSphereBasic(Color color)
|
||||
{
|
||||
int rings = 16;
|
||||
int slices = 16;
|
||||
|
||||
// Make sure there is enough space in the internal render batch
|
||||
// buffer to store all required vertex, batch is reseted if required
|
||||
rlCheckRenderBatchLimit((rings + 2)*slices*6);
|
||||
|
||||
rlBegin(RL_TRIANGLES);
|
||||
rlColor4ub(color.r, color.g, color.b, color.a);
|
||||
|
||||
for (int i = 0; i < (rings + 2); i++)
|
||||
{
|
||||
for (int j = 0; j < slices; j++)
|
||||
{
|
||||
rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*i))*sinf(DEG2RAD*(j*360/slices)),
|
||||
sinf(DEG2RAD*(270+(180/(rings + 1))*i)),
|
||||
cosf(DEG2RAD*(270+(180/(rings + 1))*i))*cosf(DEG2RAD*(j*360/slices)));
|
||||
rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*sinf(DEG2RAD*((j+1)*360/slices)),
|
||||
sinf(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
|
||||
cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*cosf(DEG2RAD*((j+1)*360/slices)));
|
||||
rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*sinf(DEG2RAD*(j*360/slices)),
|
||||
sinf(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
|
||||
cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*cosf(DEG2RAD*(j*360/slices)));
|
||||
|
||||
rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*i))*sinf(DEG2RAD*(j*360/slices)),
|
||||
sinf(DEG2RAD*(270+(180/(rings + 1))*i)),
|
||||
cosf(DEG2RAD*(270+(180/(rings + 1))*i))*cosf(DEG2RAD*(j*360/slices)));
|
||||
rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i)))*sinf(DEG2RAD*((j+1)*360/slices)),
|
||||
sinf(DEG2RAD*(270+(180/(rings + 1))*(i))),
|
||||
cosf(DEG2RAD*(270+(180/(rings + 1))*(i)))*cosf(DEG2RAD*((j+1)*360/slices)));
|
||||
rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*sinf(DEG2RAD*((j+1)*360/slices)),
|
||||
sinf(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
|
||||
cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*cosf(DEG2RAD*((j+1)*360/slices)));
|
||||
}
|
||||
}
|
||||
rlEnd();
|
||||
}
|
||||
BIN
raylib/examples/models/models_rlgl_solar_system.png
Normal file
|
After Width: | Height: | Size: 30 KiB |
271
raylib/examples/models/models_skybox.c
Normal file
|
|
@ -0,0 +1,271 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Skybox loading and drawing
|
||||
*
|
||||
* Example originally created with raylib 1.8, last time updated with raylib 4.0
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2017-2024 Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#include "rlgl.h"
|
||||
#include "raymath.h" // Required for: MatrixPerspective(), MatrixLookAt()
|
||||
|
||||
#if defined(PLATFORM_DESKTOP)
|
||||
#define GLSL_VERSION 330
|
||||
#else // PLATFORM_ANDROID, PLATFORM_WEB
|
||||
#define GLSL_VERSION 100
|
||||
#endif
|
||||
|
||||
// Generate cubemap (6 faces) from equirectangular (panorama) texture
|
||||
static TextureCubemap GenTextureCubemap(Shader shader, Texture2D panorama, int size, int format);
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [models] example - skybox loading and drawing");
|
||||
|
||||
// Define the camera to look into our 3d world
|
||||
Camera camera = { 0 };
|
||||
camera.position = (Vector3){ 1.0f, 1.0f, 1.0f }; // Camera position
|
||||
camera.target = (Vector3){ 4.0f, 1.0f, 4.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.projection = CAMERA_PERSPECTIVE; // Camera projection type
|
||||
|
||||
// Load skybox model
|
||||
Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f);
|
||||
Model skybox = LoadModelFromMesh(cube);
|
||||
|
||||
// Set this to true to use an HDR Texture, Note that raylib must be built with HDR Support for this to work SUPPORT_FILEFORMAT_HDR
|
||||
bool useHDR = false;
|
||||
|
||||
// Load skybox shader and set required locations
|
||||
// NOTE: Some locations are automatically set at shader loading
|
||||
skybox.materials[0].shader = LoadShader(TextFormat("resources/shaders/glsl%i/skybox.vs", GLSL_VERSION),
|
||||
TextFormat("resources/shaders/glsl%i/skybox.fs", GLSL_VERSION));
|
||||
|
||||
SetShaderValue(skybox.materials[0].shader, GetShaderLocation(skybox.materials[0].shader, "environmentMap"), (int[1]){ MATERIAL_MAP_CUBEMAP }, SHADER_UNIFORM_INT);
|
||||
SetShaderValue(skybox.materials[0].shader, GetShaderLocation(skybox.materials[0].shader, "doGamma"), (int[1]) { useHDR ? 1 : 0 }, SHADER_UNIFORM_INT);
|
||||
SetShaderValue(skybox.materials[0].shader, GetShaderLocation(skybox.materials[0].shader, "vflipped"), (int[1]){ useHDR ? 1 : 0 }, SHADER_UNIFORM_INT);
|
||||
|
||||
// Load cubemap shader and setup required shader locations
|
||||
Shader shdrCubemap = LoadShader(TextFormat("resources/shaders/glsl%i/cubemap.vs", GLSL_VERSION),
|
||||
TextFormat("resources/shaders/glsl%i/cubemap.fs", GLSL_VERSION));
|
||||
|
||||
SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, SHADER_UNIFORM_INT);
|
||||
|
||||
char skyboxFileName[256] = { 0 };
|
||||
|
||||
if (useHDR)
|
||||
{
|
||||
TextCopy(skyboxFileName, "resources/dresden_square_2k.hdr");
|
||||
|
||||
// Load HDR panorama (sphere) texture
|
||||
Texture2D panorama = LoadTexture(skyboxFileName);
|
||||
|
||||
// Generate cubemap (texture with 6 quads-cube-mapping) from panorama HDR texture
|
||||
// NOTE 1: New texture is generated rendering to texture, shader calculates the sphere->cube coordinates mapping
|
||||
// NOTE 2: It seems on some Android devices WebGL, fbo does not properly support a FLOAT-based attachment,
|
||||
// despite texture can be successfully created.. so using PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 instead of PIXELFORMAT_UNCOMPRESSED_R32G32B32A32
|
||||
skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = GenTextureCubemap(shdrCubemap, panorama, 1024, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8);
|
||||
|
||||
UnloadTexture(panorama); // Texture not required anymore, cubemap already generated
|
||||
}
|
||||
else
|
||||
{
|
||||
Image img = LoadImage("resources/skybox.png");
|
||||
skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = LoadTextureCubemap(img, CUBEMAP_LAYOUT_AUTO_DETECT); // CUBEMAP_LAYOUT_PANORAMA
|
||||
UnloadImage(img);
|
||||
}
|
||||
|
||||
DisableCursor(); // Limit cursor to relative movement inside the window
|
||||
|
||||
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, CAMERA_FIRST_PERSON);
|
||||
|
||||
// Load new cubemap texture on drag&drop
|
||||
if (IsFileDropped())
|
||||
{
|
||||
FilePathList droppedFiles = LoadDroppedFiles();
|
||||
|
||||
if (droppedFiles.count == 1) // Only support one file dropped
|
||||
{
|
||||
if (IsFileExtension(droppedFiles.paths[0], ".png;.jpg;.hdr;.bmp;.tga"))
|
||||
{
|
||||
// Unload current cubemap texture to load new one
|
||||
UnloadTexture(skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture);
|
||||
|
||||
if (useHDR)
|
||||
{
|
||||
// Load HDR panorama (sphere) texture
|
||||
Texture2D panorama = LoadTexture(droppedFiles.paths[0]);
|
||||
|
||||
// Generate cubemap from panorama texture
|
||||
skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = GenTextureCubemap(shdrCubemap, panorama, 1024, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8);
|
||||
|
||||
UnloadTexture(panorama); // Texture not required anymore, cubemap already generated
|
||||
}
|
||||
else
|
||||
{
|
||||
Image img = LoadImage(droppedFiles.paths[0]);
|
||||
skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture = LoadTextureCubemap(img, CUBEMAP_LAYOUT_AUTO_DETECT);
|
||||
UnloadImage(img);
|
||||
}
|
||||
|
||||
TextCopy(skyboxFileName, droppedFiles.paths[0]);
|
||||
}
|
||||
}
|
||||
|
||||
UnloadDroppedFiles(droppedFiles); // Unload filepaths from memory
|
||||
}
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
BeginMode3D(camera);
|
||||
|
||||
// We are inside the cube, we need to disable backface culling!
|
||||
rlDisableBackfaceCulling();
|
||||
rlDisableDepthMask();
|
||||
DrawModel(skybox, (Vector3){0, 0, 0}, 1.0f, WHITE);
|
||||
rlEnableBackfaceCulling();
|
||||
rlEnableDepthMask();
|
||||
|
||||
DrawGrid(10, 1.0f);
|
||||
|
||||
EndMode3D();
|
||||
|
||||
if (useHDR) DrawText(TextFormat("Panorama image from hdrihaven.com: %s", GetFileName(skyboxFileName)), 10, GetScreenHeight() - 20, 10, BLACK);
|
||||
else DrawText(TextFormat(": %s", GetFileName(skyboxFileName)), 10, GetScreenHeight() - 20, 10, BLACK);
|
||||
|
||||
DrawFPS(10, 10);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadShader(skybox.materials[0].shader);
|
||||
UnloadTexture(skybox.materials[0].maps[MATERIAL_MAP_CUBEMAP].texture);
|
||||
|
||||
UnloadModel(skybox); // Unload skybox model
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Generate cubemap texture from HDR texture
|
||||
static TextureCubemap GenTextureCubemap(Shader shader, Texture2D panorama, int size, int format)
|
||||
{
|
||||
TextureCubemap cubemap = { 0 };
|
||||
|
||||
rlDisableBackfaceCulling(); // Disable backface culling to render inside the cube
|
||||
|
||||
// STEP 1: Setup framebuffer
|
||||
//------------------------------------------------------------------------------------------
|
||||
unsigned int rbo = rlLoadTextureDepth(size, size, true);
|
||||
cubemap.id = rlLoadTextureCubemap(0, size, format, 1);
|
||||
|
||||
unsigned int fbo = rlLoadFramebuffer();
|
||||
rlFramebufferAttach(fbo, rbo, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_RENDERBUFFER, 0);
|
||||
rlFramebufferAttach(fbo, cubemap.id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_CUBEMAP_POSITIVE_X, 0);
|
||||
|
||||
// Check if framebuffer is complete with attachments (valid)
|
||||
if (rlFramebufferComplete(fbo)) TraceLog(LOG_INFO, "FBO: [ID %i] Framebuffer object created successfully", fbo);
|
||||
//------------------------------------------------------------------------------------------
|
||||
|
||||
// STEP 2: Draw to framebuffer
|
||||
//------------------------------------------------------------------------------------------
|
||||
// NOTE: Shader is used to convert HDR equirectangular environment map to cubemap equivalent (6 faces)
|
||||
rlEnableShader(shader.id);
|
||||
|
||||
// Define projection matrix and send it to shader
|
||||
Matrix matFboProjection = MatrixPerspective(90.0*DEG2RAD, 1.0, rlGetCullDistanceNear(), rlGetCullDistanceFar());
|
||||
rlSetUniformMatrix(shader.locs[SHADER_LOC_MATRIX_PROJECTION], matFboProjection);
|
||||
|
||||
// Define view matrix for every side of the cubemap
|
||||
Matrix fboViews[6] = {
|
||||
MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 1.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }),
|
||||
MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ -1.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }),
|
||||
MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, 1.0f, 0.0f }, (Vector3){ 0.0f, 0.0f, 1.0f }),
|
||||
MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }, (Vector3){ 0.0f, 0.0f, -1.0f }),
|
||||
MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, 0.0f, 1.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }),
|
||||
MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, 0.0f, -1.0f }, (Vector3){ 0.0f, -1.0f, 0.0f })
|
||||
};
|
||||
|
||||
rlViewport(0, 0, size, size); // Set viewport to current fbo dimensions
|
||||
|
||||
// Activate and enable texture for drawing to cubemap faces
|
||||
rlActiveTextureSlot(0);
|
||||
rlEnableTexture(panorama.id);
|
||||
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
// Set the view matrix for the current cube face
|
||||
rlSetUniformMatrix(shader.locs[SHADER_LOC_MATRIX_VIEW], fboViews[i]);
|
||||
|
||||
// Select the current cubemap face attachment for the fbo
|
||||
// WARNING: This function by default enables->attach->disables fbo!!!
|
||||
rlFramebufferAttach(fbo, cubemap.id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_CUBEMAP_POSITIVE_X + i, 0);
|
||||
rlEnableFramebuffer(fbo);
|
||||
|
||||
// Load and draw a cube, it uses the current enabled texture
|
||||
rlClearScreenBuffers();
|
||||
rlLoadDrawCube();
|
||||
|
||||
// ALTERNATIVE: Try to use internal batch system to draw the cube instead of rlLoadDrawCube
|
||||
// for some reason this method does not work, maybe due to cube triangles definition? normals pointing out?
|
||||
// TODO: Investigate this issue...
|
||||
//rlSetTexture(panorama.id); // WARNING: It must be called after enabling current framebuffer if using internal batch system!
|
||||
//rlClearScreenBuffers();
|
||||
//DrawCubeV(Vector3Zero(), Vector3One(), WHITE);
|
||||
//rlDrawRenderBatchActive();
|
||||
}
|
||||
//------------------------------------------------------------------------------------------
|
||||
|
||||
// STEP 3: Unload framebuffer and reset state
|
||||
//------------------------------------------------------------------------------------------
|
||||
rlDisableShader(); // Unbind shader
|
||||
rlDisableTexture(); // Unbind texture
|
||||
rlDisableFramebuffer(); // Unbind framebuffer
|
||||
rlUnloadFramebuffer(fbo); // Unload framebuffer (and automatically attached depth texture/renderbuffer)
|
||||
|
||||
// Reset viewport dimensions to default
|
||||
rlViewport(0, 0, rlGetFramebufferWidth(), rlGetFramebufferHeight());
|
||||
rlEnableBackfaceCulling();
|
||||
//------------------------------------------------------------------------------------------
|
||||
|
||||
cubemap.width = size;
|
||||
cubemap.height = size;
|
||||
cubemap.mipmaps = 1;
|
||||
cubemap.format = format;
|
||||
|
||||
return cubemap;
|
||||
}
|
||||
BIN
raylib/examples/models/models_skybox.png
Normal file
|
After Width: | Height: | Size: 417 KiB |
119
raylib/examples/models/models_waving_cubes.c
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Waving cubes
|
||||
*
|
||||
* Example originally created with raylib 2.5, last time updated with raylib 3.7
|
||||
*
|
||||
* Example contributed by Codecat (@codecat) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2019-2024 Codecat (@codecat) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#include <math.h> // Required for: sinf()
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, "raylib [models] example - waving cubes");
|
||||
|
||||
// Initialize the camera
|
||||
Camera3D camera = { 0 };
|
||||
camera.position = (Vector3){ 30.0f, 20.0f, 30.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 = 70.0f; // Camera field-of-view Y
|
||||
camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
|
||||
|
||||
// Specify the amount of blocks in each direction
|
||||
const int numBlocks = 15;
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
double time = GetTime();
|
||||
|
||||
// Calculate time scale for cube position and size
|
||||
float scale = (2.0f + (float)sin(time))*0.7f;
|
||||
|
||||
// Move camera around the scene
|
||||
double cameraTime = time*0.3;
|
||||
camera.position.x = (float)cos(cameraTime)*40.0f;
|
||||
camera.position.z = (float)sin(cameraTime)*40.0f;
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
BeginMode3D(camera);
|
||||
|
||||
DrawGrid(10, 5.0f);
|
||||
|
||||
for (int x = 0; x < numBlocks; x++)
|
||||
{
|
||||
for (int y = 0; y < numBlocks; y++)
|
||||
{
|
||||
for (int z = 0; z < numBlocks; z++)
|
||||
{
|
||||
// Scale of the blocks depends on x/y/z positions
|
||||
float blockScale = (x + y + z)/30.0f;
|
||||
|
||||
// Scatter makes the waving effect by adding blockScale over time
|
||||
float scatter = sinf(blockScale*20.0f + (float)(time*4.0f));
|
||||
|
||||
// Calculate the cube position
|
||||
Vector3 cubePos = {
|
||||
(float)(x - numBlocks/2)*(scale*3.0f) + scatter,
|
||||
(float)(y - numBlocks/2)*(scale*2.0f) + scatter,
|
||||
(float)(z - numBlocks/2)*(scale*3.0f) + scatter
|
||||
};
|
||||
|
||||
// Pick a color with a hue depending on cube position for the rainbow color effect
|
||||
// NOTE: This function is quite costly to be done per cube and frame,
|
||||
// pre-catching the results into a separate array could improve performance
|
||||
Color cubeColor = ColorFromHSV((float)(((x + y + z)*18)%360), 0.75f, 0.9f);
|
||||
|
||||
// Calculate cube size
|
||||
float cubeSize = (2.4f - scale)*blockScale;
|
||||
|
||||
// And finally, draw the cube!
|
||||
DrawCube(cubePos, cubeSize, cubeSize, cubeSize, cubeColor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
EndMode3D();
|
||||
|
||||
DrawFPS(10, 10);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
raylib/examples/models/models_waving_cubes.png
Normal file
|
After Width: | Height: | Size: 37 KiB |
123
raylib/examples/models/models_yaw_pitch_roll.c
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib [models] example - Plane rotations (yaw, pitch, roll)
|
||||
*
|
||||
* Example originally created with raylib 1.8, last time updated with raylib 4.0
|
||||
*
|
||||
* Example contributed by Berni (@Berni8k) and reviewed by Ramon Santamaria (@raysan5)
|
||||
*
|
||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||
* BSD-like license that allows static linking with closed source software
|
||||
*
|
||||
* Copyright (c) 2017-2024 Berni (@Berni8k) and Ramon Santamaria (@raysan5)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#include "raymath.h" // Required for: MatrixRotateXYZ()
|
||||
|
||||
//------------------------------------------------------------------------------------
|
||||
// Program main entry point
|
||||
//------------------------------------------------------------------------------------
|
||||
int main(void)
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
//SetConfigFlags(FLAG_MSAA_4X_HINT | FLAG_WINDOW_HIGHDPI);
|
||||
InitWindow(screenWidth, screenHeight, "raylib [models] example - plane rotations (yaw, pitch, roll)");
|
||||
|
||||
Camera camera = { 0 };
|
||||
camera.position = (Vector3){ 0.0f, 50.0f, -120.0f };// Camera position perspective
|
||||
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 = 30.0f; // Camera field-of-view Y
|
||||
camera.projection = CAMERA_PERSPECTIVE; // Camera type
|
||||
|
||||
Model model = LoadModel("resources/models/obj/plane.obj"); // Load model
|
||||
Texture2D texture = LoadTexture("resources/models/obj/plane_diffuse.png"); // Load model texture
|
||||
model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture
|
||||
|
||||
float pitch = 0.0f;
|
||||
float roll = 0.0f;
|
||||
float yaw = 0.0f;
|
||||
|
||||
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
|
||||
//----------------------------------------------------------------------------------
|
||||
// Plane pitch (x-axis) controls
|
||||
if (IsKeyDown(KEY_DOWN)) pitch += 0.6f;
|
||||
else if (IsKeyDown(KEY_UP)) pitch -= 0.6f;
|
||||
else
|
||||
{
|
||||
if (pitch > 0.3f) pitch -= 0.3f;
|
||||
else if (pitch < -0.3f) pitch += 0.3f;
|
||||
}
|
||||
|
||||
// Plane yaw (y-axis) controls
|
||||
if (IsKeyDown(KEY_S)) yaw -= 1.0f;
|
||||
else if (IsKeyDown(KEY_A)) yaw += 1.0f;
|
||||
else
|
||||
{
|
||||
if (yaw > 0.0f) yaw -= 0.5f;
|
||||
else if (yaw < 0.0f) yaw += 0.5f;
|
||||
}
|
||||
|
||||
// Plane roll (z-axis) controls
|
||||
if (IsKeyDown(KEY_LEFT)) roll -= 1.0f;
|
||||
else if (IsKeyDown(KEY_RIGHT)) roll += 1.0f;
|
||||
else
|
||||
{
|
||||
if (roll > 0.0f) roll -= 0.5f;
|
||||
else if (roll < 0.0f) roll += 0.5f;
|
||||
}
|
||||
|
||||
// Tranformation matrix for rotations
|
||||
model.transform = MatrixRotateXYZ((Vector3){ DEG2RAD*pitch, DEG2RAD*yaw, DEG2RAD*roll });
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
// Draw 3D model (recomended to draw 3D always before 2D)
|
||||
BeginMode3D(camera);
|
||||
|
||||
DrawModel(model, (Vector3){ 0.0f, -8.0f, 0.0f }, 1.0f, WHITE); // Draw 3d model with texture
|
||||
DrawGrid(10, 10.0f);
|
||||
|
||||
EndMode3D();
|
||||
|
||||
// Draw controls info
|
||||
DrawRectangle(30, 370, 260, 70, Fade(GREEN, 0.5f));
|
||||
DrawRectangleLines(30, 370, 260, 70, Fade(DARKGREEN, 0.5f));
|
||||
DrawText("Pitch controlled with: KEY_UP / KEY_DOWN", 40, 380, 10, DARKGRAY);
|
||||
DrawText("Roll controlled with: KEY_LEFT / KEY_RIGHT", 40, 400, 10, DARKGRAY);
|
||||
DrawText("Yaw controlled with: KEY_A / KEY_S", 40, 420, 10, DARKGRAY);
|
||||
|
||||
DrawText("(c) WWI Plane Model created by GiaHanLam", screenWidth - 240, screenHeight - 20, 10, DARKGRAY);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
UnloadModel(model); // Unload model data
|
||||
UnloadTexture(texture); // Unload texture data
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
raylib/examples/models/models_yaw_pitch_roll.png
Normal file
|
After Width: | Height: | Size: 143 KiB |
22
raylib/examples/models/resources/LICENSE.md
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
| resource | author | licence | notes |
|
||||
| :------------------- | :---------: | :------ | :---- |
|
||||
| models/obj/castle.obj,<br>models/obj/castle_diffuse.png | [Alberto Cano](https://www.artstation.com/albertocano) | [CC-BY-NC](https://creativecommons.org/licenses/by-nc/4.0/legalcode) | - |
|
||||
| models/obj/bridge.obj,<br>models/obj/bridge_diffuse.png | [Alberto Cano](https://www.artstation.com/albertocano) | [CC-BY-NC](https://creativecommons.org/licenses/by-nc/4.0/legalcode) | - |
|
||||
| models/obj/house.obj,<br>models/obj/house_diffuse.png | [Alberto Cano](https://www.artstation.com/albertocano) | [CC-BY-NC](https://creativecommons.org/licenses/by-nc/4.0/legalcode) | - |
|
||||
| models/obj/market.obj,<br>models/obj/market_diffuse.png | [Alberto Cano](https://www.artstation.com/albertocano) | [CC-BY-NC](https://creativecommons.org/licenses/by-nc/4.0/legalcode) | - |
|
||||
| models/obj/turret.obj,<br>models/obj/turret_diffuse.png | [Alberto Cano](https://www.artstation.com/albertocano) | [CC-BY-NC](https://creativecommons.org/licenses/by-nc/4.0/legalcode) | - |
|
||||
| models/obj/well.obj,<br>models/obj/well_diffuse.png | [Alberto Cano](https://www.artstation.com/albertocano) | [CC-BY-NC](https://creativecommons.org/licenses/by-nc/4.0/legalcode) | - |
|
||||
| models/obj/cube.obj,<br>models/obj/cube_diffuse.png | [@raysan5](https://github.com/raysan5) | [CC0](https://creativecommons.org/publicdomain/zero/1.0/) | - |
|
||||
| models/obj/plane.obj,<br>models/obj/plane_diffuse.png | [GiaHanLam](https://sketchfab.com/GiaHanLam) | [CC-BY](https://creativecommons.org/licenses/by/4.0/) | Used by: [`models_yaw_pitch_roll.c`](https://github.com/raysan5/raylib/blob/master/examples/models/models_yaw_pitch_roll.c)
|
||||
| models/iqm/guy.iqm,<br>models/iqm/guyanim.iqm,<br>models/iqm/guytex.png,<br>models/iqm/guy.blend | [@culacant](https://github.com/culacant) | [CC0](https://creativecommons.org/publicdomain/zero/1.0/) | - |
|
||||
| models/gltf/robot.glb,<br>models/gltf/robot.blend | [CC0](https://creativecommons.org/publicdomain/zero/1.0/) | - |
|
||||
| models/vox/chr_knight.vox | ❔ | ❔ | - |
|
||||
| models/vox/chr_sword.vox | ❔ | ❔ | - |
|
||||
| models/vox/monu9.vox | ❔ | ❔ | - |
|
||||
| billboard.png | [@emegeme](https://github.com/emegeme) | [CC0](https://creativecommons.org/publicdomain/zero/1.0/) | - |
|
||||
| cubicmap.png | [@raysan5](https://github.com/raysan5) | [CC0](https://creativecommons.org/publicdomain/zero/1.0/) | - |
|
||||
| cubicmap_atlas.png | [@emegeme](https://github.com/emegeme) | [CC0](https://creativecommons.org/publicdomain/zero/1.0/) | - |
|
||||
| heightmap.png | [@raysan5](https://github.com/raysan5) | [CC0](https://creativecommons.org/publicdomain/zero/1.0/) | - |
|
||||
| dresden_square_1k.hdr | [HDRIHaven](https://hdrihaven.com/hdri/?h=dresden_square) | [CC0](https://hdrihaven.com/p/license.php) | - |
|
||||
| dresden_square_2k.hdr | [HDRIHaven](https://hdrihaven.com/hdri/?h=dresden_square) | [CC0](https://hdrihaven.com/p/license.php) | - |
|
||||
| skybox.png | ❔ | ❔ | - |
|
||||
BIN
raylib/examples/models/resources/billboard.png
Normal file
|
After Width: | Height: | Size: 22 KiB |
BIN
raylib/examples/models/resources/cubicmap.png
Normal file
|
After Width: | Height: | Size: 164 B |
BIN
raylib/examples/models/resources/cubicmap_atlas.png
Normal file
|
After Width: | Height: | Size: 36 KiB |
BIN
raylib/examples/models/resources/dresden_square_1k.hdr
Normal file
BIN
raylib/examples/models/resources/dresden_square_2k.hdr
Normal file
BIN
raylib/examples/models/resources/heightmap.png
Normal file
|
After Width: | Height: | Size: 11 KiB |
5
raylib/examples/models/resources/models/gltf/LICENSE
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
robot.glb model by @Quaternius (https://www.patreon.com/quaternius)
|
||||
Licensed under CC0 1.0 Universal (CC0 1.0) - Public Domain Dedication (https://creativecommons.org/publicdomain/zero/1.0/)
|
||||
|
||||
greenman.glb, greenman_hat.glb, greenman_sword.glb, greenman_shield.glb models by @iP (https://github.com/ipzaur)
|
||||
Licensed under CC0 1.0 Universal (CC0 1.0) - Public Domain Dedication (https://creativecommons.org/publicdomain/zero/1.0/)
|
||||
BIN
raylib/examples/models/resources/models/gltf/greenman.glb
Normal file
BIN
raylib/examples/models/resources/models/gltf/greenman_hat.glb
Normal file
BIN
raylib/examples/models/resources/models/gltf/greenman_shield.glb
Normal file
BIN
raylib/examples/models/resources/models/gltf/greenman_sword.glb
Normal file
BIN
raylib/examples/models/resources/models/gltf/raylib_logo_3d.glb
Normal file
BIN
raylib/examples/models/resources/models/gltf/robot.blend
Normal file
BIN
raylib/examples/models/resources/models/gltf/robot.glb
Normal file
BIN
raylib/examples/models/resources/models/iqm/guy.blend
Normal file
BIN
raylib/examples/models/resources/models/iqm/guy.iqm
Normal file
BIN
raylib/examples/models/resources/models/iqm/guyanim.iqm
Normal file
BIN
raylib/examples/models/resources/models/iqm/guytex.png
Normal file
|
After Width: | Height: | Size: 295 KiB |
BIN
raylib/examples/models/resources/models/m3d/cesium_man.m3d
Normal file
BIN
raylib/examples/models/resources/models/m3d/seagull.m3d
Normal file
BIN
raylib/examples/models/resources/models/m3d/suzanne.m3d
Normal file
1725
raylib/examples/models/resources/models/obj/bridge.obj
Normal file
BIN
raylib/examples/models/resources/models/obj/bridge_diffuse.png
Normal file
|
After Width: | Height: | Size: 311 KiB |
12919
raylib/examples/models/resources/models/obj/castle.obj
Normal file
BIN
raylib/examples/models/resources/models/obj/castle_diffuse.png
Normal file
|
After Width: | Height: | Size: 434 KiB |
68
raylib/examples/models/resources/models/obj/cube.obj
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
# reference material
|
||||
#mtllib cube.mtl
|
||||
|
||||
# object box
|
||||
|
||||
# vertex (XZY)
|
||||
v 5.5 0 1.5
|
||||
v 8.5 0 1.5
|
||||
v 5.5 0 -1.5
|
||||
v 8.5 0 -1.5
|
||||
v 5.5 3 1.5
|
||||
v 8.5 3 1.5
|
||||
v 5.5 3 -1.5
|
||||
v 8.5 3 -1.5
|
||||
|
||||
# normals (XYZ)
|
||||
vn 0 -1 0
|
||||
vn 0 1 0
|
||||
vn 0 0 1
|
||||
vn 1 0 0
|
||||
vn 0 0 -1
|
||||
vn -1 0 0
|
||||
|
||||
# UVs (XY)
|
||||
vt 0.5 0 0
|
||||
vt 1 0 0
|
||||
vt 1 0.5 0
|
||||
vt 0.5 0.5 0
|
||||
vt 0.5 0.5 0
|
||||
vt 1 0.5 0
|
||||
vt 0.5 1 0
|
||||
vt 1 1 0
|
||||
vt 0 0.5 0
|
||||
vt 1 0.5 0
|
||||
vt 1 0 0
|
||||
vt 0 0 0
|
||||
vt 0 0.5 0
|
||||
vt 1 0.5 0
|
||||
vt 1 1 0
|
||||
vt 0 1 0
|
||||
vt 0.5 0 0
|
||||
vt 0 0 0
|
||||
vt 0 0.5 0
|
||||
vt 0.5 0.5 0
|
||||
vt 0 0.5 0
|
||||
vt 0.5 0.5 0
|
||||
vt 0.5 1 0
|
||||
vt 0 1 0
|
||||
|
||||
# merger
|
||||
g box
|
||||
|
||||
# reference material
|
||||
#usemtl mat01
|
||||
|
||||
# faces
|
||||
f 1/9/1 3/10/1 4/11/1
|
||||
f 4/11/1 2/12/1 1/9/1
|
||||
f 5/13/2 6/14/2 8/15/2
|
||||
f 8/15/2 7/16/2 5/13/2
|
||||
f 1/17/6 2/18/6 6/19/6
|
||||
f 6/19/6 5/20/6 1/17/6
|
||||
f 2/6/1 4/5/1 8/7/1
|
||||
f 8/7/1 6/8/1 2/6/1
|
||||
f 4/2/3 3/1/3 7/4/3
|
||||
f 7/4/3 8/3/3 4/2/3
|
||||
f 3/22/5 1/21/5 5/24/5
|
||||
f 5/24/5 7/23/5 3/22/5
|
||||
BIN
raylib/examples/models/resources/models/obj/cube_diffuse.png
Normal file
|
After Width: | Height: | Size: 23 KiB |
4564
raylib/examples/models/resources/models/obj/house.obj
Normal file
BIN
raylib/examples/models/resources/models/obj/house_diffuse.png
Normal file
|
After Width: | Height: | Size: 383 KiB |
7301
raylib/examples/models/resources/models/obj/market.obj
Normal file
BIN
raylib/examples/models/resources/models/obj/market_diffuse.png
Normal file
|
After Width: | Height: | Size: 380 KiB |
10858
raylib/examples/models/resources/models/obj/plane.obj
Normal file
BIN
raylib/examples/models/resources/models/obj/plane_diffuse.png
Normal file
|
After Width: | Height: | Size: 804 KiB |
1888
raylib/examples/models/resources/models/obj/turret.obj
Normal file
BIN
raylib/examples/models/resources/models/obj/turret_diffuse.png
Normal file
|
After Width: | Height: | Size: 371 KiB |
1030
raylib/examples/models/resources/models/obj/well.obj
Normal file
BIN
raylib/examples/models/resources/models/obj/well_diffuse.png
Normal file
|
After Width: | Height: | Size: 334 KiB |
9
raylib/examples/models/resources/models/vox/LICENSE
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
The following models are provided by the official github repo of voxel-model format by MagikaVoxel developer @ephtracy
|
||||
|
||||
GitHub official repo: https://github.com/ephtracy/voxel-model
|
||||
|
||||
- chr_knight.vox - https://github.com/ephtracy/voxel-model/blob/master/vox/character/chr_knight.vox
|
||||
- chr_sword.vox - https://github.com/ephtracy/voxel-model/blob/master/vox/character/chr_sword.vox
|
||||
- monu9.vox - https://github.com/ephtracy/voxel-model/blob/master/vox/monument/monu9.vox
|
||||
|
||||
Worth mentioning there is no license specified for the models yet: https://github.com/ephtracy/voxel-model/issues/22
|
||||
BIN
raylib/examples/models/resources/models/vox/chr_knight.vox
Normal file
BIN
raylib/examples/models/resources/models/vox/chr_sword.vox
Normal file
BIN
raylib/examples/models/resources/models/vox/fez.vox
Normal file
BIN
raylib/examples/models/resources/models/vox/monu9.vox
Normal file
29
raylib/examples/models/resources/shaders/glsl100/cubemap.fs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#version 100
|
||||
|
||||
precision mediump float;
|
||||
|
||||
// Input vertex attributes (from vertex shader)
|
||||
varying vec3 fragPosition;
|
||||
|
||||
// Input uniform values
|
||||
uniform sampler2D equirectangularMap;
|
||||
|
||||
vec2 SampleSphericalMap(vec3 v)
|
||||
{
|
||||
vec2 uv = vec2(atan(v.z, v.x), asin(v.y));
|
||||
uv *= vec2(0.1591, 0.3183);
|
||||
uv += 0.5;
|
||||
return uv;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
// Normalize local position
|
||||
vec2 uv = SampleSphericalMap(normalize(fragPosition));
|
||||
|
||||
// Fetch color from texture map
|
||||
vec3 color = texture2D(equirectangularMap, uv).rgb;
|
||||
|
||||
// Calculate final fragment color
|
||||
gl_FragColor = vec4(color, 1.0);
|
||||
}
|
||||
20
raylib/examples/models/resources/shaders/glsl100/cubemap.vs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#version 100
|
||||
|
||||
// Input vertex attributes
|
||||
attribute vec3 vertexPosition;
|
||||
|
||||
// Input uniform values
|
||||
uniform mat4 matProjection;
|
||||
uniform mat4 matView;
|
||||
|
||||
// Output vertex attributes (to fragment shader)
|
||||
varying vec3 fragPosition;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Calculate fragment position based on model transformations
|
||||
fragPosition = vertexPosition;
|
||||
|
||||
// Calculate final vertex position
|
||||
gl_Position = matProjection*matView*vec4(vertexPosition, 1.0);
|
||||
}
|
||||
20
raylib/examples/models/resources/shaders/glsl100/skinning.fs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#version 100
|
||||
|
||||
precision mediump float;
|
||||
|
||||
// Input vertex attributes (from vertex shader)
|
||||
varying vec2 fragTexCoord;
|
||||
varying vec4 fragColor;
|
||||
|
||||
// Input uniform values
|
||||
uniform sampler2D texture0;
|
||||
uniform vec4 colDiffuse;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Fetch color from texture sampler
|
||||
vec4 texelColor = texture2D(texture0, fragTexCoord);
|
||||
|
||||
// Calculate final fragment color
|
||||
gl_FragColor = texelColor*colDiffuse*fragColor;
|
||||
}
|
||||
59
raylib/examples/models/resources/shaders/glsl100/skinning.vs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
#version 100
|
||||
|
||||
#define MAX_BONE_NUM 64
|
||||
|
||||
// Input vertex attributes
|
||||
attribute vec3 vertexPosition;
|
||||
attribute vec2 vertexTexCoord;
|
||||
attribute vec4 vertexColor;
|
||||
attribute vec4 vertexBoneIds;
|
||||
attribute vec4 vertexBoneWeights;
|
||||
|
||||
// Input uniform values
|
||||
uniform mat4 mvp;
|
||||
uniform mat4 boneMatrices[MAX_BONE_NUM];
|
||||
|
||||
// Output vertex attributes (to fragment shader)
|
||||
varying vec2 fragTexCoord;
|
||||
varying vec4 fragColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
int boneIndex0 = int(vertexBoneIds.x);
|
||||
int boneIndex1 = int(vertexBoneIds.y);
|
||||
int boneIndex2 = int(vertexBoneIds.z);
|
||||
int boneIndex3 = int(vertexBoneIds.w);
|
||||
|
||||
// WARNING: OpenGL ES 2.0 does not support automatic matrix transposing, neither transpose() function
|
||||
mat4 boneMatrixTransposed0 = mat4(
|
||||
vec4(boneMatrices[boneIndex0][0].x, boneMatrices[boneIndex0][1].x, boneMatrices[boneIndex0][2].x, boneMatrices[boneIndex0][3].x),
|
||||
vec4(boneMatrices[boneIndex0][0].y, boneMatrices[boneIndex0][1].y, boneMatrices[boneIndex0][2].y, boneMatrices[boneIndex0][3].y),
|
||||
vec4(boneMatrices[boneIndex0][0].z, boneMatrices[boneIndex0][1].z, boneMatrices[boneIndex0][2].z, boneMatrices[boneIndex0][3].z),
|
||||
vec4(boneMatrices[boneIndex0][0].w, boneMatrices[boneIndex0][1].w, boneMatrices[boneIndex0][2].w, boneMatrices[boneIndex0][3].w));
|
||||
mat4 boneMatrixTransposed1 = mat4(
|
||||
vec4(boneMatrices[boneIndex1][0].x, boneMatrices[boneIndex1][1].x, boneMatrices[boneIndex1][2].x, boneMatrices[boneIndex1][3].x),
|
||||
vec4(boneMatrices[boneIndex1][0].y, boneMatrices[boneIndex1][1].y, boneMatrices[boneIndex1][2].y, boneMatrices[boneIndex1][3].y),
|
||||
vec4(boneMatrices[boneIndex1][0].z, boneMatrices[boneIndex1][1].z, boneMatrices[boneIndex1][2].z, boneMatrices[boneIndex1][3].z),
|
||||
vec4(boneMatrices[boneIndex1][0].w, boneMatrices[boneIndex1][1].w, boneMatrices[boneIndex1][2].w, boneMatrices[boneIndex1][3].w));
|
||||
mat4 boneMatrixTransposed2 = mat4(
|
||||
vec4(boneMatrices[boneIndex2][0].x, boneMatrices[boneIndex2][1].x, boneMatrices[boneIndex2][2].x, boneMatrices[boneIndex2][3].x),
|
||||
vec4(boneMatrices[boneIndex2][0].y, boneMatrices[boneIndex2][1].y, boneMatrices[boneIndex2][2].y, boneMatrices[boneIndex2][3].y),
|
||||
vec4(boneMatrices[boneIndex2][0].z, boneMatrices[boneIndex2][1].z, boneMatrices[boneIndex2][2].z, boneMatrices[boneIndex2][3].z),
|
||||
vec4(boneMatrices[boneIndex2][0].w, boneMatrices[boneIndex2][1].w, boneMatrices[boneIndex2][2].w, boneMatrices[boneIndex2][3].w));
|
||||
mat4 boneMatrixTransposed3 = mat4(
|
||||
vec4(boneMatrices[boneIndex3][0].x, boneMatrices[boneIndex3][1].x, boneMatrices[boneIndex3][2].x, boneMatrices[boneIndex3][3].x),
|
||||
vec4(boneMatrices[boneIndex3][0].y, boneMatrices[boneIndex3][1].y, boneMatrices[boneIndex3][2].y, boneMatrices[boneIndex3][3].y),
|
||||
vec4(boneMatrices[boneIndex3][0].z, boneMatrices[boneIndex3][1].z, boneMatrices[boneIndex3][2].z, boneMatrices[boneIndex3][3].z),
|
||||
vec4(boneMatrices[boneIndex3][0].w, boneMatrices[boneIndex3][1].w, boneMatrices[boneIndex3][2].w, boneMatrices[boneIndex3][3].w));
|
||||
|
||||
vec4 skinnedPosition =
|
||||
vertexBoneWeights.x*(boneMatrixTransposed0*vec4(vertexPosition, 1.0)) +
|
||||
vertexBoneWeights.y*(boneMatrixTransposed1*vec4(vertexPosition, 1.0)) +
|
||||
vertexBoneWeights.z*(boneMatrixTransposed2*vec4(vertexPosition, 1.0)) +
|
||||
vertexBoneWeights.w*(boneMatrixTransposed3*vec4(vertexPosition, 1.0));
|
||||
|
||||
fragTexCoord = vertexTexCoord;
|
||||
fragColor = vertexColor;
|
||||
|
||||
gl_Position = mvp*skinnedPosition;
|
||||
}
|
||||
31
raylib/examples/models/resources/shaders/glsl100/skybox.fs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#version 100
|
||||
|
||||
precision mediump float;
|
||||
|
||||
// Input vertex attributes (from vertex shader)
|
||||
varying vec3 fragPosition;
|
||||
|
||||
// Input uniform values
|
||||
uniform samplerCube environmentMap;
|
||||
uniform bool vflipped;
|
||||
uniform bool doGamma;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Fetch color from texture map
|
||||
vec4 texelColor = vec4(0.0);
|
||||
|
||||
if (vflipped) texelColor = textureCube(environmentMap, vec3(fragPosition.x, -fragPosition.y, fragPosition.z));
|
||||
else texelColor = textureCube(environmentMap, fragPosition);
|
||||
|
||||
vec3 color = vec3(texelColor.x, texelColor.y, texelColor.z);
|
||||
|
||||
if (doGamma) // Apply gamma correction
|
||||
{
|
||||
color = color/(color + vec3(1.0));
|
||||
color = pow(color, vec3(1.0/2.2));
|
||||
}
|
||||
|
||||
// Calculate final fragment color
|
||||
gl_FragColor = vec4(color, 1.0);
|
||||
}
|
||||
24
raylib/examples/models/resources/shaders/glsl100/skybox.vs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#version 100
|
||||
|
||||
// Input vertex attributes
|
||||
attribute vec3 vertexPosition;
|
||||
|
||||
// Input uniform values
|
||||
uniform mat4 matProjection;
|
||||
uniform mat4 matView;
|
||||
|
||||
// Output vertex attributes (to fragment shader)
|
||||
varying vec3 fragPosition;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Calculate fragment position based on model transformations
|
||||
fragPosition = vertexPosition;
|
||||
|
||||
// Remove translation from the view matrix
|
||||
mat4 rotView = mat4(mat3(matView));
|
||||
vec4 clipPos = matProjection*rotView*vec4(vertexPosition, 1.0);
|
||||
|
||||
// Calculate final vertex position
|
||||
gl_Position = clipPos;
|
||||
}
|
||||
30
raylib/examples/models/resources/shaders/glsl330/cubemap.fs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#version 330
|
||||
|
||||
// Input vertex attributes (from vertex shader)
|
||||
in vec3 fragPosition;
|
||||
|
||||
// Input uniform values
|
||||
uniform sampler2D equirectangularMap;
|
||||
|
||||
// Output fragment color
|
||||
out vec4 finalColor;
|
||||
|
||||
vec2 SampleSphericalMap(vec3 v)
|
||||
{
|
||||
vec2 uv = vec2(atan(v.z, v.x), asin(v.y));
|
||||
uv *= vec2(0.1591, 0.3183);
|
||||
uv += 0.5;
|
||||
return uv;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
// Normalize local position
|
||||
vec2 uv = SampleSphericalMap(normalize(fragPosition));
|
||||
|
||||
// Fetch color from texture map
|
||||
vec3 color = texture(equirectangularMap, uv).rgb;
|
||||
|
||||
// Calculate final fragment color
|
||||
finalColor = vec4(color, 1.0);
|
||||
}
|
||||
20
raylib/examples/models/resources/shaders/glsl330/cubemap.vs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#version 330
|
||||
|
||||
// Input vertex attributes
|
||||
in vec3 vertexPosition;
|
||||
|
||||
// Input uniform values
|
||||
uniform mat4 matProjection;
|
||||
uniform mat4 matView;
|
||||
|
||||
// Output vertex attributes (to fragment shader)
|
||||
out vec3 fragPosition;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Calculate fragment position based on model transformations
|
||||
fragPosition = vertexPosition;
|
||||
|
||||
// Calculate final vertex position
|
||||
gl_Position = matProjection*matView*vec4(vertexPosition, 1.0);
|
||||
}
|
||||
17
raylib/examples/models/resources/shaders/glsl330/skinning.fs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#version 330
|
||||
|
||||
// Input vertex attributes (from vertex shader)
|
||||
in vec2 fragTexCoord;
|
||||
in vec4 fragColor;
|
||||
|
||||
// Output fragment color
|
||||
out vec4 finalColor;
|
||||
|
||||
uniform sampler2D texture0;
|
||||
uniform vec4 colDiffuse;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 texelColor = texture(texture0, fragTexCoord);
|
||||
finalColor = texelColor*colDiffuse*fragColor;
|
||||
}
|
||||
37
raylib/examples/models/resources/shaders/glsl330/skinning.vs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#version 330
|
||||
|
||||
#define MAX_BONE_NUM 128
|
||||
|
||||
// Input vertex attributes
|
||||
in vec3 vertexPosition;
|
||||
in vec2 vertexTexCoord;
|
||||
in vec4 vertexColor;
|
||||
in vec4 vertexBoneIds;
|
||||
in vec4 vertexBoneWeights;
|
||||
|
||||
// Input uniform values
|
||||
uniform mat4 mvp;
|
||||
uniform mat4 boneMatrices[MAX_BONE_NUM];
|
||||
|
||||
// Output vertex attributes (to fragment shader)
|
||||
out vec2 fragTexCoord;
|
||||
out vec4 fragColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
int boneIndex0 = int(vertexBoneIds.x);
|
||||
int boneIndex1 = int(vertexBoneIds.y);
|
||||
int boneIndex2 = int(vertexBoneIds.z);
|
||||
int boneIndex3 = int(vertexBoneIds.w);
|
||||
|
||||
vec4 skinnedPosition =
|
||||
vertexBoneWeights.x*(boneMatrices[boneIndex0]*vec4(vertexPosition, 1.0)) +
|
||||
vertexBoneWeights.y*(boneMatrices[boneIndex1]*vec4(vertexPosition, 1.0)) +
|
||||
vertexBoneWeights.z*(boneMatrices[boneIndex2]*vec4(vertexPosition, 1.0)) +
|
||||
vertexBoneWeights.w*(boneMatrices[boneIndex3]*vec4(vertexPosition, 1.0));
|
||||
|
||||
fragTexCoord = vertexTexCoord;
|
||||
fragColor = vertexColor;
|
||||
|
||||
gl_Position = mvp*skinnedPosition;
|
||||
}
|
||||
30
raylib/examples/models/resources/shaders/glsl330/skybox.fs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#version 330
|
||||
|
||||
// Input vertex attributes (from vertex shader)
|
||||
in vec3 fragPosition;
|
||||
|
||||
// Input uniform values
|
||||
uniform samplerCube environmentMap;
|
||||
uniform bool vflipped;
|
||||
uniform bool doGamma;
|
||||
|
||||
// Output fragment color
|
||||
out vec4 finalColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Fetch color from texture map
|
||||
vec3 color = vec3(0.0);
|
||||
|
||||
if (vflipped) color = texture(environmentMap, vec3(fragPosition.x, -fragPosition.y, fragPosition.z)).rgb;
|
||||
else color = texture(environmentMap, fragPosition).rgb;
|
||||
|
||||
if (doGamma)// Apply gamma correction
|
||||
{
|
||||
color = color/(color + vec3(1.0));
|
||||
color = pow(color, vec3(1.0/2.2));
|
||||
}
|
||||
|
||||
// Calculate final fragment color
|
||||
finalColor = vec4(color, 1.0);
|
||||
}
|
||||
24
raylib/examples/models/resources/shaders/glsl330/skybox.vs
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#version 330
|
||||
|
||||
// Input vertex attributes
|
||||
in vec3 vertexPosition;
|
||||
|
||||
// Input uniform values
|
||||
uniform mat4 matProjection;
|
||||
uniform mat4 matView;
|
||||
|
||||
// Output vertex attributes (to fragment shader)
|
||||
out vec3 fragPosition;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Calculate fragment position based on model transformations
|
||||
fragPosition = vertexPosition;
|
||||
|
||||
// Remove translation from the view matrix
|
||||
mat4 rotView = mat4(mat3(matView));
|
||||
vec4 clipPos = matProjection*rotView*vec4(vertexPosition, 1.0);
|
||||
|
||||
// Calculate final vertex position
|
||||
gl_Position = clipPos;
|
||||
}
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
#version 330
|
||||
|
||||
// Input vertex attributes (from vertex shader)
|
||||
in vec3 fragPosition;
|
||||
//in vec2 fragTexCoord;
|
||||
in vec4 fragColor;
|
||||
in vec3 fragNormal;
|
||||
|
||||
// Input uniform values
|
||||
//uniform sampler2D texture0;
|
||||
uniform vec4 colDiffuse;
|
||||
|
||||
// Output fragment color
|
||||
out vec4 finalColor;
|
||||
|
||||
// NOTE: Add here your custom variables
|
||||
|
||||
#define MAX_LIGHTS 4
|
||||
#define LIGHT_DIRECTIONAL 0
|
||||
#define LIGHT_POINT 1
|
||||
|
||||
struct Light {
|
||||
int enabled;
|
||||
int type;
|
||||
vec3 position;
|
||||
vec3 target;
|
||||
vec4 color;
|
||||
};
|
||||
|
||||
// Input lighting values
|
||||
uniform Light lights[MAX_LIGHTS];
|
||||
uniform vec4 ambient;
|
||||
uniform vec3 viewPos;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Texel color fetching from texture sampler
|
||||
//vec4 texelColor = texture(texture0, fragTexCoord);
|
||||
vec3 lightDot = vec3(0.0);
|
||||
vec3 normal = normalize(fragNormal);
|
||||
vec3 viewD = normalize(viewPos - fragPosition);
|
||||
vec3 specular = vec3(0.0);
|
||||
|
||||
// NOTE: Implement here your fragment shader code
|
||||
|
||||
for (int i = 0; i < MAX_LIGHTS; i++)
|
||||
{
|
||||
if (lights[i].enabled == 1)
|
||||
{
|
||||
vec3 light = vec3(0.0);
|
||||
|
||||
if (lights[i].type == LIGHT_DIRECTIONAL)
|
||||
{
|
||||
light = -normalize(lights[i].target - lights[i].position);
|
||||
}
|
||||
|
||||
if (lights[i].type == LIGHT_POINT)
|
||||
{
|
||||
light = normalize(lights[i].position - fragPosition);
|
||||
}
|
||||
|
||||
float NdotL = max(dot(normal, light), 0.0);
|
||||
lightDot += lights[i].color.rgb*NdotL;
|
||||
|
||||
float specCo = 0.0;
|
||||
if (NdotL > 0.0) specCo = pow(max(0.0, dot(viewD, reflect(-(light), normal))), 16.0); // 16 refers to shine
|
||||
specular += specCo;
|
||||
}
|
||||
}
|
||||
|
||||
finalColor = (fragColor*((colDiffuse + vec4(specular, 1.0))*vec4(lightDot, 1.0)));
|
||||
finalColor += fragColor*(ambient/10.0)*colDiffuse;
|
||||
|
||||
// Gamma correction
|
||||
finalColor = pow(finalColor, vec4(1.0/2.2));
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
#version 330
|
||||
|
||||
// Input vertex attributes
|
||||
in vec3 vertexPosition;
|
||||
//in vec2 vertexTexCoord;
|
||||
in vec3 vertexNormal;
|
||||
in vec4 vertexColor;
|
||||
|
||||
// Input uniform values
|
||||
uniform mat4 mvp;
|
||||
uniform mat4 matModel;
|
||||
uniform mat4 matNormal;
|
||||
|
||||
// Output vertex attributes (to fragment shader)
|
||||
out vec3 fragPosition;
|
||||
//out vec2 fragTexCoord;
|
||||
out vec4 fragColor;
|
||||
out vec3 fragNormal;
|
||||
|
||||
// NOTE: Add here your custom variables
|
||||
|
||||
void main()
|
||||
{
|
||||
// Send vertex attributes to fragment shader
|
||||
fragPosition = vec3(matModel*vec4(vertexPosition, 1.0));
|
||||
//fragTexCoord = vertexTexCoord;
|
||||
fragColor = vertexColor;
|
||||
fragNormal = normalize(vec3(matNormal*vec4(vertexNormal, 1.0)));
|
||||
|
||||
// Calculate final vertex position
|
||||
gl_Position = mvp*vec4(vertexPosition, 1.0);
|
||||
}
|
||||