CGVIS Utils
Loading...
Searching...
No Matches
GLApp.h
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4
5#include "GLEnv.h"
6#include "GLProgram.h"
7#include "GLArray.h"
8#include "GLBuffer.h"
9#include "GLTexture2D.h"
10#include "Image.h"
11#include "GLAppKeyTranslation.h"
12
26
35
39enum class TrisDrawType {
43};
44 }
52* void draw() override { /* issue draws */ }
53* void animate(double t) override { /* update state */ }
54* };
55* int main(){ MyApp().run(); }
56* @endcode
57*/
58class GLApp {
59public:
69 GLApp(uint32_t w=640, uint32_t h=480, uint32_t s=4,
70 const std::string& title = "My OpenGL App",
71 bool fpsCounter=true, bool sync=true);
73 virtual ~GLApp();
74
82 void run();
83
90 void setAnimation(bool animationActive);
92 bool getAnimation() const;
95
99 float getAspect() const;
100
106 void setImageFilter(GLint magFilter, GLint minFilter);
107
110
111 void drawRect(const Vec4& color, const Vec2& bl, const Vec2& tr);
113 void drawRect(const Vec4& color,
114 const Vec3& bl=Vec3{-1.0f,-1.0f,0.0f},
115 const Vec3& br=Vec3{1.0f,-1.0f,0.0f},
116 const Vec3& tl=Vec3{-1.0f,1.0f,0.0f},
117 const Vec3& tr=Vec3{1.0f,1.0f,0.0f});
118
120 void drawImage(const GLTexture2D& image, const Vec2& bl, const Vec2& tr);
122 void drawImage(const Image& image, const Vec2& bl, const Vec2& tr);
124 void drawImage(const GLTexture2D& image,
125 const Vec3& bl=Vec3{-1.0f,-1.0f,0.0f},
126 const Vec3& br=Vec3{1.0f,-1.0f,0.0f},
127 const Vec3& tl=Vec3{-1.0f,1.0f,0.0f},
128 const Vec3& tr=Vec3{1.0f,1.0f,0.0f});
130 void drawImage(const Image& image,
131 const Vec3& bl=Vec3{-1.0f,-1.0f,0.0f},
132 const Vec3& br=Vec3{1.0f,-1.0f,0.0f},
133 const Vec3& tl=Vec3{-1.0f,1.0f,0.0f},
134 const Vec3& tr=Vec3{1.0f,1.0f,0.0f});
136
139
145 void drawPoints(const std::vector<float>& data, float pointSize=1.0f, bool useTex=false);
146
153 void drawLines(const std::vector<float>& data, LineDrawType t, float lineThickness=1.0f);
154
164 void drawTriangles(const std::vector<float>& data, TrisDrawType t, bool wireframe, bool lighting);
166 void redrawTriangles(bool wireframe);
168
171
172 Mat4 computeImageTransform(const Vec2ui& imageSize) const;
175 float height=1.0f,
176 const Vec3& center=Vec3{0.0f,0.0f,0.0f}) const;
179 float width=1.0f,
180 const Vec3& center=Vec3{0.0f,0.0f,0.0f}) const;
182
185
186 void setDrawProjection(const Mat4& mat);
188 void setDrawTransform(const Mat4& mat);
190 Mat4 getDrawProjection() const;
192 Mat4 getDrawTransform() const;
194
197
198 void resetPointTexture(uint32_t resolution=64);
200 void setPointTexture(const std::vector<uint8_t>& shape, uint32_t x,
201 uint32_t y, uint8_t components);
203 void setPointTexture(const Image& shape);
205 void setPointHighlightTexture(const Image& shape);
209
210 // ===== Lifecycle hooks (override in subclasses) =====
212 virtual void init() {}
214 virtual void draw() {}
216 virtual void animate(double animationTime) {}
218 virtual void resize(int width, int height);
219
220 // ===== Input hooks (override in subclasses) =====
222 virtual void keyboard(int key, int scancode, int action, int mods) {}
224 virtual void keyboardChar(unsigned int key) {}
226 virtual void mouseMove(double xPosition, double yPosition) {}
228 virtual void mouseButton(int button, int state, int mods, double xPosition, double yPosition) {}
230 virtual void mouseWheel(double x_offset, double y_offset, double xPosition, double yPosition) {}
231
232protected:
248 double resumeTime;
249#ifdef __EMSCRIPTEN__
250 float xMousePos;
251 float yMousePos;
252#endif
253
255 void shaderUpdate();
256
258 void closeWindow() { glEnv.setClose(); }
259
260private:
261 bool animationActive;
262 TrisDrawType lastTrisType;
263 GLsizei lastTrisCount;
264 bool lastLighting;
265 double startTime;
266
268 void mainLoop();
269
270#ifdef __EMSCRIPTEN__
272 static void mainLoopWrapper(void* arg) {
273 GLApp* app = static_cast<GLApp*>(arg);
274 app->mainLoop();
275 }
276
278 static bool sizeCallback(int eventType, const EmscriptenUiEvent *uiEvent, void *userData);
280 static bool keyCallback(int eventType, const EmscriptenKeyboardEvent* keyEvent, void* userData);
282 static bool cursorPositionCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData);
284 static bool mouseButtonUpCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData);
286 static bool mouseButtonDownCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData);
288 static bool mouseButtonCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData);
290 static bool scrollCallback(int eventType, const EmscriptenWheelEvent *wheelEvent, void *userData);
291#else
293 static GLApp* staticAppPtr;
295 static void sizeCallback(GLFWwindow* window, int width, int height);
297 static void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
299 static void keyCharCallback(GLFWwindow* window, unsigned int codepoint);
301 static void cursorPositionCallback(GLFWwindow* window, double xPosition, double yPosition);
303 static void mouseButtonCallback(GLFWwindow* window, int button, int state, int mods);
305 static void scrollCallback(GLFWwindow* window, double x_offset, double y_offset);
306#endif
307
319 void triangulate(const Vec3& p0,
320 const Vec3& p1, const Vec4& c1,
321 const Vec3& p2, const Vec4& c2,
322 const Vec3& p3,
323 float lineThickness,
324 std::vector<float>& trisData);
325};
LineDrawType
Line primitive topology for drawLines().
Definition GLApp.h:30
@ LIST
Disconnected line segments (GL_LINES)
Definition GLApp.h:31
@ STRIP
Connected polyline (GL_LINE_STRIP)
Definition GLApp.h:32
@ LOOP
Closed polyline (GL_LINE_LOOP)
Definition GLApp.h:33
TrisDrawType
Triangle primitive topology for drawTriangles().
Definition GLApp.h:39
@ FAN
Triangle fan (GL_TRIANGLE_FAN)
Definition GLApp.h:42
Cross‑platform key/mouse translation layer for GLFW vs. Emscripten.
RAII wrapper for an OpenGL Vertex Array Object (VAO).
Thin RAII wrapper for an OpenGL buffer object with upload/binding helpers.
Cross‑platform OpenGL window/context helper for desktop and Emscripten.
RAII wrapper around an OpenGL shader program with rich uniform/texture setters.
RAII wrapper for an OpenGL 2D texture with byte/half/float upload helpers.
Lightweight image container with basic processing utilities.
Mat4t< float > Mat4
Single-precision 4×4 matrix.
Definition Mat4.h:567
Vec2t< float > Vec2
Float vector (x,y).
Definition Vec2.h:258
Vec2t< uint32_t > Vec2ui
32-bit unsigned integer vector (x,y).
Definition Vec2.h:262
Vec3t< float > Vec3
3D float vector (x,y,z).
Definition Vec3.h:424
Vec4t< float > Vec4
4D float vector (x,y,z,w).
Definition Vec4.h:337
Simple app runner with a GL context, stock shaders, and draw helpers.
Definition GLApp.h:58
virtual void animate(double animationTime)
Per‑frame animation/update hook; parameter is seconds since start.
Definition GLApp.h:216
void drawPoints(const std::vector< float > &data, float pointSize=1.0f, bool useTex=false)
Draw an array of colored points.
Definition GLApp.cpp:511
GLProgram simpleTexProg
Textured quad shader (pos+uv).
Definition GLApp.h:241
void setPointTexture(const std::vector< uint8_t > &shape, uint32_t x, uint32_t y, uint8_t components)
Set the point sprite from raw bytes.
Definition GLApp.cpp:302
virtual void keyboard(int key, int scancode, int action, int mods)
Keyboard event; parameters mirror backend callbacks.
Definition GLApp.h:222
float getAspect() const
Current window aspect ratio (width/height).
GLTexture2D pointSprite
Point‑sprite base texture.
Definition GLApp.h:246
Mat4 mvi
Inverse of mv (for lighting helpers).
Definition GLApp.h:236
void drawRect(const Vec4 &color, const Vec2 &bl, const Vec2 &tr)
Draw a solid‑color rectangle given 2D corners.
Definition GLApp.cpp:824
GLTexture2D pointSpriteHighlight
Optional highlight overlay sprite.
Definition GLApp.h:247
Mat4 computeImageTransform(const Vec2ui &imageSize) const
Compute a uniform image scale so the longer side fits the window.
Definition GLApp.cpp:837
void redrawTriangles(bool wireframe)
Re‑issue the last triangle draw using cached settings.
Definition GLApp.cpp:556
Mat4 getDrawTransform() const
Current model‑view matrix.
Definition GLApp.cpp:737
Mat4 p
Projection matrix used by stock shaders.
Definition GLApp.h:234
void setAnimation(bool animationActive)
Enable/disable the animation step inside the main loop.
void setPointHighlightTexture(const Image &shape)
Set an optional highlight overlay sprite from an Image.
Definition GLApp.cpp:298
virtual void draw()
Per‑frame draw hook.
Definition GLApp.h:214
void setDrawTransform(const Mat4 &mat)
Set the model‑view matrix used by the stock shaders.
Definition GLApp.cpp:741
GLProgram simpleSpriteProg
Point sprite shader (pos+color+texture).
Definition GLApp.h:239
GLProgram simpleProg
Solid color shader (pos+color).
Definition GLApp.h:237
void drawImage(const GLTexture2D &image, const Vec2 &bl, const Vec2 &tr)
Draw a textured quad from a texture handle using 2D corners.
Definition GLApp.cpp:772
void setImageFilter(GLint magFilter, GLint minFilter)
Update filtering for images drawn by drawImage().
Definition GLApp.cpp:768
void resetAnimation()
Reset the animation timer and invoke animate(0).
GLArray simpleArray
VAO used by helpers.
Definition GLApp.h:243
Mat4 mv
Model‑view matrix used by stock shaders.
Definition GLApp.h:235
void setDrawProjection(const Mat4 &mat)
Set the projection matrix used by the stock shaders.
Definition GLApp.cpp:729
void shaderUpdate()
Update stock shader uniforms (MVP/MV/MVit) to current transforms.
Definition GLApp.cpp:746
GLProgram simplePointProg
Point shader (pos+color), desktop uses glPointSize.
Definition GLApp.h:238
void drawLines(const std::vector< float > &data, LineDrawType t, float lineThickness=1.0f)
Draw colored lines using the given topology.
Definition GLApp.cpp:411
Mat4 getDrawProjection() const
Current projection matrix.
Definition GLApp.cpp:733
void run()
Create the context/window and enter the render loop.
Definition GLApp.cpp:346
GLEnv glEnv
Window/context + platform utilities.
Definition GLApp.h:233
virtual void mouseWheel(double x_offset, double y_offset, double xPosition, double yPosition)
Mouse wheel/trackpad scroll event with cursor position.
Definition GLApp.h:230
GLProgram simpleLightProg
Lit shader (pos+color+normal).
Definition GLApp.h:242
void resetPointTexture(uint32_t resolution=64)
Create a default circular mask texture of given resolution.
Definition GLApp.cpp:307
void closeWindow()
Request closing the window.
Definition GLApp.h:258
bool getAnimation() const
Query whether animation is currently active.
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.
Definition GLApp.cpp:854
virtual ~GLApp()
Virtual destructor.
Definition GLApp.cpp:291
virtual void keyboardChar(unsigned int key)
Unicode character input.
Definition GLApp.h:224
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 resize(int width, int height)
Resize notification; override to update projection, etc.
Definition GLApp.cpp:359
virtual void mouseButton(int button, int state, int mods, double xPosition, double yPosition)
Mouse button event with cursor position.
Definition GLApp.h:228
virtual void init()
One‑time initialization hook (resources, buffers, etc.).
Definition GLApp.h:212
GLBuffer simpleVb
VBO used by helpers.
Definition GLApp.h:244
double resumeTime
Timestamp used to keep animation time continuous on resume.
Definition GLApp.h:248
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.
Definition GLApp.cpp:845
GLTexture2D raster
Temporary texture for drawImage(Image).
Definition GLApp.h:245
virtual void mouseMove(double xPosition, double yPosition)
Mouse move event (window coordinates).
Definition GLApp.h:226
void resetPointHighlightTexture()
Clear the highlight overlay sprite.
Definition GLApp.cpp:323
void drawTriangles(const std::vector< float > &data, TrisDrawType t, bool wireframe, bool lighting)
Upload triangle data and draw immediately (or as wireframe edges).
Definition GLApp.cpp:698
GLProgram simpleHLSpriteProg
Point sprite + highlight overlay.
Definition GLApp.h:240
Definition GLArray.h:24
Definition GLBuffer.h:17
Window/context manager for OpenGL (GLFW+GLEW) or WebGL (Emscripten).
Definition GLEnv.h:63
OpenGL shader program with helpers for creation and data binding.
Definition GLProgram.h:59
Definition GLTexture2D.h:19