CGVIS Utils
Loading...
Searching...
No Matches
GLApp Class Reference

Simple app runner with a GL context, stock shaders, and draw helpers. More...

#include <GLApp.h>

Collaboration diagram for GLApp:
[legend]

Public Member Functions

 GLApp (uint32_t w=640, uint32_t h=480, uint32_t s=4, const std::string &title="My OpenGL App", bool fpsCounter=true, bool sync=true)
 Construct the environment and boot the stock shaders/buffers.
virtual ~GLApp ()
 Virtual destructor.
void run ()
 Create the context/window and enter the render loop.
void setAnimation (bool animationActive)
 Enable/disable the animation step inside the main loop.
bool getAnimation () const
 Query whether animation is currently active.
void resetAnimation ()
 Reset the animation timer and invoke animate(0).
float getAspect () const
 Current window aspect ratio (width/height).
void setImageFilter (GLint magFilter, GLint minFilter)
 Update filtering for images drawn by drawImage().
virtual void init ()
 One‑time initialization hook (resources, buffers, etc.).
virtual void draw ()
 Per‑frame draw hook.
virtual void animate (double animationTime)
 Per‑frame animation/update hook; parameter is seconds since start.
virtual void resize (int width, int height)
 Resize notification; override to update projection, etc.
virtual void keyboard (int key, int scancode, int action, int mods)
 Keyboard event; parameters mirror backend callbacks.
virtual void keyboardChar (unsigned int key)
 Unicode character input.
virtual void mouseMove (double xPosition, double yPosition)
 Mouse move event (window coordinates).
virtual void mouseButton (int button, int state, int mods, double xPosition, double yPosition)
 Mouse button event with cursor position.
virtual void mouseWheel (double x_offset, double y_offset, double xPosition, double yPosition)
 Mouse wheel/trackpad scroll event with cursor position.
Rect / textured quad
void drawRect (const Vec4 &color, const Vec2 &bl, const Vec2 &tr)
 Draw a solid‑color rectangle given 2D corners.
void drawRect (const Vec4 &color, const Vec3 &bl=Vec3{-1.0f,-1.0f, 0.0f}, const Vec3 &br=Vec3{1.0f,-1.0f, 0.0f}, const Vec3 &tl=Vec3{-1.0f, 1.0f, 0.0f}, const Vec3 &tr=Vec3{1.0f, 1.0f, 0.0f})
 Draw a solid‑color rectangle given 3D corners (defaults to NDC quad).
void drawImage (const GLTexture2D &image, const Vec2 &bl, const Vec2 &tr)
 Draw a textured quad from a texture handle using 2D corners.
void drawImage (const Image &image, const Vec2 &bl, const Vec2 &tr)
 Draw a textured quad from an Image using 2D corners.
void drawImage (const GLTexture2D &image, const Vec3 &bl=Vec3{-1.0f,-1.0f, 0.0f}, const Vec3 &br=Vec3{1.0f,-1.0f, 0.0f}, const Vec3 &tl=Vec3{-1.0f, 1.0f, 0.0f}, const Vec3 &tr=Vec3{1.0f, 1.0f, 0.0f})
 Draw a textured quad from a texture handle using 3D corners.
void drawImage (const Image &image, const Vec3 &bl=Vec3{-1.0f,-1.0f, 0.0f}, const Vec3 &br=Vec3{1.0f,-1.0f, 0.0f}, const Vec3 &tl=Vec3{-1.0f, 1.0f, 0.0f}, const Vec3 &tr=Vec3{1.0f, 1.0f, 0.0f})
 Draw a textured quad from an Image using 3D corners.
Primitives
void drawPoints (const std::vector< float > &data, float pointSize=1.0f, bool useTex=false)
 Draw an array of colored points.
void drawLines (const std::vector< float > &data, LineDrawType t, float lineThickness=1.0f)
 Draw colored lines using the given topology.
void drawTriangles (const std::vector< float > &data, TrisDrawType t, bool wireframe, bool lighting)
 Upload triangle data and draw immediately (or as wireframe edges).
void redrawTriangles (bool wireframe)
 Re‑issue the last triangle draw using cached settings.
Transforms
Mat4 computeImageTransform (const Vec2ui &imageSize) const
 Compute a uniform image scale so the longer side fits the window.
Mat4 computeImageTransformFixedHeight (const Vec2ui &imageSize, float height=1.0f, const Vec3 &center=Vec3{0.0f, 0.0f, 0.0f}) const
 Compute a transform that fits an image to a given height.
Mat4 computeImageTransformFixedWidth (const Vec2ui &imageSize, float width=1.0f, const Vec3 &center=Vec3{0.0f, 0.0f, 0.0f}) const
 Compute a transform that fits an image to a given width.
