OpenFrameworks Lection: 2d Graphics
OpenFrameworks Lection: 2d Graphics
Two-dimensional graphics
See in-depth details in my book Mastering openFrameworks Books examples are free, see masteringof.wordpress.com
Display Settings
in main.cpp: ofSetupOpenGL(& Window, 1024,768, OF_WINDOW); 1024, 768 - screen sizes, OF_WINDOW - output window. To display full screen in 1280x1024:
Display Settings
Switching between full screen during the program (in the update ()): ofSetFullscreen(bool fullscreen) Example: by pressing the '1 '/ '2' - on / off full screen mode: void testApp:: keyPressed (int key) { if (key == '1 ') { ofSetFullscreen(True); } if (key == '2 ') { ofSetFullscreen(False); } }
ofSetBackgroundAuto(bool bAuto) - Turns on / off cleaning mode images in each frame before calling draw () (default true).
Drawing Shapes
LineofLine(float x1, float y1, float x2, float y2) Rectangle ofRect(float x1, float y1, float w, float h) Circle ofCircle(float x, float y, float radius)
Triangle ofTriangle(float x1, float y1, float x2, float y2, float x3, float y3)
Ellipse ofEllipse Polygon ofBeginShape (), ofVertex (), ofEndShape () Smooth curve ofCurve
Drawing Shapes
Options: Drawing Color ofSetColor(int red, int green, int blue), where the numbers from 0 to 255. ofSetColor(int red, int green, int blue, int alpha) alpha is transparency, see below ofSetColor(int hexColor) 0x00ff00 means green Line Thickness ofSetLineWidth(float lineWidth) line thickness in pixels
Text output
- A simple text output, without setting the font and size: ofDrawBitmapString("Some text", 50, 50), // options: text and coordinates - To derive a normal font and size - to use ofTrueTypeFont: 1) copy bin / data font For example, verdana.ttf (There is a folder openframeworks) 2) declare: ofTrueTypeFont myFont; 3) in the setup (): myFont.loadFont ("verdana.ttf", 32 / * size * /); 4) in the draw (): myFont.drawString ("Good", 50, 50); - Output to a text console window: cout <<"Text" <<endl;
Example
Example
Declaring Variables float px; // top line float py; float qx; // indent float qy; float col; // color setup () ofBackground (255, 255, 255); ofSetBackgroundAuto (false); px = 320; py = 240; qx = 0; qy = 0; col = 0;
Example
update () px + = ofRandom (-1, 1); // ofRandom (a, b) - random value in [a, b] py + = ofRandom (-1, 1); qx + = ofRandom (-0.3, 0.3); qy + = ofRandom (-0.3, 0.3); if (px <0) px + = 640; if (px>= 640) px -= 640; if (py <0) py + = 480; if (py>= 480) py -= 480; if (qx <-30) qx + = 15; if (qx> 30) qx -= 15; if (qy <-30) qy + = 15; if (qy> 30) qy -= 15; col + = 0.02; if (col>= 256) col = col - 256;
Example
draw () int r = col; int g = int (col * 2) % 256; int b = 255 - col; ofSetColor (r, g, b); ofLine (px, py, px + qx, py + qy);
Drawing Images
Collage
Collage (From Fr.collage- Sticking) - technique in visual art, which consists in sticking to a substrate of objects and materials that differ from the basis of color and texture. Collage is also called the product is entirely made in this technique. (Wikipedia) And here we understand the placement of a collage of various images on the screen.
For a collage for you: -Load a pictures - Rotation - Transfer, - Change the size, - Transparency.
http://www.chinesecontemporary.com/hong_hao_5.htm
Rotate image
in the draw ()
ofPushMatrix (); // Remember the transformation matrix ofRotate (10.0); // Rotation in degrees of the left upper hand. angle image.draw (0.0, 0.0); // Draw ofPopMatrix (); // Restore matrix
Transparency
// 2-d option with the exact function for transparency: // GlEnable (GL_BLEND); // GlBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
for (int i = 0; i <5; i + +) { ofPushMatrix (); ofTranslate (300.0 + i * 100.0, 300.0); // Move ofRotate (angle); // Rotation ofScale (1.0 + 0.2 * i, 1.0 + 0.2 * i);// Increase the size of image.draw (-image.width / 2,-image.height / 2); ofPopMatrix (); } ofDisableAlphaBlending (); // Disable transparency // GlDisable (GL_BLEND); }
Result
These files must be added to the project, like so: 1. Copy them into the src of the project 2. in VisualStudio poke right-click on the project in the menu - Add Existing Items and add them both. Correct copy all the addons folder openframeworks / addons, but it can be uncomfortable if the project should be mobile, that is collected on different computers.
in the setup () // Create a buffer buffer.allocate (ofGetWidth (), ofGetHeight (), false // no autoclean every drawing - since will be there to collect pictures );
buffer.end (); // end drawing to clipboard buffer.draw (0, 0) // output buffer to the screen // Else draw ... In this case, the procedure of drawing into the buffer will be from frame to frame (the way: that was a pendulum), and procedures for the rest of the painting - only appear in one frame (itself a pendulum with a rubber band).
Homework (*)
Draw a polygon filled with a (textured) with some images. Hint. Scheme of the function call: ofTexture tex = image.getTextureReference (); tex.bind (); glBegin (GL_QUADS); glTexCoord2f (...) glVertex2f (...) ... glEnd (); tex.unbind ();
For large scale exciting field capture rate may be very low. Do not forget to shoot your project set the Release, not Debug. Better to use the codec CamStudio Lossless codec, it is fast and does not spoil the image. But the files are large in size. Therefore, before publication, it is better to convert the file using VirtualDub to another codec, for example, XVID.