openframeworks / openframeworks/openFrameworks
Feature Proposal: ofDrawBackgroundGrid
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 10.4k
- Forks
- 2.6k
- Avg merge
- 1d 21h
- Merged PRs (30d)
- 9
Description
Hi all, just wanted to get some feedback on an idea and possible implementation. It is sometimes useful to have a gridded / checkerboard background (e.g. like Photoshop). Since we have ofBackgroundGradient, it seems like it would be easy enough to also have a simple grid background.
Here's a very quick and dirty mesh-based solution (that is likely using way too many mesh points and the wrong mesh mode ... but anyway).
void ofDrawBackgroundGrid(float size, const ofColor& onColor, const ofColor& offColor)
{
float w = ofGetViewportWidth(), h = ofGetViewportHeight();
gridMesh.clear();
gridMesh.setMode(OF_PRIMITIVE_TRIANGLES);
#ifndef TARGET_EMSCRIPTEN
#ifdef TARGET_OPENGLES
if(ofIsGLProgrammableRenderer()) gridMesh.setUsage(GL_STREAM_DRAW);
#else
gridMesh.setUsage(GL_STREAM_DRAW);
#endif
#endif
std::vector<glm::vec3> verts;
for (std::size_t y = 0; y < 3; ++y)
{
for (std::size_t x = 0; x < 3; ++x)
{
verts.push_back({x * size, y * size, 0.f});
}
}
std::vector<ofColor> colors = { onColor, offColor };
float twoSize = size * 2;
for (std::size_t y = 0; y < h; y += twoSize)
{
for (std::size_t x = 0; x < w; x += twoSize)
{
glm::vec3 offset(x, y, 0.f);
for (std::size_t i = 0; i < 2; ++i)
{
gridMesh.addVertex(verts[i + 0] + offset);
gridMesh.addColor(colors[i]);
gridMesh.addVertex(verts[i + 3] + offset);
gridMesh.addColor(colors[i]);
gridMesh.addVertex(verts[i + 4] + offset);
gridMesh.addColor(colors[i]);
gridMesh.addVertex(verts[i + 0] + offset);
gridMesh.addColor(colors[i]);
gridMesh.addVertex(verts[i + 4] + offset);
gridMesh.addColor(colors[i]);
gridMesh.addVertex(verts[i + 1] + offset);
gridMesh.addColor(colors[i]);
std::size_t j = colors.size() - i - 1;
gridMesh.addVertex(verts[i + 3] + offset);
gridMesh.addColor(colors[j]);
gridMesh.addVertex(verts[i + 6] + offset);
gridMesh.addColor(colors[j]);
gridMesh.addVertex(verts[i + 7] + offset);
gridMesh.addColor(colors[j]);
gridMesh.addVertex(verts[i + 3] + offset);
gridMesh.addColor(colors[j]);
gridMesh.addVertex(verts[i + 7] + offset);
gridMesh.addColor(colors[j]);
gridMesh.addVertex(verts[i + 4] + offset);
gridMesh.addColor(colors[j]);
}
}
}
GLboolean depthMaskEnabled;
glGetBooleanv(GL_DEPTH_WRITEMASK,&depthMaskEnabled);
glDepthMask(GL_FALSE);
gridMesh.draw();
if(depthMaskEnabled){
glDepthMask(GL_TRUE);
}
}
Seems to me that this would probably be good to just implement using a shader, but was curious to get other thoughts on that -- including for compatibility. I'm assuming background gradient could also be done w/ a shader, so I wasn't sure why it was implemented as a mesh (and reconstructed each time, not just on window resize).
Anyway your feedback is appreciated.
Changed method to ofDrawBackgroundGrid
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the proposed ofDrawBackgroundGrid entry point and compare the supplied mesh approach with the existing ofBackgroundGradient behavior. Resolve whether the feature should use a shader or mesh and how compatibility is handled; done means a supported checkerboard background API is implemented and its behavior is covered.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- computer-graphics
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100