Game Physics Template 1.0
A template for game physics in C++ WEBGPU
Loading...
Searching...
No Matches
Renderer Class Reference

Renderer. More...

#include <Renderer.h>

Classes

struct  LightingUniforms
 Uniform buffer for lighting. More...
 
struct  RenderUniforms
 Essential uniform buffer for rendering. More...
 

Public Types

enum  DrawFlags { unlit = 1 << 0 , dontCull = 1 << 1 }
 Flags to modify the rendering of objects. More...
 
enum  UniformFlags { cullingPlane = 1 << 0 }
 If enabled, the renderer will draw the culling plane.
 

Public Member Functions

uint32_t drawCube (glm::vec3 position=vec3(0), glm::quat rotation=glm::quat(vec3(0)), glm::vec3 scale=vec3(1), glm::vec4 color=vec4(1), uint32_t flags=0)
 Draw a cube in the next frame.
 
uint32_t drawSphere (glm::vec3 position=vec3(0), float radius=1, glm::vec4 color=vec4(1), uint32_t flags=0)
 Draw a sphere in the next frame.
 
uint32_t drawEllipsoid (glm::vec3 position=vec3(0), glm::quat rotation=glm::quat(vec3(0)), glm::vec3 scale=vec3(1), glm::vec4 color=vec4(1), uint32_t flags=0)
 Draw an ellipsoid in the next frame.
 
uint32_t drawQuad (glm::vec3 position=vec3(0), glm::quat rotation=glm::quat(vec3(0)), glm::vec2 scale=glm::vec2(1), glm::vec4 color=vec4(1), uint32_t flags=0)
 Draw a quad in the next frame.
 
void drawLine (glm::vec3 position1, glm::vec3 position2, glm::vec3 color)
 Draw a line in the next frame.
 
void drawLine (glm::vec3 position1, glm::vec3 position2, glm::vec3 color1, glm::vec3 color2)
 Draw a line with a color gradient in the next frame.
 
void drawWireCube (glm::vec3 position=vec3(0), glm::vec3 scale=vec3(1), glm::vec3 color=vec3(1))
 Draw a wire cube in the next frame.
 
void drawImage (std::vector< float > data, int height, int width, Colormap colormap=Colormap("hot"), glm::vec2 screenPosition={0, 0}, glm::vec2 screenSize={1, 1})
 Draw an image in the next frame.
 
void drawImage (std::vector< float > data, int height, int width, float vmin, float vmax, Colormap colormap=Colormap("hot"), glm::vec2 screenPosition={0, 0}, glm::vec2 screenSize={1, 1})
 Draw an image in the next frame.
 
size_t objectCount ()
 Get the number of spheres, ellipsoids, cubes and quads to be drawn next frame.
 
size_t lineCount ()
 Get the number of lines to be drawn next frame.
 
size_t imageCount ()
 Get the number of images to be drawn next frame.
 
void onFrame ()
 Draw all current objects to the screen.
 
bool isRunning ()
 Check if the window is still open. If the window is closed, the rendering engine will stop.
 
void onResize ()
 Callback function that is called when the window is resized.
 
void clearScene ()
 Clear all objects that are currently drawn.
 
void setPresentMode (wgpu::PresentMode mode)
 Enable or disable frame rate synchronization.
 

Public Attributes

glm::vec3 backgroundColor = {0.05f, 0.05f, 0.05f}
 The background color of the scene.
 
std::function< void()> defineGUI = nullptr
 This function is called once per frame inside an ImGui context.
 
double lastDrawTime = 0
 Tracks the time taken up by the actual rendering last frame.
 
RenderUniforms renderUniforms
 
LightingUniforms lightingUniforms
 

Static Public Attributes

static Camera camera = Camera()
 The main camera of the scene.
 

Detailed Description

Renderer.

