“painter"–openframeworks之绘画系统

本文介绍了使用openframeworks构建绘画系统的过程,包括界面设计、笔刷展示和程序操作说明。通过设置画布和笔刷,用户可以实现多样化的绘画效果。程序运用了OpenGL和自定义笔刷算法,如mask、stars、lines等,同时支持快捷键操作,如截屏和全屏显示。虽然存在局限性,如代码量较大,但为拓展绘画提供了新的视角和可能性。
摘要由CSDN通过智能技术生成

总体设计方案

本程序使用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());

}

界面设计

  1. 总界面设计:
    在这里插入图片描述
  2. 左上角用来调整画笔的颜色、大小、透明度:
    在这里插入图片描述
  3. 最右边呈现所有笔刷的按钮:
    在这里插入图片描述

程序操作说明

点击对应按钮,显示对应的笔刷样式,用判断函数表示

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();
}

笔刷展示

  1. 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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值