1

The following R code generates an HTML file and opens it in the browser:

library(rgl)
M <- rbind(
  c(0,0,0),
  c(-1,4,0),
  c(4,9,0),
  c(6,3,0)
  )
  quads3d(M,col='red')
browseURL(paste("file://", writeWebGL(dir=file.path(tempdir(), "webGL"), 
          width=500), sep=""))

The rendering is an interactive planar polyhedron in the 3D space.

With the latest version of the rgl package (0.93.935), the HTML rendering does not work for Windows users (as well as iOS users, I think) with default configuration browser. With the older version 0.93.928, it works.

I have posted the html output of rgl 0.93.928 and the html output of rgl 0.93.935.

I have reported this issue to Duncan Murdoch (author of rgl) and he has given me the following solution for Firefox: type and run "about: config" in the address bar, and turn the parameters webgl.prefer-native-gl and webgl.force-enabled to true. Then the HTML rendering works.

My questions:

  • How to do with Google Chrome?

  • Is it possible to change something in the HTML code in order that the HTML rendering works with the default configuration? (as for the 0.93.928 version).

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • As the only differences in the output are the shaders, I'd suggest try copying the shadercode from the old version to the new one. Other than that it's close to impossible to debug this problem with so much code so deeply nested inside a html page. – havarc Jul 03 '13 at 15:50
  • Hello @havarc. Do you mean the part of the code from line 12 to line 78 for first version and from line 12 to line 80 for the second version ? – Stéphane Laurent Jul 03 '13 at 15:56
  • Well that's the problem, I don't see much to begin with. Only thing I could do was run your sample pages through the Firebug html inspector since everything is inserted via javascript inside more javascript. To really debug this I would need the page in plain HTML & JS. – havarc Jul 03 '13 at 16:06
  • What exactly do you want to show on the site? Maybe you're better on by creating a webpage yourself that requests plain data from your program and manages output via js or some library like [three.js](http://threejs.org/) or [D3](http://d3js.org/). – havarc Jul 05 '13 at 07:53
  • @havarc This is for my blog http://stla.overblog.com/using-r-to-compute-the-kantorovich-distance . I'm just a R user, I am ignorant about html. Then I generate a html file with R, and I copy/paste the code. I don't want to spend some time to learn html (I'm very busy ;-) – Stéphane Laurent Jul 05 '13 at 09:06

1 Answers1

1

As difficult as the getting to the problem was as easy is the solution to it.

As of latest version of rgl I could acquire the problem resides inside the fragment shader of the html output:

varying vec4 vCol; // carries alpha
varying vec4 vPosition;
varying vec3 vNormal;

vec3 eye = normalize(-vPosition.xyz);
const vec3 emission = vec3(0., 0., 0.);
const vec3 ambient1 = vec3(0., 0., 0.);
const vec3 specular1 = vec3(1., 1., 1.);// light*material
const float shininess1 = 50.;
vec4 colDiff1 = vec4(vCol.rgb * vec3(1., 1., 1.), vCol.a);
const vec3 lightDir1 = vec3(0., 0., 1.);
vec3 halfVec1 = normalize(lightDir1 + eye);

void main(void) {
    vec4 lighteffect = vec4(emission, 0.);
    vec3 n = normalize(vNormal);
    n = -faceforward(n, n, eye);
    vec3 col1 = ambient1;
    float nDotL1 = dot(n, lightDir1);
    col1 = col1 + max(nDotL1, 0.) * colDiff1.rgb;
    col1 = col1 + pow(max(dot(halfVec1, n), 0.), shininess1) * specular1;
    lighteffect = lighteffect + vec4(col1, colDiff1.a);
    gl_FragColor = lighteffect;
}

it defines variables outside on the main function which skips value assignments and thus fails to compile with division-by-zero failures. The solution is to move the start of the main function above the definition block directly after the varying values:

varying vec4 vCol; // carries alpha
varying vec4 vPosition;
varying vec3 vNormal;

void main(void) {
    vec3 eye = normalize(-vPosition.xyz);
    const vec3 emission = vec3(0., 0., 0.);
    const vec3 ambient1 = vec3(0., 0., 0.);
    const vec3 specular1 = vec3(1., 1., 1.);// light*material
    const float shininess1 = 50.;
    vec4 colDiff1 = vec4(vCol.rgb * vec3(1., 1., 1.), vCol.a);
    const vec3 lightDir1 = vec3(0., 0., 1.);
    vec3 halfVec1 = normalize(lightDir1 + eye);

    vec4 lighteffect = vec4(emission, 0.);
    vec3 n = normalize(vNormal);
    n = -faceforward(n, n, eye);
    vec3 col1 = ambient1;
    float nDotL1 = dot(n, lightDir1);
    col1 = col1 + max(nDotL1, 0.) * colDiff1.rgb;
    col1 = col1 + pow(max(dot(halfVec1, n), 0.), shininess1) * specular1;
    lighteffect = lighteffect + vec4(col1, colDiff1.a);
    gl_FragColor = lighteffect;
}

This will show the object as desired.

You may want to contact Duncan Murdoch again and send him a link to this post if as you said you're not versed in html and webgl.

havarc
  • 934
  • 5
  • 11
  • It works !! http://pagist.github.io/?5936018 Thank you. Also, I have sent an email to Duncan Murdoch and he has asked me to acknowledge you ! – Stéphane Laurent Jul 05 '13 at 17:35