This class is the main interface to the rendering engine. It provides functions to draw cubes, spheres, lines, images, etc. One instance of this class should be created in the main function of the application. The class is responsible for creating the window, initializing the device, and handling the rendering. After each onFrame call, the rendering engine will draw the scene and present it to the screen. renderer.clearScreen() should be called at the beginning of each frame to clear all added draw objects. Otherwise, the objects will be drawn in every frame.

Member Enumeration Documentation

◆ DrawFlags

Flags to modify the rendering of objects.

Enumerator
unlit 

The object will be drawn without lighting, meaning full brightness.

dontCull 

The object will be drawn even if it is cut by the culling plane.

Member Function Documentation

◆ drawCube()

uint32_t Renderer::drawCube ( glm::vec3  position = vec3(0),
glm::quat  rotation = glm::quat(vec3(0)),
glm::vec3  scale = vec3(1),
glm::vec4  color = vec4(1),
uint32_t  flags = 0 
)

Draw a cube in the next frame.

Call this function every frame you want to draw a cube

Parameters
positionThe center of the cube
rotationThe rotation of the cube
scaleThe scale of the cube. scale (1,1,1) will result in a cube with side length 1
colorThe color of the cube: (r, g, b, a)
flagsFlags to modify the cube: Renderer::DrawFlags. Can be combined with bitwise OR. Possible flags:
- unlit: The cube will be drawn without lighting, meaning full brightness

- dontCull: The cube will be drawn even if it is cut by the culling plane
Returns
The id of the cube.

◆ drawEllipsoid()

uint32_t Renderer::drawEllipsoid ( glm::vec3  position = vec3(0),
glm::quat  rotation = glm::quat(vec3(0)),
glm::vec3  scale = vec3(1),
glm::vec4  color = vec4(1),
uint32_t  flags = 0 
)

Draw an ellipsoid in the next frame.

Call this function every frame you want to draw an ellipsoid

Parameters
positionThe center of the ellipsoid
rotationThe rotation of the ellipsoid
scaleThe scale of the ellipsoid. scale (1,1,1) will result in a sphere with radius 1, scale (1,2,1) will result in an ellipsoid with radius 1 in x and z direction and 2 in y direction
colorThe color of the ellipsoid: (r, g, b, a)
flagsFlags to modify the ellipsoid: Renderer::DrawFlags. Can be combined with bitwise OR. Possible flags:
- unlit: The ellipsoid will be drawn without lighting, meaning full brightness

- dontCull: The ellipsoid will be drawn even if it is cut by the culling plane
Returns
The id of the ellipsoid.

◆ drawImage() [1/2]

void Renderer::drawImage ( std::vector< float >  data,
int  height,
int  width,
Colormap  colormap = Colormap("hot"),
glm::vec2  screenPosition = {0, 0},
glm::vec2  screenSize = {1, 1} 
)

Draw an image in the next frame.

Call this function every frame you want to draw an image.

The colormap will automatically adjust to the data!

Parameters
dataThe image data. The data should be a 1D vectro of floats, where each float represents a pixel. Pixel rows should be concatenated.
heightNumber of pixels in y direction of the input data
widthNumber of pixels in x direction of the input data
colormapThe colormap to use. Default is "hot". See Colormap documentation for available colormaps (all from matplotlib).
screenPositionThe center of the image on the screen. (0,0) is the middle of the screen, (1,1) is the top right corner.
screenSizeThe size of the image on the screen. (1,1) will fill the whole screen.

◆ drawImage() [2/2]

void Renderer::drawImage ( std::vector< float >  data,
int  height,
int  width,
float  vmin,
float  vmax,
Colormap  colormap = Colormap("hot"),
glm::vec2  screenPosition = {0, 0},
glm::vec2  screenSize = {1, 1} 
)

Draw an image in the next frame.

Call this function every frame you want to draw an image. Use this if you do not want the colormap range to change each frame.

