2023-05-16 15:04:59 +00:00
|
|
|
/*******************************************************************************************
|
|
|
|
*
|
|
|
|
* 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-2023 Ramon Santamaria (@raysan5)
|
|
|
|
*
|
|
|
|
********************************************************************************************/
|
|
|
|
// Initialization
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
const screenWidth = 800;
|
|
|
|
const screenHeight = 450;
|
|
|
|
|
|
|
|
initWindow(screenWidth, screenHeight, "raylib [models] example - cubesmap loading and drawing");
|
|
|
|
|
|
|
|
// Define the camera to look into our 3d world
|
|
|
|
const position = new Vector3(16.0, 14.0, 16.0); // Camera position
|
|
|
|
const target = new Vector3(0.0, 0.0, 0.0); // Camera looking at point
|
|
|
|
const up = Vector3(0.0, 1.0, 0.0); // Camera up vector (rotation towards target)
|
|
|
|
const fovy = 45.0; // Camera field-of-view Y
|
|
|
|
const projection = CAMERA_PERSPECTIVE; // Camera projection type
|
|
|
|
const camera = new Camera3D(position, target, up, fovy, projection)
|
|
|
|
|
2023-05-26 20:09:53 +00:00
|
|
|
let image = loadImage("resources/cubicmap.png"); // Load cubicmap image (RAM)
|
2023-05-20 19:34:27 +00:00
|
|
|
let cubicmap = loadTextureFromImage(image); // Convert image to texture to display (VRAM)
|
2023-05-16 15:04:59 +00:00
|
|
|
|
|
|
|
const mesh = genMeshCubicmap(image, new Vector3(1.0, 1.0, 1.0));
|
2023-06-13 21:03:38 +00:00
|
|
|
const floor = loadModelFromMesh(mesh);
|
2023-05-16 15:04:59 +00:00
|
|
|
|
|
|
|
// NOTE: By default each cube is mapped to one part of texture atlas
|
2023-05-26 20:09:53 +00:00
|
|
|
let texture = loadTexture("resources/cubicmap_atlas.png"); // Load map texture
|
2023-05-20 19:34:27 +00:00
|
|
|
|
2023-05-16 15:04:59 +00:00
|
|
|
//model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture
|
2023-05-20 19:34:27 +00:00
|
|
|
const mat = loadMaterialDefault()
|
|
|
|
setMaterialTexture(mat, MATERIAL_MAP_DIFFUSE, texture)
|
2023-06-13 21:03:38 +00:00
|
|
|
setModelMaterial(floor,0,mat)
|
2023-05-16 15:04:59 +00:00
|
|
|
|
|
|
|
const mapPosition = new Vector3(-16.0, 0.0, -8.0); // Set model position
|
|
|
|
|
2023-05-20 19:34:27 +00:00
|
|
|
unloadImage(image); // Unload cubesmap image from RAM, already uploaded to VRAM
|
2023-05-16 15:04:59 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
//----------------------------------------------------------------------------------
|
2023-05-20 19:34:27 +00:00
|
|
|
|
2023-05-16 15:04:59 +00:00
|
|
|
// Draw
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
beginDrawing();
|
|
|
|
|
|
|
|
clearBackground(RAYWHITE);
|
|
|
|
|
|
|
|
beginMode3D(camera);
|
|
|
|
|
2023-06-13 21:03:38 +00:00
|
|
|
drawModel(floor, mapPosition, 1.0, WHITE);
|
2023-05-16 15:04:59 +00:00
|
|
|
|
|
|
|
endMode3D();
|
|
|
|
|
|
|
|
drawTextureEx(cubicmap, new Vector2(screenWidth - cubicmap.width*4.0 - 20, 20.0), 0.0, 4.0, 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
|
|
|
|
//--------------------------------------------------------------------------------------
|
2023-05-20 19:34:27 +00:00
|
|
|
unloadTexture(cubicmap);
|
|
|
|
unloadTexture(texture);
|
2023-06-13 21:03:38 +00:00
|
|
|
unloadModel(floor);
|
2023-05-16 15:04:59 +00:00
|
|
|
|
|
|
|
closeWindow(); // Close window and OpenGL context
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|