Code Monkey home page Code Monkey logo

ob-glsl's Introduction

GLSL code blocks for Emacs Org-mode

This org-mode extension adds the capability to run GLSL code blocks directly from inside Emacs and immediately displays the rendering results inline in your org-mode buffers.

Requirements

  • SDL2 (for creating OpenGL context)
  • SDL2_image (for saving PNG files)
  • glbinding (for OpenGL SDK)

The rendering is done with OpenGL 3.3 so should work on any graphcs card.

Building

  1. Generate project with CMake:
    cmake -G "Ninja" .
        
  2. Build the code:
    ninja
        

This will create the Emacs dynamic module ob-glsl-module.so (Linux) or ob-glsl-module.dll (Windows) or ob-glsl-module.dylib (MacOS).

Installing

  1. Put both ob-glsl.el and the dynamic module under your elisp load path.
  2. Add (glsl . t) to org-babel-load-languages. You can either customize the variable or add it manually in lisp code.

Supported parameters

  • :file The output file path (required)
  • :width The render width in pixels
  • :height The render height in pixels

You can omit either :width or :height and the omitted one will be calculated based on the other parameter. If you omit both, then the default output size is 400x300.

A simple pixel shader example

vec3 mandel(vec2 z0) {
    float k = 0.0;
    vec2 z = vec2(0.0);
    for(int i = 0; i < 420; ++i) {
        z = vec2(z.x*z.x-z.y*z.y, z.x*z.y*2.0) + z0;
        if (length(z) > 20.0) break;
        k += 1.0;
    }
    float mu = k + 1.0 - log2(log(length(z)));
    return sin(mu*0.1 + vec3(0.0,0.5,1.0));
}
void main() {
    float ar = iResolution.x / iResolution.y;
    vec2 uv = gl_FragCoord.xy / iResolution.yy - vec2(0.66 * ar, 0.5);
    // uv = uv * 2.0 + vec2(-0.3, 0.0);
    float p = 30.0;
    float t = mod(13.0, p);
    if (t > p/2.0) t = p - t;
    float scale = 0.5 + pow(2.0, t);
    vec2 offset = vec2(-1.36799, .01);
    uv += offset*scale;
    uv /= scale;
    fragColor = vec4(mandel(uv), 1.0);
}

img/mandel.png

ob-glsl's People

Contributors

finalpatch avatar lifengsc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ob-glsl's Issues

""SDL2::SDL2-static" was not found" on Archlinux

CMake Error at CMakeLists.txt:11 (add_library):
  Target "ob-glsl-module" links to target "SDL2::SDL2-static" but the target
  was not found.  Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?

For unknown and mythical reason, on Archlinux, the static lib is named as libSDL2main.a rather than libSDL2.a.
https://github.com/archlinux/svntogit-packages/blob/07aecf0c44807cd19307b693b9768e3557dc0d53/trunk/PKGBUILD#L51

To build on Archlinux, one have to change this line

target_link_libraries(${PROJECT_NAME} SDL2::SDL2-static ${GLBINDING_LIB} ${SDL2_IMAGE_LIB})

to

target_link_libraries(${PROJECT_NAME} SDL2::SDL2main ${GLBINDING_LIB} ${SDL2_IMAGE_LIB})

And the ob-glsl works :D
๐Ÿ‘

build failure "Main.cpp:92:14: error: expected type-specifier"

rob@oven:~/src/ob-glsl$ ninja
[1/2] Building CXX object CMakeFiles/ob-glsl-module.dir/Main.cpp.o
FAILED: CMakeFiles/ob-glsl-module.dir/Main.cpp.o 
/usr/bin/c++  -Dob_glsl_module_EXPORTS -I/usr/local/include -fPIC   -std=gnu++17 -MD -MT CMakeFiles/ob-glsl-module.dir/Main.cpp.o -MF CMakeFiles/ob-glsl-module.dir/Main.cpp.o.d -o CMakeFiles/ob-glsl-module.dir/Main.cpp.o -c Main.cpp
Main.cpp:92:14: error: expected type-specifier
     operator glbinding::GetProcAddress() const {return nullptr;}
              ^~~~~~~~~
ninja: build stopped: subcommand failed.
rob@oven:~/src/ob-glsl$ 

Always get a 1kb image file on M1 MacOS

I successfully built the module and loaded it to emacs by

(add-to-list 'load-path "~/.emacs.d/lisp")

(org-babel-do-load-languages
 'org-babel-load-languages
 '((glsl . t)))

However, I always get no result, the .png file is always an empty 1 Kb image only.

image

When compiling the module, I get this warning:

[3/4] Building CXX object CMakeFiles/ob-glsl-module.dir/Main.cpp.o
Main.cpp:86:40: warning: implicit conversion of nullptr constant to 'bool' [-Wnull-conversion]
        glbinding::Binding::initialize(nullptr);
        ~~~~~~~~~                      ^~~~~~~
                                       false
1 warning generated.
[4/4] Linking CXX shared library ob-glsl-module.dylib

Maybe this is the reason?

I have very limited knowledge both Emacs modules in C or openGL, and I am not sure about how to debug in this situation. Sorry for that.

Also, the glbinding from my package manager is still 2.1.4.

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.