public unsafe static int Main() { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available) InitWindow(screenWidth, screenHeight, "raylib [shaders] example - fog"); // Define the camera to look into our 3d world Camera3D camera = new Camera3D( new Vector3(2.0f, 2.0f, 6.0f), // position new Vector3(0.0f, 0.5f, 0.0f), // target new Vector3(0.0f, 1.0f, 0.0f), // up 45.0f, CAMERA_PERSPECTIVE); // fov, type // Load models and texture Model modelA = LoadModelFromMesh(GenMeshTorus(0.4f, 1.0f, 16, 32)); Model modelB = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f)); Model modelC = LoadModelFromMesh(GenMeshSphere(0.5f, 32, 32)); Texture2D texture = LoadTexture("resources/texel_checker.png"); // Assign texture to default model material Utils.SetMaterialTexture(ref modelA, 0, MAP_ALBEDO, ref texture); Utils.SetMaterialTexture(ref modelB, 0, MAP_ALBEDO, ref texture); Utils.SetMaterialTexture(ref modelC, 0, MAP_ALBEDO, ref texture); // Load shader and set up some uniforms Shader shader = LoadShader("resources/shaders/glsl330/base_lighting.vs", "resources/shaders/glsl330/fog.fs"); int * locs = (int *)shader.locs.ToPointer(); locs[(int)LOC_MATRIX_MODEL] = GetShaderLocation(shader, "matModel"); locs[(int)LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos"); // Ambient light level int ambientLoc = GetShaderLocation(shader, "ambient"); Utils.SetShaderValue(shader, ambientLoc, new float[] { 0.2f, 0.2f, 0.2f, 1.0f }, UNIFORM_VEC4); float fogDensity = 0.15f; int fogDensityLoc = GetShaderLocation(shader, "fogDensity"); Utils.SetShaderValue(shader, fogDensityLoc, fogDensity, UNIFORM_FLOAT); // NOTE: All models share the same shader Utils.SetMaterialShader(ref modelA, 0, ref shader); Utils.SetMaterialShader(ref modelB, 0, ref shader); Utils.SetMaterialShader(ref modelC, 0, ref shader); // Using just 1 point lights CreateLight(0, LightType.LIGHT_POINT, new Vector3(0, 2, 6), Vector3.Zero, WHITE, shader); SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital 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(ref camera); // Update camera if (IsKeyDown(KEY_UP)) { fogDensity += 0.001f; if (fogDensity > 1.0) { fogDensity = 1.0f; } } if (IsKeyDown(KEY_DOWN)) { fogDensity -= 0.001f; if (fogDensity < 0.0) { fogDensity = 0.0f; } } Utils.SetShaderValue(shader, fogDensityLoc, fogDensity, UNIFORM_FLOAT); // Rotate the torus modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateX(-0.025f)); modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateZ(0.012f)); // Update the light shader with the camera view position Utils.SetShaderValue(shader, locs[(int)LOC_VECTOR_VIEW], camera.position.X, ShaderUniformDataType.UNIFORM_VEC3); //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(GRAY); BeginMode3D(camera); // Draw the three models DrawModel(modelA, Vector3.Zero, 1.0f, WHITE); DrawModel(modelB, new Vector3(-2.6f, 0, 0), 1.0f, WHITE); DrawModel(modelC, new Vector3(2.6f, 0, 0), 1.0f, WHITE); for (int i = -20; i < 20; i += 2) { DrawModel(modelA, new Vector3(i, 0, 2), 1.0f, WHITE); } EndMode3D(); DrawText(string.Format("Use KEY_UP/KEY_DOWN to change fog density [{0}]", fogDensity), 10, 10, 20, RAYWHITE); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadModel(modelA); // Unload the model A UnloadModel(modelB); // Unload the model B UnloadModel(modelC); // Unload the model C UnloadTexture(texture); // Unload the texture UnloadShader(shader); // Unload shader CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return(0); }
public unsafe static int Main() { // 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 Camera3D camera = new Camera3D(new Vector3(18.0f, 16.0f, 18.0f), new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f), 45.0f, 0); Image image = LoadImage("resources/heightmap.png"); // Load heightmap image (RAM) Texture2D texture = LoadTextureFromImage(image); // Convert image to texture (VRAM) Mesh mesh = GenMeshHeightmap(image, new Vector3(16, 8, 16)); // Generate heightmap mesh (RAM and VRAM) Model model = LoadModelFromMesh(mesh); // Load model from generated mesh // Set map diffuse texture Utils.SetMaterialTexture(ref model, 0, MAP_ALBEDO, ref texture); Vector3 mapPosition = new Vector3(-8.0f, 0.0f, -8.0f); // Define model position UnloadImage(image); // Unload heightmap image from RAM, already uploaded to VRAM SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital 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(ref camera); // Update camera //---------------------------------------------------------------------------------- // 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); }
public unsafe static int Main() { // 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 Camera3D camera; camera.position = new Vector3(20.0f, 20.0f, 20.0f); // Camera3D position camera.target = new Vector3(0.0f, 8.0f, 0.0f); // Camera3D looking at point camera.up = new Vector3(0.0f, 1.6f, 0.0f); // Camera3D up vector (rotation towards target) camera.fovy = 45.0f; // Camera3D field-of-view Y camera.type = CAMERA_PERSPECTIVE; // Camera3D mode type Ray ray = new Ray(); // Picking ray Model tower = LoadModel("resources/models/turret.obj"); // Load OBJ model Texture2D texture = LoadTexture("resources/models/turret_diffuse.png"); // Load model texture Utils.SetMaterialTexture(ref tower, 0, MAP_ALBEDO, ref texture); // Set map diffuse texture Vector3 towerPos = new Vector3(0.0f, 0.0f, 0.0f); // Set model position Mesh * meshes = (Mesh *)tower.meshes.ToPointer(); BoundingBox towerBBox = MeshBoundingBox(meshes[0]); // Get mesh bounding box bool hitMeshBBox = false; bool hitTriangle = false; // Test triangle Vector3 ta = new Vector3(-25.0f, 0.5f, 0.0f); Vector3 tb = new Vector3(-4.0f, 2.5f, 1.0f); Vector3 tc = new Vector3(-8.0f, 6.5f, 0.0f); Vector3 bary = new Vector3(0.0f, 0.0f, 0.0f); SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode SetTargetFPS(60); // Set our game to run at 60 frames-per-second //---------------------------------------------------------------------------------- // Main game loop //-------------------------------------------------------------------------------------- while (!WindowShouldClose()) // Detect window close button or ESC key { //---------------------------------------------------------------------------------- // Update //---------------------------------------------------------------------------------- UpdateCamera(ref camera); // Update camera // Display information about closest hit RayHitInfo nearestHit = new RayHitInfo(); string hitObjectName = "None"; nearestHit.distance = FLT_MAX; nearestHit.hit = false; Color cursorColor = WHITE; // Get ray and test against ground, triangle, and mesh ray = GetMouseRay(GetMousePosition(), camera); // Check ray collision aginst ground plane RayHitInfo groundHitInfo = GetCollisionRayGround(ray, 0.0f); if ((groundHitInfo.hit) && (groundHitInfo.distance < nearestHit.distance)) { nearestHit = groundHitInfo; cursorColor = GREEN; hitObjectName = "Ground"; } // Check ray collision against test triangle RayHitInfo triHitInfo = GetCollisionRayTriangle(ray, ta, tb, tc); if ((triHitInfo.hit) && (triHitInfo.distance < nearestHit.distance)) { nearestHit = triHitInfo; cursorColor = PURPLE; hitObjectName = "Triangle"; bary = Vector3Barycenter(nearestHit.position, ta, tb, tc); hitTriangle = true; } else { hitTriangle = false; } RayHitInfo meshHitInfo = new RayHitInfo(); // Check ray collision against bounding box first, before trying the full ray-mesh test if (CheckCollisionRayBox(ray, towerBBox)) { hitMeshBBox = true; // Check ray collision against model // NOTE: It considers model.transform matrix! meshHitInfo = GetCollisionRayModel(ray, tower); if ((meshHitInfo.hit) && (meshHitInfo.distance < nearestHit.distance)) { nearestHit = meshHitInfo; cursorColor = ORANGE; hitObjectName = "Mesh"; } } hitMeshBBox = false; //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); BeginMode3D(camera); // Draw the tower // WARNING: If scale is different than 1.0f, // not considered by GetCollisionRayModel() DrawModel(tower, towerPos, 1.0f, WHITE); // Draw the test triangle DrawLine3D(ta, tb, PURPLE); DrawLine3D(tb, tc, PURPLE); DrawLine3D(tc, ta, PURPLE); // Draw the mesh bbox if we hit it if (hitMeshBBox) { DrawBoundingBox(towerBBox, LIME); } // If we hit something, draw the cursor at the hit point if (nearestHit.hit) { DrawCube(nearestHit.position, 0.3f, 0.3f, 0.3f, cursorColor); DrawCubeWires(nearestHit.position, 0.3f, 0.3f, 0.3f, RED); Vector3 normalEnd; normalEnd.X = nearestHit.position.X + nearestHit.normal.X; normalEnd.Y = nearestHit.position.Y + nearestHit.normal.Y; normalEnd.Z = nearestHit.position.Z + nearestHit.normal.Z; DrawLine3D(nearestHit.position, normalEnd, RED); } DrawRay(ray, MAROON); DrawGrid(10, 10.0f); EndMode3D(); // Draw some debug GUI text DrawText(string.Format("Hit Object: {0}", hitObjectName), 10, 50, 10, BLACK); if (nearestHit.hit) { int ypos = 70; var x = string.Format("Distance: {0:000.00}", nearestHit.distance); DrawText(string.Format("Distance: {0:000.00}", nearestHit.distance), 10, ypos, 10, BLACK); DrawText(string.Format("Hit Pos: {0:000.00} {1:000.00} {2:000.00}", nearestHit.position.X, nearestHit.position.Y, nearestHit.position.Z), 10, ypos + 15, 10, BLACK); DrawText(string.Format("Hit Norm: {0:000.00} {1:000.00} {2:000.00}", nearestHit.normal.X, nearestHit.normal.Y, nearestHit.normal.Z), 10, ypos + 30, 10, BLACK); if (hitTriangle) { DrawText(string.Format("Barycenter:{0:000.00} {1:000.00} {2:000.00}", bary.X, bary.Y, bary.Z), 10, ypos + 45, 10, BLACK); } } DrawText("Use Mouse to Move Camera", 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); }
public unsafe static int Main() { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available) InitWindow(screenWidth, screenHeight, "raylib [shaders] example - basic lighting"); // Define the camera to look into our 3d world Camera3D camera = new Camera3D(); camera.position = new Vector3(2.0f, 2.0f, 6.0f); // Camera position camera.target = new Vector3(0.0f, 0.5f, 0.0f); // Camera looking at point camera.up = new Vector3(0.0f, 1.0f, 0.0f); // Camera up vector (rotation towards target) camera.fovy = 45.0f; // Camera field-of-view Y camera.type = CAMERA_PERSPECTIVE; // Camera mode type // Load models Model modelA = LoadModelFromMesh(GenMeshTorus(0.4f, 1.0f, 16, 32)); Model modelB = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f)); Model modelC = LoadModelFromMesh(GenMeshSphere(0.5f, 32, 32)); // Load models texture Texture2D texture = LoadTexture("resources/texel_checker.png"); // Assign texture to default model material Utils.SetMaterialTexture(ref modelA, 0, MAP_ALBEDO, ref texture); Utils.SetMaterialTexture(ref modelB, 0, MAP_ALBEDO, ref texture); Utils.SetMaterialTexture(ref modelC, 0, MAP_ALBEDO, ref texture); Shader shader = LoadShader("resources/shaders/glsl330/base_lighting.vs", "resources/shaders/glsl330/lighting.fs"); // Get some shader loactions int *locs = (int *)shader.locs.ToPointer(); locs[(int)LOC_MATRIX_MODEL] = GetShaderLocation(shader, "matModel"); locs[(int)LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos"); // ambient light level int ambientLoc = GetShaderLocation(shader, "ambient"); Utils.SetShaderValue(shader, ambientLoc, new float[] { 0.2f, 0.2f, 0.2f, 1.0f }, ShaderUniformDataType.UNIFORM_VEC4); float angle = 6.282f; // All models use the same shader Utils.SetMaterialShader(ref modelA, 0, ref shader); Utils.SetMaterialShader(ref modelB, 0, ref shader); Utils.SetMaterialShader(ref modelC, 0, ref shader); // Using 4 point lights, white, red, green and blue Light[] lights = new Light[MAX_LIGHTS]; lights[0] = CreateLight(LightType.LIGHT_POINT, new Vector3(4, 2, 4), Vector3Zero(), WHITE, shader); lights[1] = CreateLight(LightType.LIGHT_POINT, new Vector3(4, 2, 4), Vector3Zero(), RED, shader); lights[2] = CreateLight(LightType.LIGHT_POINT, new Vector3(0, 4, 2), Vector3Zero(), GREEN, shader); lights[3] = CreateLight(LightType.LIGHT_POINT, new Vector3(0, 4, 2), Vector3Zero(), BLUE, shader); SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital 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 //---------------------------------------------------------------------------------- if (IsKeyPressed(KEY_W)) { lights[0].enabled = !lights[0].enabled; } if (IsKeyPressed(KEY_R)) { lights[1].enabled = !lights[1].enabled; } if (IsKeyPressed(KEY_G)) { lights[2].enabled = !lights[2].enabled; } if (IsKeyPressed(KEY_B)) { lights[3].enabled = !lights[3].enabled; } UpdateCamera(ref camera); // Update camera // Make the lights do differing orbits angle -= 0.02f; lights[0].position.X = (float)Math.Cos(angle) * 4.0f; lights[0].position.Z = (float)Math.Sin(angle) * 4.0f; lights[1].position.X = (float)Math.Cos(-angle * 0.6f) * 4.0f; lights[1].position.Z = (float)Math.Sin(-angle * 0.6f) * 4.0f; lights[2].position.Y = (float)Math.Cos(angle * 0.2f) * 4.0f; lights[2].position.Z = (float)Math.Sin(angle * 0.2f) * 4.0f; lights[3].position.Y = (float)Math.Cos(-angle * 0.35f) * 4.0f; lights[3].position.Z = (float)Math.Sin(-angle * 0.35f) * 4.0f; UpdateLightValues(shader, lights[0]); UpdateLightValues(shader, lights[1]); UpdateLightValues(shader, lights[2]); UpdateLightValues(shader, lights[3]); // Rotate the torus modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateX(-0.025f)); modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateZ(0.012f)); // Update the light shader with the camera view position float[] cameraPos = { camera.position.X, camera.position.Y, camera.position.Z }; Utils.SetShaderValue(shader, locs[(int)LOC_VECTOR_VIEW], cameraPos, ShaderUniformDataType.UNIFORM_VEC3); //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); BeginMode3D(camera); // Draw the three models DrawModel(modelA, Vector3Zero(), 1.0f, WHITE); DrawModel(modelB, new Vector3(-1.6f, 0, 0), 1.0f, WHITE); DrawModel(modelC, new Vector3(1.6f, 0, 0), 1.0f, WHITE); // Draw markers to show where the lights are if (lights[0].enabled) { DrawSphereEx(lights[0].position, 0.2f, 8, 8, WHITE); } if (lights[1].enabled) { DrawSphereEx(lights[1].position, 0.2f, 8, 8, RED); } if (lights[2].enabled) { DrawSphereEx(lights[2].position, 0.2f, 8, 8, GREEN); } if (lights[3].enabled) { DrawSphereEx(lights[3].position, 0.2f, 8, 8, BLUE); } DrawGrid(10, 1.0f); EndMode3D(); DrawFPS(10, 10); DrawText("Keys RGB & W toggle lights", 10, 30, 20, DARKGRAY); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadModel(modelA); // Unload the modelA UnloadModel(modelB); // Unload the modelB UnloadModel(modelC); // Unload the modelC UnloadTexture(texture); // Unload the texture UnloadShader(shader); // Unload shader CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return(0); }
public unsafe static int Main() { // 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 Camera3D camera = new Camera3D(); camera.position = new Vector3(10.0f, 10.0f, 10.0f); // Camera position camera.target = new Vector3(0.0f, 0.0f, 0.0f); // Camera looking at point camera.up = new Vector3(0.0f, 1.0f, 0.0f); // Camera up vector (rotation towards target) camera.fovy = 45.0f; // Camera field-of-view Y camera.type = CAMERA_PERSPECTIVE; // Camera mode type Model model = LoadModel("resources/guy/guy.iqm"); // Load the animated model mesh and basic data Texture2D texture = LoadTexture("resources/guy/guytex.png"); // Load model texture and set material Utils.SetMaterialTexture(ref model, 0, MAP_ALBEDO, ref texture); // Set model material map texture Vector3 position = new Vector3(0.0f, 0.0f, 0.0f); // Set model position // Load animation data int animsCount = 0; IntPtr animsPtr = LoadModelAnimations("resources/guy/guyanim.iqm", ref animsCount); ModelAnimation *anims = (ModelAnimation *)animsPtr.ToPointer(); int animFrameCounter = 0; SetCameraMode(camera, CAMERA_FREE); // Set free camera mode SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- UpdateCamera(ref camera); // 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, new Vector3(1.0f, 0.0f, 0.0f), -90.0f, new Vector3(1.0f, 1.0f, 1.0f), WHITE); for (int i = 0; i < model.boneCount; i++) { Transform **framePoses = (Transform **)anims[0].framePoses.ToPointer(); DrawCube(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 // Unload model animations data for (int i = 0; i < animsCount; i++) { UnloadModelAnimation(anims[i]); } UnloadModel(model); // Unload model CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return(0); }
public unsafe static int Main() { // 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 Camera3D camera = new Camera3D(new Vector3(1.0f, 1.0f, 1.0f), new Vector3(4.0f, 1.0f, 4.0f), new Vector3(0.0f, 1.0f, 0.0f), 45.0f, CAMERA_PERSPECTIVE); // Load skybox model Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f); Model skybox = LoadModelFromMesh(cube); // Load skybox shader and set required locations // NOTE: Some locations are automatically set at shader loading Shader shader = LoadShader("resources/shaders/glsl330/skybox.vs", "resources/shaders/glsl330/skybox.fs"); Utils.SetMaterialShader(ref skybox, 0, ref shader); Utils.SetShaderValue(shader, GetShaderLocation(shader, "environmentMap"), new int[] { (int)MAP_CUBEMAP }, UNIFORM_INT); // Load cubemap shader and setup required shader locations Shader shdrCubemap = LoadShader("resources/shaders/glsl330/cubemap.vs", "resources/shaders/glsl330/cubemap.fs"); Utils.SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), new int[] { 0 }, UNIFORM_INT); // Load HDR panorama (sphere) texture Texture2D texHDR = LoadTexture("resources/dresden_square.hdr"); // 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 Texture2D cubemap = GenTextureCubemap(shdrCubemap, texHDR, 512); Utils.SetMaterialTexture(ref skybox, 0, MAP_CUBEMAP, ref cubemap); UnloadTexture(texHDR); // Texture not required anymore, cubemap already generated UnloadShader(shdrCubemap); // Unload cubemap generation shader, not required anymore 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(ref camera); // Update camera //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); BeginMode3D(camera); DrawModel(skybox, new Vector3(0, 0, 0), 1.0f, WHITE); DrawGrid(10, 1.0f); EndMode3D(); DrawFPS(10, 10); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadModel(skybox); // Unload skybox model (and textures) CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return(0); }
public unsafe static int Main() { // 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 Camera3D camera = new Camera3D(new Vector3(0.2f, 0.4f, 0.2f), new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f), 45.0f, CameraType.CAMERA_PERSPECTIVE); 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, new 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 // Set map diffuse texture Utils.SetMaterialTexture(ref model, 0, MAP_ALBEDO, ref texture); // Get map image data to be used for collision detection IntPtr mapPixelsData = GetImageData(imMap); UnloadImage(imMap); // Unload image from RAM Vector3 mapPosition = new Vector3(-16.0f, 0.0f, -8.0f); // Set model position Vector3 playerPosition = camera.position; // Set player position SetCameraMode(camera, CAMERA_FIRST_PERSON); // Set 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 //---------------------------------------------------------------------------------- Vector3 oldCamPos = camera.position; // Store old camera position UpdateCamera(ref camera); // Update camera // Check player collision (we simplify to 2D collision detection) Vector2 playerPos = new Vector2(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++) { Color *mapPixels = (Color *)mapPixelsData.ToPointer(); if ((mapPixels[y * cubicmap.width + x].r == 255) && // Collision: white pixel, only check R channel (CheckCollisionCircleRec(playerPos, playerRadius, new 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 // DrawCubeV(playerPosition, new Vector3( 0.2f, 0.4f, 0.2f ), RED); // Draw player EndMode3D(); DrawTextureEx(cubicmap, new Vector2(GetScreenWidth() - cubicmap.width * 4 - 20, 20), 0.0f, 4.0f, WHITE); DrawRectangleLines(GetScreenWidth() - cubicmap.width * 4 - 20, 20, cubicmap.width * 4, cubicmap.height * 4, GREEN); // Draw player position radar DrawRectangle(GetScreenWidth() - cubicmap.width * 4 - 20 + playerCellX * 4, 20 + playerCellY * 4, 4, 4, RED); DrawFPS(10, 10); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadTexture(cubicmap); // Unload cubicmap texture UnloadTexture(texture); // Unload map texture UnloadModel(model); // Unload map model CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return(0); }
public unsafe static int Main() { // 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 Camera3D camera = new Camera3D(new Vector3(16.0f, 14.0f, 16.0f), new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f), 45.0f, 0); 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, new 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 // Set map diffuse texture Utils.SetMaterialTexture(ref model, 0, MAP_ALBEDO, ref texture); Vector3 mapPosition = new Vector3(-16.0f, 0.0f, -8.0f); // Set model position UnloadImage(image); // Unload cubesmap image from RAM, already uploaded to VRAM SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital 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(ref camera); // Update camera //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); BeginMode3D(camera); DrawModel(model, mapPosition, 1.0f, WHITE); EndMode3D(); DrawTextureEx(cubicmap, new Vector2(screenWidth - cubicmap.width * 4 - 20, 20), 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; }
public static int Main() { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; SetConfigFlags(ConfigFlag.FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available) InitWindow(screenWidth, screenHeight, "raylib [shaders] example - model shader"); // Define the camera to look into our 3d world Camera3D camera = new Camera3D(); camera.position = new Vector3(4.0f, 4.0f, 4.0f); camera.target = new Vector3(0.0f, 1.0f, -1.0f); camera.up = new Vector3(0.0f, 1.0f, 0.0f); camera.fovy = 45.0f; camera.type = CAMERA_PERSPECTIVE; Model model = LoadModel("resources/models/watermill.obj"); // Load OBJ model Texture2D texture = LoadTexture("resources/models/watermill_diffuse.png"); // Load model texture Shader shader = LoadShader("resources/shaders/glsl330/base.vs", "resources/shaders/glsl330/grayscale.fs"); // Load model shader Utils.SetMaterialShader(ref model, 0, ref shader); // Set shader effect to 3d model Utils.SetMaterialTexture(ref model, 0, MAP_ALBEDO, ref texture); // Bind texture to model Vector3 position = new Vector3(0.0f, 0.0f, 0.0f); // Set model position SetCameraMode(camera, CAMERA_FREE); // Set an orbital 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(ref camera); // Update camera //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); BeginMode3D(camera); DrawModel(model, position, 0.2f, WHITE); // Draw 3d model with texture DrawGrid(10, 1.0f); // Draw a grid EndMode3D(); DrawText("(c) Watermill 3D model by Alberto Cano", screenWidth - 210, screenHeight - 20, 10, GRAY); DrawText(string.Format("Camera3D position: ({0:0.00}, {0:0.00}, {0:0.00})", camera.position.X, camera.position.Y, camera.position.Z), 600, 20, 10, BLACK); DrawText(string.Format("Camera3D target: ({0:0.00}, {0:0.00}, {0:0.00})", camera.target.X, camera.target.Y, camera.target.Z), 600, 40, 10, GRAY); DrawFPS(10, 10); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadShader(shader); // Unload shader UnloadTexture(texture); // Unload texture UnloadModel(model); // Unload model CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return(0); }
public unsafe static int Main() { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib [models] example - mesh generation"); // We generate a isChecked image for texturing Image isChecked = GenImageChecked(2, 2, 1, 1, RED, GREEN); Texture2D texture = LoadTextureFromImage(isChecked); UnloadImage(isChecked); Model[] models = new Model[NUM_MODELS]; models[0] = LoadModelFromMesh(GenMeshPlane(2, 2, 5, 5)); 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)); // Set isChecked texture as default diffuse component for all models material for (int i = 0; i < NUM_MODELS; i++) { // Set map diffuse texture Utils.SetMaterialTexture(ref models[i], 0, MAP_ALBEDO, ref texture); } // Define the camera to look into our 3d world Camera3D camera = new Camera3D(new Vector3(5.0f, 5.0f, 5.0f), new Vector3(0.0f, 0.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f), 45.0f, 0); // Model drawing position Vector3 position = new Vector3(0.0f, 0.0f, 0.0f); int currentModel = 0; SetCameraMode(camera, CAMERA_ORBITAL); // Set a orbital 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(ref camera); // Update internal camera and our camera if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) { currentModel = (currentModel + 1) % NUM_MODELS; // Cycle between the textures } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); BeginMode3D(camera); DrawModel(models[currentModel], position, 1.0f, WHITE); DrawGrid(10, 1.0f); EndMode3D(); DrawRectangle(30, 400, 310, 30, ColorAlpha(SKYBLUE, 0.5f)); DrawRectangleLines(30, 400, 310, 30, ColorAlpha(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; default: break; } EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- // Unload models data (GPU VRAM) for (int i = 0; i < NUM_MODELS; i++) { UnloadModel(models[i]); } CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return(0); }
public unsafe static int Main() { // 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 Camera3D camera = new Camera3D(); camera.position = new Vector3(50.0f, 50.0f, 50.0f); // Camera position camera.target = new Vector3(0.0f, 10.0f, 0.0f); // Camera looking at point camera.up = new Vector3(0.0f, 1.0f, 0.0f); // Camera up vector (rotation towards target) camera.fovy = 45.0f; // Camera field-of-view Y camera.type = CAMERA_PERSPECTIVE; // Camera mode type Model model = LoadModel("resources/models/castle.obj"); // Load model Texture2D texture = LoadTexture("resources/models/castle_diffuse.png"); // Load model texture // Set map diffuse texture Utils.SetMaterialTexture(ref model, 0, MAP_ALBEDO, ref texture); Vector3 position = new Vector3(0.0f, 0.0f, 0.0f); // Set model position Mesh * meshes = (Mesh *)model.meshes.ToPointer(); BoundingBox bounds = MeshBoundingBox(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 SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode bool selected = false; // Selected object flag 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(ref camera); // Load new models/textures on dragref if (IsFileDropped()) { int count = 0; string[] droppedFiles = Utils.MarshalDroppedFiles(ref count); if (count == 1) // Only support one file dropped { if (IsFileExtension(droppedFiles[0], ".obj") || IsFileExtension(droppedFiles[0], ".gltf") || IsFileExtension(droppedFiles[0], ".iqm")) // Model file formats supported { UnloadModel(model); // Unload previous model model = LoadModel(droppedFiles[0]); // Load new model // Set current map diffuse texture Utils.SetMaterialTexture(ref model, 0, MAP_ALBEDO, ref texture); meshes = (Mesh *)model.meshes.ToPointer(); bounds = MeshBoundingBox(meshes[0]); // TODO: Move camera position from target enough distance to visualize model properly } else if (IsFileExtension(droppedFiles[0], ".png")) // Texture file formats supported { // Unload current model texture and load new one UnloadTexture(texture); texture = LoadTexture(droppedFiles[0]); Utils.SetMaterialTexture(ref model, 0, MAP_ALBEDO, ref texture); } } ClearDroppedFiles(); // Clear internal buffers } // Select model on mouse click if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) { // Check collision between ray and box if (CheckCollisionRayBox(GetMouseRay(GetMousePosition(), camera), bounds)) { 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); }
public unsafe static int Main() { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; SetConfigFlags(ConfigFlag.FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available) InitWindow(screenWidth, screenHeight, "raylib [shaders] example - postprocessing shader"); // Define the camera to look into our 3d world Camera3D camera = new Camera3D(new Vector3(2.0f, 3.0f, 2.0f), new Vector3(0.0f, 1.0f, 0.0f), new Vector3(0.0f, 1.0f, 0.0f), 45.0f, 0); Model model = LoadModel("resources/models/church.obj"); // Load OBJ model Texture2D texture = LoadTexture("resources/models/church_diffuse.png"); // Load model texture (diffuse map) // Set model diffuse texture Utils.SetMaterialTexture(ref model, 0, MAP_ALBEDO, ref texture); Vector3 position = new Vector3(0.0f, 0.0f, 0.0f); // Set model position // Load all postpro shaders // NOTE 1: All postpro shader use the base vertex shader (DEFAULT_VERTEX_SHADER) // NOTE 2: We load the correct shader depending on GLSL version Shader[] shaders = new Shader[MAX_POSTPRO_SHADERS]; // NOTE: Defining null (NULL) for vertex shader forces usage of internal default vertex shader string shaderPath = "resources/shaders/glsl330"; shaders[(int)PostproShader.FX_GRAYSCALE] = LoadShader(null, $"{shaderPath}/grayscale.fs"); shaders[(int)PostproShader.FX_POSTERIZATION] = LoadShader(null, $"{shaderPath}/posterization.fs"); shaders[(int)PostproShader.FX_DREAM_VISION] = LoadShader(null, $"{shaderPath}/dream_vision.fs"); shaders[(int)PostproShader.FX_PIXELIZER] = LoadShader(null, $"{shaderPath}/pixelizer.fs"); shaders[(int)PostproShader.FX_CROSS_HATCHING] = LoadShader(null, $"{shaderPath}/cross_hatching.fs"); shaders[(int)PostproShader.FX_CROSS_STITCHING] = LoadShader(null, $"{shaderPath}/cross_stitching.fs"); shaders[(int)PostproShader.FX_PREDATOR_VIEW] = LoadShader(null, $"{shaderPath}/predator.fs"); shaders[(int)PostproShader.FX_SCANLINES] = LoadShader(null, $"{shaderPath}/scanlines.fs"); shaders[(int)PostproShader.FX_FISHEYE] = LoadShader(null, $"{shaderPath}/fisheye.fs"); shaders[(int)PostproShader.FX_SOBEL] = LoadShader(null, $"{shaderPath}/sobel.fs"); shaders[(int)PostproShader.FX_BLOOM] = LoadShader(null, $"{shaderPath}/bloom.fs"); shaders[(int)PostproShader.FX_BLUR] = LoadShader(null, $"{shaderPath}/blur.fs"); int currentShader = (int)PostproShader.FX_GRAYSCALE; // Create a RenderTexture2D to be used for render to texture RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight); // Setup orbital camera SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital 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(ref camera); // Update camera if (IsKeyPressed(KEY_RIGHT)) { currentShader++; } else if (IsKeyPressed(KEY_LEFT)) { currentShader--; } if (currentShader >= MAX_POSTPRO_SHADERS) { currentShader = 0; } else if (currentShader < 0) { currentShader = MAX_POSTPRO_SHADERS - 1; } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); BeginTextureMode(target); // Enable drawing to texture ClearBackground(RAYWHITE); BeginMode3D(camera); DrawModel(model, position, 0.1f, WHITE); // Draw 3d model with texture DrawGrid(10, 1.0f); // Draw a grid EndMode3D(); EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) // Render previously generated texture using selected postpro shader BeginShaderMode(shaders[currentShader]); // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom) DrawTextureRec(target.texture, new Rectangle(0, 0, target.texture.width, -target.texture.height), new Vector2(0, 0), WHITE); EndShaderMode(); DrawRectangle(0, 9, 580, 30, ColorAlpha(LIGHTGRAY, 0.7f)); DrawText("(c) Church 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY); DrawText("CURRENT POSTPRO SHADER:", 10, 15, 20, BLACK); DrawText(postproShaderText[currentShader], 330, 15, 20, RED); DrawText("< >", 540, 10, 30, DARKBLUE); DrawFPS(700, 15); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- // Unload all postpro shaders for (int i = 0; i < MAX_POSTPRO_SHADERS; i++) { UnloadShader(shaders[i]); } UnloadTexture(texture); // Unload texture UnloadModel(model); // Unload model UnloadRenderTexture(target); // Unload render texture CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return(0); }
public unsafe static int Main() { // Initialization //-------------------------------------------------------------------------------------- const int screenWidth = 800; const int screenHeight = 450; SetConfigFlags(ConfigFlag.FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available) InitWindow(screenWidth, screenHeight, "raylib [shaders] example - custom uniform variable"); // Define the camera to look into our 3d world Camera3D camera = new Camera3D(); camera.position = new Vector3(8.0f, 8.0f, 8.0f); camera.target = new Vector3(0.0f, 1.5f, 0.0f); camera.up = new Vector3(0.0f, 1.0f, 0.0f); camera.fovy = 45.0f; camera.type = CAMERA_PERSPECTIVE; Model model = LoadModel("resources/models/barracks.obj"); // Load OBJ model Texture2D texture = LoadTexture("resources/models/barracks_diffuse.png"); // Load model texture (diffuse map) // Set model diffuse texture Utils.SetMaterialTexture(ref model, 0, MAP_ALBEDO, ref texture); Vector3 position = new Vector3(0.0f, 0.0f, 0.0f); // Set model position Shader shader = LoadShader("resources/shaders/glsl330/base.vs", "resources/shaders/glsl330/swirl.fs"); // Load postpro shader // Get variable (uniform) location on the shader to connect with the program // NOTE: If uniform variable could not be found in the shader, function returns -1 int swirlCenterLoc = GetShaderLocation(shader, "center"); float[] swirlCenter = new float[2] { (float)screenWidth / 2, (float)screenHeight / 2 }; // Create a RenderTexture2D to be used for render to texture RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight); // Setup orbital camera SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital 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 //---------------------------------------------------------------------------------- Vector2 mousePosition = GetMousePosition(); swirlCenter[0] = mousePosition.X; swirlCenter[1] = screenHeight - mousePosition.Y; // Send new value to the shader to be used on drawing IntPtr value = Marshal.UnsafeAddrOfPinnedArrayElement(swirlCenter, 0); SetShaderValue(shader, swirlCenterLoc, value, ShaderUniformDataType.UNIFORM_VEC2); UpdateCamera(ref camera); // Update camera //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); BeginTextureMode(target); // Enable drawing to texture ClearBackground(RAYWHITE); BeginMode3D(camera); DrawModel(model, position, 0.5f, WHITE); // Draw 3d model with texture DrawGrid(10, 1.0f); // Draw a grid EndMode3D(); DrawText("TEXT DRAWN IN RENDER TEXTURE", 200, 10, 30, RED); EndTextureMode(); // End drawing to texture (now we have a texture available for next passes) BeginShaderMode(shader); // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom) DrawTextureRec(target.texture, new Rectangle(0, 0, target.texture.width, -target.texture.height), new Vector2(0, 0), WHITE); EndShaderMode(); DrawText("(c) Barracks 3D model by Alberto Cano", screenWidth - 220, screenHeight - 20, 10, GRAY); DrawFPS(10, 10); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadShader(shader); // Unload shader UnloadTexture(texture); // Unload texture UnloadModel(model); // Unload model UnloadRenderTexture(target); // Unload render texture CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return(0); }
public static int Main() { // 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 Camera3D camera = new Camera3D(new Vector3(1.0f, 1.0f, 1.0f), new Vector3(4.0f, 1.0f, 4.0f), new Vector3(0.0f, 1.0f, 0.0f), 45.0f, CAMERA_PERSPECTIVE); // Load skybox model Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f); Model skybox = LoadModelFromMesh(cube); // Load skybox shader and set required locations // NOTE: Some locations are automatically set at shader loading Shader shader = LoadShader("resources/shaders/glsl330/skybox.vs", "resources/shaders/glsl330/skybox.fs"); Utils.SetMaterialShader(ref skybox, 0, ref shader); Utils.SetShaderValue(shader, GetShaderLocation(shader, "environmentMap"), new int[] { (int)MAP_CUBEMAP }, UNIFORM_INT); Utils.SetShaderValue(shader, GetShaderLocation(shader, "vflipped"), new int[] { 1 }, UNIFORM_INT); // Load cubemap shader and setup required shader locations Shader shdrCubemap = LoadShader("resources/shaders/glsl330/cubemap.vs", "resources/shaders/glsl330/cubemap.fs"); Utils.SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), new int[] { 0 }, UNIFORM_INT); // Load HDR panorama (sphere) texture string panoFileName = "resources/dresden_square_2k.hdr"; Texture2D panorama = LoadTexture(panoFileName); // 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 Texture2D cubemap = GenTextureCubemap(shdrCubemap, panorama, 1024, PixelFormat.UNCOMPRESSED_R8G8B8A8); Utils.SetMaterialTexture(ref skybox, 0, MAP_CUBEMAP, ref cubemap); UnloadTexture(panorama); // Texture not required anymore, cubemap already generated 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(ref camera); // Update camera // Load new cubemap texture on drag&drop if (IsFileDropped()) { int count = 0; string[] droppedFiles = Utils.MarshalDroppedFiles(ref count); // Only support one file dropped if (count == 1) { if (IsFileExtension(droppedFiles[0], ".png;.jpg;.hdr;.bmp;.tga")) { // Unload current cubemap texture and load new one UnloadTexture(Utils.GetMaterialTexture(ref skybox, 0, MAP_CUBEMAP)); panorama = LoadTexture(droppedFiles[0]); panoFileName = droppedFiles[0]; // Generate cubemap from panorama texture cubemap = GenTextureCubemap(shdrCubemap, panorama, 1024, PixelFormat.UNCOMPRESSED_R8G8B8A8); Utils.SetMaterialTexture(ref skybox, 0, MAP_CUBEMAP, ref cubemap); UnloadTexture(panorama); } } // Clear internal buffers ClearDroppedFiles(); } //---------------------------------------------------------------------------------- // Draw //---------------------------------------------------------------------------------- BeginDrawing(); ClearBackground(RAYWHITE); BeginMode3D(camera); DrawModel(skybox, new Vector3(0, 0, 0), 1.0f, WHITE); DrawGrid(10, 1.0f); EndMode3D(); DrawFPS(10, 10); EndDrawing(); //---------------------------------------------------------------------------------- } // De-Initialization //-------------------------------------------------------------------------------------- UnloadModel(skybox); // Unload skybox model (and textures) CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return(0); }