🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Where to clear my BezierCurve vector?

Started by
1 comment, last by JoeJ 2 years, 1 month ago

I have built a Bezier Curve tool and each time the coordinates for each curve segment are calculated they are stored in a vector. Every single frame I am adding the entire curve's points on it over and over again. That is, frame 1, might have { p1, p2, p3 } then frame 2, it would have { p1, p2, p3, p1, p2, p3 } and so on. This would cause the line to loop back on itself when the loop at the end of the render function draws lines between the points p3 and p1. I am struggling to find where and how I should clear my BezierCurve vector between frames. Clearing after glClear() or before glSwapBuffers() only shows the previously drawn curve segment and has a straight line between points.

You can find my source code here: https://github.com/arjansingh00/BezierCurveTool2.0

Advertisement

What a mess ; )

You need to separate things:

  1. user input
  2. rendering
  3. functionality

Currently you mix all those things wildly, and so i can no imagine what the program is doing precisely.
The confusion starts with mouse input. I'm not sure if a point is added every frame, or just if the mouse button is clicked. Notice the difference between a state (button is currently down) and an event (button was up before, but has been pressed). You get the event only once (so that's where you want to add a point), but you could poll for the state in every frame (that's where you want to update tangent position for the next tangent to add, but you actually add it only at an event of released button). I don't know GLUT, so i don't know what's going on, but it seems you where confused about this. Usually frameworks have different ways to handle input events and state.

In the render function you do some things which do not belong there:
1. MouseDown = MouseReleased; // Render function should not change any input data. Render and input processing could be out of sync, so things would probably mess up. This line smells fishy.
2. You may no want to calculate the curve each frame, but only after it has changed from adding points or dragging them. Not really an issue, but would be a nice exercise while improving your programs structure.

If you can display some text on screen, i would use that for visual feedback at runtime. E.g. it could print the size of vectors, so you see when a point was added and how many.
Logging to a text file would also help. E.g. your logging file could look like this:

myMouse()
frame: 1
point added at (55,100)

myDisplay()

myMouse()
frame: 2
point added at (55,100)

myDisplay()

myMouse()
frame: 3
point added at (55,100)

myMouse()
frame: 4
point added at (55,100)

myDisplay()

...

So you see which things happened in what order in time, and with a logging file that's often easier than with using the debugger.
And you would notice multiple points at the same position are added, and input is processed twice between two frames.

Just meant as an example. But such things help to figure out if the program flow is as expected.

This topic is closed to new replies.

Advertisement