View/projection
void setDrawProjection (const Mat4 &mat)
 Set the projection matrix used by the stock shaders.
void setDrawTransform (const Mat4 &mat)
 Set the model‑view matrix used by the stock shaders.
Mat4 getDrawProjection () const
 Current projection matrix.
Mat4 getDrawTransform () const
 Current model‑view matrix.
Point sprite textures
void resetPointTexture (uint32_t resolution=64)
 Create a default circular mask texture of given resolution.
void setPointTexture (const std::vector< uint8_t > &shape, uint32_t x, uint32_t y, uint8_t components)
 Set the point sprite from raw bytes.
void setPointTexture (const Image &shape)
 Set the point sprite from an Image.
void setPointHighlightTexture (const Image &shape)
 Set an optional highlight overlay sprite from an Image.
void resetPointHighlightTexture ()
 Clear the highlight overlay sprite.

Protected Member Functions

void shaderUpdate ()
 Update stock shader uniforms (MVP/MV/MVit) to current transforms.
void closeWindow ()
 Request closing the window.

Protected Attributes

GLEnv glEnv
 Window/context + platform utilities.
Mat4 p
 Projection matrix used by stock shaders.
Mat4 mv
 Model‑view matrix used by stock shaders.
Mat4 mvi
 Inverse of mv (for lighting helpers).
GLProgram simpleProg
 Solid color shader (pos+color).
GLProgram simplePointProg
 Point shader (pos+color), desktop uses glPointSize.
GLProgram simpleSpriteProg
 Point sprite shader (pos+color+texture).
GLProgram simpleHLSpriteProg
 Point sprite + highlight overlay.
GLProgram simpleTexProg
 Textured quad shader (pos+uv).
GLProgram simpleLightProg
 Lit shader (pos+color+normal).
GLArray simpleArray
 VAO used by helpers.
GLBuffer simpleVb
 VBO used by helpers.
GLTexture2D raster
 Temporary texture for drawImage(Image).
GLTexture2D pointSprite
 Point‑sprite base texture.
GLTexture2D pointSpriteHighlight
 Optional highlight overlay sprite.
double resumeTime
 Timestamp used to keep animation time continuous on resume.

Detailed Description

Simple app runner with a GL context, stock shaders, and draw helpers.

Typical usage:

struct MyApp : GLApp {
void init() override { /* load data */ }
void draw() override { /* issue draws */ }
void animate(double t) override { /* update state */ }
};
int main(){ MyApp().run(); }
virtual void animate(double animationTime)
Per‑frame animation/update hook; parameter is seconds since start.
Definition GLApp.h:216
virtual void draw()
Per‑frame draw hook.
Definition GLApp.h:214
GLApp(uint32_t w=640, uint32_t h=480, uint32_t s=4, const std::string &title="My OpenGL App", bool fpsCounter=true, bool sync=true)
Construct the environment and boot the stock shaders/buffers.
Definition GLApp.cpp:7
virtual void init()
One‑time initialization hook (resources, buffers, etc.).
Definition GLApp.h:212

Constructor & Destructor Documentation

◆ GLApp()

GLApp::GLApp ( uint32_t w = 640,
uint32_t h = 480,
uint32_t s = 4,
const std::string & title = "My OpenGL App",
bool fpsCounter = true,
bool sync = true )

Construct the environment and boot the stock shaders/buffers.

Parameters
wWindow width in pixels.
hWindow height in pixels.
sMultisample count (GLFW window hint).
titleWindow/page title.
fpsCounterIf true, show an FPS counter in the title.
syncIf true, enable vsync.
Here is the call graph for this function:

◆ ~GLApp()

GLApp::~GLApp ( )
virtual

Virtual destructor.

Member Function Documentation

◆ animate()

virtual void GLApp::animate ( double animationTime)
inlinevirtual

Per‑frame animation/update hook; parameter is seconds since start.

◆ closeWindow()

void GLApp::closeWindow ( )
inlineprotected

Request closing the window.

◆ computeImageTransform()

Mat4 GLApp::computeImageTransform ( const Vec2ui & imageSize) const

Compute a uniform image scale so the longer side fits the window.

Here is the call graph for this function:

◆ computeImageTransformFixedHeight()

Mat4 GLApp::computeImageTransformFixedHeight ( const Vec2ui & imageSize,
float height = 1.0f,
const Vec3 & center = Vec3{0.0f,0.0f,0.0f} ) const

Compute a transform that fits an image to a given height.

Here is the call graph for this function:

◆ computeImageTransformFixedWidth()

Mat4 GLApp::computeImageTransformFixedWidth ( const Vec2ui & imageSize,
float width = 1.0f,
const Vec3 & center = Vec3{0.0f,0.0f,0.0f} ) const

Compute a transform that fits an image to a given width.

Here is the call graph for this function:

◆ draw()

virtual void GLApp::draw ( )
inlinevirtual

Per‑frame draw hook.

◆ drawImage() [1/4]

void GLApp::drawImage ( const GLTexture2D & image,
const Vec2 & bl,
const Vec2 & tr )

Draw a textured quad from a texture handle using 2D corners.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ drawImage() [2/4]

void GLApp::drawImage ( const GLTexture2D & image,
const Vec3 & bl = Vec3{-1.0f,-1.0f,0.0f},
const Vec3 & br = Vec3{1.0f,-1.0f,0.0f},
const Vec3 & tl = Vec3{-1.0f,1.0f,0.0f},
const Vec3 & tr = Vec3{1.0f,1.0f,0.0f} )

Draw a textured quad from a texture handle using 3D corners.

Here is the call graph for this function:

◆ drawImage() [3/4]

void GLApp::drawImage ( const Image & image,
const Vec2 & bl,
const Vec2 & tr )

Draw a textured quad from an Image using 2D corners.

Here is the call graph for this function:

◆ drawImage() [4/4]

void GLApp::drawImage ( const Image & image,
const Vec3 & bl = Vec3{-1.0f,-1.0f,0.0f},
const Vec3 & br = Vec3{1.0f,-1.0f,0.0f},
const Vec3 & tl = Vec3{-1.0f,1.0f,0.0f},
const Vec3 & tr = Vec3{1.0f,1.0f,0.0f} )

Draw a textured quad from an Image using 3D corners.

Here is the call graph for this function:

◆ drawLines()

void GLApp::drawLines ( const std::vector< float > & data,
LineDrawType t,
float lineThickness = 1.0f )

Draw colored lines using the given topology.

Parameters
dataInterleaved vertices (x,y,z,r,g,b,a) → 7 floats.
tTopology: LIST/STRIP/LOOP.
lineThicknessIf >1, thick lines are triangulated in screen space.
Here is the call graph for this function:

◆ drawPoints()

void GLApp::drawPoints ( const std::vector< float > & data,
float pointSize = 1.0f,
bool useTex = false )

Draw an array of colored points.

Parameters
dataInterleaved floats per vertex (x,y,z,r,g,b,a) → 7 floats.
pointSizePixel size (ignored on desktop when using simplePointProg).
useTexIf true, draw as point sprites using the configured textures.
Here is the call graph for this function:

◆ drawRect() [1/2]

void GLApp::drawRect ( const Vec4 & color,
const Vec2 & bl,
const Vec2 & tr )

Draw a solid‑color rectangle given 2D corners.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ drawRect() [2/2]

void GLApp::drawRect ( const Vec4 & color,
const Vec3 & bl = Vec3{-1.0f,-1.0f,0.0f},
const Vec3 & br = Vec3{1.0f,-1.0f,0.0f},
const Vec3 & tl = Vec3{-1.0f,1.0f,0.0f},
const Vec3 & tr = Vec3{1.0f,1.0f,0.0f} )

Draw a solid‑color rectangle given 3D corners (defaults to NDC quad).

Here is the call graph for this function:

◆ drawTriangles()

void GLApp::drawTriangles ( const std::vector< float > & data,
TrisDrawType t,
bool wireframe,
bool lighting )

Upload triangle data and draw immediately (or as wireframe edges).

Parameters
dataInterleaved floats per vertex; 7 floats for color only (x,y,z,r,g,b,a) or 10 floats when lighting is enabled (x,y,z,r,g,b,a,nx,ny,nz).
tTopology: LIST/STRIP/FAN.
wireframeIf true, edges are derived and drawn as lines.
lightingIf true, expect normals and use the lighting shader.
Here is the call graph for this function:

◆ getAnimation()

bool GLApp::getAnimation ( ) const

Query whether animation is currently active.

◆ getAspect()

float GLApp::getAspect ( ) const

Current window aspect ratio (width/height).

◆ getDrawProjection()

Mat4 GLApp::getDrawProjection ( ) const

Current projection matrix.

◆ getDrawTransform()

Mat4 GLApp::getDrawTransform ( ) const

Current model‑view matrix.

◆ init()

virtual void GLApp::init ( )
inlinevirtual

One‑time initialization hook (resources, buffers, etc.).

Here is the caller graph for this function:

◆ keyboard()

virtual void GLApp::keyboard ( int key,
int scancode,
int action,
int mods )
inlinevirtual

Keyboard event; parameters mirror backend callbacks.

◆ keyboardChar()

virtual void GLApp::keyboardChar ( unsigned int key)
inlinevirtual

Unicode character input.

◆ mouseButton()

virtual void GLApp::mouseButton ( int button,
int state,
int mods,
double xPosition,
double yPosition )
inlinevirtual

Mouse button event with cursor position.

◆ mouseMove()

virtual void GLApp::mouseMove ( double xPosition,
double yPosition )
inlinevirtual

Mouse move event (window coordinates).

◆ mouseWheel()

virtual void GLApp::mouseWheel ( double x_offset,
double y_offset,
double xPosition,
double yPosition )
inlinevirtual

Mouse wheel/trackpad scroll event with cursor position.

◆ redrawTriangles()

void GLApp::redrawTriangles ( bool wireframe)

Re‑issue the last triangle draw using cached settings.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ resetAnimation()

void GLApp::resetAnimation ( )

Reset the animation timer and invoke animate(0).

◆ resetPointHighlightTexture()

void GLApp::resetPointHighlightTexture ( )

Clear the highlight overlay sprite.

Here is the call graph for this function:

◆ resetPointTexture()

void GLApp::resetPointTexture ( uint32_t resolution = 64)

Create a default circular mask texture of given resolution.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ resize()

void GLApp::resize ( int width,
int height )
virtual

Resize notification; override to update projection, etc.

Here is the caller graph for this function:

◆ run()

void GLApp::run ( )

Create the context/window and enter the render loop.

Calls init(), then resize() with the framebuffer size, and finally enters the platform‑specific main loop that repeatedly calls animate() and draw() until the window closes.

Here is the call graph for this function:

◆ setAnimation()

void GLApp::setAnimation ( bool animationActive)

Enable/disable the animation step inside the main loop.

Parameters
animationActiveIf true, animate() is called each frame.

Pausing stores a timestamp so resuming keeps animation time continuous.

◆ setDrawProjection()

void GLApp::setDrawProjection ( const Mat4 & mat)

Set the projection matrix used by the stock shaders.

◆ setDrawTransform()

void GLApp::setDrawTransform ( const Mat4 & mat)

Set the model‑view matrix used by the stock shaders.

Here is the call graph for this function:

◆ setImageFilter()

void GLApp::setImageFilter ( GLint magFilter,
GLint minFilter )

Update filtering for images drawn by drawImage().

Parameters
magFilterGL magnification filter.
minFilterGL minification filter.

◆ setPointHighlightTexture()

void GLApp::setPointHighlightTexture ( const Image & shape)

Set an optional highlight overlay sprite from an Image.

Here is the caller graph for this function:

◆ setPointTexture() [1/2]

void GLApp::setPointTexture ( const Image & shape)

Set the point sprite from an Image.

Here is the call graph for this function:

◆ setPointTexture() [2/2]

void GLApp::setPointTexture ( const std::vector< uint8_t > & shape,
uint32_t x,
uint32_t y,
uint8_t components )

Set the point sprite from raw bytes.

Here is the caller graph for this function:

◆ shaderUpdate()

void GLApp::shaderUpdate ( )
protected

Update stock shader uniforms (MVP/MV/MVit) to current transforms.

Here is the caller graph for this function:

Member Data Documentation

◆ glEnv

GLEnv GLApp::glEnv
protected

Window/context + platform utilities.

◆ mv

Mat4 GLApp::mv
protected

Model‑view matrix used by stock shaders.

◆ mvi

Mat4 GLApp::mvi
protected

Inverse of mv (for lighting helpers).

◆ p

Mat4 GLApp::p
protected

Projection matrix used by stock shaders.

◆ pointSprite

GLTexture2D GLApp::pointSprite
protected

Point‑sprite base texture.

◆ pointSpriteHighlight

GLTexture2D GLApp::pointSpriteHighlight
protected

Optional highlight overlay sprite.

◆ raster

GLTexture2D GLApp::raster
protected

Temporary texture for drawImage(Image).

◆ resumeTime

double GLApp::resumeTime
protected

Timestamp used to keep animation time continuous on resume.

◆ simpleArray

GLArray GLApp::simpleArray
protected

VAO used by helpers.

◆ simpleHLSpriteProg

GLProgram GLApp::simpleHLSpriteProg
protected

Point sprite + highlight overlay.

◆ simpleLightProg

GLProgram GLApp::simpleLightProg
protected

Lit shader (pos+color+normal).

◆ simplePointProg

GLProgram GLApp::simplePointProg
protected

Point shader (pos+color), desktop uses glPointSize.

◆ simpleProg

GLProgram GLApp::simpleProg
protected

Solid color shader (pos+color).

◆ simpleSpriteProg

GLProgram GLApp::simpleSpriteProg
protected

Point sprite shader (pos+color+texture).

◆ simpleTexProg

GLProgram GLApp::simpleTexProg
protected

Textured quad shader (pos+uv).

◆ simpleVb

GLBuffer GLApp::simpleVb
protected

VBO used by helpers.


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