Search Unity

Question Translate OpenGL code to Unity's way

Discussion in 'General Graphics' started by Danil0v3s, Jan 16, 2021.

  1. Danil0v3s

    Danil0v3s

    Joined:
    Sep 22, 2014
    Posts:
    45
    Hey, I've been trying to port an old MMORPG client to Unity, the unfamous Ragnarok Online. Among many things, the clients reads its effects from files, which are now known to the community and we've been able to parse them. Fast forward to my problem, I do not know anything about OpenGL and my main reference is written on top of OpenGL, so I've decided to come here and ask you guys if you would help me translate the OpenGL code to Unity's way of doing graphics. Any help would be appreciated, that includes explanation of what the following code does.

    Here's the function responsible for rendering:
    Code (JavaScript):
    1. /**
    2.      * Setup geometries, send data to GPU
    3.      *
    4.      * @param {object} webgl context
    5.      * @param {glTexture} webgl texture
    6.      * @param {StrAnimation} animation object
    7.      */
    8.     StrEffect.prototype.renderAnimation = function renderAnimation( gl, material, anim )
    9.     {
    10.         var uniform   = _program.uniform;
    11.         var attribute = _program.attribute;
    12.  
    13.         // Update geometries
    14.         _bufferData[0]  = anim.xy[0];
    15.         _bufferData[1]  = anim.xy[4];
    16.         _bufferData[2]  = 0; //anim.uv[0];
    17.         _bufferData[3]  = 0; //anim.uv[1];
    18.  
    19.         _bufferData[4]  = anim.xy[1];
    20.         _bufferData[5]  = anim.xy[5];
    21.         _bufferData[6]  = 1; //anim.uv[2];
    22.         _bufferData[7]  = 0; //anim.uv[3];
    23.  
    24.         _bufferData[8]  = anim.xy[3];
    25.         _bufferData[9]  = anim.xy[7];
    26.         _bufferData[10] = 0; //anim.uv[4];
    27.         _bufferData[11] = 1; //anim.uv[5];
    28.  
    29.         _bufferData[12] = anim.xy[2];
    30.         _bufferData[13] = anim.xy[6];
    31.         _bufferData[14] = 1; //anim.uv[6];
    32.         _bufferData[15] = 1; //anim.uv[7];
    33.  
    34.         if (anim.angle !== _lastAngle) {
    35.             mat4.identity(_matrix);
    36.             mat4.rotateZ( _matrix, _matrix, - anim.angle / 180 * Math.PI );
    37.             _lastAngle = anim.angle;
    38.         }
    39.  
    40.         anim.pos[0] -= 320;
    41.         anim.pos[1] -= 320;
    42.  
    43.         // Send effect parameters
    44.         gl.uniform4fv( uniform.uSpriteColor,    anim.color );
    45.         gl.uniform2fv( uniform.uSpriteOffset,   anim.pos );
    46.         gl.uniform3fv( uniform.uSpritePosition, this.position );
    47.  
    48.         gl.uniformMatrix4fv( uniform.uSpriteAngle, false, _matrix );
    49.  
    50.         // Send new buffer
    51.         gl.bindBuffer( gl.ARRAY_BUFFER, _buffer );
    52.  
    53.         // Link attribute
    54.         gl.vertexAttribPointer( attribute.aPosition,     2, gl.FLOAT, false,  4*4, 0*4 );
    55.         gl.vertexAttribPointer( attribute.aTextureCoord, 2, gl.FLOAT, false,  4*4, 2*4 );
    56.  
    57.         gl.bufferData( gl.ARRAY_BUFFER, _bufferData, gl.STREAM_DRAW );
    58.  
    59.         // Send texture and data
    60.         gl.blendFunc( D3DBLEND[anim.srcalpha], D3DBLEND[anim.destalpha] );
    61.         gl.bindTexture( gl.TEXTURE_2D, material );
    62.         gl.drawArrays( gl.TRIANGLE_STRIP, 0, 4 );
    63.     };

    Full code can be found at Github and the project I'm working is also open-source and can be found here.

    Thank you!