Skip to content

Instantly share code, notes, and snippets.

@lerno
Created January 24, 2024 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lerno/c8767d1bcd343d5337d91d3ee2bd9b73 to your computer and use it in GitHub Desktop.
Save lerno/c8767d1bcd343d5337d91d3ee2bd9b73 to your computer and use it in GitHub Desktop.
Raylib
module abc;
import raylib;
import std::io;
const NUM_MODELS = 9;
fn void main()
{
// Initialization
//--------------------------------------------------------------------------------------
int screenWidth = 800;
int screenHeight = 450;
raylib::init_window(screenWidth, screenHeight, "raylib [models] example - mesh generation");
// We generate a checked image for texturing
Image checked = raylib::gen_image_checked(2, 2, 1, 1, raylib::RED, raylib::GREEN);
Texture2D texture = raylib::load_texture_from_image(checked);
raylib::unload_image(checked);
Model[NUM_MODELS] models;
models[0] = raylib::load_model_from_mesh(raylib::gen_mesh_plane(2, 2, 4, 3));
models[1] = raylib::load_model_from_mesh(raylib::gen_mesh_cube(2.0f, 1.0f, 2.0f));
models[2] = raylib::load_model_from_mesh(raylib::gen_mesh_sphere(2, 32, 32));
models[3] = raylib::load_model_from_mesh(raylib::gen_mesh_hemi_sphere(2, 16, 16));
models[4] = raylib::load_model_from_mesh(raylib::gen_mesh_cyliner(1, 2, 16));
models[5] = raylib::load_model_from_mesh(raylib::gen_mesh_torus(0.25f, 4.0f, 16, 32));
models[6] = raylib::load_model_from_mesh(raylib::gen_mesh_knot(1.0f, 2.0f, 16, 128));
models[7] = raylib::load_model_from_mesh(raylib::gen_mesh_poly(5, 2.0f));
models[8] = raylib::load_model_from_mesh(gen_mesh_custom());
// 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[MaterialMapIndex.ALBEDO].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;
raylib::set_target_fps(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// Main game loop
while (!raylib::window_should_close()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
raylib::update_camera(&camera, (int)CameraMode.ORBITAL);
if (raylib::is_mouse_button_pressed((int)MouseButton.BUTTON_LEFT))
{
currentModel = (currentModel + 1)%NUM_MODELS; // Cycle between the textures
}
if (raylib::is_key_pressed(keyboard::RIGHT))
{
currentModel++;
if (currentModel >= NUM_MODELS) currentModel = 0;
}
else if (raylib::is_key_pressed(keyboard::LEFT))
{
currentModel--;
if (currentModel < 0) currentModel = NUM_MODELS - 1;
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
raylib::begin_drawing();
raylib::clear_background(raylib::RAYWHITE);
raylib::begin_mode3d(camera);
raylib::draw_model(models[currentModel], position, 1.0f, raylib::WHITE);
raylib::draw_grid(10, 1.0);
raylib::end_mode3d();
raylib::draw_rectangle(30, 400, 310, 30, raylib::fade(raylib::SKYBLUE, 0.5f));
raylib::draw_rectangle_lines(30, 400, 310, 30, raylib::fade(raylib::DARKBLUE, 0.5f));
raylib::draw_text("MOUSE LEFT BUTTON to CYCLE PROCEDURAL MODELS", 40, 410, 10, raylib::BLUE);
switch(currentModel)
{
case 0: raylib::draw_text("PLANE", 680, 10, 20, raylib::DARKBLUE); break;
case 1: raylib::draw_text("CUBE", 680, 10, 20, raylib::DARKBLUE); break;
case 2: raylib::draw_text("SPHERE", 680, 10, 20, raylib::DARKBLUE); break;
case 3: raylib::draw_text("HEMISPHERE", 640, 10, 20, raylib::DARKBLUE); break;
case 4: raylib::draw_text("CYLINDER", 680, 10, 20, raylib::DARKBLUE); break;
case 5: raylib::draw_text("TORUS", 680, 10, 20, raylib::DARKBLUE); break;
case 6: raylib::draw_text("KNOT", 680, 10, 20, raylib::DARKBLUE); break;
case 7: raylib::draw_text("POLY", 680, 10, 20, raylib::DARKBLUE); break;
case 8: raylib::draw_text("Custom (triangle)", 580, 10, 20, raylib::DARKBLUE); break;
default: break;
}
raylib::end_drawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
raylib::unload_texture(texture); // Unload texture
// Unload models data (GPU VRAM)
for (int i = 0; i < NUM_MODELS; i++) raylib::unload_model(models[i]);
raylib::close_window(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
}
// Generate a simple triangle mesh from code
fn Mesh gen_mesh_custom()
{
Mesh mesh;
mesh.triangle_count = 1;
mesh.vertex_count = mesh.triangle_count*3;
mesh.vertices = (float *)raylib::mem_alloc(mesh.vertex_count*3L*float.sizeof); // 3 vertices, 3 coordinates each (x, y, z)
mesh.texcoords = (float *)raylib::mem_alloc(mesh.vertex_count*2L*float.sizeof); // 3 vertices, 2 coordinates each (x, y)
mesh.normals = (float *)raylib::mem_alloc(mesh.vertex_count*3L*float.sizeof); // 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
raylib::upload_mesh(&mesh, false);
return mesh;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment