{"action":"created","comment":{"url":"https://api.github.com/repos/vispy/vispy/pulls/comments/26182744","id":26182744,"diff_hunk":"@@ -0,0 +1,216 @@\n+#!/usr/bin/env python\n+import numpy as np\n+\n+from vispy.util.transforms import perspective\n+from vispy.util import transforms\n+from vispy import gloo\n+from vispy import app\n+from vispy import io\n+\n+import galaxy_specrend\n+from galaxy_simulation import Galaxy\n+\n+VERT_SHADER = \"\"\"\n+#version 120\n+uniform mat4  u_model;\n+uniform mat4  u_view;\n+uniform mat4  u_projection;\n+\n+//sampler that maps [0, n] -> color according to blackbody law\n+uniform sampler1D u_colormap;\n+//index to sample the colormap at\n+attribute float a_color_index;\n+\n+//size of the star\n+attribute float a_size;\n+//type\n+//type 0 - stars\n+//type 1 - dust\n+//type 2 - h2a objects\n+//type 3 - h2a objects\n+attribute float a_type;\n+attribute vec2  a_position;\n+//brightness of the star\n+attribute float a_brightness;\n+\n+varying vec3 v_color;\n+void main (void)\n+{\n+    gl_Position = u_projection * u_view * u_model * vec4(a_position, 0.0,1.0);\n+\n+    //find base color according to physics from our sampler\n+    vec3 base_color = texture1D(u_colormap, a_color_index).rgb;\n+    //scale it down according to brightness\n+    v_color = base_color * a_brightness;\n+\n+\n+    if (a_size > 2.0)\n+    {\n+        gl_PointSize = a_size;\n+    } else {\n+        gl_PointSize = 0.0;\n+    }\n+\n+    if (a_type == 2) {\n+        v_color *= vec3(2, 1, 1);\n+    }\n+    else if (a_type == 3) {\n+        v_color = vec3(.9);\n+    }\n+}\n+\"\"\"\n+\n+FRAG_SHADER = \"\"\"\n+#version 120\n+//star texture\n+uniform sampler2D u_texture;\n+//predicted color from black body\n+varying vec3 v_color;\n+\n+void main()\n+{\n+    //amount of intensity from the grayscale star\n+    float star_tex_intensity = texture2D(u_texture, gl_PointCoord).r;\n+    gl_FragColor = vec4(v_color * star_tex_intensity, 0.8);\n+}\n+\"\"\"\n+\n+galaxy = Galaxy(10000)\n+galaxy.reset(13000, 4000, 0.0004, 0.90, 0.90, 0.5, 200, 300)\n+# coldest and hottest temperatures of out galaxy\n+t0, t1 = 200.0, 10000.0\n+# total number of discrete colors between t0 and t1\n+n = 1000\n+dt = (t1 - t0) / n\n+\n+# maps [0, n) -> colors\n+# generate a linear interpolation of temperatures\n+# then map the temperatures to colors using black body\n+# color predictions\n+colors = np.zeros(n, dtype=(np.float32, 3))\n+for i in range(n):\n+    temperature = t0 + i * dt\n+    x, y, z = galaxy_specrend.spectrum_to_xyz(galaxy_specrend.bb_spectrum,\n+                                              temperature)\n+    r, g, b = galaxy_specrend.xyz_to_rgb(galaxy_specrend.SMPTEsystem, x, y, z)\n+    r = min((max(r, 0), 1))\n+    g = min((max(g, 0), 1))\n+    b = min((max(b, 0), 1))\n+    colors[i] = galaxy_specrend.norm_rgb(r, g, b)\n+\n+\n+# load the PNG that we use to blend the star with\n+# to provide a circular look to each star.\n+def load_galaxy_star_image():\n+    from vispy.io import load_data_file\n+\n+    fname = load_data_file('galaxy/star-particle.png')\n+    raw_image = io.read_png(fname)\n+\n+    return raw_image\n+\n+\n+class Canvas(app.Canvas):\n+\n+    def __init__(self):\n+        # setup initial width, height\n+        self.width = 800\n+        self.height = 600\n+        app.Canvas.__init__(self, keys='interactive')\n+        self.size = self.width, self.height\n+\n+        self._timer = app.Timer('auto', connect=self.update, start=True)\n+\n+    # create the numpy array corresponding to the shader data\n+    def __create_galaxy_vertex_data(self):\n+        data = np.zeros(len(galaxy),\n+                        dtype=[('a_size', np.float32, 1),\n+                               ('a_position', np.float32, 2),\n+                               ('a_color_index', np.float32, 1),\n+                               ('a_brightness', np.float32, 1),\n+                               ('a_type', np.float32, 1)])\n+\n+        # see shader for parameter explanations\n+        data['a_size'] = galaxy['size'] * max(self.width / 800.0,\n+                                              self.height / 800.0)\n+        data['a_position'] = galaxy['position'] / 13000.0\n+\n+        data['a_color_index'] = (galaxy['temperature'] - t0) / (t1 - t0)\n+        data['a_brightness'] = galaxy['brightness']\n+        data['a_type'] = galaxy['type']\n+\n+        return data\n+\n+    def on_initialize(self, event):\n+        # create a new shader program\n+        self.program = gloo.Program(VERT_SHADER, FRAG_SHADER,\n+                                    count=len(galaxy))\n+\n+        # load the star texture\n+        self.texture = gloo.Texture2D(load_galaxy_star_image(),\n+                                      interpolation='linear')\n+        self.program['u_texture'] = self.texture\n+\n+        # construct the model, view and projection matrices\n+        self.view = np.eye(4, dtype=np.float32)\n+        transforms.translate(self.view, 0, 0, -5)\n+        self.program['u_view'] = self.view\n+\n+        self.model = np.eye(4, dtype=np.float32)\n+        self.program['u_model'] = self.model\n+\n+        self.program['u_colormap'] = colors\n+\n+        self.projection = perspective(45.0, self.width / float(self.height),\n+                                      1.0, 1000.0)\n+        self.program['u_projection'] = self.projection\n+\n+        # start the galaxy to some decent point in the future\n+        galaxy.update(100000)\n+        data = self.__create_galaxy_vertex_data()\n+\n+        # setup the VBO once the galaxy vertex data has been setup\n+        # bind the VBO for the first time\n+        self.data_vbo = gloo.VertexBuffer(data)\n+        self.program.bind(self.data_vbo)\n+\n+        # setup blending\n+        self.__setup_blending_mode()\n+\n+    def __setup_blending_mode(self):\n+        # set the clear color to almost black\n+        gloo.gl.glClearColor(0.0, 0.0, 0.03, 1.0)\n+        gloo.gl.glDisable(gloo.gl.GL_DEPTH_TEST)\n+        # use additive blend mode so that the\n+        # particles overlap and intensify\n+        gloo.gl.glEnable(gloo.gl.GL_BLEND)\n+        gloo.gl.glBlendFunc(gloo.gl.GL_SRC_ALPHA, gloo.gl.GL_ONE)\n+\n+    def on_resize(self, event):\n+        self.width, self.height = event.size\n+        # setup the new viewport\n+        gloo.set_viewport(0, 0, self.width, self.height)\n+        # recompute the projection matrix\n+        self.projection = perspective(45.0, self.width / float(self.height),\n+                                      1.0, 1000.0)\n+        self.program['u_projection'] = self.projection\n+\n+    def on_draw(self, event):\n+        # update the galaxy\n+        galaxy.update(50000)  # in years !\n+\n+        # recreate the numpy array that will be sent as the VBO data\n+        data = self.__create_galaxy_vertex_data()\n+        # update the VBO\n+        self.data_vbo.set_data(data)\n+        # bind the VBO to the GL context\n+        self.program.bind(self.data_vbo)\n+\n+        # clear the screen and render\n+        gloo.clear(color=True, depth=True)\n+        self.program.draw('points')\n+\n+if __name__ == '__main__':\n+    c = Canvas()\n+    c.show()\n+    app.run()","path":"examples/demo/gloo/galaxy/galaxy.py","position":216,"original_position":216,"commit_id":"dea9e5df962380fe29f2ec52fe573afd31d0c697","original_commit_id":"dea9e5df962380fe29f2ec52fe573afd31d0c697","user":{"login":"Eric89GXL","id":2365790,"avatar_url":"https://avatars.githubusercontent.com/u/2365790?v=3","gravatar_id":"","url":"https://api.github.com/users/Eric89GXL","html_url":"https://github.com/Eric89GXL","followers_url":"https://api.github.com/users/Eric89GXL/followers","following_url":"https://api.github.com/users/Eric89GXL/following{/other_user}","gists_url":"https://api.github.com/users/Eric89GXL/gists{/gist_id}","starred_url":"https://api.github.com/users/Eric89GXL/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Eric89GXL/subscriptions","organizations_url":"https://api.github.com/users/Eric89GXL/orgs","repos_url":"https://api.github.com/users/Eric89GXL/repos","events_url":"https://api.github.com/users/Eric89GXL/events{/privacy}","received_events_url":"https://api.github.com/users/Eric89GXL/received_events","type":"User","site_admin":false},"body":"We usually protect this with a `sys.interactive` check, see other examples","created_at":"2015-03-11T01:24:10Z","updated_at":"2015-03-11T01:24:10Z","html_url":"https://github.com/vispy/vispy/pull/738#discussion_r26182744","pull_request_url":"https://api.github.com/repos/vispy/vispy/pulls/738","_links":{"self":{"href":"https://api.github.com/repos/vispy/vispy/pulls/comments/26182744"},"html":{"href":"https://github.com/vispy/vispy/pull/738#discussion_r26182744"},"pull_request":{"href":"https://api.github.com/repos/vispy/vispy/pulls/738"}}},"pull_request":{"url":"https://api.github.com/repos/vispy/vispy/pulls/738","id":30711000,"html_url":"https://github.com/vispy/vispy/pull/738","diff_url":"https://github.com/vispy/vispy/pull/738.diff","patch_url":"https://github.com/vispy/vispy/pull/738.patch","issue_url":"https://api.github.com/repos/vispy/vispy/issues/738","number":738,"state":"open","locked":false,"title":"Galaxy demo","user":{"login":"bollu","id":1694861,"avatar_url":"https://avatars.githubusercontent.com/u/1694861?v=3","gravatar_id":"","url":"https://api.github.com/users/bollu","html_url":"https://github.com/bollu","followers_url":"https://api.github.com/users/bollu/followers","following_url":"https://api.github.com/users/bollu/following{/other_user}","gists_url":"https://api.github.com/users/bollu/gists{/gist_id}","starred_url":"https://api.github.com/users/bollu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bollu/subscriptions","organizations_url":"https://api.github.com/users/bollu/orgs","repos_url":"https://api.github.com/users/bollu/repos","events_url":"https://api.github.com/users/bollu/events{/privacy}","received_events_url":"https://api.github.com/users/bollu/received_events","type":"User","site_admin":false},"body":"Ported the [Galaxy demo from glumpy](https://github.com/glumpy/glumpy/blob/master/examples/galaxy.py) to ```VisPy```\r\n\r\nThis depends on the 1D sampler implemented [in this pull request](https://github.com/vispy/vispy/pull/737). So please make sure to pull that first before merging this example change.\r\n\r\nI've tried to comment the example as meaningfully as possible so that the code is easy to follow.\r\n\r\nImages:\r\n![#1](http://i.imgur.com/LPS2PsO.png)\r\n![#2](http://i.imgur.com/Y6K5uDj.png)\r\n![#3](http://i.imgur.com/9lB9lN6.png)\r\n![#4](http://i.imgur.com/X2qff6l.png)","created_at":"2015-03-07T23:04:00Z","updated_at":"2015-03-11T01:24:10Z","closed_at":null,"merged_at":null,"merge_commit_sha":"7cfc19d769440a993f83ec94a2f958a11938a4c5","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/vispy/vispy/pulls/738/commits","review_comments_url":"https://api.github.com/repos/vispy/vispy/pulls/738/comments","review_comment_url":"https://api.github.com/repos/vispy/vispy/pulls/comments{/number}","comments_url":"https://api.github.com/repos/vispy/vispy/issues/738/comments","statuses_url":"https://api.github.com/repos/vispy/vispy/statuses/dea9e5df962380fe29f2ec52fe573afd31d0c697","head":{"label":"bollu:galaxy-demo","ref":"galaxy-demo","sha":"dea9e5df962380fe29f2ec52fe573afd31d0c697","user":{"login":"bollu","id":1694861,"avatar_url":"https://avatars.githubusercontent.com/u/1694861?v=3","gravatar_id":"","url":"https://api.github.com/users/bollu","html_url":"https://github.com/bollu","followers_url":"https://api.github.com/users/bollu/followers","following_url":"https://api.github.com/users/bollu/following{/other_user}","gists_url":"https://api.github.com/users/bollu/gists{/gist_id}","starred_url":"https://api.github.com/users/bollu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bollu/subscriptions","organizations_url":"https://api.github.com/users/bollu/orgs","repos_url":"https://api.github.com/users/bollu/repos","events_url":"https://api.github.com/users/bollu/events{/privacy}","received_events_url":"https://api.github.com/users/bollu/received_events","type":"User","site_admin":false},"repo":{"id":31825193,"name":"vispy","full_name":"bollu/vispy","owner":{"login":"bollu","id":1694861,"avatar_url":"https://avatars.githubusercontent.com/u/1694861?v=3","gravatar_id":"","url":"https://api.github.com/users/bollu","html_url":"https://github.com/bollu","followers_url":"https://api.github.com/users/bollu/followers","following_url":"https://api.github.com/users/bollu/following{/other_user}","gists_url":"https://api.github.com/users/bollu/gists{/gist_id}","starred_url":"https://api.github.com/users/bollu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bollu/subscriptions","organizations_url":"https://api.github.com/users/bollu/orgs","repos_url":"https://api.github.com/users/bollu/repos","events_url":"https://api.github.com/users/bollu/events{/privacy}","received_events_url":"https://api.github.com/users/bollu/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/bollu/vispy","description":"Main repository for Vispy","fork":true,"url":"https://api.github.com/repos/bollu/vispy","forks_url":"https://api.github.com/repos/bollu/vispy/forks","keys_url":"https://api.github.com/repos/bollu/vispy/keys{/key_id}","collaborators_url":"https://api.github.com/repos/bollu/vispy/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/bollu/vispy/teams","hooks_url":"https://api.github.com/repos/bollu/vispy/hooks","issue_events_url":"https://api.github.com/repos/bollu/vispy/issues/events{/number}","events_url":"https://api.github.com/repos/bollu/vispy/events","assignees_url":"https://api.github.com/repos/bollu/vispy/assignees{/user}","branches_url":"https://api.github.com/repos/bollu/vispy/branches{/branch}","tags_url":"https://api.github.com/repos/bollu/vispy/tags","blobs_url":"https://api.github.com/repos/bollu/vispy/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/bollu/vispy/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/bollu/vispy/git/refs{/sha}","trees_url":"https://api.github.com/repos/bollu/vispy/git/trees{/sha}","statuses_url":"https://api.github.com/repos/bollu/vispy/statuses/{sha}","languages_url":"https://api.github.com/repos/bollu/vispy/languages","stargazers_url":"https://api.github.com/repos/bollu/vispy/stargazers","contributors_url":"https://api.github.com/repos/bollu/vispy/contributors","subscribers_url":"https://api.github.com/repos/bollu/vispy/subscribers","subscription_url":"https://api.github.com/repos/bollu/vispy/subscription","commits_url":"https://api.github.com/repos/bollu/vispy/commits{/sha}","git_commits_url":"https://api.github.com/repos/bollu/vispy/git/commits{/sha}","comments_url":"https://api.github.com/repos/bollu/vispy/comments{/number}","issue_comment_url":"https://api.github.com/repos/bollu/vispy/issues/comments{/number}","contents_url":"https://api.github.com/repos/bollu/vispy/contents/{+path}","compare_url":"https://api.github.com/repos/bollu/vispy/compare/{base}...{head}","merges_url":"https://api.github.com/repos/bollu/vispy/merges","archive_url":"https://api.github.com/repos/bollu/vispy/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/bollu/vispy/downloads","issues_url":"https://api.github.com/repos/bollu/vispy/issues{/number}","pulls_url":"https://api.github.com/repos/bollu/vispy/pulls{/number}","milestones_url":"https://api.github.com/repos/bollu/vispy/milestones{/number}","notifications_url":"https://api.github.com/repos/bollu/vispy/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/bollu/vispy/labels{/name}","releases_url":"https://api.github.com/repos/bollu/vispy/releases{/id}","created_at":"2015-03-07T19:53:28Z","updated_at":"2015-03-07T19:53:30Z","pushed_at":"2015-03-11T00:53:08Z","git_url":"git://github.com/bollu/vispy.git","ssh_url":"git@github.com:bollu/vispy.git","clone_url":"https://github.com/bollu/vispy.git","svn_url":"https://github.com/bollu/vispy","homepage":"http://vispy.org","size":7252,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"vispy:master","ref":"master","sha":"0444b69eaa88fff1a480708f1ffb74e67a38cbab","user":{"login":"vispy","id":3934254,"avatar_url":"https://avatars.githubusercontent.com/u/3934254?v=3","gravatar_id":"","url":"https://api.github.com/users/vispy","html_url":"https://github.com/vispy","followers_url":"https://api.github.com/users/vispy/followers","following_url":"https://api.github.com/users/vispy/following{/other_user}","gists_url":"https://api.github.com/users/vispy/gists{/gist_id}","starred_url":"https://api.github.com/users/vispy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vispy/subscriptions","organizations_url":"https://api.github.com/users/vispy/orgs","repos_url":"https://api.github.com/users/vispy/repos","events_url":"https://api.github.com/users/vispy/events{/privacy}","received_events_url":"https://api.github.com/users/vispy/received_events","type":"Organization","site_admin":false},"repo":{"id":8935948,"name":"vispy","full_name":"vispy/vispy","owner":{"login":"vispy","id":3934254,"avatar_url":"https://avatars.githubusercontent.com/u/3934254?v=3","gravatar_id":"","url":"https://api.github.com/users/vispy","html_url":"https://github.com/vispy","followers_url":"https://api.github.com/users/vispy/followers","following_url":"https://api.github.com/users/vispy/following{/other_user}","gists_url":"https://api.github.com/users/vispy/gists{/gist_id}","starred_url":"https://api.github.com/users/vispy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vispy/subscriptions","organizations_url":"https://api.github.com/users/vispy/orgs","repos_url":"https://api.github.com/users/vispy/repos","events_url":"https://api.github.com/users/vispy/events{/privacy}","received_events_url":"https://api.github.com/users/vispy/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/vispy/vispy","description":"Main repository for Vispy","fork":false,"url":"https://api.github.com/repos/vispy/vispy","forks_url":"https://api.github.com/repos/vispy/vispy/forks","keys_url":"https://api.github.com/repos/vispy/vispy/keys{/key_id}","collaborators_url":"https://api.github.com/repos/vispy/vispy/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/vispy/vispy/teams","hooks_url":"https://api.github.com/repos/vispy/vispy/hooks","issue_events_url":"https://api.github.com/repos/vispy/vispy/issues/events{/number}","events_url":"https://api.github.com/repos/vispy/vispy/events","assignees_url":"https://api.github.com/repos/vispy/vispy/assignees{/user}","branches_url":"https://api.github.com/repos/vispy/vispy/branches{/branch}","tags_url":"https://api.github.com/repos/vispy/vispy/tags","blobs_url":"https://api.github.com/repos/vispy/vispy/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/vispy/vispy/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/vispy/vispy/git/refs{/sha}","trees_url":"https://api.github.com/repos/vispy/vispy/git/trees{/sha}","statuses_url":"https://api.github.com/repos/vispy/vispy/statuses/{sha}","languages_url":"https://api.github.com/repos/vispy/vispy/languages","stargazers_url":"https://api.github.com/repos/vispy/vispy/stargazers","contributors_url":"https://api.github.com/repos/vispy/vispy/contributors","subscribers_url":"https://api.github.com/repos/vispy/vispy/subscribers","subscription_url":"https://api.github.com/repos/vispy/vispy/subscription","commits_url":"https://api.github.com/repos/vispy/vispy/commits{/sha}","git_commits_url":"https://api.github.com/repos/vispy/vispy/git/commits{/sha}","comments_url":"https://api.github.com/repos/vispy/vispy/comments{/number}","issue_comment_url":"https://api.github.com/repos/vispy/vispy/issues/comments{/number}","contents_url":"https://api.github.com/repos/vispy/vispy/contents/{+path}","compare_url":"https://api.github.com/repos/vispy/vispy/compare/{base}...{head}","merges_url":"https://api.github.com/repos/vispy/vispy/merges","archive_url":"https://api.github.com/repos/vispy/vispy/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/vispy/vispy/downloads","issues_url":"https://api.github.com/repos/vispy/vispy/issues{/number}","pulls_url":"https://api.github.com/repos/vispy/vispy/pulls{/number}","milestones_url":"https://api.github.com/repos/vispy/vispy/milestones{/number}","notifications_url":"https://api.github.com/repos/vispy/vispy/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/vispy/vispy/labels{/name}","releases_url":"https://api.github.com/repos/vispy/vispy/releases{/id}","created_at":"2013-03-21T18:43:22Z","updated_at":"2015-03-11T01:17:52Z","pushed_at":"2015-03-11T01:17:52Z","git_url":"git://github.com/vispy/vispy.git","ssh_url":"git@github.com:vispy/vispy.git","clone_url":"https://github.com/vispy/vispy.git","svn_url":"https://github.com/vispy/vispy","homepage":"http://vispy.org","size":15304,"stargazers_count":502,"watchers_count":502,"language":"Python","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":80,"mirror_url":null,"open_issues_count":150,"forks":80,"open_issues":150,"watchers":502,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/vispy/vispy/pulls/738"},"html":{"href":"https://github.com/vispy/vispy/pull/738"},"issue":{"href":"https://api.github.com/repos/vispy/vispy/issues/738"},"comments":{"href":"https://api.github.com/repos/vispy/vispy/issues/738/comments"},"review_comments":{"href":"https://api.github.com/repos/vispy/vispy/pulls/738/comments"},"review_comment":{"href":"https://api.github.com/repos/vispy/vispy/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/vispy/vispy/pulls/738/commits"},"statuses":{"href":"https://api.github.com/repos/vispy/vispy/statuses/dea9e5df962380fe29f2ec52fe573afd31d0c697"}}}}