总体设计方案
本程序使用openframeworks,fbo设置一块画布,所有效果均呈现在画布上;右边空白区域画矩形作为按钮,设置一个gui修改笔刷的大小和颜色。
ofFbo fbo;
ofxPanel gui;
ofParameter<int> scale;
ofParameter<ofColor> color;
由ofApp.cpp写主要代码,通过main.cpp运行:
int main( ){
ofSetupOpenGL(1000,800, OF_WINDOW); // <-------- setup the GL context
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp( new ofApp());
}
界面设计
- 总界面设计:
- 左上角用来调整画笔的颜色、大小、透明度:
- 最右边呈现所有笔刷的按钮:
程序操作说明
点击对应按钮,显示对应的笔刷样式,用判断函数表示
int ofApp::judge()//判断鼠标是否点击到按钮
{
if (mouseX > ofGetWidth() - 90 && mouseX < ofGetWidth() - 10)
{
if (mouseY < 40 && mouseY>10)
{
temp = 1;
printf("clicked1 ");
return temp;
}
else if (mouseY > 50 && mouseY < 80)
{
temp = 2;
printf("clicked2 ");
return temp;
}
else if (mouseY > 610 && mouseY < 640)
{
temp = 16;
printf("clear ");
return temp;
}
}
else
{
return temp = 0;
}
}
对应的按钮执行对应的笔刷,用temp的值来判断
fbo.begin();
ofSetColor(color);//改变颜色
if (temp == 1)
{
brush1();
}
if (temp == 9)
{
if (usecamera)
{
float rotateAmount = ofMap(ofGetMouseX(), 0, ofGetWidth(), 0, 360);
ofVec3f furthestPoint;
if (points.size() > 0)
{
furthestPoint = points[0];
}
else
{
furthestPoint = ofVec3f(x, y, 0);
}
ofVec3f directionToFurthestPoint = (furthestPoint - center);
ofVec3f directionToFurthestPointRotated = directionToFurthestPoint.getRotated(rotateAmount, ofVec3f(0, 1, 0));
camera.setPosition(center + directionToFurthestPointRotated);
camera.lookAt(center);
}
//otherwise add points like before
else
{
ofVec3f mousePoint(x, y, 0);
points.push_back(mousePoint);
}
}
if (temp == 16)
{
eraser();
}
else
{
ofDrawCircle(0,0,0);//默认啥都不画
}
fbo.end();
}
笔刷展示
- png
导入一个ps的内置笔刷,这里只随机选用了其中一种笔刷,定义一个参数brushdist,让每个笔刷元素之间的间距成为一个定值,变量 lastX,lastY 用于保存上个笔刷的坐标位置,dist 代表上个笔刷坐标到当前鼠标坐标的距离。当 dist 小于笔刷间距 brushDist,就说明距离太靠近,不应该绘制下一个笔刷。这可以避免绘制过慢时导致笔刷过密。当 dist 大于等于 brushDist,才会开始绘制。
void ofApp::brush1()
{
//ofDrawCircle(mouseX, mouseY, scale);
int brushNum;
float dist = ofDist(mouseX, mouseY, lastX, lastY);
float angle = atan2(mouseY - lastY, mouseX - lastX);
if (dist > brushDist)
{
brushNum = int(dist / brushDist);
float newX, newY;
for (int i = 1; i <= brushNum; i++)
{
float length = i * brushDist;
newX = lastX + cos(angle)*length;
newY = lastY + sin(angle)*length;
ofSetColor(color);
b1.draw(newX, newY, 2*scale, 2*scale);
}
lastX = newX;
lastY = new