<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Igor Segalla</title>
    <description>The latest articles on DEV Community by Igor Segalla (@igorsegallafa).</description>
    <link>https://dev.to/igorsegallafa</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F484028%2Ff4e2c9de-6b9c-437b-b0de-58e0fa27c460.png</url>
      <title>DEV Community: Igor Segalla</title>
      <link>https://dev.to/igorsegallafa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/igorsegallafa"/>
    <language>en</language>
    <item>
      <title>Avoiding the use of “Auto Clicker/Keyboard” tools</title>
      <dc:creator>Igor Segalla</dc:creator>
      <pubDate>Tue, 23 Feb 2021 15:42:30 +0000</pubDate>
      <link>https://dev.to/igorsegallafa/avoiding-the-use-of-auto-clicker-keyboard-tools-5469</link>
      <guid>https://dev.to/igorsegallafa/avoiding-the-use-of-auto-clicker-keyboard-tools-5469</guid>
      <description>&lt;p&gt;Still on the topic of cheating and as a follow up on my last post in this community, I will try to explain one of the alternatives I used, and that you can also try, to avoid using tools that simulate mouse clicks or keyboard keystrokes.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is it?
&lt;/h1&gt;

&lt;p&gt;These tools that simulate Input events are usually used to create Bots, where the tool will repeat a user-defined input pattern (mouse or keyboard) without having a "real" interaction with the application, e.g., the tool will simulate behavior as if a person is in charge.&lt;/p&gt;

&lt;p&gt;Alright, but where does the user use this type of tool? It is very common to use it in games such as RPG, where the player needs to use a skill every time, a potion to recover its life or even click on a certain position on the screen to attack a monster. All of this can be done by simulating clicks or keystrokes.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Auto clickers can be as simple as a program that simulates mouse clicking. This type of auto-clicker is fairly generic and will often work alongside any other computer program running at the time and acting as though a physical mouse button is pressed. (Source: Wikipedia)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This kind of “cheating” can be very harmful to your application, since the user is having an advantage over other players and he can set the “Bot” playing for him 24 hours non-stop (which would be very difficult for us humans).&lt;/p&gt;

&lt;h1&gt;
  
  
  How does it work?
&lt;/h1&gt;

&lt;p&gt;Usually, the operation of these tools is quite simple. The most common method is to send an &lt;strong&gt;Input&lt;/strong&gt; message to the &lt;strong&gt;HANDLE&lt;/strong&gt; of the window that we want to simulate the click or keystroke. This can be done using the Win32 API's &lt;strong&gt;SendMessage&lt;/strong&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LRESULT SendMessage(
  HWND   hWnd,
  UINT   Msg,
  WPARAM wParam,
  LPARAM lParam
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In the first parameter we specify the &lt;strong&gt;HANDLE&lt;/strong&gt; of the target window. In &lt;strong&gt;uMsg&lt;/strong&gt; we specify the identifier of the message we are sending (Eg WM_KEYDOWN), and the other parameters are additional information related to that type of message. For example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SendMessage(hTargetWindow, WM_KEYDOWN, VK_RETURN, 0);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the case above, we are sending a &lt;strong&gt;WM_KEYDOWN&lt;/strong&gt; message, e.g., a key is being pressed and with the &lt;em&gt;WPARAM&lt;/em&gt; being &lt;strong&gt;VK_RETURN&lt;/strong&gt;, means that we are simulating the pressing of the &lt;strong&gt;Enter&lt;/strong&gt; key.&lt;/p&gt;

&lt;p&gt;As explained, you can replicate this operation for other types of events and build your own logic, like building a Bot to automatically use skills in a game.&lt;/p&gt;
&lt;h1&gt;
  
  
  Problem
&lt;/h1&gt;

&lt;p&gt;The problem we have when trying to avoid this type of cheating in a "native" way, is that we were unable to identify the source of a message using the &lt;strong&gt;Win32&lt;/strong&gt; API. That is, all messages are valid regardless of where they are coming from for our application.&lt;/p&gt;

