Skip to content Skip to sidebar Skip to footer

WebGL Rendering With Rgl 0.93.935 R Package

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') brow

Solution 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.


Post a Comment for "WebGL Rendering With Rgl 0.93.935 R Package"