Apply texture to grid - grid

I would like to apply a texture to a grid in 3d space using Processing, I've tried declaring a PImage and setting the .jpg file with loadimage with no results..
PImage tex;
void setup() {
size(800, 600, P3D);
tex=loadImage("ground104");
noCursor();
}
void draw()
{
directionalLight(255, 255, 255, 0, 0, -1);
background(180);
drawPlane();
}
void drawPlane()
{ //lights();
int s = 10000;
int ss = 500;
fill(0, 200, 20); stroke(200); for(int i = 0; i < s; i += ss) {
stroke(0,100,0);
strokeWeight(3);
beginShape(QUAD_STRIP);
for(int j = 0; j <= s; j += ss)
{
texture(tex);
vertex(j, i, 0);
vertex(j, i + ss, 0);
}
endShape(); } noStroke(); }
Do you have any advice?
Thanks!

looks like you need a file extension like .jpg. You can troubleshoot by running the code from the reference page:
http://www.processing.org/reference/texture_
size(100, 100, P3D);
noStroke();
PImage img = loadImage("laDefense.jpg");
beginShape();
texture(img);
vertex(10, 20, 0, 0);
vertex(80, 5, 100, 0);
vertex(95, 90, 100, 100);
vertex(40, 95, 0, 100);
endShape();
and replace laDefense.jpg with your image name. Also, it needs to be placed in the data directory in the sketch directory. If it works, then your problem is elsewhere. I am not sure what your plan is in the sketch, but you might find peasycam useful for troubleshooting in 3d.
http://mrfeinberg.com/peasycam/
another technique is to increment a float by 0.01 per frame and call a RotateX(), Y or Z or all of the above at the beginning of your draw() method / loop.

Thank you for your reply!
I've changed my code to this and now you can run it and see what I get (first download the grass.jpg from https://www.dropbox.com/s/fsda0tih67q8tll/grass.jpg?m). I'm close but I wonder why the grid is green when it was supposed to be a grass land...
PImage tex;
void setup()
{
size(800, 600, P3D);
tex=loadImage("grass.jpg");
noCursor();
}
void draw()
{
translate(width/2 , height/2 , 0); // center of screen
rotateX(QUARTER_PI * 1.0); // move camera up
rotateZ(QUARTER_PI * 1.8);
//rotateZ(camZ.val + offset); // rotate around turret
rotateZ(map(mouseX, mouseY, width, 2.5, -2.5));
translate(-1000, 0, -1000);
directionalLight(255, 255, 255, 0, 0, -1);
background(180);
drawPlane();
}
void drawPlane()
{ //lights();
int s = 10000;
int ss = 500;
fill(0, 200, 20); stroke(200); for(int i = 0; i < s; i += ss) {
stroke(0,100,0);
strokeWeight(3);
beginShape(QUAD_STRIP);
for(int j = 0; j <= s; j += ss)
{
texture(tex);
vertex(j, i, 0);
vertex(j, i + ss, 0);
}
endShape();
}
noStroke();
}

Related

Stumped. Bool values from class objects are returning ascending integers?

