mirror of
https://github.com/kuhyx/engineer-thesis-WUT.git
synced 2026-07-04 11:43:05 +02:00
feat: added text rendering class
This commit is contained in:
parent
a311ece772
commit
7b9961455c
@ -1791,13 +1791,13 @@ void TextRenderer::Load(const std::string &font, unsigned int fontSize)
|
||||
\label{fig:loadingFont}
|
||||
\end{figure}
|
||||
|
||||
\paragraph{Loading characters}
|
||||
\subsubsection{Loading characters}
|
||||
In order to preload ASCII characters we use a method loadCharacter from TextRenderer, it uses FreeType library to load character, then generates textures for a character (it needs to allign a texture to character), sets options for texture and adds the character object to characters array
|
||||
|
||||
\begin{figure}[htp]
|
||||
\centering
|
||||
\begin{tikzpicture}[node distance=1.5cm]
|
||||
\useasboundingbox (-5,0) rectangle (5, -13cm); % Set a custom bounding box
|
||||
\useasboundingbox (-5,0) rectangle (5, -10cm); % Set a custom bounding box
|
||||
\node (start) [startstop] {Load character};
|
||||
\node (pro1) [process, below of=start] {Load character using FreeType};
|
||||
\draw [arrow] (start) -- (pro1);
|
||||
@ -1822,6 +1822,49 @@ In order to preload ASCII characters we use a method loadCharacter from TextRend
|
||||
\label{fig:loadingCharacter}
|
||||
\end{figure}
|
||||
|
||||
\newpage
|
||||
\paragraph{Texture options}
|
||||
|
||||
\begin{itemize}
|
||||
\item The first two lines set the wrapping mode of the texture in the S and T directions (usually the x and y axes) to \texttt{GL\_CLAMP\_TO\_EDGE}. This means if the texture coordinates go beyond [0,1], the texture will not repeat but instead clamp to the edge values.
|
||||
\item The next two lines set the minification and magnification filter to \texttt{GL\_LINEAR}. This means when the texture is scaled down or up, it will use linear interpolation between the texture coordinates, resulting in a smoother texture appearance.
|
||||
\end{itemize}
|
||||
|
||||
\begin{lstlisting}[style=C++Style]
|
||||
void TextRenderer::setTextureOptions() const {
|
||||
// set texture options
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
}
|
||||
\end{lstlisting}
|
||||
|
||||
\newpage
|
||||
\subsubsection{Rendering text}
|
||||
We are given text as a C++ string and a position, method is supposed to automatically render text so that it fits in a position that was provided \\
|
||||
We achieve that by iterating through each character of a string and putting it next to each other at a gap equal to a previous character width and height
|
||||
|
||||
\begin{figure}[htp]
|
||||
\centering
|
||||
\includegraphics[scale=1.00]{images/glyph_offset.png}
|
||||
\caption{Glyph metrics from learnopengl}
|
||||
\label{}
|
||||
\end{figure}
|
||||
|
||||
Calculating x and y positions of each character \\
|
||||
$x_{pos}$ and $y_{pos}$ is the final x/y position of character \\
|
||||
$x_{start}$ and $y_{start}$ is the position inputed to the method render text \\
|
||||
$x_{bearing}$ and $y_{bearing}$ is a parameter shown in image above \\
|
||||
$s$ is scale \\
|
||||
We calculate final bearing of $y_{pos}$ by subtracting bearing of "H" character from the bearing of actual character, we do it this way because "y" bearing of "H" character is highest \\
|
||||
\[
|
||||
x_{pos} = x_{start} + x_{bearing} * s
|
||||
\]
|
||||
\[
|
||||
y_{pos} = y_{start} + (H_{bearing} - y_{bearing}) * s
|
||||
\]
|
||||
|
||||
\subsection{Dependency Management}
|
||||
There are several libraries: OpenGL, GLFW, stb\_image and freetype being integrated into engine, they are simply included at the top of any files that need them.
|
||||
|
||||
|
||||
BIN
Thesis/images/glyph_offset.png
Normal file
BIN
Thesis/images/glyph_offset.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 7.3 KiB |
Loading…
Reference in New Issue
Block a user