Game Physics Template 1.0
A template for game physics in C++ WEBGPU
|
#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. | |
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.
enum Renderer::DrawFlags |
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
position | The center of the cube |
rotation | The rotation of the cube |
scale | The scale of the cube. scale (1,1,1) will result in a cube with side length 1 |
color | The color of the cube: (r, g, b, a) |
flags | Flags 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 |
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
position | The center of the ellipsoid |
rotation | The rotation of the ellipsoid |
scale | The 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 |
color | The color of the ellipsoid: (r, g, b, a) |
flags | Flags 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 |
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!
data | The image data. The data should be a 1D vectro of floats, where each float represents a pixel. Pixel rows should be concatenated. |
height | Number of pixels in y direction of the input data |
width | Number of pixels in x direction of the input data |
colormap | The colormap to use. Default is "hot". See Colormap documentation for available colormaps (all from matplotlib). |
screenPosition | The center of the image on the screen. (0,0) is the middle of the screen, (1,1) is the top right corner. |
screenSize | The size of the image on the screen. (1,1) will fill the whole screen. |
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.
data | The image data. The data should be a 1D vectro of floats, where each float represents a pixel. Pixel rows should be concatenated. |
height | Number of pixels in y direction of the input data |
width | Number of pixels in x direction of the input data |
vmin | The minimum value of the data. All values below this will be clipped. |
vmax | The maximum value of the data. All values above this will be clipped. |
colormap | The colormap to use. Default is "hot". See Colormap documentation for available colormaps (all from matplotlib). |
screenPosition | The center of the image on the screen. (0,0) is the middle of the screen, (1,1) is the top right corner. |
screenSize | The size of the image on the screen. (1,1) will fill the whole screen. |
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
position1 | The start of the line |
position2 | The end of the line |
color | The color of the line: (r, g, b). lines do not support transparency |
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
position1 | The start of the line |
position2 | The end of the line |
color1 | The color of the start of the line: (r, g, b). |
color2 | The color of the end of the line: (r, g, b). |
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
position | The center of the quad |
rotation | The rotation of the quad |
scale | The scale of the quad. scale (1,1) will result in a quad with side length 1. |
color | The color of the quad: (r, g, b, a) |
flags | Flags 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 |
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
position | The center of the sphere |
radius | The radius of the sphere |
color | The color of the sphere: (r, g, b, a) |
flags | Flags 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 |
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
position | The center of the cube |
scale | The scale of the cube. scale (1,1,1) will result in a cube with side length 1 |
color | The color of the wire cube: (r, g, b) |
|
inline |
Get the number of images to be drawn next frame.
bool Renderer::isRunning | ( | ) |
Check if the window is still open. If the window is closed, the rendering engine will stop.
|
inline |
Get the number of lines to be drawn next frame.
|
inline |
Get the number of spheres, ellipsoids, cubes and quads to be drawn next frame.