AGL
A graphics library
agl::Window Class Reference

Manages the window and user input. More...

#include <window.h>

Public Member Functions

 Window ()
 Constructor. More...
 
void run ()
 Opens the window and starts the main application loop. More...
 
bool screenshot (const std::string &filename)
 Save the current screen image to a file. More...
 

Protected Member Functions

void noLoop ()
 Stop the main application loop. More...
 
void background (const glm::vec3 &color)
 Set the background color. More...
 
Respond to events
virtual void setup ()
 Override this method to perform setup before the main application loop. More...
 
virtual void draw ()
 Override this method to draw. More...
 
virtual void mouseMotion (int x, int y, int dx, int dy)
 Override this method to respond to mouse movement. More...
 
virtual void mouseDown (int button, int mods)
 Override this method to respond to mouse press (button down) More...
 
virtual void mouseUp (int button, int mods)
 Override this method to respond to mouse press (button up) More...
 
virtual void scroll (float dx, float dy)
 Override this method to respond to scrolling the middle mouse button. More...
 
virtual void keyUp (int key, int mods)
 Override this method to respond to key presses (button up) More...
 
virtual void keyDown (int key, int mods)
 Override this method to respond to key presses (button down) More...
 
virtual void resize (int width, int height)
 Override this method to respond to window resizing. More...
 
Query input and time
bool keyIsDown (int key) const
 Query whether the given key is down @key The key to test. More...
 
bool mouseIsDown (int button) const
 Query whether the given mouse button is down. More...
 
glm::vec2 mousePosition () const
 Return the current mouse position (in screen coordinates)
 
float dt () const
 Return the amount of time since the previous frame (in seconds) More...
 
float elapsedTime () const
 Return the amount of time since the setup() was called (in seconds)
 
float height () const
 Return the window height in pixels.
 
float width () const
 Return the window width in pixels.
 
Projections and view
void setWindowSize (int w, int h)
 Set the size of the window in pixels. More...
 
void setupOrthoScene (const glm::vec3 &center, const glm::vec3 &dim)
 Initialize the projection and camera to fit the given dimensions and center using an orthographic projection. More...
 
void setupPerspectiveScene (const glm::vec3 &center, const glm::vec3 &dim)
 Initialize the projection and camera to fit the given dimensions and center using a perspective projection. More...
 
void lookAt (const glm::vec3 &camPos, const glm::vec3 &camLook, const glm::vec3 &up=glm::vec3(0, 1, 0))
 
void perspective (float fovRadians, float aspect, float near, float far)
 Set the current projection to a perspective view. More...
 
void ortho (float minx, float maxx, float miny, float maxy, float minz, float maxz)
 Set the current projection to an orthographic view. More...
 
bool cameraEnabled () const
 Returns true if the camera controls are active; false otherwise. More...
 
void setCameraEnabled (bool on)
 Set whether the window's camera controls are active. More...
 

Protected Attributes

Renderer renderer
 
Camera camera
 

Detailed Description

Manages the window and user input.

Override this class to create a custom application.

// Copyright 2020, Savvy Sine, Aline Normoyle

#include "agl/window.h"

class MyWindow : public agl::Window {
  void draw() {
    renderer.sphere();
  }
};

int main() {
  MyWindow window;
  window.run();
}

Constructor & Destructor Documentation

◆ Window()

agl::Window::Window ( )

Constructor.

Override this class to create a custom application. The default window is sized 500x500 and draws an empty (black) scene.

// Copyright 2020, Savvy Sine, Aline Normoyle

#include "agl/window.h"
int main() {
  agl::Window window;
  window.run();
}

Member Function Documentation

◆ background()

void agl::Window::background ( const glm::vec3 &  color)
protected

Set the background color.

This function can be called from draw and will additionally clear the color and depth buffers

◆ cameraEnabled()

bool agl::Window::cameraEnabled ( ) const
inlineprotected

Returns true if the camera controls are active; false otherwise.

See also
Camera

◆ draw()

virtual void agl::Window::draw ( )
inlineprotectedvirtual

Override this method to draw.

// Copyright 2020, Savvy Sine, Aline Normoyle

#include "agl/window.h"

using glm::vec2;
using glm::vec3;

