mirror of
https://github.com/kuhyx/engineer-thesis-WUT.git
synced 2026-07-04 13:23:09 +02:00
feat: added shader class dsecription
This commit is contained in:
parent
7b9961455c
commit
22865b699c
@ -991,9 +991,7 @@ The engine's architecture is segmented into modules:
|
||||
|
||||
\item Game Level: (game\_level.cpp) Loads and saves game levels.
|
||||
|
||||
\item Particle Generator: (particle\_generator.cpp) Generates particle effects for tiles
|
||||
|
||||
\item Post Processor: (post\_processor.cpp) Combines particle and shader effects
|
||||
\item Particle Generator: (particle\_generator.cpp) Generates particle effects for tilesz
|
||||
|
||||
\item Resource Manager: (resource\_manager.cpp) Handles assets and shaders files
|
||||
|
||||
@ -1026,10 +1024,6 @@ The engine's architecture is segmented into modules:
|
||||
.3 game\_object.cpp\DTcomment{Used for tiles}.
|
||||
.3 game\_object.h.
|
||||
.3 glad.c.
|
||||
.3 post\_processing.fs.
|
||||
.3 post\_processing.vs.
|
||||
.3 post\_processor.cpp.
|
||||
.3 post\_processor.h.
|
||||
.3 resource\_manager.cpp\DTcomment{For loading assets}.
|
||||
.3 resource\_manager.h.
|
||||
.3 shader.cpp.
|
||||
@ -1190,7 +1184,6 @@ Game class defined in game.h and game.cpp is the main and biggest class in the p
|
||||
#include "./filesystem.h"
|
||||
#include "./game_level.h"
|
||||
#include "./game_object.h"
|
||||
#include "./post_processor.h"
|
||||
#include "./resource_manager.h"
|
||||
#include "./sprite_renderer.h"
|
||||
#include "./text_renderer.h"
|
||||
@ -1865,6 +1858,121 @@ x_{pos} = x_{start} + x_{bearing} * s
|
||||
y_{pos} = y_{start} + (H_{bearing} - y_{bearing}) * s
|
||||
\]
|
||||
|
||||
\subsection{Resource Manager}
|
||||
Resource manager class loads textures and shaders from files.
|
||||
|
||||
\paragraph{Loading textures}
|
||||
After giving a name of texture to a method it is loaded (converted to raw byte data) using stb image library, we receive texture width, height and number of channels and later use it to generate texture using texture class. At the end we free image data
|
||||
|
||||
\begin{lstlisting}[style=C++Style]
|
||||
// RGB for Red Green Blue, RGBA for alpha channel
|
||||
GLenum getTextureFormat(int nrChannels) {
|
||||
switch (nrChannels) {
|
||||
case 1: return GL_RED;
|
||||
case 3: return GL_RGB;
|
||||
case 4: return GL_RGBA;
|
||||
default:
|
||||
throw std::runtime_error("Unsupported number of channels in the image.");
|
||||
}
|
||||
}
|
||||
|
||||
Texture2D ResourceManager::loadTextureFromFile(const char *file)
|
||||
{
|
||||
// create texture object
|
||||
Texture2D texture;
|
||||
// load image
|
||||
int width;
|
||||
int height;
|
||||
int nrChannels;
|
||||
unsigned char* data = stbi_load(file, &width, &height, &nrChannels, 0);
|
||||
GLenum format = getTextureFormat(nrChannels);
|
||||
texture.Internal_Format = format;
|
||||
texture.Image_Format = format;
|
||||
// now generate texture
|
||||
texture.Generate(width, height, data);
|
||||
// and finally free image data
|
||||
stbi_image_free(data);
|
||||
return texture;
|
||||
}
|
||||
\end{lstlisting}
|
||||
|
||||
\subsubsection{Loading shaders}
|
||||
In order to load shaders we load up to three different source codes for vertex, fragment and geometry shaders and compile them in shader class.
|
||||
|
||||
\paragraph{Shader types}
|
||||
\begin{itemize}
|
||||
\item \textbf{Vertex Shader:} This is the first stage in the graphics pipeline that processes each vertex and is responsible for transforming vertex positions from object space (local coordinates) to clip space. Additionally, it can manipulate vertex attributes such as colors, normals, and texture coordinates.
|
||||
\item \textbf{Fragment Shader:} Often referred to as a pixel shader, this stage operates on each fragment (potential pixel) generated by rasterization. It determines the final color of the pixels on the screen. Fragment shaders can apply textures, compute lighting, and handle other surface details.
|
||||
\item \textbf{Geometry Shader:} This is an optional shader stage that sits between the vertex and fragment shaders. It can generate new graphics primitives (like points, lines, and triangles) from input primitives. It offers more flexibility in processing than the vertex shader but can be computationally intensive.
|
||||
\end{itemize}
|
||||
|
||||
\subsection{Shader class}
|
||||
Shader object compiles shader from shader string code and sets its input parameters
|
||||
|
||||
\subsubsection{Compiling shader}
|
||||
When compiling shader we provide shader source code, create shader from shader type (vertex, fragment or geometry), compile it, attach to program and link program so that it can be used by engine, at the end we remove the shader code as it is already in our engine in order to free resources.
|
||||
|
||||
\begin{lstlisting}[style=C++Style]
|
||||
unsigned int Shader::compileShader(const char *shaderSource, const std::string& type, const GLenum shaderType) {
|
||||
unsigned int shaderPointer = 0;
|
||||
if(shaderSource != nullptr) {
|
||||
shaderPointer = glCreateShader(shaderType);
|
||||
glShaderSource(shaderPointer, 1, &shaderSource, nullptr);
|
||||
glCompileShader(shaderPointer);
|
||||
checkCompileErrors(shaderPointer, type);
|
||||
}
|
||||
return shaderPointer;
|
||||
}
|
||||
|
||||
void Shader::Compile(const char* vertexSource, const char* fragmentSource, const char* geometrySource)
|
||||
{
|
||||
unsigned int sVertex = this->compileShader(vertexSource, "VERTEX", GL_VERTEX_SHADER);
|
||||
// shader program
|
||||
this->ID = glCreateProgram();
|
||||
glAttachShader(this->ID, sVertex);
|
||||
...
|
||||
glLinkProgram(this->ID);
|
||||
checkCompileErrors(this->ID, "PROGRAM");
|
||||
// delete the shaders as they're linked into our program now and no longer necessary
|
||||
glDeleteShader(sVertex);
|
||||
...
|
||||
}
|
||||
\end{lstlisting}
|
||||
|
||||
\begin{figure}[htp]
|
||||
\centering
|
||||
\begin{tikzpicture}[node distance=1.5cm]
|
||||
\useasboundingbox (-5,0) rectangle (5, -13cm); % Set a custom bounding box
|
||||
\node (start) [startstop] {Processing shader};
|
||||
\node (pro1) [process, below of=start] {Create shader};
|
||||
\draw [arrow] (start) -- (pro1);
|
||||
|
||||
\node (pro2) [process, below of=pro1] {Get shader source};
|
||||
\draw [arrow] (pro1) -- (pro2);
|
||||
|
||||
\node (pro3) [process, below of=pro2] {Compile shader};
|
||||
\draw [arrow] (pro2) -- (pro3);
|
||||
|
||||
\node (pro4) [process, below of=pro3] {Create shader program};
|
||||
\draw [arrow] (pro3) -- (pro4);
|
||||
|
||||
\node (pro5) [process, below of=pro4] {Attach shader to program};
|
||||
\draw [arrow] (pro4) -- (pro5);
|
||||
|
||||
\node (pro6) [process, below of=pro5] {Link program to engine};
|
||||
\draw [arrow] (pro5) -- (pro6);
|
||||
|
||||
\node (pro7) [process, below of=pro6] {Delete shaders};
|
||||
\draw [arrow] (pro6) -- (pro7);
|
||||
|
||||
\node (stop) [startstop, below of=pro7] {Finish processing};
|
||||
\draw [arrow] (pro7) -- (stop);
|
||||
|
||||
\end{tikzpicture}
|
||||
\caption{Processing shader}
|
||||
\label{fig:processShader}
|
||||
\end{figure}
|
||||
|
||||
\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.
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user