&lt;p&gt;With that in mind, what we should do is filter out these Input messages received in our application, limiting them to only reliable devices and that they originate from hardware, and not virtual messages (which are sent by software).&lt;/p&gt;
&lt;h1&gt;
  
  
  Implementation
&lt;/h1&gt;

&lt;p&gt;The implementation of the proposed solution is quite simple. We will change the way we handle the Input messages received in our application, starting to use the concept of Raw Input (Win32 API).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The raw input API provides a stable and robust way for applications to accept raw input from any HID, including the keyboard and mouse.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To use Raw Input, first of all we need to register the devices that we want to receive the inputs. Thus, we can choose whether to receive data from a Joystick, a Mouse, keyboard, gamepad etc. No input will be detected if we don't have a registered device. &lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;In the code above, we are registering only two devices: mouse and keyboard. You can check the &lt;a href="https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-rawinputdevice"&gt;RAWINPUTDEVICE&lt;/a&gt; documentation for more information.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With the devices already registered, our application will start receiving Input events through the &lt;strong&gt;WM_INPUT&lt;/strong&gt; message. What we need to do now is to simply handle these messages, and identify whether we are dealing with a keyboard, mouse event, etc. I am showing the way I used it in my Gist application below, where I am handling messages from keystrokes and mouse clicks.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The operation of the code above is quite simple. We are retrieving the input data with the function &lt;strong&gt;GetRawInput&lt;/strong&gt;, and with the output of this function we can handle the events according to our application. In my specific example, I check if the input message is of the &lt;em&gt;keyboard&lt;/em&gt; or &lt;em&gt;mouse&lt;/em&gt; type, and if it is valid, the application processes normally. Otherwise, the message will be discarded.&lt;/p&gt;




&lt;p&gt;Although the solution is quite simple, this type of content (about cheating) is a quite difficult to find on the Internet, so the programmer's creativity is of great importance. It should also be borne in mind that for any solution, there will always be a possible gap (it is up to you to make it difficult to access the gap).&lt;/p&gt;

&lt;p&gt;With the proposed solution, we will avoid the vast majority of Auto Click tools, as they all follow the same operation. The only case that our solution cannot avoid is when the Input is simulated by some type of hardware, because if the event is emitted by a hardware, we consider the event to be reliable and process the Input normally.&lt;/p&gt;

&lt;p&gt;Just a reminder, architect your server properly!🤗&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>cpp</category>
      <category>gamedev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Tips for writing an Anti-Cheat</title>
      <dc:creator>Igor Segalla</dc:creator>
      <pubDate>Fri, 09 Oct 2020 02:25:23 +0000</pubDate>
      <link>https://dev.to/igorsegallafa/tips-for-writing-an-anti-cheat-4m6k</link>
      <guid>https://dev.to/igorsegallafa/tips-for-writing-an-anti-cheat-4m6k</guid>
      <description>&lt;p&gt;If you have ever been curious about how an anti-cheat works or if you plan to write your own, then in this post I'm going to give you a few tips on how to detect and protect your application from these well-known cheaters.&lt;/p&gt;

&lt;p&gt;It is worth noting that in this post I'm going to emphasize the client-side part, and not the server. To avoid future problems and have a totally secure application, the best you can do is to have a well written program that doesn't allow the exploitation of faults by third parties in any way.&lt;/p&gt;




&lt;h2&gt;
  
  
  Anti-Debugging
&lt;/h2&gt;

&lt;p&gt;One of the best known and most used methods to protect a program from an intruder is to prevent users from debugging it and thus reverse engineer it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Reverse engineering is the process of discovering the technological principles and functioning of a device, object or system, through the analysis of its structure, function and operation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I made a collection of 12 different anti-debugging methods while working on my anti-cheat. All the ones I found are efficient and fully functional. Below you can check out these methods and use them as you like. Just keep in mind that the code with all the methods is in a separate &lt;a href="https://gist.github.com/igorsegallafa/3dd15c67e7091e9734a417fe1079129b"&gt;Gist&lt;/a&gt; so as not to pollute this post.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Find Window
&lt;/h2&gt;