class MyWindow : public agl::Window {
  void setup() {
    renderer.setUniform("Fog.enabled", true);
    renderer.setUniform("Fog.color", vec3(0.9f));
    renderer.setUniform("Fog.minDist", 5.0f);
    renderer.setUniform("Fog.maxDist", 9.0f);
    renderer.setUniform("Material.specular", vec3(0.0));
    renderer.setUniform("MainTexture.enabled", true);
    renderer.setUniform("MainTexture.tile", vec2(10.0));

    renderer.loadTexture("bricks", "../textures/bricks.png", 0);
    renderer.texture("MainTexture.texture", "bricks");

    setupPerspectiveScene(vec3(0.0), vec3(10.0));
    background(vec3(0.9f));
  }

  void draw() {
    renderer.scale(vec3(20.0f));
    renderer.plane();
  }
};

int main() {
  MyWindow window;
  window.run();
}

◆ dt()

float agl::Window::dt ( ) const
protected

Return the amount of time since the previous frame (in seconds)

If the frame rate is 30 frames per second, dt would be approximately 1/30 = 0.033333 seconds each frame

◆ keyDown()

virtual void agl::Window::keyDown ( int  key,
int  mods 
)
inlineprotectedvirtual

Override this method to respond to key presses (button down)

Parameters
keyThe key, e.g. as either a constant or capital ascii letter, such as 'P'.
modsModifiers that are pressed (e.g. shift, control, etc)

https://www.glfw.org/docs/latest/group__keys.html

See also
keyUp(int,int)

◆ keyIsDown()

bool agl::Window::keyIsDown ( int  key) const
protected

Query whether the given key is down @key The key to test.

More than one key can be pressed at once. The key can be an ascii capital letter or a GLFW constant https://www.glfw.org/docs/latest/input_guide.html

◆ keyUp()

virtual void agl::Window::keyUp ( int  key,
int  mods 
)
inlineprotectedvirtual

Override this method to respond to key presses (button up)

Parameters
keyThe key, e.g. as either a constant or capital ascii letter, such as 'P'.
modsModifiers that are pressed (e.g. shift, control, etc)

https://www.glfw.org/docs/latest/group__keys.html

// Copyright 2020, Savvy Sine, Aline Normoyle
#include "agl/window.h"

using glm::vec3;
class MyWindow : public agl::Window {
  void setup() {
    setupPerspectiveScene(vec3(0.0), vec3(10.0));
  }

  void draw() {
    renderer.scale(vec3(10.0f));
    renderer.plane();
  }

  void keyUp(int key, int mods) {
    if (key == 'C') {
      std::cout << "Toggle camera controls " << cameraEnabled() << std::endl;
      setCameraEnabled(!cameraEnabled());
    }
  }
};

int main() {
  MyWindow window;
  window.run();
}
See also
keyDown(int,int)
keyIsDown(int)

◆ lookAt()

void agl::Window::lookAt ( const glm::vec3 &  camPos,
const glm::vec3 &  camLook,
const glm::vec3 &  up = glm::vec3(0, 1, 0) 
)
protected

If you're not using the default camera controls and you don't call setupSecene, the camera is at position (0,0,2) and faces the point (0,0,0)

See also
setupPerspectiveScene(const glm::vec3&, const glm::vec3&)
setupOrthoScene(const glm::vec3&, const glm::vec3&)

◆ mouseDown()

virtual void agl::Window::mouseDown ( int  button,
int  mods 
)
inlineprotectedvirtual

Override this method to respond to mouse press (button down)

Parameters
buttonThe mouse button that is pressed, e.g. GLFW_MOUSE_BUTTON_LEFT
modsModifiers that are pressed (e.g. shift, control, etc)

https://www.glfw.org/docs/latest/input_guide.html

See also
mouseUp(int, int)
mouseIsDown(int)

◆ mouseIsDown()

bool agl::Window::mouseIsDown ( int  button) const
protected

Query whether the given mouse button is down.

Parameters
buttonThe button to test

More than one button can be pressed at once. The button should be a GLFW constant, such as GLFW_MOUSE_BUTTON_LEFT https://www.glfw.org/docs/latest/input.html

◆ mouseMotion()

virtual void agl::Window::mouseMotion ( int  x,
int  y,
int  dx,
int  dy 
)
inlineprotectedvirtual

Override this method to respond to mouse movement.

Parameters
xThe current x position
yThe current y position
dxThe change in x since the last mouse event (in pixels)
dyThe change in y since the last mouse event (in pixels)

