1. Sebastien Mondet
  2. webpdb

Commits

Sebastien Mondet  committed d5ededc

webpdb: add colors to the shaders/rendering

To keep this patch very concise we use the normals as colors.

  • Participants
  • Parent commits 14754aa
  • Branches master

Comments (0)

Files changed (2)

File mk

View file
  • Ignore whitespace
 <script id="vertex-shader" type="x-shader/x-vertex">
   attribute vec3 a_position;
   attribute vec3 a_normal;
+  attribute vec3 a_color;
 
   uniform mat4 u_proj;
 
   varying mediump vec3 v_position;
   varying mediump vec3 v_normal;
+  varying mediump vec3 v_color;
 
   void main() {
     vec4 pos = u_proj * vec4(a_position,1);
     vec4 norm = u_proj * vec4(a_normal,1);
     v_position = pos.xyz;
     v_normal = norm.xyz;
+    v_color = a_color;
     gl_Position = pos;
   }
 </script>
   precision mediump float;
   varying vec3 v_position;
   varying vec3 v_normal;
+  varying vec3 v_color;
 
   uniform vec3 u_lightPos;
   uniform vec3 u_ambientLight;
   void main() {
     vec3 lightDirection = normalize(u_lightPos - v_position);
     float lighting = max(dot(normalize(v_normal), lightDirection), 0.);
-    vec3 col = vec3(1,1,1);
-    gl_FragColor = vec4( col * lighting + u_ambientLight, 1);
+    gl_FragColor = vec4( v_color * lighting + u_ambientLight, 1);
   }
 </script>
     <script src="./webpdb.js"></script>

File webpdb.ml

View file
  • Ignore whitespace
     debug "pos, norm crated";
     let gl = renderer.gl in
     let prog = renderer.prog in
+
     let pos_attr = gl##getAttribLocation(prog, string "a_position") in
     gl##enableVertexAttribArray(pos_attr);
-
     let array_buffer = gl##createBuffer() in
     gl##bindBuffer(gl##_ARRAY_BUFFER_,array_buffer);
     gl##bufferData(gl##_ARRAY_BUFFER_,pos,gl##_STATIC_DRAW_);
     gl##vertexAttribPointer(pos_attr, 3, gl##_FLOAT, _false, 0, 0);
+
     let norm_attr = gl##getAttribLocation(prog, string "a_normal") in
     gl##enableVertexAttribArray(norm_attr);
-
     let norm_buffer = gl##createBuffer() in
     gl##bindBuffer(gl##_ARRAY_BUFFER_,norm_buffer);
     gl##bufferData(gl##_ARRAY_BUFFER_,norm,gl##_STATIC_DRAW_);
     gl##vertexAttribPointer(norm_attr, 3, gl##_FLOAT, _false, 0, 0);
 
+    let color_attr = gl##getAttribLocation(prog, string "a_color") in
+    gl##enableVertexAttribArray(color_attr);
+    let color_buffer = gl##createBuffer() in
+    gl##bindBuffer(gl##_ARRAY_BUFFER_,color_buffer);
+    gl##bufferData(gl##_ARRAY_BUFFER_,norm,gl##_STATIC_DRAW_);
+    gl##vertexAttribPointer(color_attr, 3, gl##_FLOAT, _false, 0, 0);
+
     renderer.render <- (fun () ->
         gl##drawArrays(gl##_TRIANGLES, 0, pos##length / 3);
       );