{"id":24517687,"url":"https://github.com/pyzh/triangle","last_synced_at":"2025-03-15T11:10:46.378Z","repository":{"id":196578633,"uuid":"164427213","full_name":"pyzh/triangle","owner":"pyzh","description":null,"archived":false,"fork":false,"pushed_at":"2018-01-19T09:48:14.000Z","size":18,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-22T01:36:41.638Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pyzh.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2019-01-07T12:08:34.000Z","updated_at":"2019-01-07T12:08:37.000Z","dependencies_parsed_at":"2023-09-26T08:19:46.508Z","dependency_job_id":null,"html_url":"https://github.com/pyzh/triangle","commit_stats":null,"previous_names":["pyzh/triangle"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyzh%2Ftriangle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyzh%2Ftriangle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyzh%2Ftriangle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pyzh%2Ftriangle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pyzh","download_url":"https://codeload.github.com/pyzh/triangle/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243719399,"owners_count":20336607,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-22T01:34:52.690Z","updated_at":"2025-03-15T11:10:46.360Z","avatar_url":"https://github.com/pyzh.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"========\ntriangle\n========\n\n.. image:: https://travis-ci.org/bhuztez/triangle.svg?branch=master\n    :target: https://travis-ci.org/bhuztez/triangle\n\n.. image:: https://codenvy.io/factory/resources/codenvy-contribute.svg\n    :target: https://codenvy.io/f?url=https://github.com/bhuztez/triangle\n\nThere are many good OpenGL tutorials out there. They have good\nexplanation on how OpenGL works. However, most of them treat OpenGL as\na black box. It would be better to implement a small part of OpenGL to\nsee how things fit together.\n\nThe biggest challenge of emulating GLSL in C++ is shader linking. This\nhas been made possible by stateful metaprogramming. Like we did in\nimplementing precise garbage collector, we also record names of all\nuniform, attribute, varying variables, so that when linking, we can\ncheck if variables of same name have same type.\n\nAnother problem is that vertex and fragment shaders will be run many\ntimes, and at each run, attribute and varying variables point to\ndifferent locations. To make it as close as possible to GLSL, and\navoid much boilerplate, there variables are declared as union. For\nexample, :code:`gl_Position`.\n\n.. code:: c++\n\n    union {\n      vec4\u0026 gl_Position;\n      vec4* _ptr_gl_Position;\n    }\n\nLet's see a complete example.\n\nthe vertex shader\n\n.. code:: c++\n\n    template\u003ctypename T\u003e\n    struct Vertex {\n      VERTEX_SHADER(Vertex, T);\n\n      UNIFORM(perspective, mat4);\n      ATTRIBUTE(position, vec3);\n      ATTRIBUTE(aColor, vec3);\n      VARYING(vColor, vec3);\n\n      void\n      main() {\n        gl_Position = perspective * vec4(position, 1.0);\n        vColor = aColor;\n      }\n    };\n\n\nthe fragment shader\n\n.. code:: c++\n\n    template\u003ctypename T\u003e\n    struct Fragment {\n      FRAGMENT_SHADER(Fragment, T);\n\n      VARYING(vColor, vec3);\n\n      void\n      main() {\n        gl_FragColor = vec4(vColor, 1.0);\n      }\n    };\n\nlink and then create program of 3 vertices\n\n.. code:: c++\n\n    using Program = ::gl::Link\u003cfloat, Vertex, Fragment\u003e;\n    using T = typename Program::Float;\n    using vec3 = typename Program::vec3;\n\n    Program prog(3);\n\nspecify locations of variables.\n\n.. code:: c++\n\n    auto p = perspective\u003cT\u003e(::gl::sl::radians(90.0), T(width)/T(height), 0.1, 100.0);\n\n    vec3 position[] = {\n      {-0.5, -0.5, -1.0},\n      { 0.5, -0.5, -1.0},\n      { 0.5,  0.5, -5.0}\n    };\n\n    vec3 color[] = {\n      {1.0, 0.0, 0.0},\n      {0.0, 1.0, 0.0},\n      {0.0, 0.0, 1.0}\n    };\n\n    prog.uniform.set(\"perspective\"_s, \u0026p);\n    prog.attribute.set(\"position\"_s, position);\n    prog.attribute.set(\"aColor\"_s, color);\n\ndraw\n\n.. code:: c++\n\n  ::gl::Context(width, height, buffer).draw(prog, ::gl::triangles);\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpyzh%2Ftriangle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpyzh%2Ftriangle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpyzh%2Ftriangle/lists"}