Sebastien Mondet avatar Sebastien Mondet committed e41fa11

webpdb: add colors for different atoms

Comments (0)

Files changed (1)

   end
 
   type scene_object =
-    | Sphere of (float array * float) (* position, scale *)
+    | Sphere of (float array * float * float array) (* position, scale, color *)
 
   type scene = scene_object list
 
-  let sphere ~position ~scale = Sphere (position, scale)
+  let white = [| 1.; 1.; 1. |]
+  let sphere ?(color=white) ~position ~scale () = Sphere (position, scale, color)
 
   type renderer = {
     gl: WebGL.renderingContext Js.t;
   let load_scene ~renderer scene =
     let arrays =
       List.map scene ~f:(function
-        | Sphere (position, scale) ->
+        | Sphere (position, scale, color) ->
           let scaled_and_translated =
             Array.mapi Data.sphere_vertices ~f:(fun i coord ->
                 let kind_of_coord = i mod 3 in
                 (coord *. scale) +. position.(kind_of_coord)) in
           Lwt_js.yield () |> Lwt.ignore_result;
-          scaled_and_translated, Data.sphere_vertices)
+          let color_array =
+            Array.mapi Data.sphere_vertices ~f:(fun i _ ->
+                let kind_of_coord = i mod 3 in
+                color.(kind_of_coord)) in
+          scaled_and_translated, Data.sphere_vertices, color_array)
     in
     debug "arrays crated";
-    let pos = float32array_concat_map arrays ~f:fst in
+    let pos = float32array_concat_map arrays ~f:(fun (x, _, _) -> x) in
     debug "pos crated";
-    let norm = float32array_concat_map arrays  ~f:snd in
-    debug "pos, norm crated";
+    let norm = float32array_concat_map arrays  ~f:(fun (_, x, _) -> x) in
+    let color = float32array_concat_map arrays  ~f:(fun (_, _, x) -> x) in
+
     let gl = renderer.gl in
     let prog = renderer.prog 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##bufferData(gl##_ARRAY_BUFFER_,color,gl##_STATIC_DRAW_);
     gl##vertexAttribPointer(color_attr, 3, gl##_FLOAT, _false, 0, 0);
 
     renderer.render <- (fun () ->
       aabb_min.(0) aabb_min.(1) aabb_min.(2)
       aabb_max.(0) aabb_max.(1) aabb_max.(2) max_coord;
     let scale = 0.2 in
+    let red = [| 1.; 0.; 0. |] in
     (
-      Renderer.sphere ~position:[| -1.; -1.; -1. |] ~scale
-      :: Renderer.sphere ~position:[|  1.;  1.;  1. |] ~scale
-      :: Renderer.sphere ~position:[| -1.;  1.;  1. |] ~scale
-      :: Renderer.sphere ~position:[|  1.; -1.;  1. |] ~scale
-      :: Renderer.sphere ~position:[|  1.; -1.; -1. |] ~scale
-      :: Renderer.sphere ~position:[|  1.;  1.; -1. |] ~scale
-      :: Renderer.sphere ~position:[| -1.;  1.; -1. |] ~scale
-      :: Renderer.sphere ~position:[| -1.; -1.;  1. |] ~scale
+         Renderer.sphere ~position:[| -1.; -1.; -1. |] ~scale ~color:red ()
+      :: Renderer.sphere ~position:[|  1.;  1.;  1. |] ~scale ~color:red ()
+      :: Renderer.sphere ~position:[| -1.;  1.;  1. |] ~scale ~color:red ()
+      :: Renderer.sphere ~position:[|  1.; -1.;  1. |] ~scale ~color:red ()
+      :: Renderer.sphere ~position:[|  1.; -1.; -1. |] ~scale ~color:red ()
+      :: Renderer.sphere ~position:[|  1.;  1.; -1. |] ~scale ~color:red ()
+      :: Renderer.sphere ~position:[| -1.;  1.; -1. |] ~scale ~color:red ()
+      :: Renderer.sphere ~position:[| -1.; -1.;  1. |] ~scale ~color:red ()
       :: List.map molecule ~f:(fun (el, position) ->
           Lwt_js.yield () |> Lwt.ignore_result;
           let scale =
             (try (float_of_int (int_of_char el.[0])) /. 3000. with _ -> 1.) in
+          let color =
+            match try el.[0] with _ -> 'A' with
+            | 'A' -> [| 1.; 1.; 0.; |]
+            | 'C' -> [| 0.; 0.; 0.; |]
+            | 'N' -> [| 0.; 0.; 1.; |]
+            | 'O' -> [| 1.; 0.; 0.; |]
+            | 'P' -> [| 0.; 1.; 0.; |]
+            | 'S' -> [| 0.; 1.; 1.; |]
+            | _ -> debug "white! %S" el; [| 1.; 1.; 1.; |] in
           let position = [|
             ((position.(0) -. aabb_min.(0)) /. max_coord) -. 0.5;
             ((position.(1) -. aabb_min.(1)) /. max_coord) -. 0.5;
             ((position.(2) -. aabb_min.(2)) /. max_coord) -. 0.5;
           |] in
-          Renderer.sphere ~position ~scale))
+          Renderer.sphere ~color ~position ~scale ()))
 
 end
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.