Screen coordinates are in pixels. x values are always between 0 and width. y values are always between 0 and height.

◆ mouseUp()

virtual void agl::Window::mouseUp ( int  button,
int  mods 
)
inlineprotectedvirtual

Override this method to respond to mouse press (button up)

See also
mouseDown(int, int)
mouseIsDown(int)

◆ noLoop()

void agl::Window::noLoop ( )
protected

Stop the main application loop.

Call this function to terminate the main loop, either from setup() or draw()

◆ ortho()

void agl::Window::ortho ( float  minx,
float  maxx,
float  miny,
float  maxy,
float  minz,
float  maxz 
)
protected

Set the current projection to an orthographic view.

Parameters
minxThe left side of the projection
maxxThe right side of the projection
minyThe bottom side of the projection
maxyThe top side of the projection
minzThe back side of the projection
maxzThe front side of the projection

If you are using Renderer from the Window class, you should call Window::ortho instead of this method.

An orthographic projection maintains parallel lines. All objects maintain their size regardless of distance to the camera. Only objects within the extents of the orthographic cuboid volume will be drawn.

The current shader should define the following uniform variables

uniform mat4 MVP A 4x4 matrix containing the product of projection * view * modelTransform

See also
setupOrthoScene(const glm::vec3&, const glm::vec3&)

◆ perspective()

void agl::Window::perspective ( float  fovRadians,
float  aspect,
float  near,
float  far 
)
protected

Set the current projection to a perspective view.

Parameters
fovRadiansThe field of view
aspectThe aspect ratio (width divided by height) of the screen
nearThe distance to the near plane from the camera
farThe distance to the far plane from the camera

If you are using Renderer from the Window class, you should call Window::perspective instead of this method.

Perspective projections foreshorten objects should that closer objects are larger than further objects. Only objects within the view volume will be drawn. For example, objects a distance further from the far plane will not be visible.

The current shader should define the following uniform variables

uniform mat4 MVP A 4x4 matrix containing the product of projection * view * modelTransform

See also
setupPerspectiveScene(const glm::vec3&, const glm::vec3&)

◆ resize()

virtual void agl::Window::resize ( int  width,
int  height 
)
inlineprotectedvirtual

Override this method to respond to window resizing.

Parameters
widthThe new window width
heightThe new window height
// Copyright 2020, Savvy Sine, Aline Normoyle

#include "agl/window.h"

using glm::vec2;
using glm::vec3;

class MyWindow : public agl::Window {
  void setup() {
    setWindowSize(600, 400);
    setCameraEnabled(false);
    lookAt(vec3(0), vec3(0, 0, -2));
    ortho(0, width(), 0, height(), -1, 1);
    background(vec3(0));

    renderer.loadShader("shadertoy",
        "../shaders/shadertoy.vs",
        "../shaders/shadertoy_raymarch_csg.fs");
  }

  void draw() {
    renderer.beginShader("shadertoy");
    renderer.setUniform("iGlobalTime", elapsedTime());
    renderer.setUniform("iResolution", vec2(width(), height()));
    renderer.translate(vec3(width()*0.5, height()*0.5, 0.0));
    renderer.scale(vec3(width(), height(), 1.0f));
    renderer.rotate(kPI/2, vec3(1, 0, 0));
    renderer.plane();
    renderer.endShader();
  }

  void resize(int width, int height) {
    ortho(0, width, 0, height, -1, 1);
  }
};

int main() {
  MyWindow window;
  window.run();
}

◆ run()

void agl::Window::run ( )

Opens the window and starts the main application loop.

// Copyright 2020, Savvy Sine, Aline Normoyle

#include "agl/window.h"

class MyWindow : public agl::Window {
  void setup() {
    setWindowSize(200, 200);
  }

  void draw() {
    renderer.sphere();
  }
};

int main() {
  MyWindow window;
  window.run();
}

This function should typically called from main after creating the Window object. This function invokes the user's setup() function and then repeatedly calls the user's draw() function. This function returns when the user closes the window (via the escape key or close menu button)

◆ screenshot()

bool agl::Window::screenshot ( const std::string &  filename)

Save the current screen image to a file.

// Copyright 2020, Savvy Sine, Aline Normoyle

#include "agl/window.h"

class MyWindow : public agl::Window {
  void setup() {
    renderer.sphere();
    screenshot("sphere.png");
  }
};

