raylib/examples/models/models_skybox.c

99 lines
4.2 KiB
C
Raw Normal View History

/*******************************************************************************************
*
* raylib [models] example - Skybox loading and drawing
*
* This example has been created using raylib 1.8 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2017 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
2019-05-20 17:36:42 +03:00
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
2019-05-20 17:36:42 +03:00
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
2019-05-27 01:18:15 +03:00
Camera camera = { { 1.0f, 1.0f, 1.0f }, { 4.0f, 1.0f, 4.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
2019-05-20 17:36:42 +03:00
// Load skybox model
Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f);
Model skybox = LoadModelFromMesh(cube);
2019-05-20 17:36:42 +03:00
// Load skybox shader and set required locations
// NOTE: Some locations are automatically set at shader loading
#if defined(PLATFORM_DESKTOP)
skybox.materials[0].shader = LoadShader("resources/shaders/glsl330/skybox.vs", "resources/shaders/glsl330/skybox.fs");
#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
skybox.materials[0].shader = LoadShader("resources/shaders/glsl100/skybox.vs", "resources/shaders/glsl100/skybox.fs");
#endif
2019-03-29 22:22:50 +03:00
SetShaderValue(skybox.materials[0].shader, GetShaderLocation(skybox.materials[0].shader, "environmentMap"), (int[1]){ MAP_CUBEMAP }, UNIFORM_INT);
// Load cubemap shader and setup required shader locations
#if defined(PLATFORM_DESKTOP)
Shader shdrCubemap = LoadShader("resources/shaders/glsl330/cubemap.vs", "resources/shaders/glsl330/cubemap.fs");
#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
Shader shdrCubemap = LoadShader("resources/shaders/glsl100/cubemap.vs", "resources/shaders/glsl100/cubemap.fs");
#endif
2019-01-10 13:25:26 +03:00
SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, UNIFORM_INT);
2019-05-20 17:36:42 +03:00
2018-06-21 17:48:20 +03:00
// Load HDR panorama (sphere) texture
Texture2D texHDR = LoadTexture("resources/dresden_square.hdr");
2019-05-20 17:36:42 +03:00
2018-06-21 17:48:20 +03:00
// Generate cubemap (texture with 6 quads-cube-mapping) from panorama HDR texture
// NOTE: New texture is generated rendering to texture, shader computes the sphre->cube coordinates mapping
2019-03-29 22:22:50 +03:00
skybox.materials[0].maps[MAP_CUBEMAP].texture = GenTextureCubemap(shdrCubemap, texHDR, 512);
2019-05-20 17:36:42 +03:00
2018-06-21 17:48:20 +03:00
UnloadTexture(texHDR); // Texture not required anymore, cubemap already generated
UnloadShader(shdrCubemap); // Unload cubemap generation shader, not required anymore
2019-05-20 17:36:42 +03:00
SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set a first person camera mode
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
UpdateCamera(&camera); // Update camera
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(camera);
DrawModel(skybox, (Vector3){0, 0, 0}, 1.0f, WHITE);
2019-05-20 17:36:42 +03:00
DrawGrid(10, 1.0f);
EndMode3D();
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
2018-06-21 17:48:20 +03:00
UnloadModel(skybox); // Unload skybox model (and textures)
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}