Code Monkey home page Code Monkey logo

csg's Introduction

C Scene Graph library

No-sweat, efficient, minimal dependency pure OpenGL C99 Scene Graph (csg) library. Useful for quick visualizations and controls of data, simulations, and more, once all the stuff gets implemented.

Features

Right now implemented features include:

  • Concise, extremely easy-to-use API
  • 3D Affine transformations, cameras, textures, shader abstraction, etc.
  • Multiple windows support with dead-simple controls
  • Smooth sharing of geometries, textures, etc. between windows/contexts
  • Embedded powerful UI (thanks to Nuklear), with no setup required
  • Immediate and relative (cursor delta, up-down transition, etc.) input states and delta-time (time the last frame took to render) available each frame
  • Draw, transforms, etc. may be used straight-away inside any OpenGL context (no need of setup, even the csg_gui adapters are fully optional).
  • Various graph traversal patterns: (DFS, BFS, path-to-root, etc.)
  • Wavefront .OBJ meshes loading

Requirements

The core library depends only on cglm and GL/GLEW. The gui_glfw3 adapter depends on glfw3.

Example

WORK IN PROGRESS. This is how a 'input-update-render' loop may look like with the API:

  csg_gui_adapter_t adapter = csg_gui_adapter_create(
      csg_gui_glfw3_adapter_ops(), 0, 0, 1024, 768, 0, NULL);
      
  // shared geometry
  csg_geometry_t* cube = csg_geometry_create_cube();

  // nodes
  csg_node_t* root = csg_node_create(NULL, NULL);
  for (size_t i = 0; i <= 16; i += 2) {
    csg_node_t* node = csg_node_create(root, NULL);
    node->transform.translation.x = i - 8;
    node->geometry = cube;
    node->geometry->material = csg_material_create();
    node->geometry->material.diffuse_color = (csg_vec4_t){
        1.0f / (rand() % 10), 1.f / (rand() % 10), 1.f / (rand() % 10), 1.0f};
  }

  csg_camera_t camera = csg_camera_default();

  while ((adapter.flags & CSG_GUI_FLAG_WANT_CLOSE) == 0) {
    // INPUTS
    csg_gui_adapter_update(&adapter);

    if (adapter.keyboard[CSG_GUI_KEY_ESCAPE] == CSG_GUI_FROM_RELEASE_TO_PRESS)
      adapter.flags |= CSG_GUI_FLAG_WANT_CLOSE;

    if (adapter.keyboard[CSG_GUI_KEY_W] == CSG_GUI_PRESS) {
      camera.position.z--;
      camera.target.z--;
    }
    if (adapter.keyboard[CSG_GUI_KEY_S] == CSG_GUI_PRESS) {
      camera.position.z++;
      camera.target.z++;
    }

    if (adapter.mouse[CSG_GUI_MOUSE_BUTTON_RIGHT]) {
      if (adapter.mouse_deltax != 0)
        camera.target.x += 0.1f * adapter.mouse_deltax;
      if (adapter.mouse_deltay != 0)
        camera.target.y -= 0.1f * adapter.mouse_deltay;
    }
    if (adapter.mouse[CSG_GUI_MOUSE_BUTTON_LEFT]) {
      if (adapter.mouse_deltax != 0)
        root->transform.rotation.y += 0.01f * adapter.mouse_deltax;
      if (adapter.mouse_deltay != 0)
        root->transform.rotation.x += 0.01f * adapter.mouse_deltay;
    }

    // RENDER
    csg_gui_adapter_begin_frame(&adapter);
    {
      // scene
      csg_render(root, camera, (csg_vec4_t){0.2f, 0.2f, 0.3f, 1.0f});
      
      // ui
      if (nk_begin(adapter.nk, "Model transform", nk_rect(0, 0, 320, 240),
                   NK_WINDOW_NO_SCROLLBAR | NK_WINDOW_TITLE |
                       NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE |
                       NK_WINDOW_MINIMIZABLE)) {
        nk_layout_row_dynamic(adapter.nk, 25, 1);
        nk_property_float(adapter.nk, "Rotation along X", -2.0f * M_PI,
                          &root->transform.rotation.x, 2.0f * M_PI, 0.10f,
                          0.01f);
        nk_property_float(adapter.nk, "Rotation along Y", -2.0f * M_PI,
                          &root->transform.rotation.y, 2.0f * M_PI, 0.10f,
                          0.01f);
        nk_property_float(adapter.nk, "Rotation along Z", -2.0f * M_PI,
                          &root->transform.rotation.z, 2.0f * M_PI, 0.10f,
                          0.01f);
      }
      nk_end(adapter.nk);
    }
    csg_gui_adapter_end_frame(&adapter);
  }

NOTE

The library is in the process of active design and development. Only a fraction of planned functionality is supported for now, but the code is already proved to be useful in some scenarios.

window sharing window sharing wavefront models

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.