stackgl / headless-gl

🎃 Windowless WebGL for node.js
1.73k stars 166 forks source link

gl function returns null #252

Closed smiilliin closed 1 year ago

smiilliin commented 1 year ago

I wrote the following example code on node v18.15.0 on debian 11 linux. I also have installed all system dependencies

sudo apt-get install -y build-essential libxi-dev libglu1-mesa-dev libglew-dev pkg-config
// Create context
var width = 64;
var height = 64;
var gl = require("gl")(width, height, { preserveDrawingBuffer: true });

//Clear screen to red
gl.clearColor(1, 0, 0, 1);
gl.clear(gl.COLOR_BUFFER_BIT);

//Write output as a PPM formatted image
var pixels = new Uint8Array(width * height * 4);
gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
process.stdout.write(["P3\n# gl.ppm\n", width, " ", height, "\n255\n"].join(""));

for (var i = 0; i < pixels.length; i += 4) {
  for (var j = 0; j < 3; ++j) {
    process.stdout.write(pixels[i + j] + " ");
  }
}

but it occurs error because gl is null I tried this code on windows 11 and it works

# gl.ppm
64 64
255
255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 0 255 0 
[...]

why it doesn't work on linux environment?

dhritzkiv commented 1 year ago

Have you made sure to start xvfb? Usually something like

xvfb-run -s "-ac -screen 0 64x64x24" <node program>

or starting xfvb in the background (e.g. with systemd, etc.):

export DISPLAY=:99.0

/usr/bin/Xvfb :99 -nolisten tcp -fbdir /tmp -screen 0 64x64x24 # start in background somehow

<node program>

(these specific commands vary per distribution)