Help in trying to recreate this p5 sketch for oF

I’m a beginner trying to get accustomed to working in oF. I’m a little more familiar with p5 and I thought of trying to recreate this sketch in oF.

My strategy thus far has been to consider the function and method calls in the p5 sketch and find their equivalents in oF but I’m falling short of the mark when it comes to many of the method calls on the image object in p5 - I’ve been trying to navigate these using ‘ofImage’ and ‘ofPixels’ but I’m coming up short. Anybody care to give me a clue?

Screenshot 2021-09-15 at 22.00.09

TIA

Hey @obAir , porting sketches in other languages is a great way to lear oF! Also check out all of the chapters in ofBook (scroll down a bit to find them), and there are some wonderful video tutorials on youtube too. The forum threads can be searched for specifc topics, and its an amazing resource for solving problems and learning some of the nuances of C++ and oF.

Using the online documentation for ofImage and ofPixels is the right place to search for equivalent functions to the code you posted. C++ uses a lot of “setters and getters” to set and get class members that are either protected or private within a class. So the following may offer some equivalency:

// create a couple of instances
ofImage image;
ofPixels pixels;

// maybe the same as img.loadPixels() ?
pixels = image.getPixels();

// imgX and imgY:  ofMap returns a float; imgX and imgY could be a floats or an ints,
// but floats may retain the rounding a bit better
float imgX = glm::round(ofMap(x, 0.f, width,  0.f, image.getWidth()));
int imgY = glm::round(ofMap(y, 0.f, height, 0.f, image.getHeight()));

// there are 2 variations of  ofImage::getColor()
ofColor c;
// get the color of the pixel in the image at coordinate (imgX, imgY).
c = image.getColor(imgX, imgY);
float grayscale = glm::round(c.r * 0.222 + c.g * 0.707 + c.b * 0.071);
1 Like

Hey @TimChi - I missed this reply entirely.

Thank you for the helpful response. I’m gonna take a little further look into that book and I’ll have a look at that code you sent on.

Man thanks for the advice and I’ll keep at it.

Hey Ben @obAir I forgot to mention that oF has a ton of example projects in the /examples folder, and they are also super helpful when learning about the different classes. Have fun!

1 Like