&lt;p&gt;It is a very common method among the existing anti-cheats, which consists of searching for a process that is running by the name of the window. As an example, it is to make that whenever there is a window with the word &lt;strong&gt;hacker&lt;/strong&gt;, the anti-cheat detects this process as malicious software (is quite common for this method to detect false positive).&lt;/p&gt;

&lt;p&gt;I use two possible ways of Find Window: one of them is to check if there is a window with the keyword chosen by me; and the other one is to analyze all HANDLES made so far and check if it could be a malicious software.&lt;/p&gt;

&lt;h3&gt;
  
  
  Find by Window name
&lt;/h3&gt;

&lt;p&gt;As previously said, this method is nothing more than using the &lt;a href="https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-findwindowa"&gt;FindWindow&lt;/a&gt; function of the Windows API and check if it has any window with the chosen words. If it returns a valid HANDLE, it means that the window has been found.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if( FindWindow( "hacker", NULL ) != NULL )
     HackerDetected();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Deep search for a Window
&lt;/h3&gt;

&lt;p&gt;This method does a deeper search for each opened process, trying to search for some pattern that identifies that program as malicious software.&lt;/p&gt;

&lt;p&gt;We used the &lt;a href="https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumchildwindows"&gt;EnumWindows&lt;/a&gt; and &lt;a href="https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumchildwindows"&gt;EnumChildWindows&lt;/a&gt; function to iterate through the existing HANDLEs recursively.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;EnumWindows( (WNDENUMPROC)EnumWindowsProc, ( LPARAM )nullptr );
void EnumWindowsProc( HWND hWnd, LPARAM lParam ) {
    EnumChildWindows(...);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If we have the HANDLE of each window, and of each element within the window (every UI object in Win32 is a HANDLE), we can make some observations and an analysis together to check whether that is or isn't a malicious program. Observations such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Having a specfic text anywhere inside the window&lt;/li&gt;
&lt;li&gt;Having an UI element with a specific Window Style (size, color)&lt;/li&gt;
&lt;li&gt;Keyword is found in any Win32 UI element&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can take a cheat tool and identify the pattern of its UI, such as the size of the buttons, the style, the text etc, and so have our anti-cheat whenever it encounters these patterns, already identify it as a malicious program, for example.&lt;/p&gt;
&lt;h2&gt;
  
  
  Avoiding injected modules
&lt;/h2&gt;

&lt;p&gt;Obviously, we don't want any DLL to be injected into our process and so it has access to our code while executing, memory etc.&lt;/p&gt;

&lt;p&gt;To avoid this kind of problem, the solution is quite simple: we check all the modules present within our application, and if there is any unknown module in the middle, we detect it as an intruder and we expel it from our execution.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h2&gt;
  
  
  Duplicated process instance
&lt;/h2&gt;

&lt;p&gt;A very common method that people use for handling a process's memory is using ReadProcessMemory and WriteProcessMemory. You must first perform an OpenProcess, to assure all the necessary privileges for improper memory manipulation in oder to use these two functions.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The OpenProcess function works by creating a new instance of our process, making it as if two distinct HANDLEs point to the same running process.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We check all instances related to the running process in order to avoid this, and if that instance is from an unknown process, we close the HANDLE of it, not allowing any more memory manipulation (either writing or reading).&lt;/p&gt;

&lt;h2&gt;
  
  
  Hooking Function
&lt;/h2&gt;

&lt;p&gt;This protection is quite simple, but it can be pretty effective against cheaters who do not have as much knowledge in reverse engineering. This protection can also be added to the others mentioned in this post, therefore, getting even better.&lt;/p&gt;

&lt;p&gt;The protection consists of simply checking if a function has been hooked, checking if the memory of the function we want to protect is using Assembly statements of type JMP.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bool __forceinline IsHookAPI( BYTE* pbFunction )
{
  //JMP
  if( pbFunction[0] == 0xE9 )
     return true;
  //JMP DWORD PTR DS:[...]
  if( pbFunction[0] == 0xFF &amp;amp;&amp;amp; pbFunction[1] == 0x25 )
     return true;
   return false;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;To use the &lt;strong&gt;IsHookAPI&lt;/strong&gt; function, we just call it by giving the memory address of the function we want to check. Example:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if( IsHookAPI( (BYTE*)&amp;amp;GetTickCount ) )
  //Do something...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;In the example above we are checking if the &lt;strong&gt;GetTickCount&lt;/strong&gt; function was hooked to another place, and if it was, the check will return positive and so we can detect a cheater.&lt;/p&gt;
&lt;h2&gt;
  
  
  Checksum
&lt;/h2&gt;

&lt;p&gt;This is one of the most used methods when it comes to protection, and it can be used in several places. What we do with this method, is basically to check the integrity of our file or memory, hoping that it matches the original. If this integrity check doesn't match the expected, we can conclude that there has been a change in the file/memory and identify the user as a cheater.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A checksum is a small-sized datum derived from a block of digital data for the purpose of detecting errors that may have been introduced during its transmission or storage. By themselves, checksums are often used to verify data integrity but are not relied upon to verify data authenticity. Source: Wikipedia&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can use this method in important files of your program, the executable file itself or even the section .text from your executable.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The .text section of the executable is where the program code is located. In it we have the permission to read and execute. Therefore, if any user makes any changes in this section, we know that the code has been tampered with.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Calculating the Checksum
&lt;/h3&gt;

&lt;p&gt;The one I choose to use was CRC32, although, there are several ways to calculate the checksum of something. This kind of verification is widely used for error detection in network protocols and storage devices.&lt;/p&gt;

&lt;p&gt;This kind of algorithm may not be the best choice for our case, but I chose it because it is relatively simple and very easy to implement.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;int VerifyChecksum::GetChecksum( void* pBuffer, DWORD dwSize )
{
   //Calculate the checksum of .text section
   boost::crc_32_type result;
   result.process_bytes( pBuffer, dwSize );
   return result.checksum();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If you do not want to implement it on your own, you can use the boost library, where we already have a function made for this.&lt;/p&gt;
&lt;h2&gt;
  
  
  Protecting function calls
&lt;/h2&gt;

&lt;p&gt;As I mentioned at the beginning of this post, the best way not to need to protect the client side of your program, is to do as much as you can on the server side. However, there are still those things that we have to leave on the client side and we would not like in any way that the user could use.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;What we do in this function is to check if the return address of the function we are protecting is within the scope of the program, and also, if it is being executed by a known thread. If any of these checks return negative, we know that the function may be being called by some external program, such as one .dll injected, and thus detect as an improper call.&lt;/p&gt;




&lt;p&gt;You can avoid future headaches and be able to protect your project from cheaters with those simple tips. Although some of these methods are quite simple, the cheater must have a considerable level of reverse engineering, and be able to get through all the existing protections (which will be scattered throughout your program).&lt;/p&gt;

&lt;p&gt;I emphasize once again, that the best way to protect your program, is to leave delicate workings to the server side, preventing your users from having access to resources that they should not have.&lt;/p&gt;

&lt;p&gt;So, architect your server properly!🤗&lt;/p&gt;

</description>
      <category>showdev</category>
      <category>cpp</category>
      <category>tutorial</category>
      <category>anticheat</category>
    </item>
    <item>
      <title>Rewriting a 2000s graphics engine</title>
      <dc:creator>Igor Segalla</dc:creator>
      <pubDate>Wed, 07 Oct 2020 02:34:18 +0000</pubDate>
      <link>https://dev.to/igorsegallafa/rewriting-a-2000s-graphics-engine-2en1</link>
      <guid>https://dev.to/igorsegallafa/rewriting-a-2000s-graphics-engine-2en1</guid>
      <description>&lt;p&gt;I have always been interested and curious about the computer graphics area, trying to understand the whole process involved in rendering a simple 3D object. How could millions of meaningless numbers be drawn on the screen, with animations, lighting, textures and everything?&lt;/p&gt;

&lt;p&gt;To start getting a little more aware of the subject, I started reading the book Introduction To 3D Game Programming With Directx 9.0C: A Shader Approach. This book covers from basic concepts (example of what a 3D object is) to even more advanced concepts, such as the use of Shaders (reason for my choice).&lt;/p&gt;

&lt;p&gt;Oh, and yes, I know that the concept of fixed-function rendering pipeline is already obsolete, but I chose Directx 9 because the implementation is simpler.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fz497zmb6qea0vyf08fqj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fz497zmb6qea0vyf08fqj.png" alt="Directx Book"&gt;&lt;/a&gt;&lt;/p&gt;
Introduction To 3D Game Programming With Directx 9.0C: A Shader Approach (Wordware Game and Graphics Library) (Frank Luna)



&lt;p&gt;Today, I'm going to tell you a little bit about my experience to rewrite a very old graphics engine and optimize it using more modern concepts.&lt;/p&gt;

&lt;p&gt;The work involved in this whole project was very complex and quite extensive. As I don't want to go into too much technical detail in this publication and end up making it very tedious, I'll focus on the main parties involved and summarize what my solution was.&lt;/p&gt;




&lt;h2&gt;
  
  
  Application
&lt;/h2&gt;

&lt;p&gt;This project was carried out in a game (nothing better to have feasible results) from the 2000s.&lt;/p&gt;

&lt;p&gt;My target in this project was to optimize this game as much as I could in comparison to the old design. At the same time, acquire experience and more understanding on the subject of computer graphics.&lt;/p&gt;

&lt;h2&gt;
  
  
  Problems
&lt;/h2&gt;

&lt;p&gt;Before starting the project, I had to take into account some existing problems that should be dealt with in some way during the execution of the project. Problems such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fixed Point:&lt;/strong&gt; as the game is from the 2000s, float operations were still very costly and so the entire graphical part of the game was done using variables of the whole type. Believe me, I never want to do a matrix operation using integers, without even being able to think about SSE.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Directx 6:&lt;/strong&gt; for a decent optimization, it was necessary to upgrade to Directx 9 (the most similar to Directx 6) to have access to more advanced rendering features.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Hard-coding:&lt;/strong&gt; the entire graphic part of the game was done on its own, without using high-level graphic libraries. The only functions used in the Directx API were DrawPrimitiveUP, Clear and SetTexture. In other words, all the vertex transformation, shading and skinning parts were done "by hand".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compatibility:&lt;/strong&gt; as I didn't want to lose compatibility, I decided to choose to use Directx 9 (which supports Windows XP), focusing on the minimum support of Shader Model 2.0 (SM2) and processors with SSE2 technology.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;SSE (Streaming SIMD Extensions) is a set of SIMD type instructions designed by Intel. They are additional instructions that can increase performance when the same operations are performed on multiple data entries.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Removing use of already transformed vertices (RHW)
&lt;/h2&gt;

&lt;p&gt;The first thing to be done and one of the key parts of all the work was to remove the use of the transformed vertices. Without this, it would be impossible to use more modern concepts such as vertex buffering, indexing, hardware skinning , etc.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Transformed Vertices means that the game performed all the transformations involved in a rendering process on its own, without any help from a graphic library.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In short, what needed to be done was:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Remove the transformation of the vertices for Projection&lt;/li&gt;
&lt;li&gt;Remove the vertex transformation for View Space (Camera)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To remove these two transformations, it was necessary to pass the vertices in World Space to the rendering buffer , and not the transformed ones anymore.&lt;/p&gt;

&lt;p&gt;In this way, it was possible to make the transformation by the Directx API itself, using the function &lt;a href="https://docs.microsoft.com/en-us/windows/win32/api/d3d9/nf-d3d9-idirect3ddevice9-settransform" rel="noopener noreferrer"&gt;SetTransform (D3DTS_View | Projection, &amp;amp;m)&lt;/a&gt;, which is much more performative than the one then hard-coded by the game.&lt;/p&gt;

&lt;h2&gt;
  
  
  Render using DrawPrimitive
&lt;/h2&gt;

&lt;p&gt;Another issue present in the rendering of the game, was that it drew all vertices using DrawPrimitiveUP.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The DrawPrimitiveUP function is used when you pass a vertex buffer created and transformed on your own. It is generally used for dynamic objects (which was not our case).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This caused a tremendous cost to the CPU, since many memory operations were carried out (memcpy mainly). The main idea in this part of the project was to start using Vertex Buffers and thus be able to draw everything using the DrawPrimitive function.&lt;/p&gt;

&lt;p&gt;To start using Vertex Buffer I had to remove the transformation of Local Space into World Space by the game itself, and also start using the function provided by the Directx API, &lt;a href="https://docs.microsoft.com/en-us/windows/win32/api/d3d9/nf-d3d9-idirect3ddevice9-settransform" rel="noopener noreferrer"&gt;SetTransform (D3DTS_World, &amp;amp;m)&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This Vertex Buffer is built during the loading of the 3D file, thus, we have a buffer that is assembled only once and remains static throughout the execution of the game, avoiding all those memory operations (if you know how much these operations cost, already can imagine the relief that the CPU gave this part of the project).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhbnntikk0xiw08nsbypa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhbnntikk0xiw08nsbypa.png" alt="Vertex Transformation"&gt;&lt;/a&gt;&lt;/p&gt;
Process involved during the transformation of the 3D object to 2D rendering on the screen. Source: http://www.shangdixinxi.com/detail-1085554.html



&lt;h2&gt;
  
  
  Vertex Indexing
&lt;/h2&gt;

&lt;p&gt;Having all the 3D objects already using Vertex Buffers, we have already taken a lot of weight out of the way, but we can still improve performance.&lt;/p&gt;

&lt;p&gt;A 3D object is formed by a list of triangles and these triangles are formed by a list of vertices. At the intersection of two triangles, some vertices are used for both triangles, and what is the problem with this? The problem is that in Vertex Buffer, these vertices that are common between the two triangles are treated as if they were different. Therefore, when the vertices are passed to the GPU, thousands of them will be passed unnecessarily.&lt;/p&gt;

&lt;p&gt;To optimize this, in addition to the Vertex Buffer, we must now create an Index Buffer, which is a list that points to the position (index) of the vertex within the Vertex Buffer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpwu8gcq4mq723ywmj4dl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpwu8gcq4mq723ywmj4dl.png" alt="Vertex Indexing"&gt;&lt;/a&gt;&lt;/p&gt;
Source: OpenGL Tutorial



&lt;p&gt;To build this Index Buffer, it is necessary to consider the vertex position (XYZ) and also the UV texture coordinates when processing the triangles of that Mesh. When a vertex coincides with another that has been read before, it takes its index and places it in the list of indexes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Skeletal Animation
&lt;/h2&gt;

&lt;p&gt;One of the most crucial parts of the entire project in terms of performance, is the skinning of Meshes.Skinning can be summarized as the process responsible for the deformation of the 3D object in relation to its skeleton.&lt;/p&gt;

&lt;p&gt;The process is summarized in going through all the vertices of that object and multiplying each one of these vertices by the local matrix resulting from the skeleton to which the vertex is linked. As you can imagine, this process on the CPU, with variables of the whole type, matrix operations, and to make matters worse, performed with each frame of the game, is very expensive and one of the weakest points of all rendering.&lt;/p&gt;

&lt;p&gt;To optimize skinning, we must pass this process to be performed within the GPU. How can you do it? We use shaders!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Shaders is like a small program used for processing the vertices and pixels of a 3D model, which is executed by the GPU. I used the HLSL (High-Level Shading Language), developed by Microsoft.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Example above of how the Skinning done in HLSL would be. GetSkinMatrix () is the function in charge of getting the local Matrix of the skeleton to which that vertex is linked.&lt;/p&gt;

&lt;h2&gt;
  
  
  Culling
&lt;/h2&gt;

&lt;p&gt;Culling is the rendering step responsible for “selecting” the objects visible by the camera and determining whether they should be rendered or not. For this, each 3D object in the game must contain a kind of delimiter that represents the useful area of that object. I used bounding sphere for Models (set of Meshes) and bounding box for Meshes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmnr0yyfua8vgairoz4wr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmnr0yyfua8vgairoz4wr.png" alt="Culling"&gt;&lt;/a&gt;&lt;/p&gt;
Comparison between Bounding Sphere, Bounding Box and Quadtree



&lt;p&gt;Having these delimiters, we can perform the culling steps, which will determine whether the object should be rendered or not. For this project, I used Frustum Culling for all 3D objects (if there is no “inside” the camera area, the object is cut) and Quadtree Node Culling for terrain objects (maps).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0oq147ei95t2er39u3dk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0oq147ei95t2er39u3dk.png" alt="Frustum Culling"&gt;&lt;/a&gt;&lt;/p&gt;
A Survey of Visibility for Walkthrough Applications — Scientific Figure on ResearchGate. Source: https://www.researchgate.net/figure/Three-types-of-visibility-culling-techniques-1-View-Frustum-Culling-2-back-face_fig1_2440562



&lt;h2&gt;
  
  
  Outcomes
&lt;/h2&gt;

&lt;p&gt;With the end of the project, there was an improvement in FPS (frames per second), however, a little discreet on certain occasions. In certain cases, the FPS improved up to 3x over the previous graphics engine.&lt;/p&gt;

&lt;p&gt;As this project took a few months to complete, there was no test environment prepared to make the comparison between before and after, so I chose to select some common scenarios and see how many FPS the current graphics engine was reaching.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frp5ljglu7rzo0ntg0cmy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frp5ljglu7rzo0ntg0cmy.png" alt="Results 1"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpu1rod8xz9nboalw0ls6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpu1rod8xz9nboalw0ls6.png" alt="Results 2"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fu4r04hfb4aep4fhw5b7f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fu4r04hfb4aep4fhw5b7f.png" alt="Results 3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0rjl75a4sjytmzvpdm9n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0rjl75a4sjytmzvpdm9n.png" alt="Test with Monsters"&gt;&lt;/a&gt;&lt;/p&gt;
Upgraded graphics engine rendering 45 monsters



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foux6tk7n5bru62nqa9mv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foux6tk7n5bru62nqa9mv.png" alt="Test with Characters"&gt;&lt;/a&gt;&lt;/p&gt;
Upgraded graphics engine rendering 300 and 600 characters (body model, head model and items)



&lt;p&gt;The biggest noticeable difference in FPS between the old graphics engine and the new one, occurs when characters are being rendered on the screen. For characters, the Skinning of the head, body and also the equipped items should be considered. As in the old graphics engine Skinning was performed on the CPU, the FPS was much lower than the current one (performed on the GPU).&lt;/p&gt;

&lt;p&gt;In addition to the considerable improvement in the game’s FPS , with the new graphics engine it was possible to add several new graphic features, such as:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foja4ljgu7ljasdfxtuwc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Foja4ljgu7ljasdfxtuwc.gif" alt="Dynamic Shadows"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;b&gt;Dynamic Shadows:&lt;/b&gt; With the new graphics engine, it was possible to implement dynamic shadows on the characters, with the minimum requirement of Shader Model 3.0. Before, it was completely impossible to have a feature like this.



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbv6pndk33ef0dnl9zjek.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbv6pndk33ef0dnl9zjek.png" alt="Lightning Map"&gt;&lt;/a&gt;&lt;/p&gt;
For the case of maps, dynamic shadows were not used for visual and performance reasons. To get around this, the Lightning Map technique was used to produce the shadow effect.
Image for post



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvge55zghyc5c7vodp5v9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvge55zghyc5c7vodp5v9.gif" alt="Ocean Effect"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;b&gt;Ocean Effect:&lt;/b&gt; This effect was done as an experiment but the result was very nice. It was implemented a refraction system, reflection and some material effects (scrolling).



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhiiyuhulohwn17bila3z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhiiyuhulohwn17bila3z.png" alt="Intersection Shader"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;b&gt;Intersection of objects:&lt;/b&gt; Effect widely used in water shaders and lately in games in the style of Battle Royale, used to represent the safe zone of the game.



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3hguwhi8deu6houob7w4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3hguwhi8deu6houob7w4.png" alt="Inner Glow"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;b&gt;Inner Glow:&lt;/b&gt; internal glow effect for any 3D model. You can control color, brightness intensity, distance, etc.



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flnoq2s5fi78uxodu53rt.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flnoq2s5fi78uxodu53rt.gif" alt="Dissolve Effect"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;b&gt;Dissolve effect:&lt;/b&gt; when a monster dies, instead of it staying on the ground and disappearing from nowhere, there is a specific effect for that.




&lt;h1&gt;
  
  
  Conclusions
&lt;/h1&gt;

&lt;p&gt;Although the performance of a graphics engine is quite relative, depending on hardware, enabled features, resolution, game settings etc., I expected a more significant improvement in FPS, mainly due to the time dedicated and complexity of the project as a whole.&lt;/p&gt;

&lt;p&gt;In contrast, the new graphics engine allowed the development of several new features that would have been impossible previously, in addition to all the knowledge acquired with the project.&lt;/p&gt;

&lt;p&gt;There are still a lot of things that can be optimized in the future and bring even higher performance, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Build a State Manager (a kind of cache of Directx states)&lt;/li&gt;
&lt;li&gt;Use a circular buffer as a cache for animation arrays&lt;/li&gt;
&lt;li&gt;Grouping frames by type of animations&lt;/li&gt;
&lt;li&gt;Refactor some Shaders&lt;/li&gt;
&lt;li&gt;Avoid division and multiplication operations by 256.f&lt;/li&gt;
&lt;li&gt;Improve Culling with new techniques (Back-Face Culling and Occlusion)&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;The entire project is available in a public repository on GitHub, licensed under the MIT license.&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/igorsegallafa" rel="noopener noreferrer"&gt;
        igorsegallafa
      &lt;/a&gt; / &lt;a href="https://github.com/igorsegallafa/delta3d" rel="noopener noreferrer"&gt;
        delta3d
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Simple rendering engine made for Priston Tale game.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Delta3D&lt;/h1&gt;
&lt;/div&gt;
&lt;p&gt;Project of a basic Game Engine that I created for learn 3D stuff. The library was made using Directx9 with Shaders and C++ 17.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;License&lt;/h2&gt;
&lt;/div&gt;
&lt;p&gt;Licensed under the MIT License.&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;Features&lt;/h2&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;Support for Pixel Shader 2.0 and 3.0.&lt;/li&gt;
&lt;li&gt;Support for Hardware and Software Skinning.&lt;/li&gt;
&lt;li&gt;Support for Lightning Map (Self Illumination map).&lt;/li&gt;
&lt;li&gt;Support for old devices.&lt;/li&gt;
&lt;li&gt;Support for material overlay.&lt;/li&gt;
&lt;li&gt;Support for Vertex Color.&lt;/li&gt;
&lt;li&gt;Supports up to 128 bones in Skinning.&lt;/li&gt;
&lt;li&gt;Support for SMD File Format from Priston Tale game.&lt;/li&gt;
&lt;li&gt;Use of SSE2 for floating optimization.&lt;/li&gt;
&lt;li&gt;Static Quad Tree for Terrain rendering.&lt;/li&gt;
&lt;li&gt;Particle Engine.&lt;/li&gt;
&lt;li&gt;Mesh Rendering Sort (transparent meshes renders last).&lt;/li&gt;
&lt;li&gt;Distance Fade at Pixel Shader.&lt;/li&gt;
&lt;li&gt;Initial implementation of reflection plane.&lt;/li&gt;
&lt;li&gt;Dynamic Lightning.&lt;/li&gt;
&lt;li&gt;Material Transformation (Scroll).&lt;/li&gt;
&lt;li&gt;Material with Animated Texture (sequence of frames).&lt;/li&gt;
&lt;li&gt;Camera implementation.&lt;/li&gt;
&lt;li&gt;Dynamic Event and Timer implementation.&lt;/li&gt;
&lt;li&gt;Frustum Culling.&lt;/li&gt;
&lt;li&gt;Value Animation with Easing.&lt;/li&gt;
&lt;li&gt;Debug Renderer.&lt;/li&gt;
&lt;li&gt;Good performance for scene with a lot of Skinned Meshes and big…&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/igorsegallafa/delta3d" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


</description>
      <category>cpp</category>
      <category>c</category>
      <category>gamedev</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