Parameters
dataThe image data. The data should be a 1D vectro of floats, where each float represents a pixel. Pixel rows should be concatenated.
heightNumber of pixels in y direction of the input data
widthNumber of pixels in x direction of the input data
vminThe minimum value of the data. All values below this will be clipped.
vmaxThe maximum value of the data. All values above this will be clipped.
colormapThe colormap to use. Default is "hot". See Colormap documentation for available colormaps (all from matplotlib).
screenPositionThe center of the image on the screen. (0,0) is the middle of the screen, (1,1) is the top right corner.
screenSizeThe size of the image on the screen. (1,1) will fill the whole screen.

◆ drawLine() [1/2]

void Renderer::drawLine ( glm::vec3  position1,
glm::vec3  position2,
glm::vec3  color 
)

Draw a line in the next frame.

Call this function every frame you want to draw a line

Parameters
position1The start of the line
position2The end of the line
colorThe color of the line: (r, g, b). lines do not support transparency

◆ drawLine() [2/2]

void Renderer::drawLine ( glm::vec3  position1,
glm::vec3  position2,
glm::vec3  color1,
glm::vec3  color2 
)

Draw a line with a color gradient in the next frame.

Call this function every frame you want to draw a line

Parameters
position1The start of the line
position2The end of the line
color1The color of the start of the line: (r, g, b).
color2The color of the end of the line: (r, g, b).

◆ drawQuad()

uint32_t Renderer::drawQuad ( glm::vec3  position = vec3(0),
glm::quat  rotation = glm::quat(vec3(0)),
glm::vec2  scale = glm::vec2(1),
glm::vec4  color = vec4(1),
uint32_t  flags = 0 
)

Draw a quad in the next frame.

Call this function every frame you want to draw a quad

Parameters
positionThe center of the quad
rotationThe rotation of the quad
scaleThe scale of the quad. scale (1,1) will result in a quad with side length 1.
colorThe color of the quad: (r, g, b, a)
flagsFlags to modify the quad: Renderer::DrawFlags. Can be combined with bitwise OR. Possible flags:
- unlit: The quad will be drawn without lighting, meaning full brightness

- dontCull: The quad will be drawn even if it is cut by the culling plane
Returns
The id of the quad.

◆ drawSphere()

uint32_t Renderer::drawSphere ( glm::vec3  position = vec3(0),
float  radius = 1,
glm::vec4  color = vec4(1),
uint32_t  flags = 0 
)

Draw a sphere in the next frame.

Call this function every frame you want to draw a sphere

Parameters
positionThe center of the sphere
radiusThe radius of the sphere
colorThe color of the sphere: (r, g, b, a)
flagsFlags to modify the sphere: Renderer::DrawFlags. Can be combined with bitwise OR. Possible flags:
- unlit: The sphere will be drawn without lighting, meaning full brightness

- dontCull: The sphere will be drawn even if it is cut by the culling plane
Returns
The id of the sphere.

◆ drawWireCube()

void Renderer::drawWireCube ( glm::vec3  position = vec3(0),
glm::vec3  scale = vec3(1),
glm::vec3  color = vec3(1) 
)

Draw a wire cube in the next frame.

Call this function every frame you want to draw a wire cube

Parameters
positionThe center of the cube
scaleThe scale of the cube. scale (1,1,1) will result in a cube with side length 1
colorThe color of the wire cube: (r, g, b)

◆ imageCount()

size_t Renderer::imageCount ( )
inline

Get the number of images to be drawn next frame.

Returns
The number of images to be drawn next frame

◆ isRunning()

bool Renderer::isRunning ( )

Check if the window is still open. If the window is closed, the rendering engine will stop.

Returns
True if the window is still open, false if the window is closed

◆ lineCount()

size_t Renderer::lineCount ( )
inline

Get the number of lines to be drawn next frame.

Returns
The number of lines to be drawn next frame

◆ objectCount()

size_t Renderer::objectCount ( )
inline

Get the number of spheres, ellipsoids, cubes and quads to be drawn next frame.

Returns
The number of objects to be drawn next frame

The documentation for this class was generated from the following files: