|
CGVIS Utils
|
Simple app runner with a GL context, stock shaders, and draw helpers. More...
#include <GLApp.h>
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 ¢er=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 ¢er=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. | |
Simple app runner with a GL context, stock shaders, and draw helpers.
Typical usage:
| 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.
| w | Window width in pixels. |
| h | Window height in pixels. |
| s | Multisample count (GLFW window hint). |
| title | Window/page title. |
| fpsCounter | If true, show an FPS counter in the title. |
| sync | If true, enable vsync. |
|
virtual |
Virtual destructor.
|
inlinevirtual |
Per‑frame animation/update hook; parameter is seconds since start.
|
inlineprotected |
Request closing the window.
Compute a uniform image scale so the longer side fits the window.
| 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.
| 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.
|
inlinevirtual |
Per‑frame draw hook.
| void GLApp::drawImage | ( | const GLTexture2D & | image, |
| const Vec2 & | bl, | ||
| const Vec2 & | tr ) |
Draw a textured quad from a texture handle using 2D corners.
| 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.
| void GLApp::drawLines | ( | const std::vector< float > & | data, |
| LineDrawType | t, | ||
| float | lineThickness = 1.0f ) |
Draw colored lines using the given topology.
| data | Interleaved vertices (x,y,z,r,g,b,a) → 7 floats. |
| t | Topology: LIST/STRIP/LOOP. |
| lineThickness | If >1, thick lines are triangulated in screen space. |
| void GLApp::drawPoints | ( | const std::vector< float > & | data, |
| float | pointSize = 1.0f, | ||
| bool | useTex = false ) |
Draw an array of colored points.
| data | Interleaved floats per vertex (x,y,z,r,g,b,a) → 7 floats. |
| pointSize | Pixel size (ignored on desktop when using simplePointProg). |
| useTex | If true, draw as point sprites using the configured textures. |
Draw a solid‑color rectangle given 2D corners.
| 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).
| void GLApp::drawTriangles | ( | const std::vector< float > & | data, |
| TrisDrawType | t, | ||
| bool | wireframe, | ||
| bool | lighting ) |
Upload triangle data and draw immediately (or as wireframe edges).
| data | Interleaved 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). |
| t | Topology: LIST/STRIP/FAN. |
| wireframe | If true, edges are derived and drawn as lines. |
| lighting | If true, expect normals and use the lighting shader. |
| bool GLApp::getAnimation | ( | ) | const |
Query whether animation is currently active.
| float GLApp::getAspect | ( | ) | const |
Current window aspect ratio (width/height).
| Mat4 GLApp::getDrawProjection | ( | ) | const |
Current projection matrix.
| Mat4 GLApp::getDrawTransform | ( | ) | const |
Current model‑view matrix.
|
inlinevirtual |
One‑time initialization hook (resources, buffers, etc.).
|
inlinevirtual |
Keyboard event; parameters mirror backend callbacks.
|
inlinevirtual |
Unicode character input.
|
inlinevirtual |
Mouse button event with cursor position.
|
inlinevirtual |
Mouse move event (window coordinates).
|
inlinevirtual |
Mouse wheel/trackpad scroll event with cursor position.
| void GLApp::redrawTriangles | ( | bool | wireframe | ) |
Re‑issue the last triangle draw using cached settings.
| void GLApp::resetAnimation | ( | ) |
Reset the animation timer and invoke animate(0).
| void GLApp::resetPointHighlightTexture | ( | ) |
Clear the highlight overlay sprite.
| void GLApp::resetPointTexture | ( | uint32_t | resolution = 64 | ) |
Create a default circular mask texture of given resolution.
|
virtual |
Resize notification; override to update projection, etc.
| void GLApp::run | ( | ) |
| void GLApp::setAnimation | ( | bool | animationActive | ) |
Enable/disable the animation step inside the main loop.
| animationActive | If true, animate() is called each frame. |
Pausing stores a timestamp so resuming keeps animation time continuous.
| void GLApp::setDrawProjection | ( | const Mat4 & | mat | ) |
Set the projection matrix used by the stock shaders.
| void GLApp::setDrawTransform | ( | const Mat4 & | mat | ) |
Set the model‑view matrix used by the stock shaders.
| void GLApp::setImageFilter | ( | GLint | magFilter, |
| GLint | minFilter ) |
Update filtering for images drawn by drawImage().
| magFilter | GL magnification filter. |
| minFilter | GL minification filter. |
| void GLApp::setPointHighlightTexture | ( | const Image & | shape | ) |
| void GLApp::setPointTexture | ( | const Image & | shape | ) |
| 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.
|
protected |
Update stock shader uniforms (MVP/MV/MVit) to current transforms.
|
protected |
Window/context + platform utilities.
|
protected |
Model‑view matrix used by stock shaders.
|
protected |
Projection matrix used by stock shaders.
|
protected |
Point‑sprite base texture.
|
protected |
Optional highlight overlay sprite.
|
protected |
Temporary texture for drawImage(Image).
|
protected |
Timestamp used to keep animation time continuous on resume.
|
protected |
VAO used by helpers.
|
protected |
Point sprite + highlight overlay.
|
protected |
Lit shader (pos+color+normal).
|
protected |
Point shader (pos+color), desktop uses glPointSize.
|
protected |
Solid color shader (pos+color).
|
protected |
Point sprite shader (pos+color+texture).
|
protected |
Textured quad shader (pos+uv).
|
protected |
VBO used by helpers.