3.4. SGL Coordinate System

When investigating a new programming language or development environment, it is traditional to write a "Hello world" program. Example 3.1 is the SGL equivalent.

Example 3.1. Hello World in SGL

#include <GL/sgl.hpp>


class HelloWindow: public sgl::Window {
public:
    HelloWindow(): sgl::Window("Simple Window",  //  Window's title
                               100, 50,          //  Left-top corner
                               400, 300,         //  Width and height
                               -10.0, 90.0,      //  Minimum and maximum x
                               -5.0, 95.0)       //  Minimum and maximum y
    {}

    void paint() override {
        sgl::set_color(sgl::BLUE);
        sgl::draw_text("Hello, world!", 30, 40, 18);
    }
};

int main() {
    //  Create a window and call its run method
    sgl::run<HelloWindow>();
}


Figure 3.1 provides a screenshot of the running hello program.

Figure 3.1. Hello World Screenshot

Hello World Screenshot


In an SGL application, the client must provide at least one concrete subclass of sgl::Window. In Example 3.1, we derive the class HelloWindow. An instance of our concrete subclass of Window constitutes the application's main window. HelloWindow's constructor passes the following information to the superclass constructor:

The title is a string, window's physical location and size is specified by four integer values, and the viewport's coordinate system is specified by four double-precision floating-point values:

    HelloWindow(): sgl::Window("Simple Window",  //  Window's title
                               100, 50,          //  Left-top corner
                               400, 300,         //  Width and height
                               -10.0, 90.0,      //  Minimum and maximum x
                               -5.0, 95.0)       //  Minimum and maximum y
    {}

Figure 3.2 shows how the four integer parameters affect the window's position and size.

Figure 3.2. Window Position and Size

Window Position and Size


As Figure 3.2 shows, in this example the left-top corner of the window's content area is 100 pixels from the left edge of the screen and 50 pixels down from the top of the screen. The window is 400 pixels wide and 300 pixels tall. The content area of the window represents a viewport into a virtual two-dimensional area. The four remaining values represent, in order, the minimum x, maximum x, minimum y, and maximum y coordinates of this virtual viewport. Figure 3.3 shows how this coordinate system relates to the physical window and the placement of the "Hello, world!" text.

Figure 3.3. Virtual Viewport Dimensions

Virtual Viewport Dimensions


The SGL drawing routines such as sgl::draw_text use the coordinate system of the virtual viewport, which means a location is specified with double-precision floating-point numbers, not integers. Unlike many computer graphics coordinate systems, the y axis is not inverted; the orientation is identical to the rectangular Cartesian coordinate system used in mathematics.

The draw_axes method of the sgl::Window class is available for use within the paint method to draw the viewport's x and y axes, along with additional grid lines.


Copyright  ©2019 Richard L. HaltermanVersion 0.9.5February 17, 2019
Creative Commons License This work is licensed under a Creative Commons License.