PDA

View Full Version : drawing GL_POINTS with mouse. Spacings? [brush stroke]



hockeyman
07-09-2012, 03:44 AM
So I have made paint app. And I want to fix one problem with my brush stroke. When I paint with mouse slowly - everything is ok. When I do it faster, there are some spacings. Its because not all mouse coordinates are registered then. But how can I fix it? Is there ant solution? I'm using vertex array with mouse locations. Here's code:



int count = [vertices count] * 2;
int sum = [vertices count];
// NSLog(@"count: %d", count);
int currIndex = 0;
GLfloat *GLVertices = (GLfloat *)malloc(count * sizeof(GLfloat));
for (NSValue *locationValue in vertices) {
CGPoint loc = locationValue.pointValue;
GLVertices[currIndex++] = loc.x;
GLVertices[currIndex++] = loc.y;
}

glEnableClientState(GL_VERTEX_ARRAY);
glEnable(GL_BLEND);
glBlendFuncSeparate(GL_ZERO, GL_ONE, GL_SRC_COLOR, GL_ZERO);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, brushTexture);
glPointSize(50);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glEnable(GL_POINT_SMOOTH);
glVertexPointer(2, GL_FLOAT, 0, GLVertices);
glDrawArrays(GL_POINTS, 0, sum);
glDisable(GL_POINT_SMOOTH);
glDisable(GL_BLEND);
glDisableClientState(GL_VERTEX_ARRAY);


And mouse location registering code:



- (void) mouseDragged:(NSEvent *)event
{
location = [self convertPoint: [event locationInWindow] fromView:self];
NSValue *locationValue = [NSValue valueWithPoint:location];
[vertices addObject:locationValue];

[self drawArray];
}


- (void) mouseDown:(NSEvent *)event
{
location = [self convertPoint: [event locationInWindow] fromView:self];
NSValue *locationValue = [NSValue valueWithPoint:location];
[vertices addObject:locationValue];

[self drawArray];
}



And my app image:

http://s9.postimage.org/5dso9ff33/Screen_Shot_2012_07_09_at_1_33_57_PM.png

Ilian Dinev
07-09-2012, 12:06 PM
Easiest way: if the distance between A and B (start and end position) is 50.3 pixels , draw 50+1 points. Each being slightly offset, the last one matching the end position.

Harder way, supporting only circular brushes: draw a quad or hexagon, with a shader. On each fragment, find the nearest point on the [A;B] line-segment (not infinite line). The transparency of that fragment is proportionate to that distance and size of the brush.

hockeyman
07-09-2012, 11:20 PM
Thank you for answer :) This solution sounds nice, but I don't really get it. It would be easy if I do it manually. I mean, describe few points and draw them (static way). But how to do it dynamically? I mean that distance would be calculated at every mouse movement? I think you know what I mean. How to do that it would calculate distances from vertex array?

Ilian Dinev
07-10-2012, 12:43 AM
Distance between current mouse position and previous mouse position. sqrt(dx*dx + dy*dy)

hockeyman
07-10-2012, 06:03 AM
Thank you. I'm trying now to insert it to my code and make it working

hockeyman
07-12-2012, 06:20 AM
I tried to implement it, but it wasn't successful. Maby you could suggest me any solution? When I count distance between points, i need to specify point's indexes. Sounds ok, but my app redraws vertex array at every new mouse location, so I need somehow to do, so that if distance between two points, who goes one after another, is bigger than x, then add points between them. I think you understood me, but i will explain it more clearly.

So lets say I have 4 points in my GLfloat (vertex array).
1st point: (x1,y1)
2nd point: (x2,y2)
3rd point: (x3,y3)
4th point: (x4,y4)

In my vertex array they look like this:


GLVertices[1] = x1
GLVertices[2] = y1
GLVertices[3] = x2
GLVertices[4] = y2
GLVertices[5] = x3
GLVertices[6] = y3
GLVertices[7] = x4
GLVertices[8] = y4


And I need to count distance between them. Normally it should look like this:


int distBetween1and2 = sqrt(GLVertices[1]*GLVertices[3] + GLVertices[2]*GLVertices[4]);
int distBetween2and3 = sqrt(GLVertices[3]*GLVertices[5] + GLVertices[4]*GLVertices[6]);
int distBetween3and4 = sqrt(GLVertices[5]*GLVertices[7] + GLVertices[6]*GLVertices[8]);


But I cannot do this, because I don't know how many vertices will be in float and what indexes they will have.

Lets go on.

I should check them if distance is larger than something like this:



if (distBetween1and2 > 20) {
what the hell should be here?
}
if (distBetween2and3 > 20) {
what the hell should be here?
}
if (distBetween3and4 > 20) {
what the hell should be here?
}


And what should I insert to "what the hell should be here?" place?

Thanks in advance :)