Lot of code to post because I can't really tell where the issue is. I am trying to run this on an ESP32 chip and am writing the code in vscode with platformio.
The function
byte _previousPoint(){
for (byte point = 0; point < maxPoints; point ++){ //loop through active points.
if (_points[point + 1].getActive() != 1){
return point;
}
else {
long t1 = point_seconds(point);
long t2 = point_seconds(point + 1);
if (t1 <= currentTime){ //If current time is after this point.
if (t2 > currentTime){ //If current time is less than the next point (it is sandwiched).
return point;
}
}
}
}
return 0;
}
Is not behaving as I expect. And it seems the cause is that the function
bool getActive(){
return _active;
}
under the class point is returning not a bool but ascending numbers for each channel. point.getActive() should return a bool value, and that bool value should default to 0 (as it is set in the class definition), which is why this makes no sense. The point that is returning the erroneous values is the last one in the _points[maxPoints] array for each channel, and that value seems to match the channel numbers (for red it is 0, for green it is 1, for blue it is 2, etc.)
If I print the value _active upon running the point.setActive() function it comes out correctly. So something is wrong with "getting" it later.
Here is full code below. Let me know if you need some clarification because I know it's a lot. And thanks for anybody patient enough to help.
#include <Arduino.h>
long currentTime;
long lastUp;
byte totalChannels = 4;
const byte maxPoints = 3;
class point {
public:
point(){
}
void clear(){
_active = 0;
_day = 0;
_hour = 0;
_minute = 0;
_second = 0;
_intensity = 0;
_mode = 0;
}
bool getActive(){
return _active;
}
uint getDay(){
return _day;
}
byte getHour(){
return _hour;
}
byte getMinute(){
return _minute;
}
byte getSecond(){
return _second;
}
int getIntensity(){
return _intensity;
}
byte getMode(){
return _mode;
}
void setActive(bool active){
_active = active;
}
void setDay(uint day){
_day = day;
}
void setHour(byte hour){
_hour = hour;
}
void setMinute(byte minute){
_minute = minute;
}
void setSecond(byte second){
_second = second;
}
void setIntensity(byte intensity){
_intensity = intensity;
}
void setMode(byte mode){
_mode = mode;
}
private:
bool _active = 0;
uint _day = 0;
byte _hour = 0;
byte _minute = 0;
byte _second = 0;
int _intensity = 0;
byte _mode = 0;
};
class channel {
public:
channel(String color, byte pin){
this->_color = color;
this->_pin = pin;
init();
};
void setpoint(byte row, point &newPoint, uint day, byte hour, byte minute, byte second, byte intensity, byte mode){ //edits points (for debug)
newPoint.setDay(day);
newPoint.setHour(hour);
newPoint.setMinute(minute);
newPoint.setSecond(second);
newPoint.setIntensity(intensity);
newPoint.setMode(mode);
newPoint.setActive(1);
_points[row] = newPoint;
}
bool getPoint(byte point){
return _points[point + 1].getActive();
}
void clearAllPoints(){
for (byte point = 0; point < maxPoints; point ++ ){
_points[point].clear();
}
}
void setPin(byte pin){
_pin = pin;
}
byte getPin(){
return _pin;
}
uint getIntensity(){
byte point1 = _previousPoint();
byte point2 = _nextPoint(point1);
byte fade_mode = _points[point1].getMode();
uint intensity;
if (point2 != 0){
if (fade_mode == 0){
intensity = _interpolate_lin(point1, point2);
}
else if (fade_mode == 1){
intensity = _interpolate_sin(point1, point2);
}
}
else if (point2 != 1){
if (fade_mode == 0){
intensity = _interpolate_lin(point1, point2);
}
else if (fade_mode == 1){
intensity = _interpolate_sin(point1, point2);
}
}
return intensity;
}
private:
//class attributes
point _points[maxPoints]; //points maximum of 64 points per channel.
byte _pin; //PWM pin output for channel
String _color; //LED color
float _interpolate_lin(byte point1, byte point2){
float idiff = _points[point2].getIntensity() - _points[point1].getIntensity();
float tdiff = point_seconds(point2) - point_seconds(point1);
float m;
if (tdiff != 0){
m = idiff / tdiff;
}
else{
m = 0;
}
float t = currentTime - point_seconds(point1);
float b = _points[point1].getIntensity();
//linear equation result
float i = (m * t) + b;
return i;
}
float _interpolate_sin(byte point1, byte point2){
float amplitude = _points[point2].getIntensity() - _points[point1].getIntensity();
float tdiff = point_seconds(point2) - point_seconds(point1);
float a = (-0.5 * amplitude);
float b = (2 * PI) / (2 * tdiff);
float t = (currentTime - point_seconds(point1));
float d = 0.5 * abs(amplitude);
//cosine equation result
float i = (a * cos(b * t)) + d;
return i;
}
byte _previousPoint(){
for (byte point = 0; point < maxPoints; point ++){ //loop through active points.
if (_points[point + 1].getActive() != 1){
return point;
}
else {
long t1 = point_seconds(point);
long t2 = point_seconds(point + 1);
if (t1 <= currentTime){ //If current time is after this point.
if (t2 > currentTime){ //If current time is less than the next point (it is sandwiched).
return point;
}
}
}
}
return 0;
}
byte _nextPoint(byte point){
if (_points[point + 1].getActive() != 1){ //if next point is inactive, previous is last in cycle. Next point is 0.
return 0;
}
else if (_points[point + 1].getActive() == 1){ //if next point is active, return it as _nextPoint.
return point + 1;
}
return 0;
}
long point_seconds(byte point){
return ((_points[point].getHour() * 3600) + (_points[point].getMinute() * 60) + _points[point].getSecond());
}
};
//declaring channels and initializing channel array.
channel red("red", 0);
channel green("green", 1);
channel blue("blue", 2);
channel royal("royal blue", 3);
channel *channels[] = {
&red,
&green,
&blue,
&royal
};
void setIntensities(){
for (byte ch = 0; ch < totalChannels; ch ++){
channel ledChannel = *channels[ch];
byte intensity = ledChannel.getIntensity();
ledcWrite(ledChannel.getPin(), intensity);
}
}
void setup() {
Serial.begin(9600);
//set up LED outputs
ledcAttachPin(12, 0);
ledcAttachPin(13, 1);
ledcAttachPin(16, 2);
ledcSetup(0, 1000, 8);
ledcSetup(1, 1000, 8);
ledcSetup(2, 1000, 8);
//clear all points
for (byte ch = 0; ch < totalChannels; ch++){
channel ledChannel = *channels[ch];
ledChannel.clearAllPoints();
}
//Set points for testing purposes
point red1;
point red2;
point red3;
point green1;
point green2;
point green3;
point blue1;
point blue2;
point blue3;
point royal1;
point royal2;
point royal3;
red.setpoint(0, red1, 0, 0, 0, 0, 0, 0);
red.setpoint(1, red2, 0, 0, 0, 10, 255, 0);
red.setpoint(2, red3, 0, 0, 0, 20, 0, 0);
green.setpoint(0, green1, 0, 0, 0, 10, 0, 0);
green.setpoint(1, green2, 0, 0, 0, 20, 255, 0);
green.setpoint(2, green3, 0, 0, 0, 30, 0, 0);
blue.setpoint(0, blue1, 0, 0, 0, 20, 0, 0);
blue.setpoint(1, blue2, 0, 0, 0, 30, 255, 0);
blue.setpoint(2, blue3, 0, 0, 0, 40, 0, 0);
royal.setpoint(0, royal1, 0, 0, 0, 30, 0, 0);
royal.setpoint(1, royal2, 0, 0, 0, 40, 255, 0);
royal.setpoint(2, royal3, 0, 0, 0, 50, 0, 0);
Serial.print("red: "); // <----------These are to debug. The channels are returning ascending bool values for point 3 (which should all be 0)
for (byte point = 0; point < 3; point++){
Serial.print(red.getPoint(point));
}
Serial.print(" green: ");
for (byte point = 0; point < 3; point++){
Serial.print(green.getPoint(point));
}
Serial.print(" blue: ");
for (byte point = 0; point < 3; point++){
Serial.print(blue.getPoint(point));
}
Serial.print(" royal: ");
for (byte point = 0; point < 3; point++){
Serial.print(royal.getPoint(point));
}
}
void loop() {
currentTime = millis() / 1000;
if (currentTime > lastUp){
setIntensities();
lastUp = currentTime;
}
}
The problem is with your getPoint() method:
bool getPoint(byte point){
return _points[point + 1].getActive();
}
You're referencing the array with point + 1 instead of point so when you attempt to read the last item in the array you're actually reading off the end of the array and getting arbitary data.
Just change this to:
bool getPoint(byte point){
return _points[point].getActive();
}
You're also doing the same thing on your _previousPoint() method:
byte _previousPoint(){
for (byte point = 0; point < maxPoints; point ++){ //loop through active points.
if (_points[point + 1].getActive() != 1){
return point;
}
Again - I think you want to just replace [point + 1] with [point] to go through the list of points and prevent reading past the end of the array.

Picking the dominant color in Qt

How can I get the dominant color of a QImage or QIcon in Qt5 ?
Does it need generating a histogram manually ?
And if yes , how ?
You can't get this data via Qt.
But I found for You this web page (http://robocraft.ru/blog/computervision/1063.html).
On this web page some guy wrote code for your question! But all description only on Russian language. I translated it for you.
He is using OpenCV lib.
//
// determination of the prevailing colors in the image
// via k-Means
//
#include <cv.h>
#include <highgui.h>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
#include <algorithm>
// getting an image pixel (by type of image and coordinates)
#define CV_PIXEL(type,img,x,y) (((type*)((img)->imageData+(y)*(img)->widthStep))+(x)*(img)->nChannels)
const CvScalar BLACK = CV_RGB(0, 0, 0); // black
const CvScalar WHITE = CV_RGB(255, 255, 255); // white
const CvScalar RED = CV_RGB(255, 0, 0); // red
const CvScalar ORANGE = CV_RGB(255, 100, 0); // orange
const CvScalar YELLOW = CV_RGB(255, 255, 0); // yellow
const CvScalar GREEN = CV_RGB(0, 255, 0); // green
const CvScalar LIGHTBLUE = CV_RGB(60, 170, 255); // blue
const CvScalar BLUE = CV_RGB(0, 0, 255); // blue 2
const CvScalar VIOLET = CV_RGB(194, 0, 255); // Violet
const CvScalar GINGER = CV_RGB(215, 125, 49); // redhead
const CvScalar PINK = CV_RGB(255, 192, 203); // pink
const CvScalar LIGHTGREEN = CV_RGB(153, 255, 153); // light green
const CvScalar BROWN = CV_RGB(150, 75, 0); // brown
typedef unsigned char uchar;
typedef unsigned int uint;
typedef struct ColorCluster {
CvScalar color;
CvScalar new_color;
int count;
ColorCluster():count(0) {
}
} ColorCluster;
float rgb_euclidean(CvScalar p1, CvScalar p2)
{
float val = sqrtf( (p1.val[0]-p2.val[0])*(p1.val[0]-p2.val[0]) +
(p1.val[1]-p2.val[1])*(p1.val[1]-p2.val[1]) +
(p1.val[2]-p2.val[2])*(p1.val[2]-p2.val[2]) +
(p1.val[3]-p2.val[3])*(p1.val[3]-p2.val[3]));
return val;
}
// sorting flowers by quantity
bool colors_sort(std::pair< int, uint > a, std::pair< int, uint > b)
{
return (a.second > b.second);
}
int main(int argc, char* argv[])
{
// for save image
IplImage* image=0, *src=0, *dst=0, *dst2=0;
//
// loading image
//
char img_name[] = "Image0.jpg";
// name of image in first arg
char* image_filename = argc >= 2 ? argv[1] : img_name;
// get image
image = cvLoadImage(image_filename, 1);
printf("[i] image: %s\n", image_filename);
if(!image){
printf("[!] Error: cant load test image: %s\n", image_filename);
return -1;
}
// show image
cvNamedWindow("image");
cvShowImage("image", image);
// resize image (for processing speed)
src = cvCreateImage(cvSize(image->width/2, image->height/2), IPL_DEPTH_8U, 3);
cvResize(image, src, CV_INTER_LINEAR);
cvNamedWindow("img");
cvShowImage("img", src);
// image for storing cluster indexes
IplImage* cluster_indexes = cvCreateImage(cvGetSize(src), IPL_DEPTH_8U, 1);
cvZero(cluster_indexes);
#define CLUSTER_COUNT 10
int cluster_count = CLUSTER_COUNT;
ColorCluster clusters[CLUSTER_COUNT];
int i=0, j=0, k=0, x=0, y=0;
// initial cluster colors
#if 0
clusters[0].new_color = RED;
clusters[1].new_color = ORANGE;
clusters[2].new_color = YELLOW;
// clusters[3].new_color = GREEN;
// clusters[4].new_color = LIGHTBLUE;
// clusters[5].new_color = BLUE;
// clusters[6].new_color = VIOLET;
#elif 0
clusters[0].new_color = BLACK;
clusters[1].new_color = GREEN;
clusters[2].new_color = WHITE;
#else
CvRNG rng = cvRNG(-1);
for(k=0; k<cluster_count; k++)
clusters[k].new_color = CV_RGB(cvRandInt(&rng)%255, cvRandInt(&rng)%255, cvRandInt(&rng)%255);
#endif
// k-means
float min_rgb_euclidean = 0, old_rgb_euclidean=0;
while(1) {
for(k=0; k<cluster_count; k++) {
clusters[k].count = 0;
clusters[k].color = clusters[k].new_color;
clusters[k].new_color = cvScalarAll(0);
}
for (y=0; y<src->height; y++) {
for (x=0; x<src->width; x++) {
// get the RGB components of the pixel
uchar B = CV_PIXEL(uchar, src, x, y)[0]; // B
uchar G = CV_PIXEL(uchar, src, x, y)[1]; // G
uchar R = CV_PIXEL(uchar, src, x, y)[2]; // R
min_rgb_euclidean = 255*255*255;
int cluster_index = -1;
for(k=0; k<cluster_count; k++) {
float euclid = rgb_euclidean(cvScalar(B, G, R, 0), clusters[k].color);
if( euclid < min_rgb_euclidean ) {
min_rgb_euclidean = euclid;
cluster_index = k;
}
}
// set the cluster index
CV_PIXEL(uchar, cluster_indexes, x, y)[0] = cluster_index;
clusters[cluster_index].count++;
clusters[cluster_index].new_color.val[0] += B;
clusters[cluster_index].new_color.val[1] += G;
clusters[cluster_index].new_color.val[2] += R;
}
}
min_rgb_euclidean = 0;
for(k=0; k<cluster_count; k++) {
// new color
clusters[k].new_color.val[0] /= clusters[k].count;
clusters[k].new_color.val[1] /= clusters[k].count;
clusters[k].new_color.val[2] /= clusters[k].count;
float ecli = rgb_euclidean(clusters[k].new_color, clusters[k].color);
if(ecli > min_rgb_euclidean)
min_rgb_euclidean = ecli;
}
//printf("%f\n", min_rgb_euclidean);
if( fabs(min_rgb_euclidean - old_rgb_euclidean)<1 )
break;
old_rgb_euclidean = min_rgb_euclidean;
}
//-----------------------------------------------------
// Now we drive the array into a vector and sort it :)
std::vector< std::pair< int, uint > > colors;
colors.reserve(CLUSTER_COUNT);
int colors_count = 0;
for(i=0; i<CLUSTER_COUNT; i++){
std::pair< int, uint > color;
color.first = i;
color.second = clusters[i].count;
colors.push_back( color );
if(clusters[i].count>0)
colors_count++;
}
// sort
std::sort( colors.begin(), colors.end(), colors_sort );
// show color
dst2 = cvCreateImage( cvGetSize(src), IPL_DEPTH_8U, 3 );
cvZero(dst2);
int h = dst2->height / CLUSTER_COUNT;
int w = dst2->width;
for(i=0; i<CLUSTER_COUNT; i++ ){
cvRectangle(dst2, cvPoint(0, i*h), cvPoint(w, i*h+h), clusters[colors[i].first].color, -1);
printf("[i] Color: %d %d %d (%d)\n", (int)clusters[colors[i].first].color.val[2],
(int)clusters[colors[i].first].color.val[1],
(int)clusters[colors[i].first].color.val[0],
clusters[colors[i].first].count);
}
cvNamedWindow("colors");
cvShowImage("colors", dst2);
//cvResize(dst2, image, CV_INTER_LINEAR);
//cvSaveImage("dominate_colors_table.png", image);
//-----------------------------------------------------
// show the picture in the colors found
dst = cvCloneImage(src);
for (y=0; y<dst->height; y++) {
for (x=0; x<dst->width; x++) {
int cluster_index = CV_PIXEL(uchar, cluster_indexes, x, y)[0];
CV_PIXEL(uchar, dst, x, y)[0] = clusters[cluster_index].color.val[0];
CV_PIXEL(uchar, dst, x, y)[1] = clusters[cluster_index].color.val[1];
CV_PIXEL(uchar, dst, x, y)[2] = clusters[cluster_index].color.val[2];
}
}
cvNamedWindow("dst");
cvShowImage("dst", dst);
//cvResize(dst, image, CV_INTER_LINEAR);
//cvSaveImage("dominate_colors.png", image);
//-----------------------------------------------------
// waiting for the keystroke
cvWaitKey(0);
// free up resources
cvReleaseImage(&image);
cvReleaseImage(&src);
cvReleaseImage(&cluster_indexes);
cvReleaseImage(&dst);
cvReleaseImage(&dst2);
// delete windows
cvDestroyAllWindows();
return 0;
}
Finally with the help of QImage class I'm able to get this done and I implemented a simple algorithm from scratch.
The idea is to get the most frequent intensity in each color channel , and then combining them to get the final RGB color.
It returns a QVector<int> of three channels.
Helper function:
int MainWindow::get_max(QVector<int> vec){
int max = vec[0];
int index = 0;
for(int i = 0 ; i < 255 ; i++){
if(vec[i] > max){
max = vec[i];
index = i;
}
}
return index;
}
And the picker function :
QVector<int> MainWindow::get_mean_rgb_for_img(const QImage img){
QRgb *ct;
int width , height;
width = img.width();
height = img.height();
QVector<int> red(256);
QVector<int> green(256);
QVector<int> blue(256);
for(int i = 0 ; i < height ; i++){
ct = (QRgb *)img.scanLine(i);
for(int j = 0 ; j < width ; j++){
red[qRed(ct[j])]+=1;
}
}
for(int i = 0 ; i < height ; i++){
ct = (QRgb *)img.scanLine(i);
for(int j = 0 ; j < width ; j++){
green[qGreen(ct[j])]+=1;
}
}
for(int i = 0 ; i < height ; i++){
ct = (QRgb *)img.scanLine(i);
for(int j = 0 ; j < width ; j++){
blue[qBlue(ct[j])]+=1;
}
}
int red_val = get_max(red);
int green_val = get_max(green);
int blue_val = get_max(blue);
QVector<int> result(3);
result.insert(0 , red_val);
result.insert(1 , green_val);
result.insert(2 , blue_val);
return result;
}
Although there is just one thread for those calculations (At least on the paper , but in practice Qt spawned a handful of threads without my notice by itself) it's pretty much fast and requires little CPU time even for 4K images !!!

Zoom graphics processing 2.2.1

help please, at Arduino Uno I receive a signal from the sensor and build a graph using processing 2.2.1, but you need to scale up without losing proportions. My attempts failed, the proportion was crumbling(I tried to multiply the values) Code:
Serial myPort;
int xPos = 1;
int yPos = 100;
float yOld = 0;
float yNew = 0;
float inByte = 0;
int lastS = 0;
PFont f;
void setup () {
size(1200, 500);
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil('\n');
background(0xff);
}
void draw () {
int s = second();
PFont f = createFont("Arial",9,false);
textFont(f,9);
fill(0);
if (s != lastS){
stroke(0xcc, 0xcc, 0xcc);
line(xPos, yPos+10, xPos, yPos+30);
text(s + " Sec.", xPos+5, yPos+30);
lastS = s;
}
}
void mousePressed(){
save(lastS + "-heart.jpg");
}
void serialEvent (Serial myPort) {
String inString = myPort.readStringUntil('\n');
if (inString != null) {
inString = trim(inString);
if (inString.equals("!")) {
stroke(0, 0, 0xff); // blue
inByte = 1023;
} else {
stroke(0xff, 0, 0); //Set stroke to red ( R, G, B)
inByte = float(inString);
}
inByte = map(inByte, 0, 1023, 0, height);
yNew = inByte;
line(xPos-1, yPos-yOld, xPos, yPos-yNew);
yOld = yNew;
if (xPos >= width) {
xPos = 1;
yPos+=200;
if (yPos > height-200){
xPos = 1;
yPos=100;
background(0xff);
}
} else {
xPos++;
}
}
}
There are multiple ways to scale graphics.
A simple method to try is to simply scale() the rendering (drawing coordinate system).
Bare in mind currently the buffer is only cleared when the xPos reaches the right hand side of the screen.
The value from Arduino is mapped to Processing here:
inByte = map(inByte, 0, 1023, 0, height);
yNew = inByte;
you should try to map change height to a different value as you see fit.
This however will scale only the Y value. The x value is incremented here:
xPos++;
you might want to change this increment to a different value that works with the proportion you are trying maintain between x and y.

why skia on windows has bad efficiency

I compared skia with gdi painting on windows. both drawing 98000 random lines. to my suprise that skia is far low efficiency than gdi(the skia painting cost 1600ms, while gdi cost 0ms). my testing code was paste below. any suggestions?
bool PaintCompare() {
//generate ramdon points
std::default_random_engine e(std::chrono::high_resolution_clock::now().time_since_epoch().count());
std::uniform_real_distribution<float> u(10, 500);
SkPoint pts[100];
for (int i = 0; i<100; i++)
pts[i].set(u(e), u(e));
SkPaint paint;
paint.setColor(SkColorSetRGB(255, 0, 0));
//create skia canvas
sk_sp<SkSurface> rasterSurface(
SkSurface::MakeRasterN32Premul(600, 600));
SkCanvas* canvas = rasterSurface->getCanvas();
//draw lines with skia
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i<1000; i++)
{
for (int j = 1; j<99; j++)
{
canvas->drawLine(pts[j].fX, pts[j].fY, pts[j + 1].fX, pts[j + 1].fY, paint);
}
}
auto cost = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - start);
sk_sp<SkImage> img(rasterSurface->makeImageSnapshot());
if (!img) { return false; }
SkBitmap skBmp;
if (!img->asLegacyBitmap(&skBmp, SkImage::kRO_LegacyBitmapMode)) {
return false;
}
//show bitmap on hdc
BITMAPINFO bmi;
memset(&bmi, 0, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = 600;
bmi.bmiHeader.biHeight = -600; // top-down image
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = 0;
HDC hdc = GetDC();
LPVOID pBits = NULL;
HBITMAP hBmp = CreateDIBSection(hdc, &bmi, DIB_RGB_COLORS, &pBits, 0, 0);
skBmp.copyPixelsTo(pBits, skBmp.getSize());
CDC memdc;
memdc.CreateCompatibleDC(hdc);
memdc.SelectBitmap(hBmp);
BitBlt(hdc, 0, 0, 600, 600, memdc, 0, 0, SRCCOPY);
memdc.DeleteDC();
//draw with gdi
CPen pen;
pen.CreatePen(PS_SOLID, 1, RGB(0, 255, 0));
RECT rc{ 0,0,600,600 };
CBitmap bmp;
bmp.CreateCompatibleBitmap(hdc, 600, 600);
memdc.CreateCompatibleDC(hdc);
memdc.SelectBitmap(bmp);
memdc.FillSolidRect(&rc, RGB(0, 0, 0));
memdc.SelectPen(pen);
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i<1000; i++)
{
for (int j = 1; j<99; j++)
{
memdc.MoveTo(pts[j].fX, pts[j].fY);
memdc.LineTo(pts[j + 1].fX, pts[j + 1].fY);
}
}
auto cost2 = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - start);
//copy bitmap to window
BitBlt(hdc, 700, 0, 600, 600, memdc, 0, 0, SRCCOPY);
ReleaseDC(hdc);
memdc.DeleteDC();
//wchar_t buf[256];
//wsprintf(buf, L"left cost=%I64d, right cost=%I64d", cost.count(), cost2.count());
//GetParent().SetWindowText(buf);
//cost == 1596615 microseconds
//cost2 == 107253 microseconds
}
I found the problem finally. i give the result in debug mode!
in debug mode that skia with raster backend is 20 times slower than gdi.
however in release mode skia with raster backend is 4-5 times slower than gdi.
i had another test that skia uses opengl as backend. the result shows skia and gdi spend almost the same time. skia is about 15% slower than gdi.

