tag:blogger.com,1999:blog-76784298880522929422024-03-13T04:17:33.629-07:00::jne100 blogНикитаhttp://www.blogger.com/profile/15950824873796543662noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-7678429888052292942.post-45151045740320122952016-01-17T03:03:00.001-08:002016-01-17T03:05:31.941-08:00Manual socketpair implementation for AF_INET sockets<div dir="ltr" style="text-align: left;" trbidi="on">
Here is my manual implementation of socketpair system call that can create AF_INET sockets needed to implement some temporary workaround; don't know is it completely safe and robust, but it is okay for testing purposes.<br />
<br />
<a name='more'></a><br />
<br />
<span style="font-family: "Courier New",Courier,monospace;">int inet_socketpair(int type, int protocol, int sv[2])<br />{<br /> int lfd;<br /> int sfd;<br /> int cfd;<br /> struct sockaddr_in saddr;<br /> struct sockaddr_in caddr;<br /> socklen_t saddrlen;<br /><br /> /* initialize all fds with -1 to simplify cleanup */<br /> lfd = -1;<br /> sfd = -1;<br /> cfd = -1;<br /><br /> /* create listening socket and client socket */<br /> lfd = socket(AF_INET, type, protocol);<br /> cfd = socket(AF_INET, type, protocol);<br /> if ((lfd == -1) || (cfd == -1)) {<br /> goto err;<br /> }<br /><br /> /* start listen */<br /> if (listen(lfd, 1) == -1) {<br /> goto err;<br /> }<br /><br /> /* get ephemeral port number allocated to listening socket */<br /> memset(&saddr, 0, sizeof(saddr));<br /> saddrlen = sizeof(saddr);<br /> if (getsockname(lfd, (struct sockaddr*)&saddr, &saddrlen) == -1) {<br /> goto err;<br /> }<br /><br /> /* prepare server address needed to connect client socket */<br /> memset(&caddr, 0, sizeof(caddr));<br /> caddr.sin_family = AF_INET;<br /> caddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);<br /> caddr.sin_port = saddr.sin_port;<br /><br /> /* connect client socket */<br /> if (connect(cfd, (struct sockaddr*)&caddr, sizeof(caddr)) == -1) {<br /> goto err;<br /> }<br /><br /> /* accept server socket */<br /> sfd = accept(lfd, NULL, NULL);<br /> if (sfd == -1) {<br /> goto err;<br /> }<br /><br /> close(lfd);<br /> sv[0] = sfd;<br /> sv[1] = cfd;<br /> return 0;<br /><br />err:<br /> close(lfd);<br /> close(sfd);<br /> close(cfd);<br /> return -1;<br />}</span><br />
<br /></div>
Никитаhttp://www.blogger.com/profile/15950824873796543662noreply@blogger.com0tag:blogger.com,1999:blog-7678429888052292942.post-8009388158618857182013-11-23T06:26:00.000-08:002014-02-16T14:31:54.940-08:00Mobius strip rendering using WebGL<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<big></big><a href="http://2.bp.blogspot.com/-QyaEGDCfpBk/UpC6INIJXwI/AAAAAAAAAlQ/h2O5NwdigPY/s1600/mobius_strip_rendering_webgl.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/-QyaEGDCfpBk/UpC6INIJXwI/AAAAAAAAAlQ/h2O5NwdigPY/s1600/mobius_strip_rendering_webgl.png" /></a></div>
<br />
<big></big><br />
<a name='more'></a><big>
</big><br />
<div style="text-align: justify;">
<big>In this post I want to share experience I accidentally got experimenting with WebGL. To get some fun and explore WebGL (never write a line of JS code before) I render a Mobius strip. It's not the hardest task in the world, but definitely more interesting than usual WebGL getting-started samples with two polygons. And after all, Mobius strip is cool, isn't it?<br />
There is not so much to talk about, we have all the usual stuff such as VBO, shaders and so on. The most interesting part is Mobius strip vertices and polygons calculation. I use parametric representation of Mobius strip for tesselation:</big></div>
<big>
</big>
<blockquote>
<div style="background-color: #f4f4f4; line-height: 100%;">
<big><small><span style="font-family: Courier New;">function calcMobiusPoint(u, v, coef) {
<br /> var x = ( 1 + ((v * coef) / 2) * Math.cos(u / 2) ) *
<br /> Math.cos( u );
<br /> var y = ( 1 + ((v * coef) / 2) * Math.cos(u / 2) ) *
<br /> Math.sin( u );
<br /> var z = ( (v * coef) / 2) * Math.sin( u / 2 );
<br /> return [x, y, z];
<br />}</span></small></big></div>
</blockquote>
<big>
</big><br />
<div style="text-align: justify;">
<big>Unfortunately got no time for long explanations; take a look at method that calculates vertices, indexes and colors of strip (below), throw a glance at <a href="http://jsfiddle.net/jne100/K2DYp/">jsfiddle</a> or download entire sample from <a href="https://bitbucket.org/jne100/code_from_blog/src">bitbucket</a> if necessary.</big></div>
<big>
</big>
<blockquote>
<div style="background-color: #e3e3e3; line-height: 100%;">
<big><small><span style="font-family: Courier New;">function initBuffers() {
<br />
<br /> // math data for tesselation
<br /> var verticesPerCurve = 120;
<br /> var tBegin = 0.0;
<br /> var tEnd = Math.PI * 2;
<br /> var dt = ( tEnd - tBegin ) / ( verticesPerCurve - 2 );
<br /> var stripCoef = 0.5;
<br />
<br /> // surface bounary colors
<br /> var beginColor = [ 0.0, 0.3, 1.0 ];
<br /> var endColor = [ 1.0, 0.3, 0.0 ];
<br />
<br /> // vertices calculation scheme:
<br /> // / /
<br /> // / /
<br /> // #2 o o #(verticesPerCurve + 2)
<br /> // / /
<br /> // / /
<br /> // #1 o o #(verticesPerCurve + 1)
<br /> // / /
<br /> // / /
<br /> // #0 o o #(verticesPerCurve + 0)
<br /> // / /
<br /> // curve1 curve2
<br />
<br /> // VERTICES
<br /> modelVerticesBuffer = gl.createBuffer();
<br /> gl.bindBuffer( gl.ARRAY_BUFFER, modelVerticesBuffer );
<br />
<br /> var vertices = new Array();
<br /> for ( var i = 0; i < verticesPerCurve; ++i ) {
<br /> var t = tBegin + dt * i;
<br /> var p1 = calcMobiusPoint(t, -1.0, stripCoef);
<br /> var p2 = calcMobiusPoint(t, 1.0, stripCoef);
<br /> // first curve vertex
<br /> vertices[ i * 3 ] = p1[ 0 ];
<br /> vertices[ i * 3 + 1 ] = p1[ 1 ];
<br /> vertices[ i * 3 + 2 ] = p1[ 2 ];
<br /> // second curve vertex
<br /> var k = ( i + verticesPerCurve );
<br /> vertices[ k * 3 ] = p2[ 0 ];
<br /> vertices[ k * 3 + 1 ] = p2[ 1 ];
<br /> vertices[ k * 3 + 2 ] = p2[ 2 ];
<br /> }
<br />
<br /> gl.bufferData( gl.ARRAY_BUFFER,
<br /> new Float32Array(vertices), gl.STATIC_DRAW );
<br /> modelVerticesBuffer.itemSize = 3;
<br /> modelVerticesBuffer.numItems = verticesPerCurve * 2;
<br />
<br /> // COLORS
<br /> modelVerticesColorBuffer = gl.createBuffer();
<br /> gl.bindBuffer( gl.ARRAY_BUFFER,
<br /> modelVerticesColorBuffer );
<br />
<br /> var verticesColors = new Array();
<br /> var halfVerticesPerCurve = verticesPerCurve / 2.0;
<br /> for ( var i = 0; i < verticesPerCurve; ++i ) {
<br /> var curColor = [ 0.0, 0.0, 0.0 ];
<br /> for ( var k = 0; k < curColor.length; ++k ) {
<br /> if ( i < halfVerticesPerCurve ) {
<br /> curColor[ k ] = beginColor[ k ] +
<br /> (endColor[k] - beginColor[k]) *
<br /> (i / halfVerticesPerCurve);
<br /> } else {
<br /> curColor[ k ] = endColor[ k ] +
<br /> (beginColor[k] - endColor[k]) *
<br /> ((i - halfVerticesPerCurve) /
<br /> halfVerticesPerCurve);
<br /> }
<br /> }
<br /> // first curve vertex color
<br /> verticesColors[ i * 3 ] = curColor[ 0 ];
<br /> verticesColors[ i * 3 + 1 ] = curColor[ 1 ];
<br /> verticesColors[ i * 3 + 2 ] = curColor[ 2 ];
<br /> // second curve vertex color
<br /> var k = ( i + verticesPerCurve );
<br /> verticesColors[ k * 3 ] = curColor[ 0 ];
<br /> verticesColors[ k * 3 + 1 ] = curColor[ 1 ];
<br /> verticesColors[ k * 3 + 2 ] = curColor[ 2 ];
<br /> }
<br />
<br /> gl.bufferData( gl.ARRAY_BUFFER,
<br /> new Float32Array(verticesColors), gl.STATIC_DRAW );
<br /> modelVerticesColorBuffer.itemSize = 3;
<br /> modelVerticesColorBuffer.numItems =
<br /> verticesPerCurve * 2;
<br />
<br /> // polygons calculation scheme:
<br /> // / \ /
<br /> // / \ /
<br /> // / #poly2 \ /
<br /> // #2 o-----------o #(verticesPerCurve + 1)
<br /> // / \ #poly1 /
<br /> // / \ /
<br /> // / \ /
<br /> // / \ /
<br /> // / #poly0 \ /
<br /> // #1 o-----------o #(verticesPerCurve + 0)
<br /> // / /
<br /> // curve1 curve2
<br />
<br /> // POLYGONS
<br /> modelVerticesIndexBuffer = gl.createBuffer();
<br /> gl.bindBuffer( gl.ELEMENT_ARRAY_BUFFER,
<br /> modelVerticesIndexBuffer );
<br />
<br /> var verticesIndices = new Array();
<br /> for ( var i1 = 0; i1 < (verticesPerCurve - 1); ++i1 ) {
<br /> var i1Next = i1 + 1;
<br /> var i2 = i1 + verticesPerCurve;
<br /> var i2Next = i2 + 1;
<br /> var nPoly1 = i1 * 2;
<br /> var nPoly2 = nPoly1 + 1;
<br /> // first polygon
<br /> verticesIndices[ nPoly1 * 3 ] = i2;
<br /> verticesIndices[ nPoly1 * 3 + 1 ] = i1;
<br /> verticesIndices[ nPoly1 * 3 + 2 ] = i1Next;
<br /> // first polygon
<br /> verticesIndices[ nPoly2 * 3 ] = i1Next;
<br /> verticesIndices[ nPoly2 * 3 + 1 ] = i2Next;
<br /> verticesIndices[ nPoly2 * 3 + 2 ] = i2;
<br /> }
<br />
<br /> gl.bufferData( gl.ELEMENT_ARRAY_BUFFER,
<br /> new Uint16Array(verticesIndices), gl.STATIC_DRAW );
<br /> modelVerticesIndexBuffer.itemSize = 3;
<br /> modelVerticesIndexBuffer.numItems =
<br /> verticesIndices.length /
<br /> modelVerticesIndexBuffer.itemSize;
<br />}</span></small></big></div>
</blockquote>
<big>
Finally, several pictures of rendered Mobius strip:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-tdE4n1NKU7Y/UpC6SiOSoRI/AAAAAAAAAlc/7AhALA0lR54/s1600/mobius_strip_webgl_001.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/-tdE4n1NKU7Y/UpC6SiOSoRI/AAAAAAAAAlc/7AhALA0lR54/s400/mobius_strip_webgl_001.png" height="300" width="400" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://2.bp.blogspot.com/-IBCtWCCSzE4/UpC6Su_-yVI/AAAAAAAAAlY/9OypagdXWzo/s1600/mobius_strip_webgl_002.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://2.bp.blogspot.com/-IBCtWCCSzE4/UpC6Su_-yVI/AAAAAAAAAlY/9OypagdXWzo/s400/mobius_strip_webgl_002.png" height="300" width="400" /></a></div>
<br />
</big></div>
Никитаhttp://www.blogger.com/profile/15950824873796543662noreply@blogger.com0tag:blogger.com,1999:blog-7678429888052292942.post-24099209998090033082013-01-26T06:08:00.002-08:002013-10-30T15:08:48.094-07:00Math2d - general purpose math library written in C++<div dir="ltr" style="text-align: left;" trbidi="on">
<big>Math2d is a simple general purpose mathematical library written in C++. Mainly Math2d contains functions and classes for work with generic mathematical entities (such as points, vectors and coordinate systems) and parametric curves (such as segments, arcs or Bezier curves). It also contains real values comparison functions, round function (missing in standart library), factorial calculator and bunch of other utilities in addition.</big><br />
<br />
<a name='more'></a><big>I really doubt you will be able to use it in serious commercial project, it' more for educational purposes (in spite of it, at least my own windows store project uses it for parametric curves rendering and modification). The main goal was to write some code I would be really proud of, through TDD, with unit test and all the cool stuff, as opposed to typycal production mess.</big><br />
<br />
<a href="https://bitbucket.org/jne100/math2d"><big>Math2d repository on Bitbucket</big></a>.<br />
<br />
<big>Update: move Math2d repository from GitHub to Bitbucket (cause Bitbucket motherf*ckin rule!).</big><br />
<big><br /></big>
<br /></div>
Никитаhttp://www.blogger.com/profile/15950824873796543662noreply@blogger.com0tag:blogger.com,1999:blog-7678429888052292942.post-60716472100941999842012-12-10T12:09:00.000-08:002013-01-26T06:17:08.481-08:00TextTouch application released<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="http://apps.microsoft.com/webpdp/en-US/app/texttouch/ebb4ef90-2509-421c-bc27-ad535289acf8"><img border="0" src="http://3.bp.blogspot.com/-CpfgGLKP6Xs/UMZAJr-b8RI/AAAAAAAAAeA/R8YrOXboc8U/s1600/pilot_banner.png" /></a> </div>
<br />
<a name='more'></a><big>
TextTouch is specific graphic editor meant for easy addition of text to the pictures, e.g. explaining inscriptions or titles. Use of touch interface makes it easy to add nice inscriptions, titles, watermarks and so on even for non-experienced users.</big><br />
<ul style="text-align: left;">
<li><a href="http://apps.microsoft.com/webpdp/en-US/app/texttouch/ebb4ef90-2509-421c-bc27-ad535289acf8"><big>Application in Windows Store</big></a></li>
<li><a href="http://jne100.blogspot.ru/p/texttouch.html"><big>Application page</big></a></li>
</ul>
</div>
Никитаhttp://www.blogger.com/profile/15950824873796543662noreply@blogger.com0