void GLWidget3D::renderImage(cv::Mat& image) {
	glClearColor(1, 1, 1, 0);

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glEnable(GL_DEPTH_TEST);

	// Model view projection行列をシェーダに渡す
	glUniformMatrix4fv(glGetUniformLocation(renderManager.program, "mvpMatrix"),  1, GL_FALSE, &camera.mvpMatrix[0][0]);
	glUniformMatrix4fv(glGetUniformLocation(renderManager.program, "mvMatrix"),  1, GL_FALSE, &camera.mvMatrix[0][0]);

	rb.pass1();
	drawScene(0);
	rb.pass2();
	drawScene(0);

	unsigned char* data = new unsigned char[sizeof(unsigned char) * 3 * width() * height()];
	glReadPixels(0, 0, width(), height(), GL_BGR, GL_UNSIGNED_BYTE, data);
	cv::Mat src_img(height(), width(), CV_8UC3, data);

	cv::cvtColor(src_img, src_img, CV_BGR2GRAY);
	cv::flip(src_img, src_img, 0);
	//cv::threshold(src_img, src_img, 230, 255, 0);

	cv::threshold(src_img, src_img, 128, 255, cv::THRESH_BINARY);

	src_img.convertTo(image, CV_32F, 1.0f, 0.0f);
				
	delete [] data;
}
void camera_viewer::image_update() {   
    cv::Mat camera_img;
    cap_->grab();
    cap_->retrieve(camera_src_, 0);
    if(!camera_src_.empty()) {
        cv::cvtColor(camera_src_, camera_img, CV_BGR2RGB);
        QImage src_img(camera_img.data, camera_img.cols, camera_img.rows, camera_img.step, QImage::Format_RGB888);
        src_img = src_img.scaled(200, 200);
        viewLabel->resize(200, 200);
        viewLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
        viewLabel->move(0, 0);
        viewLabel->setPixmap(QPixmap::fromImage(src_img));
        viewLabel->show();

        viewLabel_2->resize(200, 200);
        viewLabel_2->move(200, 0);
        viewLabel_2->setPixmap(QPixmap::fromImage(src_img));
        viewLabel_2->show();
    }
}
	Halftoner::Halftoner( const QPixmap& src, QImage& dest, int scale, bool generateGCode, const CNCParameters& params )
		: m_cutCount(0)
	{
		QImage	src_img( src.toImage() );
		int offset = params.m_step/2;
		int radius = params.m_step/2;
		double	max_dot_size( params.m_fullToolWidth * params.m_maxCutPercent );
		double	scale_factor( scale );
		bool		write_y(true);

		dest.fill( qRgb(0, 0, 0 ) );

		// Basic approach: Step through the source image and convert each 
		// point to a circle in the destination image and a tool cut in the
		// g code.  Every other row is offset by half a step to achieve the 
		// zig-zag pattern of a typical halftone image.
		for ( int y = params.m_step/2, cy = src_img.height()/params.m_step; y < src_img.height(); y+=params.m_step, --cy, write_y = true )
		{
			for ( int x = offset, cx = 1; x < src_img.width(); x+=params.m_step, ++cx )
			{
				double ds( getDotSize( src_img, x, y, radius ) );

				// Simple optimization: if the dot size is zero, just fill the destination
				// area with black pixels and don't generate any g code.
				if ( ds == 0 )
				{
					for ( int i =  scale_factor*(x - radius); i < scale_factor*(x + radius); ++i )
					{
						for ( int j = scale_factor*(y - radius); j < scale_factor*(y + radius); ++j )
						{
							if ( i >= 0 && i < dest.width() && j >= 0 && j < dest.height() )
							{
								dest.setPixel( i, j, qRgb( 0, 0, 0 ) );
							}
						}
					}
				}
				else
				{
					// Draw a circle and generate some tool movement g code.

					++m_cutCount;

					if ( generateGCode )
					{
						// Write the g code to cut this dot.
						// Lift tool to safe 'fast z' depth.
						m_gCode += "G00Z" + QString::number( params.m_fastZ ) + "\n";
					
						// Move tool to cut location.
						double cut_x( cx * ( max_dot_size + params.m_minDotGap ) );

						if ( offset )
							cut_x -= max_dot_size / 2.0;
						m_gCode += "G00X" + QString::number( cut_x ); 
						if ( write_y )
						{
							double cut_y( cy * ( max_dot_size + params.m_minDotGap ) );
							m_gCode += "Y" + QString::number( cut_y ); 
							write_y = false;
						}
						m_gCode += "\n";
					
						// Move tool to cut depth.
						m_gCode += "G01Z" +
							QString::number( - params.m_fullToolDepth * params.m_maxCutPercent * ds ) + 
							"\n";
					}

					// Draw a circle in the preview image.
					double	ds2( radius*radius*ds*ds*scale_factor*scale_factor );
					for ( int i = scale_factor*(x - radius); i < scale_factor*(x + radius); ++i )
					{
						for ( int j = scale_factor*(y - radius); j < scale_factor*(y + radius); ++j )
						{
							if ( i >= 0 && i < dest.width() && j >= 0 && j < dest.height() )
							{
								int dx( i - scale_factor*x ), dy( j - scale_factor*y );

								if ( dx * dx + dy * dy < ds2 - 0.5 )
									dest.setPixel(i, j, qRgb(255, 255, 255) );
								// Make the border pixels grey to improve the appearance a bit.
								else if ( dx * dx + dy * dy < ds2 + 0.5 )
									dest.setPixel(i, j, qRgb(127, 127, 127) );
								else
								{
									dest.setPixel(i, j, qRgb(0, 0, 0) );
								}
							}
						}
					}
				}
			}
			// Zig-zag in x.
			if ( offset )
				offset = 0;
			else
				offset = params.m_step/2;
		}
		// Finally, make sure the tool is parked at a safe depth.
		m_gCode += "G00Z" + QString::number( params.m_fastZ ); // Lift tool to safe 'fast z' depth.
		m_gCode += "\n";
	}