How to fix QTGLWidget using QTimer to decrease rendering time?

I am currently using QtOpenGL for my program. My program plots dots in the 3D space. These dots could be any number between one to several thousand. I am using the function, 'glusphere', for drawing the sphere to represent each dot.
With this, each frame is created and I need to display each frames as an animation using QTimer. This animation should be controlled by fps.For example, if fps=30, I would put t=1000/30 for QTimer's setInterval(t).Whether fps=10 or 30, I found that fps does not change my program much.
I looked for a problem by setting fps to 0 and observed how much the rendering time takes by using setInterval(0). The following is the results:
n = number of spheres
when n=10: 28ms => about 30ms
when n=100: 87ms => about 90ms
when n=150: 137ms => about 140ms
when n=1000: 598ms => about 600ms => 0.6sec
From the results above, the rendering time of each frame linearly increases with the number of spheres. The following is a part of my code:
GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent)
{
setFormat(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer));
scale=1.0;
rot_x=0;
rot_y=0;
rot_z=0;
trans_x=0;
trans_y=0;
trans_z=0;
isloaded=false;
isplayed=false;
currentFrame=1;
sphereNum=1;
myTimer=new QTimer(this);
//fps=60;
myTimer->setInterval(0);
//myTimer->setInterval();
connect(myTimer, SIGNAL(timeout()), this, SLOT(timerEvent()));
}
void GLWidget::initializeGL()
{
qglClearColor(QColor(Qt::black));
glEnable(GL_DEPTH_TEST);
srand((unsigned)time(NULL));
//QGLFormat::setDoubleBuffer(true);
//qDebug() << "initGL context: " << this->context();
}
void GLWidget::timerEvent()
{
static QTime current_time, last_time;
int elapsed_time;
last_time=current_time;
current_time=QTime::currentTime();
elapsed_time=(current_time.second()*1000 + current_time.msec())-(last_time.second()*1000 + last_time.msec());
//qDebug() << "Timer elapsed time: " << elapsed_time;
if(isloaded && isplayed) {
if(currentFrame<markerData.frameSize) currentFrame++;
makeCurrent();
updateGL();
}
if(currentFrame==markerData.frameSize) {
isplayed=false;
myTimer->stop();
currentFrame=1;
}
}
void GLWidget::drawSphere(GLfloat x, GLfloat y, GLfloat z, GLfloat radius)
{
qglColor(Qt::yellow);
glPushMatrix();
glTranslatef(x, y, z);
gluSphere(p, radius, 10, 10);
//gluCylinder(p, 10, 10, 10, 4, 4);
glPopMatrix();
}
void GLWidget::drawMarkers() {
int markerIndex;
glPushMatrix();
/*for(int i=0; i<markerData.rowSize; i++) {
markerIndex=(currentFrame-1)*markerData.rowSize+i;
//qDebug() << markerIndex;
drawSphere(markerData.markerSet[markerIndex].x, markerData.markerSet[markerIndex].y, markerData.markerSet[markerIndex].z, 10);
}*/
for(int i=0; i<sphereNum; i++) {
markerIndex=rand()%1000;
drawSphere(markerIndex, markerIndex, markerIndex, 10);
}
glPopMatrix();
}
void GLWidget::drawText(double x, double y, double z, QString txt)
{
glDisable(GL_DEPTH_TEST);
qglColor(Qt::white);
renderText(x, y, z, txt, QFont("Arial", 12, QFont::Bold, false) );
glEnable(GL_DEPTH_TEST);
}
void GLWidget::paintGL()
{
static QTime current_time, last_time;
int elapsed_time;
float paint_fps;
//if file data is loaded
if(isloaded)
{
//calculate elapsed time drown 1 frame per second
//elapsed time is millisecond
last_time=current_time;
current_time=QTime::currentTime();
elapsed_time=(current_time.second()*1000 + current_time.msec())-(last_time.second()*1000 + last_time.msec());
paint_fps=1000/elapsed_time;
//qDebug() << "elapsed time: " << elapsed_time << " fps: " << paint_fps;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-20.0f);
glRotatef(30.0, 0.0, 1.0, 0.0);
glRotatef(15.0, 1.0, 0.0, 0.0);
glRotatef(rot_x, 1.0, 0.0, 0.0);
glRotatef(rot_y, 0.0, 1.0, 0.0);
glRotatef(rot_z, 0.0, 0.0, 1.0);
glScalef(scale, scale, scale);
glTranslatef(trans_x, trans_y, trans_z);
//draw elapsed time(ms)
glPushMatrix();
drawText(100, -300, 100, QString::number(elapsed_time) + "ms");
glPopMatrix();
//draw 3-axis
glBegin(GL_LINES);
qglColor(Qt::red);
glVertex3f(0, 0, 0);
glVertex3f(3000, 0, 0);
glEnd();
glBegin(GL_LINES);
qglColor(Qt::green);
glVertex3f(0, 0, 0);
glVertex3f(0, 3000, 0);
glEnd();
glBegin(GL_LINES);
qglColor(Qt::blue);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, 3000);
glEnd();
//draw spheres
p=gluNewQuadric();
drawMarkers();
gluDeleteQuadric(p);
//swapBuffers
if(doubleBuffer()) {
swapBuffers();
makeCurrent();
}
}
}
To clarify:
Rendering time for each frame changes depending on the number of spheres.
The number of spheres should not change the rendering time
Even though it draw 1000 spheres, it spends half second
Do you think the problem is using glusphere?
If there is another way to draw the sphere, could it be possible to do it more than 100 fps?
Could someone please explain where the bottleneck is that is creating this slow rendering?

Resources