Book Image

OpenFrameworks Essentials

Book Image

OpenFrameworks Essentials

Overview of this book

Table of Contents (19 chapters)
openFrameworks Essentials
Credits
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

2D drawing


The basic drawing functions are the following (see Chapter 2, Creating Your First openFrameworks Project, for details):

Example of function usage

Description

ofBackground( ofColor::white );

This sets the background to the white color.

float w = ofGetWidth();

The ofGetWidth() function returns the current width of the application's screen in pixels. In this code, this value is set to the variable w.

float h = ofGetHeight();

The ofGetHeight() function returns the current height of the application's screen in pixels. In this code, this value is set to the variable h.

ofLine( 100, 200, 300, 400 );

This draws a line segment connecting the points (100, 200) and (300, 400).

ofRect( 100, 200, 300, 400 );

This draws a rectangle with the top-left corner (100, 200), a width of 300 pixels, and a height of 400 pixels.

The ofSetRectMode( OF_RECTMODE_CENTER ) command enables the mode for specifying the center of the rectangle instead of the top-left corner. The ofSetRectMode( OF_RECTMODE_CORNER ) command enables the top-left corner mode back.

ofTriangle( 10, 20, 30, 40, 50, 60 );

This draws a triangle with the vertices (10, 20), (30, 40), and (50, 60).

ofCircle( 100, 200, 30 );

This draws a circle with the center (100, 200) and a radius of 30.

The ofSetCircleResolution( 10 ) command sets the circle resolution to 10, that is, the circle will be drawn consisting of 10 line segments.

ofFill();

This enables the mode for drawing filled figures.

ofNoFill();

This enables the mode for drawing unfilled figures.

ofSetColor( 100, 150, 200 );

This sets the drawing color with a red value of 100, a green value of 150, and a blue value of 200.

ofSetColor(255,200);

This sets the drawing color with the red, green, and blue values equal to 255 and the alpha value equal to 200.

To enable additive blending, use the ofEnableBlendMode( OF_BLENDMODE_ADD ) command.

To enable alpha blending back, use the ofEnableAlphaBlending() command. (See Chapter 4, Working with Raster Graphics – Images, Videos, and Shaders, for details.)

ofSetColor( ofColor::yellow );

This sets the yellow drawing color.

ofPushMatrix();

This stores the current coordinate system to a special stack.

ofPopMatrix();

This restores the coordinate system from the stack.

ofTranslate( 100, 200 );

This translates the coordinate system by (200, 100) pixels.

ofRotate( 90 );

This rotates the coordinate system by 90 degrees clockwise.

ofScale( 2, 2 );

This scales the coordinate system; all objects will grow bigger by two times.