<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Whipsmartnanthana on Medium]]></title>
        <description><![CDATA[Stories by Whipsmartnanthana on Medium]]></description>
        <link>https://medium.com/@whipsmartnanthana2003?source=rss-433e24b3d934------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*WOWpB0rKs3pVAYDQ</url>
            <title>Stories by Whipsmartnanthana on Medium</title>
            <link>https://medium.com/@whipsmartnanthana2003?source=rss-433e24b3d934------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 04:00:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@whipsmartnanthana2003/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Delving into 3D graphics with WebGL]]></title>
            <link>https://medium.com/spidernitt/delving-into-3d-graphics-with-webgl-7101fbcd2756?source=rss-433e24b3d934------2</link>
            <guid isPermaLink="false">https://medium.com/p/7101fbcd2756</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[webgl]]></category>
            <dc:creator><![CDATA[Whipsmartnanthana]]></dc:creator>
            <pubDate>Wed, 16 Nov 2022 12:43:57 GMT</pubDate>
            <atom:updated>2022-11-16T12:43:57.878Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QcrN21BlVa9FsGMf06TvZw.png" /></figure><p>We are heading towards an era where virtual reality will become as authentic as reality. With every passing gaming or metaverse experience, we find our virtual experience enhanced by cutting-edge technologies to give us amazing results. This article focuses on designing an elementary 3D model using WebGL.</p><h3>WebGL — A brief walkthrough</h3><p>WebGL is a JavaScript API that renders 2D and 3D graphics on browsers by combining the ease and power of JavaScript with the performance of OpenGL, a low-level API that provides 2D and 3D capabilities. WebGL doesn&#39;t require a virtual machine and uses the local machine&#39;s GPU (Graphics Processing Unit), which gives WebGL an edge over other counterparts. Since the browsers that support WebGL have their setup, there is no need to set up an environment for WebGL.</p><p>It uses the following parameters to render graphics:</p><ol><li>Vertices: Represented by a maximum of three coordinates</li><li>Indices: Used for naming vertices</li><li>Face: Flat or curved surface</li><li>Mesh: A polygon drawn by the integration of simpler polygons called fragments(e.g. triangles)</li><li>Buffer: Stores data regarding vertices, indices, and fragments</li><li>Shader programs: Programs that transfer data regarding vertices, fragments, and indices from CPU to GPU. They are written in Embedded System Shader Language (ES SL)</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/906/0*BC3ahom9jDeFSayF.png" /></figure><h3>Rendering a 3D pyramid</h3><p>Let us create a rotating pyramid using WebGL. WebGL uses canvas in HTML to render graphic components.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6195f8966ec5dd11bb5c94cd11bfc763/href">https://medium.com/media/6195f8966ec5dd11bb5c94cd11bfc763/href</a></iframe><p>After obtaining the canvas element, the context is set to &quot;experimental-webgl&quot; (some browsers support &quot;webgl&quot;).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/adc63590e41ed65396fdebdcc6a8b1d8/href">https://medium.com/media/adc63590e41ed65396fdebdcc6a8b1d8/href</a></iframe><p>A list of vertices and indices is passed as arrays. Indices are structured in such a way that vertices are connected to appropriate faces. <em>gl.createBuffer()</em> creates buffer. <em>void bindBuffer(target, buffer)</em> binds the buffer (here <em>target </em>is ARRAY_BUFFER for vertices and ELEMENT_ARRAY_BUFFER for indices). <em>void bufferData(target, object, usage)</em> transfers data to the buffer (<em>usage</em> conveys how the data provided is used). After using buffers, they are unbound by re-binding them to null.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/58412b66a5d123d3f785ebbe6ffe19fd/href">https://medium.com/media/58412b66a5d123d3f785ebbe6ffe19fd/href</a></iframe><p>Now, the data in the buffer is transferred to the GPU using OpenGL ES SL. It provides datatypes such as int, float, vec2, vec3, vec4 (n-component vectors), mat3, mat4, etc., and qualifiers like attribute, uniform, and varying. Shader programs are created (using <em>createShader()</em>), data is attached to them (using <em>shaderSource()</em>) and compiled (using <em>compileShader()</em>). The programs are then associated and used.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/19ee3b357a33920cea081c973a094d22/href">https://medium.com/media/19ee3b357a33920cea081c973a094d22/href</a></iframe><p>After this, the vertex and color attributes created are associated with the shader programs and are enabled.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ab6b04422b3d9125007ddec5ac65288c/href">https://medium.com/media/ab6b04422b3d9125007ddec5ac65288c/href</a></iframe><h3>Rotation of pyramid</h3><p>The pyramid that we are creating will rotate about the y-axis. In this case, the x and z coordinates should change with the angle, and the y-coordinate should remain constant.</p><p>Let&#39;s suppose an object rotates about the z-axis. The x and y coordinates during rotation are determined by the formula:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/252/1*3LdohKcG4puMXAt62Fdvlg.png" /><figcaption>(x’, y’) — current coordinates (x, y)- point to be rotated</figcaption></figure><p>Let&#39;s consider a case where a rectangular object rotates around the z-axis in the clockwise direction. At the start, the x and y coordinates of one of its edges are 3.0 and 9.0, respectively. After it rotates through an angle of 30 degrees, its coordinates can be calculated using the above formula.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/247/1*Q1BeN4MLDKHlzVXWqyDfNg.png" /><figcaption>The position of the object when the angle is -30 degree</figcaption></figure><p>Here x = 3.0, y = 9.0 and theta= -30 degree</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/417/1*e6ahegBlX-qQW25Tp0xjfQ.png" /><figcaption>cos30 = 0.87 sin30 = 0.50</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/338/1*uH5D-dLhBoicQfbzZWAOGg.png" /></figure><p>We use this formula to frame the function that rotates the pyramid. Where m[0], m[4], m[8] denote the x coordinates and m[2], m[6], m[10] denote the z coordinates of the vertices.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd84b2f7bb9a8cf7bd2b9a68e8c29490/href">https://medium.com/media/dd84b2f7bb9a8cf7bd2b9a68e8c29490/href</a></iframe><h3>Projection, view, and world transformation matrices</h3><p>The object to be rendered is in object space originally. It is first transformed into world space by a model matrix(Mmatrix). Then it is transformed into the view space by a view matrix(Vmatrix) and subsequently to clip space by a projection matrix(Pmatrix). World space depicts the object&#39;s position along with other objects. View space portrays how the object is perceived along the view of the camera whereas clip space represents the projection of the object&#39;s points.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/765/1*SY6MkcBwNPiEmOroWCw4Yg.png" /></figure><p>The browser renders the object in the clip space. Let MVP be the matrix that stores the object&#39;s position in clip space, then MVP=Pmatrix*Vmatrix*Mmatrix. The following code snippets show these transformation tasks.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51eeac6572945709f65e826ab164d5cb/href">https://medium.com/media/51eeac6572945709f65e826ab164d5cb/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/20f930b916e6c257ab7c5be14eb5e5da/href">https://medium.com/media/20f930b916e6c257ab7c5be14eb5e5da/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/61839997ff70954ecbb007bc536f415e/href">https://medium.com/media/61839997ff70954ecbb007bc536f415e/href</a></iframe><p>Finally, we use the <em>windows.requestAnimationFrame()</em> to execute <em>animate()</em> repeatedly so that the rotating pyramid is rendered. <em>drawElements()</em> draws the model using vertices and indices. <em>clearColor()</em> clears the canvas and sets its color to the values passed in the function. After enabling DEPTH_TEST and clearing COLOR_BUFFER_BIT or DEPTH_BUFFER_BIT, the viewport is set to create a rectangular frame where the pyramid will be rendered.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fffa7a244d2d389a10a8e3bfada25d26/href">https://medium.com/media/fffa7a244d2d389a10a8e3bfada25d26/href</a></iframe><p>The entire code is given below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3fd3a91291ac589364444478467b1b74/href">https://medium.com/media/3fd3a91291ac589364444478467b1b74/href</a></iframe><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FJDLM_vso2tk%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DJDLM_vso2tk&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FJDLM_vso2tk%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="640" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/70ff4c83925a248de2bc76d788637ae0/href">https://medium.com/media/70ff4c83925a248de2bc76d788637ae0/href</a></iframe><h3>Conclusion</h3><p>The rotating pyramid we&#39;ve created is just a spec in the unfathomable and fascinating field of graphics. Nevertheless, these simple models form the basis for complex graphic designs, one of the best aids for increasing user interactivity with machines.</p><p>Happy Coding!</p><h3>Resources</h3><ol><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Tutorial">documentation</a></li><li><a href="https://www.tutorialspoint.com/webgl/index.htm">tutorial</a></li><li><a href="https://webglfundamentals.org/webgl/lessons/webgl-2d-rotation.html">understanding the rotation of elements</a></li><li><a href="https://www.cuemath.com/algebra/rotation-matrix/">rotation matrix</a></li><li><a href="https://www.3dgep.com/understanding-the-view-matrix/#Transformations">world, view, projection matrices</a></li></ol><p><em>This article is published as a part of &#39;</em><strong><em>JavaScripted&#39;</em></strong><em> under </em><a href="https://medium.com/spidernitt"><em>Spider Research and Development Club, NIT Trichy</em></a>,<em> on a Web Wednesday!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7101fbcd2756" width="1" height="1" alt=""><hr><p><a href="https://medium.com/spidernitt/delving-into-3d-graphics-with-webgl-7101fbcd2756">Delving into 3D graphics with WebGL</a> was originally published in <a href="https://medium.com/spidernitt">Spider R&amp;D</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>