GithubHelp home page GithubHelp logo

nikolausrauch / 2d-viewer Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 2.27 MB

A 2D Viewer/Interface Library (OpenGL2) for drawing primitives / ui / plots

License: MIT License

CMake 5.73% C 0.32% C++ 93.94%
2d-render cpp minimal-application opengl viewer

2d-viewer's Introduction

2D Viewer

This repository contains a small viewer library (C++ and OpenGL2) encapsulating window, and input handling, primitive, and graphical user interface (ImGui/ImPlot) rendering.
I wrote it originally for visualizing physically based simulation approaches (mass-spring systems, rigid-bodies, SPH, etc.). Over the years it has been used for teaching purposes, e.g. practical assignments, at my university.

It has the following in-source external dependencies: glfw, glm, imgui, implot, stb_image.

Minimal Example

The Viewer class provides the interface to configure window/render behaviour and to register callbacks for the following events:

  • init: is called after the opengl context is initialized
  • shutdown: is called right before the opengl context is destroyed
  • key: triggered if a key is pressed or released
  • mouseButton: triggered if a mouse button is pressed or released
  • update: called before rendering (once per frame)
  • draw: used to submit render calls (points, lines, quads, triangles, circles, ...)
  • gui: used to draw/respond to user-interface via ImGui and ImPlot
    Viewer viewer;
    viewer.mWindow.title = "Example";
    viewer.mWindow.width = 1280;
    viewer.mWindow.height = 720;
    viewer.mWindow.vsync = true;
    viewer.mWindow.mHDPI = false;

    /* install callback functions for events */
    viewer.onUpdate([](Window& window, double dt){ });
    viewer.onDraw([](Window& window, double dt){ });
    viewer.onGui([](Window& window, double dt){ });
    viewer.onMouseButton([](Window& window, Mouse& mouse, int button, int mod, bool press){ });
    viewer.onKey([](Window& window, Keyboard& keyboard, int key, int mod, bool press){ });
 
    /* start viewer (OpenGL context is created, use drawcommands and loading functions only in callbacks) */
    viewer.run();

See demo/demo.cpp for a detailed overview of the libraries functionalities.

Examples

Assignments for a physically-based simulation course (mass-spring systems, rigid-body simulation, stable fluid, finite element method). viewer_examples

2d-viewer's People

Contributors

nikolausrauch avatar

Watchers

 avatar

2d-viewer's Issues

Memory Corruption on MacOS

In your demo, there's a code passage at

2D-viewer/demo/demo.cpp

Lines 71 to 77 in 9bb5068

viewer.drawCircles(circles.begin(), circles.end(), [](auto& c, auto& pos, auto& radius, auto& color)
{
pos = c.first;
radius = c.second;
color = {0.0, 1.0, 0.0, 1.0};
return true;
});

When executed on a MacBook (with an M1 processor), it leads to the following error:

demo(55959,0x1dc8fd000) malloc: Incorrect checksum for freed object 0x14a608310: probably modified after being freed.
Corrupt value: 0x343bbd2e3f800000
demo(55959,0x1dc8fd000) malloc: *** set a breakpoint in malloc_error_break to debug
[1]    55959 abort      ./bin/demo

It appears that the initialization of the new vector (vertices) in this function is broken on MacOS. Specifically, referring to

std::vector<glm::vec2> vertices(mRender.circleVertices);
for(unsigned int i = 0; i <= mRender.circleVertices; i++)
{
vertices[i] =
{
radius * glm::cos(2.0*glm::pi<float>() * static_cast<float>(i) / mRender.circleVertices),
radius * glm::sin(2.0*glm::pi<float>() * static_cast<float>(i) / mRender.circleVertices)
};
}

There could be two (or more) ways (for now) to fix this:

You could do something like:

#ifndef __APPLE__
std::vector<glm::vec2> vertices(mRender.circleVertices);
for (unsigned int i = 0; i <= mRender.circleVertices; i++) {
    vertices[i] = {
        radius * glm::cos(2.0 * glm::pi<float>() * static_cast<float>(i) / mRender.circleVertices),
        radius * glm::sin(2.0 * glm::pi<float>() * static_cast<float>(i) / mRender.circleVertices)};
}
#endif

...

for (unsigned int i = 0; i <= mRender.circleVertices; i++) {
#ifdef __APPLE__
    glVertex2f(position.x + radius * radius * glm::cos(2.0 * glm::pi<float>() * static_cast<float>(i) / mRender.circleVertices),
                       position.y + radius * radius * glm::sin(2.0 * glm::pi<float>() * static_cast<float>(i) / mRender.circleVertices));
#else
    glVertex2f(position.x + radius * vertices[i].x,
                       position.y + radius * vertices[i].y);
#endif
}

Or, without the #ifdef:

...
for (unsigned int i = 0; i <= mRender.circleVertices; i++) {
    const float angle = 2.0 * glm::pi<float>() * static_cast<float>(i) / mRender.circleVertices;  // For readability purposes
    glVertex2f(position.x + radius * radius * glm::cos(angle),
                       position.y + radius * radius * glm::sin(angle));
}

As you mentioned, you're not exactly sure why this happens, but with these changes, it wouldn't occur anymore. Additionally, you questioned why the vertices array is used in the first place, since, from your understanding, these vertices are only used once.

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.