int main() {
  MyWindow window;
  window.run();
}
Parameters
filenameimage file name (should be a .png file)
Returns
(bool) Returns false if the image cannot be saved; true otherwise

Filenames should include the png file extension. Image with relative paths will be written relative to the directory from which you run the executable. Images are saved in RGBA format.

◆ scroll()

virtual void agl::Window::scroll ( float  dx,
float  dy 
)
inlineprotectedvirtual

Override this method to respond to scrolling the middle mouse button.

Parameters
dxThe change in the x direction (in scroll coordinates)
dyThe change in the x direction (in scroll coordinates)
// Copyright 2020, Savvy Sine, Aline Normoyle

#include "agl/window.h"

using glm::vec2;
using glm::vec3;

class MyWindow : public agl::Window {
  void setup() {
    renderer.setUniform("Fog.enabled", true);
    renderer.setUniform("Fog.color", vec3(0.9f));
    renderer.setUniform("Fog.minDist", 5.0f);
    renderer.setUniform("Fog.maxDist", 9.0f);
    renderer.setUniform("Material.specular", vec3(0.0));
    renderer.setUniform("MainTexture.enabled", true);
    renderer.setUniform("MainTexture.tile", vec2(10.0));

    renderer.loadTexture("bricks", "../textures/bricks.png", 0);
    renderer.texture("MainTexture.texture", "bricks");

    setupPerspectiveScene(vec3(0.0), vec3(10.0));
    background(vec3(0.9f));
  }

  void draw() {
    renderer.scale(vec3(20.0f));
    renderer.plane();
  }
};

int main() {
  MyWindow window;
  window.run();
}

◆ setCameraEnabled()

void agl::Window::setCameraEnabled ( bool  on)
inlineprotected

Set whether the window's camera controls are active.

See also
Camera

◆ setup()

virtual void agl::Window::setup ( )
inlineprotectedvirtual

Override this method to perform setup before the main application loop.

Use this method to load textures, load meshes, set the window size, and initialize the projection and view.

// Copyright 2020, Savvy Sine, Aline Normoyle

#include "agl/window.h"

using glm::vec2;
using glm::vec3;

class MyWindow : public agl::Window {
  void setup() {
    renderer.setUniform("Fog.enabled", true);
    renderer.setUniform("Fog.color", vec3(0.9f));
    renderer.setUniform("Fog.minDist", 5.0f);
    renderer.setUniform("Fog.maxDist", 9.0f);
    renderer.setUniform("Material.specular", vec3(0.0));
    renderer.setUniform("MainTexture.enabled", true);
    renderer.setUniform("MainTexture.tile", vec2(10.0));

    renderer.loadTexture("bricks", "../textures/bricks.png", 0);
    renderer.texture("MainTexture.texture", "bricks");

    setupPerspectiveScene(vec3(0.0), vec3(10.0));
    background(vec3(0.9f));
  }

  void draw() {
    renderer.scale(vec3(20.0f));
    renderer.plane();
  }
};

int main() {
  MyWindow window;
  window.run();
}
See also
setWindowSize(int,int)
setupPerspectiveScene(const glm::vec3&, const glm::vec3&)
setupOrthoScene(const glm::vec3&, const glm::vec3&)
Renderer.loadTexture(const std::string&, const std::string&, int)
Renderer.loadCubemap(const std::string&, const std::string&, int)

◆ setupOrthoScene()

void agl::Window::setupOrthoScene ( const glm::vec3 &  center,
const glm::vec3 &  dim 
)
protected

Initialize the projection and camera to fit the given dimensions and center using an orthographic projection.

Parameters
centerThe center of the scene
dimThe dimension of the scene
See also
ortho(float, float, float, float, float, float)
lookAt(const glm::vec3, const glm::vec3&, const glm::vec3&)

◆ setupPerspectiveScene()

void agl::Window::setupPerspectiveScene ( const glm::vec3 &  center,
const glm::vec3 &  dim 
)
protected

Initialize the projection and camera to fit the given dimensions and center using a perspective projection.

Parameters
centerThe center of the scene
dimThe dimension of the scene
See also
perspective(float, float, float, float)
lookAt(const glm::vec3, const glm::vec3&, const glm::vec3&)

◆ setWindowSize()

void agl::Window::setWindowSize ( int  w,
int  h 
)
protected

Set the size of the window in pixels.

See also
width
height

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